This generates the LaTeX code for creating vectors on a unit circle.
Generates vectors on the unit circle.
Date Created:Saturday February 21st, 2009 11:58 AM
Date Modified:Saturday February 21st, 2009 09:37 PM

<?
/*
*
* Author: Dan Lynch
*
*/
echo "<html>\n<body>\n";
function no_whitespace($str) {
return str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $str);
}
function no_returns($title) {
$title = str_replace("\n"," ",$title);
$title = str_replace("\t"," ",$title);
$title = str_replace("\r"," ",$title);
return $title;
}
function no_spaces_on_ends($title) {
if ( $title == '' ) return False;
$title = str_replace("\n"," ",$title);
$title = str_replace("\t"," ",$title);
$title = str_replace("\r"," ",$title);
if (substr($title, 0, 1) == ' ' || substr($title, -1) == ' ') {
if ( substr($title, 0, 1) == ' ' ) $title = substr($title, 1);
if ( substr($title, -1) == ' ' ) $title = substr($title, 0, strlen($title) - 1 );
return no_spaces_on_ends($title);
} else return $title;
}
function contains($str, $content, $ignorecase=true){
if ($ignorecase){
$str = strtolower($str);
$content = strtolower($content);
}
return ereg($str,$content) ? true : false;
}
if ($_POST['latex'] ==1) {
# defaults
$angle = $_POST['angle'];
$radius = $_POST['radius'];
$caption = $_POST['caption'];
$filename = $_POST['filename'];
$showcircle = $_POST['showcircle'];
$unitvector = $_POST['unitvector'];
$yaxis = $_POST['yaxis'];
$xaxis = $_POST['xaxis'];
$quad1 = $_POST['quad1'];
$quad2 = $_POST['quad2'];
$quad3 = $_POST['quad3'];
$quad4 = $_POST['quad4'];
$aput = $_POST['aput'];
$len = $radius * 1.25;
$cos = number_format($radius * cos($angle), 3, '.', '');
$sin = number_format($radius * sin($angle), 3, '.', '');
$smallradii = $radius * .2;
$magx = 0.5 * $cos;
$magx2 = $magx -0.2;
$magy = 0.5 * $sin;
$zx = 1.05 * $cos;
$zy = 1.05 * $sin;
$vectorx = $cos + 0.5;
$vectory = $sin + 0.5;
$bound = $radius * 1.25;
$iradius = $radius + 0.3;
#### HEADER
$content = <<<EOF
\documentclass{article}
\usepackage{pst-all}
\begin{document}
\psset{unit=0.5in}
\begin{pspicture}(-$radius,-$radius)($radius,$radius)
% y-axis
\\rput(0.3,$bound){ $yaxis }
\psline{->}(0,-$len)(0,$len)
% x-axis
\\rput($bound,0.3){ $xaxis }
\psline{->}(-$len,0)($len,0)
EOF;
#### SHOW TRIG SINE AND COSINE RELATIONSHIPS
if ( $unitvector == 1 ) {
$content .= <<<EOF
% i sin \\theta
\psline[linestyle=dashed](0,$sin)($cos,$sin)
\\rput($zx,$magy){\$i\sin (\\theta) \$}
% cos \\theta
\psline[linestyle=dashed]($cos,0)($cos,$sin)
\\rput($magx,$zy){\$ \cos (\\theta)\$}
EOF;
}
#### SHOW THE UNIT CIRCLE??
if ( $showcircle == 1) {
$showcircle = " checked";
$content .= <<<EOF
% the circle
\pscircle(0,0){ $radius }
EOF;
}
#### DISPLAY quadrants
$content .= <<<EOF
% quadrants
\\rput(0.3,$iradius){ $quad2 }
\\rput(0.3,-$iradius){ $quad3 }
\\rput(-$iradius,0.3){ $quad4 }
\\rput($iradius,0.3){ $quad1 }
EOF;
#### SHOW UNIT VECTOR?
if ($unitvector == 1) {
$unitvector = " checked";
$content .= <<<EOF
% magnitude
\psline{->}($cos,$sin)
\\rput($magx2,$magy){\$\mathbf{|z|}\$}
% angle line
\psarc(0,0){ $smallradii }{ 360 }{ $angle }
% angle omega
\\rput(0.4,0.3){\$\mathbf{\\theta}\$}
% z = e^{i \omega}
\\rput($vectorx,$vectory){\$\mathbf{z} = e^{i \\theta}\$}
EOF;
}
###### ADD NEW VECTORS
$matrix = $_POST['matrix'];
$matrix = str_replace("\n",';',$matrix);
if (contains(";",$matrix)) $matrix = explode(';', $matrix);
else $matrix = array($matrix);
$iter=1;
foreach($matrix as $row) {
$row = no_spaces_on_ends($row);
if ($row == '') continue;
$elements = explode(' ', $row);
$t1 = is_numeric($elements[0]);
$t2 = is_numeric($elements[1]);
$t3 = is_numeric($elements[2]);
$t4 = is_numeric($elements[3]);
if ($t1 && $t2) { # t tests
$x1 = number_format($elements[0] * $radius, 3, '.', '');
$y1 = number_format($elements[1] * $radius, 3, '.', '');
if ($t3 && $t4) {
$x2 = number_format($elements[2] * $radius, 3, '.', '');
$y2 = number_format($elements[3] * $radius, 3, '.', '');
$name = $elements[4];
$coords = '('.$x1.','.$y1.')('.$x2.','.$y2.')';
} else {
$name = $elements[2];
$x2 = 0;
$y2 = 0;
if ( $aput == 1 || $aput == " checked" ) {
$coords = '(0,0)('.$x1.','.$y1.')';
} else {
$coords = '('.$x1.','.$y1.')';
}
}
} else if ($t1) {
$x1 = number_format($radius * cos( $elements[0] * 3.14159 / 180 ), 3, '.', '');
$y1 = number_format($radius * sin( $elements[0] * 3.14159 / 180 ), 3, '.', '');
$name = $elements[1];
if ( $aput == 1 || $aput == " checked" ) {
$coords = '(0,0)('.$x1.','.$y1.')';
} else {
$coords = '('.$x1.','.$y1.')';
}
}
if ( no_spaces_on_ends($name) == '') {
$name = '$\mathbf{z}_'.$iter.'$';
$iter++;
}
$nx = $x1 - 0.2;
### WHAT QUADRANT?
if ($y1 > 0 || $x1 > 0 ) {
$ny = $y1;
} else {
$ny = $y1 - 0.2;
}
$content .= "\n % new vector \n";
if ( $aput == 1 || $aput == " checked" ) {
$aput = " checked";
$content .= "\pcline[linewidth=1.5 pt]{->}".$coords."\n";
$content .= '\Aput{\small '.$name.'}'."\n";
} else {
$content .= "\\rput(".$nx.','.$ny.'){'.$name.'}'."\n";
$content .= "\psline[linewidth=1.5 pt]{->}".$coords."\n";
}
} # end foreach
$content .= <<<EOF
\end{pspicture}
\end{document}
EOF;
$additional = <<<EOF
\begin{figure}[htp]
\centering
EOF;
$additional .='\includegraphics{images/'.$filename.'.ps}';
$additional .= <<<EOF
\caption{ $caption }
\label{ fig:$filename }
\end{figure}
EOF;
} else {
echo "<img src=\"unitvectors.gif\" height=\"450\"><br>";
echo "enter in data either separating rows by a return or semi-colon. For example, the following data are equivalent:\n\n";
echo "<pre>";
echo <<<EOF
30 $\pi/2$ 30 $\pi/2$;45 $\alpha$;120;-1 -1;0.4 0.7;-30 $\beta$
45 $\alpha$
120
-1 -1
0.4 0.7
-30 $\beta$
EOF;
echo "</pre>";
echo "One number is a unit vector along the unit circle\n\n";
echo "Two numbers is the vector that points to the respective coords (x,y)\n\n";
echo "Four numbers is the vector that points from the first pair to the second pair of coords.\n\n";
echo "<br><br>\n\nYou may always add a name after these arguments. If you use LaTeX math, then use \$ ... text ....\$ with no spaces.";
# defaults
$angle = 45;
$radius = 3;
$showcircle = "";
$unitvector = "";
$yaxis = '$Im$';
$xaxis = '$Re$';
$quad1 = '$i^0$';
$quad2 = '$i^1$';
$quad3 = '$i^2$';
$quad4 = '$i^3$';
$caption = 'Caption goes here...';
$filename = 'Filename';
}
echo "<pre style=\"border:1px solid black;\">".htmlspecialchars($content)."</pre>";
echo "<pre style=\"border:1px solid black;\">".htmlspecialchars($additional)."</pre>";
echo "</body></html>";
?>
<form name="input" action=""
method="post">
Matrix Data:
<textarea name="matrix" cols=40 rows=20><?=$_POST['matrix'];?></textarea><br>
Scale (radius): <input type="text" name="radius" size="3" value="<?=$radius;?>">
<br>
Show Circle: <input type="checkbox" name="showcircle" value="1"<?=$showcircle;?>>
<br>
Unit Vector: <input type="checkbox" name="unitvector" value="1"<?=$unitvector;?>>
Unit Vector Angle: <input type="text" size="3" name="angle" value="<?=$angle;?>">
<br>
y-axis: <input type="text" name="yaxis" size="5" value="<?=$yaxis;?>">
x-axis: <input type="text" name="xaxis" size="5" value="<?=$xaxis;?>">
<br>
Use Aput Labeling: <input type="checkbox" name="aput" value="1"<?=$aput;?>>
<br>
Quadrants:
<input type="text" size="7" name="quad1" value="<?=$quad1;?>">
<input type="text" size="7" name="quad2" value="<?=$quad2;?>">
<input type="text" size="7" name="quad3" value="<?=$quad3;?>">
<input type="text" size="7" name="quad4" value="<?=$quad4;?>">
<br>
Caption <input type="text" size="20" name="caption" value="<?=$caption;?>">
<br>
Filename <input type="text" size="20" name="filename" value="<?=$filename;?>">
<input type="hidden" name="latex" value="1"><br>
<input type="submit" value="Submit">
</form>
Downloads:
Download: unitvectors.php 9 KB
Please login or Click Here to register for downloads
Unit Vectors PStricks by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
