Example: Polarization state of light

Published 2009-08-04 | Author: Jeff Hein

This demonstrates the use of the fixed point calculation package fp.sty to process user-defined parameters while rendering a TikZ picture. In this example, a ellipse is rendered, representing the polarization state of light for an arbitrary x-amplitude y-amplitude, and relative phase. See http://en.wikipedia.org/wiki/Polarization for background information on polarization states of light.

In this example, a relatively simple command is called from within a tikzpicture environment:

\begin{tikzpicture}
%usage: \polellipse{ E_{0x} }{ E_{0y} }{ phase }
\polellipse{3}{2}{45}
\end{tikzpicture}

The contents of the \polellipse command is rather lengthy, but can be broken down into two main parts. The first part performs the necessary math to convert the user-defined parameters E_{0x}, E_{0y}, and phase into values which are useful to the drawing process. The second part uses the calculated numbers to draw a coordinate axis, the ellipse, and appropriate annotation.

This example can be helpful when preparing a document describing various polarization states of light. The document can contain an arbitrary number of \polellipse commands, each with an arbitrary configuration of parameters. Each rendered figure is based on the same code, so a desired change to the style can update all figures.

This general technique can be applied to any TikZ figure, where the document calls any number of commands with some passed parameters, and some computation is be performed to render the diagrams.

Download as: [PDF] [TEX]

Polarization state of light

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% Polarization state of light
% Author: Jeff Hein
\documentclass{article}
%========
%packages
%========
\usepackage{tikz} %tikz graphics package for drawing with pgf
\usepackage{fp} %for fixed point math
\usepackage{ifthen} %for \ifthenelse{}{}{} conditional statements
%===================
%Style configuration
%===================
\pagestyle{empty}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}
\usetikzlibrary{calc}
%line thickness styles: ultra thin, very thin, (thin), semithick, thick, very thick, ultra thick
%line dash styles: loosely dashed, densely dashed, loosely dotted, densely dotted
\tikzset{bkgd_line/.style={very thin,color=gray}}
\tikzset{dashed_bkgd_line/.style={thin, dashed,color=gray}}
\tikzset{dimension_line/.style={very thin,color=gray}}
\tikzset{axis_line/.style={thin}}
\tikzset{object_line/.style={thick}}
\tikzset{textlabel/.style={color=red}}
%===================
%Command Definitions
%===================
%Command: polellipse
%
%parameters:
%
%1: x-axis magnitude
%2: y-axis magnitude
%3: phase between x-y components: \phase = \delta_y - \delta_x
%
\newcommand{\polellipse}[3]{%
%
%NOTE:see http://spie.org/x32373.xml for some background on the math behind this calculation.
%
\coordinate(origin) at (0,0);
%constants
\FPset{\LABELOFFSET}{.1}
%input parameters
%================
%\Eoy: Electric field magnitude in the y-direction, (Eoy \geq 0)
\FPset{\Eoy}{#2}
%\Eox: Electric field magnitude in the x-direction, (Eoy \geq 0)
\FPset{\Eox}{#1}
%\phase: phase difference between E_y and E_x, in degrees (\phase \leq 0 < 360)
\FPset{\phase}{#3}
\FPiflt{\phase}{0}
\FPadd{\phase}{\phase}{360}
\else \fi
%\convert phase to radians
\FPmul{\phase}{\phase}{\FPpi} %\phase = \phase * \PI
\FPdiv{\phase}{\phase}{180} %\phase = \phase / 180
%Begin calculations, and reality check
%=====================================
\ifthenelse{\equal{\Eox}{0}}{%
%error case: \Eox = \Eoy = 0
\FPifzero{\Eoy}
\errmessage{ELLIPSE PLOT ERROR: Both Eox and Eoy cannot be zero!}
\else \fi
%special case: \Eox = 0
\FPdiv{\alp}{\FPpi}{2} %\alp = \pi/2
}
{%
%normal case, \Eox \neq 0
\FPdiv{\scratch}{\Eoy}{\Eox} %\scratch = \Eoy/\Eox
\FParctan{\alp}{\scratch} %\alp = \arctan{\scratch}
}
%normalize magnitudes, used for plotting
%=======================================
\FPpow{\scratchy}{\Eoy}{2} %\scratchy = \Eoy^2
\FPpow{\scratchx}{\Eox}{2} %\scratchx = \Eox^2
\FPadd{\scratch}{\scratchy}{\scratchx}%\scratch = \scratchy + \scratchx
\FProot{\norm}{\scratch}{2} %\norm = \sqrt{\scratch}
\FPdiv{\Eoynorm}{\Eoy}{\norm} %\Eoynorm = \Eoy / \norm
\FPdiv{\Eoxnorm}{\Eox}{\norm} %\Eoynorm = \Eox / \norm
 
 

Comments

  • #1 AML, August 5, 2009 at 7:48 p.m.

    Good work.

    Being rather ignorant of the technical aspects, was there a specific reason to use FP instead of PGFmath?

  • #2 Jeff Hein, August 5, 2009 at 9:14 p.m.

    Actually, I had only recently heard of the fixed point math capabilities withing pgf, and will be looking the possibility of rebuilding this example using the internal library.

  • #3 Kjell Magne Fauske, August 5, 2009 at 9:28 p.m.

    Could you have used the mathematical engine already bundled with PGF 2.0, or is it not accurate enough?

  • #4 Brandy, January 21, 2013 at 5 p.m.

    Thanks for the marvelous posting! I actually enjoyed reading it, you are a great author.I will remember to bookmark your blog and definitely will come back sometime soon. I want to encourage one to continue your great job, have a nice afternoon!

Adding comments is currently not enabled.