Debian Codegraph @

texmf-dist/doc/latex/mathspic/sourcecode113.nw

raw
1 <center><h1> <tt>mathspic</tt> in Perl </h1></center>
2 <center><h2>
3 <table>
4 <tr><td><address>
5 <b>Apostolos Syropoulos</b><br>
6 366, 28th October Str.<br>
7 GR-671 00 Xanthi<br>
8 Greece<br>
9 email: <tt>[email protected]</tt>
10 </address></td>
11 <td><address>
12 <b>R.W.D. Nickalls</b><br/>
13 Consultant in Anaesthesia & Intensive Care (retired)<br/>
14 c/o Department of Anaesthesia<br/>
15 Nottingham University Hospitals<br/>
16 City Hospital Campus<br/>
17 Hucknall Road<br/>
18 Nottingham NG5 1PB, UK<br/>
19 email:<tt>[email protected]</tt>
20 </address></td>
21 </table></h2>
22 version 1.13 Apr 26, 2010
23 </center>
24
25 @ <h3><b>Introduction</b></h3><p>
26 <tt>mathspic</tt> is a graphics program which implements a simple
27 programming notation, <i>mathspic</i>, suitable for the
28 creation of diagrams or mathematical figures.
29 <tt>mathspic</tt>'s input is a LaTeX file containing
30 <tt>mathspic</tt> plotting commands.
31 <tt>mathspic</tt>'s output is the equivalent LaTeX file
32 containing PiCTeX plotting commands.
33 Technically, therefore, <tt>mathspic</tt>
34 is a preprocessor or `filter' for use with the PiCTeX drawing engine.
35 <tt>mathspic</tt> was originally written in PowerBASIC 3.5, a
36 DOS-based programming language. Since, many
37 potential users are working in rather different programming environments,
38 the authors thought of porting <tt>mathspic</tt> into another programming
39 cross-platform language which would be widely available.
40 The authors decided to rewrite <tt>mathspic</tt> in Perl
41 since not only is Perl pretty stable, but it has
42 extensive mathematical support.<p>
43
44 <h3><b>Program Structure</b></h3><p>
45 Initially, we define a little package that is used to implement the [[loop]]
46 command. Then, we must do is to check the possible command line arguments.
47 Next, we process the input file.
48 If the user has used the [[-b]] (see below), the program will `beep'
49 if any errors are found during processing.
50 We need some auxiliary subroutines in order to properly parse the input
51 file and of course to handle the various commands. We also need a
52 few global variables.
53
54 <<*>>=
55 #!/usr/bin/perl
56 #
57 #(c) Copyright 2005-2010
58 # Apostolos Syropoulos & R.W.D. Nickalls
59 # [email protected] [email protected]
60 #
61 # This program can be redistributed and/or modified under the terms
62 # of the LaTeX Project Public License Distributed from CTAN
63 # archives in directory macros/latex/base/lppl.txt; either
64 # version 1 of the License, or any later version.
65 #
66 <<package <tt>DummyFH</tt> >>
67 package main;
68 use Math::Trig;
69 <<Define global variables>>
70 <<subroutine definitions>>
71 <<Check for command line arguments>>
72 <<process file>>
73 print $alarm if $no_errors > 0;
74 __END__
75
76 @ The package [[DummyFH]] is used in the implementation of the [[loop]] command.
77 It creates a dummy filehandle that is associated with an array of strings. Since
78 we only read data from this dummy filehandle, we implement the [[READLINE]] subroutine.
79 When we read a line from this dummy filehandle, we actually requesting the next entry
80 of the array (if any). That is why we use the package variable [[$index]]. When there
81 are no more entries in the array, subroutine [[READLINE]] returns the value [[undef]]
82 so to falsify loop that controls the consumption of input from this dummy filehandle.
83
84 <<package <tt>DummyFH</tt> >>=
85 package DummyFH;
86 my $index = 0;
87 sub TIEHANDLE {
88 my $class = shift;
89 my $self= shift;
90 bless $self, $class;
91 }
92 sub READLINE {
93 my $self = shift;
94 #shift @$self;
95 if ($index > $#$self) {
96 $index = 0;
97 return undef;
98 }
99 else {
100 return $self->[$index++];
101 }
102 }
103
104 @ <tt>mathspic</tt> accepts at most four command-line switches, namely
105 <tt>-b</tt> for enabling the beep, <tt>-s</tt> for automatic
106 screen viewing of the output-file,
107 <tt>-c</tt> for cleaning out all comment-lines,
108 and <tt>-o</tt> with a following file-name
109 for specifying the output file-name.
110 <tt>mathspic</tt> requires the name of an existing input-file
111 (the so-called <tt>mathspic</tt>-file) containing
112 <tt>mathspic</tt>commands.
113 If no command-line arguments are supplied, we print a
114 suitable usage message indicating the syntax.
115 For each command-line argument we set a global
116 variable. The default behavior is that the `bell' does not beep
117 and comment-lines are not removed from the output-file.
118
119 <<Check for command line arguments>>=
120 our $alarm="";
121 our $comments_on=1;
122 our $out_file="default";
123 our $argc=@ARGV;
124 if ($argc == 0 || $argc > 5 ){ # no command line arguments or more than 4
125 # arguments
126 die "\nmathspic version $version_number\n" .
127 "Usage: mathspic [-h] [-b] [-c] [-o <out file>] <in file>\n\n";
128 }
129 else {
130 <<Process command line arguments>>
131 print "This is mathspic version $version_number\n";
132 }
133 <<Check if .m file exists>>
134
135 @ In order to get the various command-line arguments we use a simple
136 [[while]] loop that checks each element of the array [[@ARGV]]. We check
137 for all the switches, and we get the name of the input-file.
138
139 <<Process command line arguments>>=
140 our $file = "";
141 SWITCHES:
142 while($_ = $ARGV[0]) {
143 shift;
144 if (/^-h$/) {
145 die "\nThis is mathspic version $version_number\n" .
146 "Type \"man mathspic\" for detailed help\n".
147 "Usage:\tmathspic [-h] [-b] [-c] [-o <out file>] <in file>\n" .
148 "\twhere,\n" .
149 "\t[-b]\tenables bell sound if error exists\n" .
150 "\t[-c]\tdisables comments in ouput file\n" .
151 "\t[-h]\tgives this help listing\n" .
152 "\t[-o]\tcreates specified output file\n\n";
153 }
154 elsif (/^-b$/) {
155 $alarm = chr(7);
156 }
157 elsif (/^-c$/) {
158 $comments_on = 0;
159 }
160 elsif (/^-o$/) {
161 die "No output file specified!\n" if !@ARGV;
162 $out_file = $ARGV[0];
163 shift;
164 }
165 elsif (/^-\w+/) {
166 die "$_: Illegal command line switch!\n";
167 }
168 else {
169 $file = $_;
170 }
171 }my ($xA, $yA, $xB, $yB, $dist)=@_;
172 die "No input file specified!\n" if $file eq "";
173
174 @ In order to check whether the input-file exists, we simply use the
175 [[-e]] operator. First we check to see if [[$file]] exits.
176 If the input-file does exist then the variable [[$file]] contains
177 the file name. In case the user has not specified an output
178 file, the default output file name is the name of the input file with
179 extension [[.mt]]. Finally, the program outputs all error messages to
180 the screen and to a log file. The name of the log file consists of
181 the contents of the variable [[$file]] and the extension [[.mlg]].
182
183 <<Check if .m file exists>>=
184 our ($source_file, $log_file);
185 if (! -e $file) {
186 die "$file: no such file!\n" if (! (-e "$file.m"));
187 $source_file = "$file.m";
188 }
189 else {
190 $source_file = $file;
191 $file = $1 if $file =~ /(\w[\w-\.]+)\.\w+/;
192 }
193 $out_file= "$file.mt" if $out_file eq "default";
194 $log_file= "$file.mlg";
195
196 @ Now that we have all the command line arguments, we can start processing
197 the input file. This is done by calling the subroutine [[process_input]].
198 Before that we must open all necessary files. Next,
199 we print some `header' information to the output file and to the log file.
200
201 <<process file>>=
202 open(IN,"$source_file")||die "Can't open source file: $source_file\n";
203 open(OUT,">$out_file")||die "Can't open output file: $out_file\n";
204 open(LOG,">$log_file")||die "Can't open log file: $log_file\n";
205 print_headers;
206 process_input(IN,"");
207
208 @ In this section we define a few global variables. More specifically:
209 the variable [[$version_number]] contains the current version number of the
210 program, the variable [[$commandLineArgs]] contains the command line arguments.
211 These two variables are used in the [[print_headers]] subroutine.
212 The variable [[$command]] will contain the whole current input line.
213 Hash [[%PointTable]] is used to store point names and related
214 information. Hash [[%VarTable]] is used to store mathspic variable names
215 and related information, while the associative array [[%ConstTable]] contains the
216 names of constants. Note that the values of both constants and variables are
217 kept in [[%VarTable]].
218 The variable [[$no_errors]] is incremented whenever the
219 program encounters an error in the input file. The variables [[$xunits]],
220 [[$yunits]] and [[$units]] are related to the [[paper]] command.
221 In particular, the variable [[$units]] is used to parse the unit part of the
222 [[unit]] part of the [[paper]] command. The variable [[$defaultsymbol]] is used to
223 set the point shape. The constant [[PI]] holds the value of the mathematical
224 constant pi.
225 The constant [[R2D]] holds the transformation factor to transform radians to
226 degrees. The constant [[D2R]] holds the transformation factor
227 to transform degrees to radians, i.e., the value [[1/R2D]]. The global variables
228 [[$arrowLength]], [[$arrowAngleB]] and [[$arrowAngleC]] are actually parameters that
229 are used by the subroutines that draw arrows. Since [[$arrowLength]] is actually
230 a length, variable [[$arrowLenghtUnits]] holds the units of measure in which
231 this length is expressed. The hash table [[%DimOfPoint]] contains the side or the
232 radius of a point whose plot-symbol is a square or a circle, respectively. In case the
233 default point symbol is a circle or a square, variable [[$GlobalDimOfPoints]] is used
234 to store the length of the radius or the length of the side of default point symbol,
235 respectively. Variable [[$LineThickness]] holds the current line thickness (the
236 default value is 0.4&nbsp;pt).
237
238 <<Define global variables>>=
239 our $version_number = "1.13 Apr 26, 2010";
240 our $commandLineArgs = join(" ", @ARGV);
241 our $command = "";
242 our $curr_in_file = "";
243 our %PointTable = ();
244 our %VarTable = ();
245 our %ConstTable = ();
246 our $no_errors = 0;
247 our $xunits = "1pt";
248 our $yunits = "1pt";
249 our $units = "pt|pc|in|bp|cm|mm|dd|cc|sp";
250 our $defaultsymbol = "\$\\bullet\$";
251 our $defaultLFradius = 0;
252 use constant PI => atan2(1,1)*4;
253 use constant R2D => 180 / PI;
254 use constant D2R => PI / 180;
255 our $arrowLength = 2;
256 our $arrowLengthUnits = "mm";
257 our $arrowAngleB = 30;
258 our $arrowAngleC = 40;
259 our %DimOfPoint = ();
260 our $GlobalDimOfPoints = 0;
261 our @Macros = ();
262 our $LineThickness = 0.4;
263
264 @ In this section we define the various subroutines that are needed in order
265 to process the input file.
266
267 <p> Subroutine <tt>mpp</tt> is a mathspic preprocessor that allows the definition
268 and use of macros with or without arguments. For the moment it is an experimental
269 feature and it should be used with care.
270
271 <p> Subroutine <tt>PrintErrorMessage</tt> is used to print error messages
272 to the screen, to the output file and to the log file.
273
274 <p> Subroutine <tt>PrintWarningMessage</tt> is used to print warning messages
275 to the screen, to the output file and to the log file.
276
277 <p> Subroutine <tt>PrintFatalError</tt> is used to print an error message
278 to the screen and to abort execution, where the error is considered fatal
279 and not recoverable.
280
281 <p>Subroutine <tt>chk_lparen</tt> checks whether the next input
282 character is a left parenthesis. Subroutine <tt>chk_rparen</tt>
283 checks whether the next input character is a right parenthesis. Subroutine
284 <tt>chk_comment</tt> checks whether a given command is followed by a trailing
285 comment. In the same spirit, we define the subroutines <tt>chk_lcb</tt>,
286 <tt>chk_rcb</tt>, <tt>chk_lsb</tt>, and <tt>chk_rsb</tt> which check for
287 opening and closing curly and square brackets respectively.
288 The subroutine [[chk_comma]] checks whether the next token is a comma.
289
290 <p> Subroutine [[print_headers]] is used to print a header to the output file,
291 so a user knows that the file has been generated by <tt>mathspic</tt>.
292
293 <p> Subroutine [[get_point]] is used to parse a point name and to
294 check whether the point exists (i.e whether the point has been defined).
295
296 <p> Subroutine [[perpendicular]] is used to compute the coordinates of the
297 foot of perpendicular line from some point P to a line AB.
298
299 <p> Subroutine [[Length]] is used to compute the distance between two
300 points A and B.
301
302 <p> Subroutine [[triangleArea]] computes the area of a triangle defined
303 by three points.
304
305 <p> Subroutine [[PointOnLine]] is used to compute the coordinates of
306 a point on a line segment AB and a distance d units from A towards B.
307
308 <p> Subroutine [[circumCircleCenter]] takes six arguments that are the
309 coordinates of three points and computes the center of the circle that
310 passes through the three points which define the triangle.
311
312 <p> Subroutine [[ComputeDist]] is used to compute a numeric value that is
313 specified by either a variable name, a pair of points, or just a number.
314
315 <p> Subroutine [[intersection4points]] is used to compute the coordinates
316 of the point of intersection of two lines specified by the four arguments
317 (i.e. two arguments for each point).
318
319 <p> Subroutine [[IncircleCenter]] is used to compute the center and
320 the radius of a circle that touches internally the sides of a triangle,
321 the coordinates of the three points which define the triangle
322 being the arguments of the subroutine.
323
324 <p> Subroutine [[Angle]] determines the opening in degrees of an angle
325 defined by three points which are the arguments of this subroutine.
326
327 <p> Subroutine [[excircle]] computes the center and the radius of
328 a circle that externally touches a given side (4th and 5th arguments) of
329 triangle (determined by the 1st, the 2nd and the 3rd argument).
330
331 <p> Subroutine [[DrawLineOrArrow]] is used to parse the arguments of the commands
332 [[drawline]], [[drawthickline]], [[drawarrow]], [[drawthickarrow]] and
333 [[drawCurve]].
334
335 <p> Subroutine [[drawarrows]] is used to draw one or more arrows between points.
336
337 <p> Subroutine [[drawlines]] is used to draw one or more lines between points.
338
339 <p> Subroutine [[drawCurve]] is used to draw a curve between an odd number of points.
340
341 <p> Subroutine [[drawpoints]] is used to draw the point symbol of one or more points.
342
343 <p> Subroutine [[drawAngleArc]] is used to draw an arc line within an angle.
344
345 <p> Subroutine [[drawAngleArrow]] is used to draw an arc line with an arrow on the end,
346 within an angle.
347
348 <p> Subroutine [[expr]] and subroutines [[term]], [[factor]] and
349 [[primitive]] are used to parse an expression that follows a variable
350 declaration.
351
352 <p> Subroutine [[memberOf]] is used to determine whether a string is a
353 member of a list of strings.
354
355 <p> Subroutine [[midpoint]] computes the midpoint of two points.
356
357 <p> Subroutine [[tand]] computes the tangent of an angle, where the
358 angle is expressed in degrees.
359
360 <p> Subroutine [[get_string]] scans a string in order to extract a
361 valid mathspic string.
362
363 <p> Subroutine [[is_tainted]] checks whether a string contains data that
364 may be proved harmful if used as arguments to a shell escape.
365
366 <p> Subroutine [[noOfDigits]] has one argument which is a number and
367 returns the number of decimal digits it has.
368
369 <p> Subroutine [[drawsquare]] has one argument which is the radius of point
370 and yields LaTeX code that draws a square.
371
372 <p> Subroutine [[X2sp]] can be used to transform a length to sp units.
373
374 <p> Subroutine [[sp2X]] can be used to transform a length expressed in sp units
375 to any other acceptable unit.
376
377 <p> Subroutine [[setLineThickness]] is used to determine the length of the
378 linethickness in the current paper units.
379
380 <p> Subroutine [[process_input]] parses the input file and any other file
381 being included in the main file, and generates output.
382
383 <<subroutine definitions>>=
384 <<subroutine <tt>mpp</tt> >>
385 <<subroutine <tt>PrintErrorMessage</tt> >>
386 <<subroutine <tt>PrintWarningMessage</tt> >>
387 <<subroutine <tt>PrintFatalError</tt> >>
388 <<subroutine <tt>chk_lparen</tt> >>
389 <<subroutine <tt>chk_rparen</tt> >>
390 <<subroutine <tt>chk_lcb</tt> >>
391 <<subroutine <tt>chk_rcb</tt> >>
392 <<subroutine <tt>chk_lsb</tt> >>
393 <<subroutine <tt>chk_rsb</tt> >>
394 <<subroutine <tt>chk_comma</tt> >>
395 <<subroutine <tt>chk_comment</tt> >>
396 <<subroutine <tt>print_headers</tt> >>
397 <<subroutine <tt>get_point</tt> >>
398 <<subroutine <tt>perpendicular</tt> >>
399 <<subroutine <tt>Length</tt> >>
400 <<subroutine <tt>triangleArea</tt> >>
401 <<subroutine <tt>pointOnLine</tt> >>
402 <<subroutine <tt>circumCircleCenter</tt> >>
403 <<subroutine <tt>ComputeDist</tt> >>
404 <<subroutine <tt>intersection4points</tt> >>
405 <<subroutine <tt>IncircleCenter</tt> >>
406 <<subroutine <tt>Angle</tt> >>
407 <<subroutine <tt>excircle</tt> >>
408 <<subroutine <tt>DrawLineOrArrow</tt> >>
409 <<subroutine <tt>drawarrows</tt> >>
410 <<subroutine <tt>drawlines</tt> >>
411 <<subroutine <tt>drawCurve</tt> >>
412 <<subroutine <tt>drawpoints</tt> >>
413 <<subroutine <tt>drawAngleArc</tt> >>
414 <<subroutine <tt>drawAngleArrow</tt> >>
415 <<subroutine <tt>expr</tt> >>
416 <<subroutine <tt>memberOf</tt> >>
417 <<subroutine <tt>midpoint</tt> >>
418 <<subroutine <tt>tand</tt> >>
419 <<subroutine <tt>get_string</tt> >>
420 <<subroutine <tt>is_tainted</tt> >>
421 <<subroutine <tt>noOfDigits</tt> >>
422 <<subroutine <tt>drawsquare</tt> >>
423 <<subroutine <tt>X2sp</tt> >>
424 <<subroutine <tt>sp2X</tt> >>
425 <<subroutine <tt>setLineThickness</tt> >>
426 <<subroutine <tt>process_input</tt> >>
427
428 @ Subroutine <tt>mpp</tt> is an implementation of a mathspic preprocessor that allows
429 the definition of one-line macros with or without arguments. Macro definition has the
430 following syntax:
431 <center>
432 <tt>"%def" macro_name "(" [ parameters ] ")" macro_code
433 </center>
434 where parameters is a list of comma separated strings (e.g., x,y,z). Once a macro is
435 defined it can be used or it can be undefined. To undefine a macro one has to use
436 the following command:
437 <center>
438 <tt>"%undef" [ macro_name ]
439 </center
440 This means that an undef command without an accompanying macro name has no effect
441 at all. In order to use a macro we simply type its name and its arguments in
442 parentheses. Note that macro arguments should not contain spaces. If a macro has no
443 argument, there is no need to type any parentheses. We will now describe briefly how
444 the macro processor operates.
445 <p> If the current input line starts with <tt>%def</tt>, then we assume that we have
446 a macro definition. We parse each component of the macro definition and finally we
447 store the macro name, the macro code and the macro parameters (if any) in an anonymous
448 hash that eventually becomes part of an array. If we encounter any error, we simply
449 skip to the next line after printing a suitable error message. Now, if the first tokens
450 of an input line are <tt>%undef</tt>, we assume the user wants to delete a macro.
451 In case these tokens are not followed by a macro name or the macro name has not been
452 defined we simply go on. Otherwise, we delete the corresponding macro data from the
453 global array [[@Macros]] that contains all the macro information. Macro expansion is
454 more difficult and it will be described in detail in a separate document. At this point
455 we would like to thank Joachim Schneider <joachim at hal dot rhein-necker dot de>
456 for a suggestion on improving macro expansion.
457
458 <<subroutine <tt>mpp</tt> >>=
459 sub mpp {
460 my $in_line;
461 chomp($in_line = shift);
462 my $LC = shift;
463 my $out_line = $in_line;
464 my $macro_name = "";
465 my @macro_param = ();
466 my $macro_code = "";
467 if ($in_line =~ s/^%def\s*//) {
468 if ($in_line =~ s/^(\w+)\s*//){
469 $macro_name = $1;
470 }
471 else {
472 PrintErrorMessage("No macro name has been found",$LC);
473 return ""
474 }
475 if ($in_line =~ s/^\(\s*//) {
476 # do nothing
477 }
478 else {
479 PrintErrorMessage("No left parenthesis after macro name has been found",$LC);
480 return "";
481 }
482 if ($in_line =~ s/^\)//) {
483 # Macro has no parameters!
484 }
485 else {
486 MACROS: while (1) {
487 if ($in_line =~ s/^(\w+)\s*//) {
488 push (@macro_param, $1);
489 }
490 else {
491 PrintErrorMessage("No macro parameter name has been found",$LC);
492 return "";
493 }
494 if ($in_line =~ s/^,\s*//) {
495 next MACROS;
496 }
497 else {
498 last MACROS;
499 }
500 }
501 if ($in_line =~ s/^\)//) {
502 # do nothing!
503 }
504 else {
505 PrintErrorMessage("No closing parenthesis after macro parameters",$LC);
506 return "";
507 }
508 }
509 $in_line =~ s/([^%]+)(%.*)/$1/;
510 $macro_code = $in_line;
511 push ( @Macros , { 'macro_name' => $macro_name,
512 'macro_code' => $macro_code,
513 'macro_param' => \@macro_param });
514 return $out_line;
515 }
516 elsif ($in_line =~ s/^%undef\s*//) {
517 if ($in_line =~ s/^(\w+)//) {
518 my $undef_macro = $1;
519 for(my $i = $#Macros; $i >= 0; $i--) {
520 if ($Macros[$i]->{'macro_name'} eq $undef_macro) {
521 splice(@Macros,$i,1);
522 }
523 }
524 }
525 return $out_line;
526 }
527 elsif ($in_line =~ s/^\s*%//) {
528 return $out_line;
529 }
530 else {
531 my $comment = $2 if $in_line =~ s/([^%]+)(%.+)/$1/;
532 EXPANSIONLOOP: while () {
533 my $org_in_line = $in_line;
534 for(my $i = $#Macros; $i >= 0; $i--) {
535 my $macro_name = $Macros[$i]->{'macro_name'};
536 if ($in_line =~ /&$macro_name\b/) { ############################
537 my $num_of_macro_args = @{$Macros[$i]->{'macro_param'}};
538 if ( $num_of_macro_args > 0 ) {
539 # Macro with parameters
540 my $pattern = "&$macro_name\\(";
541 foreach my $p ( 1..$num_of_macro_args ) {
542 my $comma = ($p == $num_of_macro_args) ? "\\s*" : "\\s*,\\s*";
543 $pattern .= "\\s*[^\\s\\)]+$comma";
544 }
545 $pattern .= "\\)";
546 while($in_line =~ /&$macro_name\b/) {
547 if ($in_line =~ /$pattern/) {
548 my $before = $`;
549 my $after = $';
550 my $match = $&;
551 my $new_code = $Macros[$i]->{'macro_code'};
552 $match =~ s/^&$macro_name\(\s*//;
553 $match =~ s/\)$//;
554 foreach my $arg ( 0..($num_of_macro_args - 1) ) {
555 my $old = $Macros[$i]->{'macro_param'}->[$arg];
556 my $comma = ($arg == ($num_of_macro_args - 1)) ? "" : ",";
557 $match =~ s/^\s*([^\s,]+)\s*$comma//;
558 my $new = $1;
559 # 'g': Parameter may occur several times
560 # in $new_code.
561 # '\b': Substitute only whole words
562 # not x in xA
563 $new_code =~ s/\b$old\b/$new/g;
564 }
565 $in_line = "$before$new_code$after";
566 }
567 else {
568 PrintErrorMessage("Usage of macro &$macro_name does not " .
569 "match its definition", $LC);
570 return "";
571 }
572 }
573 }
574 else {
575 # Macro without parameters
576 my $replacement = $Macros[$i]->{'macro_code'};
577 # '\b': Substitute only whole words
578 # not x in xA
579 $in_line =~ s/&$macro_name\b/$replacement/g;
580 }
581 }
582 }
583 last EXPANSIONLOOP if ( $org_in_line eq $in_line );
584 }
585 return "$in_line$comment";
586 }
587 }
588
589 @ Subroutine <tt>PrintErrorMessage</tt> has two parameters: the
590 error message that will be printed on the screen, the log file and
591 the output file, and the line number of the line containing the
592 error was detected.
593 The general form of the error message is the following:
594 <pre>
595 line X: paper{units(
596 ,mm)xrange(0,20)yrange(0,30)axes(B)ticks(10,10)}
597
598 ***Error: Error_Message
599 </pre>
600 where [[X]] denotes the line number and [[Error_Message]] is the
601 actual error message. Note, that we print the tokens processed so far
602 and on the text line the unprocessed tokens, so that the user knows
603 exactly where the error is. In the variable [[$A]] we store the processed
604 tokens, while the variable [[$l]] holds the length of [[$A]] plus the
605 length of the [[$error_line]] (that is the number of the input line where
606 the error occurred) plus 7, i.e., 4 (the length of the word
607 [[line]]) plus 2 (the two blank spaces) plus 1 (the symbol [[:]]).
608 Finally, we increment the error counter (variable [[$no_errors]]). Note, that
609 in case the user has specified the [[-c]] command line switch, we will not
610 print any messages to the output file.
611
612 <<subroutine <tt>PrintErrorMessage</tt> >>=
613 sub PrintErrorMessage {
614 my $errormessage = shift;
615 my $error_line = shift;
616 my ($l,$A);
617 $l = 1+length($command)-length;
618 $A = substr($command,0,$l);
619 $l += 7 +length($error_line);
620
621 for my $fh (STDOUT, LOG) {
622 print $fh "$curr_in_file", "Line $error_line: $A\n";
623 print $fh " " x $l ,$_,"***Error: $errormessage\n";
624 }
625 if ($comments_on) { #print to output file file
626 print OUT "%% *** $curr_in_file", "Line $error_line: $A\n";
627 print OUT "%% *** "," " x $l ,$_,"%% ... Error: $errormessage\n";
628 }
629 $no_errors++;
630 }
631
632 @ Subroutine <tt>PrintWarningMessage</tt> behaves exactly like the subroutine
633 <tt>PrintErrorMessage</tt>. The only difference is that the second
634 subroutine prints only a warning message. A warning is issued when
635 the system detects parameters that do nothing.
636
637 <<subroutine <tt>PrintWarningMessage</tt> >>=
638 sub PrintWarningMessage {
639 my $warningMessage = shift;
640 my $warning_line = shift;
641 my ($l,$A);
642 $l = 1+length($command)-length;
643 $A = substr($command,0,$l);
644 $l += 7 +length($warning_line);
645
646 for my $fh (STDOUT, LOG) {
647 print $fh "$curr_in_file", "Line $warning_line: $A\n";
648 print $fh " " x $l ,$_,"***Warning: $warningMessage\n";
649 }
650 if ($comments_on) { #print to output file file
651 print OUT "%% *** $curr_in_file", "Line $warning_line: $A\n";
652 print OUT "%% *** "," " x $l ,$_,"%% ... Warning: $warningMessage\n";
653 }
654 }
655
656 @ The subroutine <tt>PrintFatalError</tt> behaves similarly to the subroutine
657 <tt>PrintErrorMessage</tt>. It prints an error message to the
658 screen and aborts execution.
659
660 <<subroutine <tt>PrintFatalError</tt> >>=
661 sub PrintFatalError {
662 my $FatalMessage = shift;
663 my $fatal_line = shift;
664 my ($l,$A);
665 $l = 1+length($command)-length;
666 $A = substr($command,0,$l);
667 $l += 7 +length($fatal_line);
668
669 die "$curr_in_file", "Line $fatal_line: $A\n" .
670 (" " x $l) . $_ . "***Fatal Error: $FatalMessage\n";
671 }
672
673 @ The subroutine <tt>chk_lparen</tt> accepts two arguments: the name
674 of the token that should be immediately before the left parenthesis (variable
675 [[$token]]), and the current line number (variable [[$lc]]). First we
676 skip any leading white space and then check whether the next
677 input character is a left parenthesis, then the subroutine skips any
678 trailing white space; otherwise it prints an error message.
679
680 <<subroutine <tt>chk_lparen</tt> >>=
681 sub chk_lparen {
682 my $token = $_[0];
683 my $lc = $_[1];
684 s/\s*//;
685 if (/^[^\(]/) {
686 PrintErrorMessage("Missing ( after $token",$lc);
687 }
688 else {
689 s/^\(\s*//;
690 }
691 }
692
693 @ The subroutine <tt>chk_rparen</tt> accepts two parameters: the name
694 of the token that should be immediately after a right parenthesis (variable
695 [[$token]]), and the current line number (variable [[$lc]]). Initially, we
696 skip any leading white space and then we check whether the next input
697 token is a right parenthesis. If it is not we issue a error message and
698 return, otherwise we skip the parenthesis and any trailing white space.
699
700 <<subroutine <tt>chk_rparen</tt> >>=
701 sub chk_rparen {
702 my $token = $_[0];
703 my $lc = $_[1];
704 s/\s*//;
705 if (s/^\)//) {
706 s/\s*//;
707 }
708 else {
709 PrintErrorMessage("Missing ) after $token",$lc);
710 }
711 }
712
713
714 @ The subroutine <tt>chk_lcb</tt> behaves in a similar way to the subroutine
715 <tt>chk_lparen</tt>.
716
717 <<subroutine <tt>chk_lcb</tt> >>=
718 sub chk_lcb {
719 my $token = $_[0];
720 my $lc = $_[1];
721 s/\s*//;
722 if ($_ !~ /^\{/) {
723 PrintErrorMessage("Missing { after $token",$lc);
724 }
725 else {
726 s/^{\s*//;
727 }
728 }
729
730 @ Subroutine <tt>chk_rcb</tt> behaves in a similar way to the subroutine
731 <tt>chk_rparen</tt>.
732
733 <<subroutine <tt>chk_rcb</tt> >>=
734 sub chk_rcb {
735 my $token = $_[0];
736 my $lc = $_[1];
737 if ($_ !~ /^\s*\}/) {
738 PrintErrorMessage("Missing } after $token",$lc);
739 }
740 else {
741 s/^\s*}\s*//;
742 }
743 }
744
745 @ Subroutine <tt>chk_lsb</tt> behaves in a similar way to the subroutine
746 <tt>chk_lparen</tt>.
747
748 <<subroutine <tt>chk_lsb</tt> >>=
749 sub chk_lsb {
750 my $token = $_[0];
751 my $lc = $_[1];
752
753 s/\s*//;
754 if ($_ !~ /^\[/) {
755 PrintErrorMessage("Missing [ after $token",$lc);
756 }
757 else {
758 s/^\[\s*//;
759 }
760 }
761
762 @ Subroutine <tt>chk_rsb</tt> behaves in a similar way to the subroutine
763 <tt>chk_rparen</tt>.
764
765 <<subroutine <tt>chk_rsb</tt> >>=
766 sub chk_rsb {
767 my $token = $_[0];
768 my $lc = $_[1];
769
770 s/\s*//;
771 if ($_ !~ /^\]/) {
772 PrintErrorMessage("Missing ] after $token",$lc);
773 }
774 else {
775 s/^\]\s*//;
776 }
777 }
778
779 @ The subroutine [[chk_comma]] checks whether the next token is a comma.
780 If it is not then it prints an error message, otherwise it consumes the
781 comma and any white space that follows the comma.
782
783 <<subroutine <tt>chk_comma</tt> >>=
784 sub chk_comma {
785 my $lc = $_[0];
786
787 s/\s*//;
788 if (/^[^,]/) {
789 PrintErrorMessage("Did not find expected comma",$lc);
790 }
791 else {
792 s/^,\s*//;
793 }
794 }
795
796 @ The subroutine [[chk_comment]] has only one parameter which is the current
797 line number. It checks whether the next input character is a comment
798 character and in this case it does nothing!. Otherwise, if there is some trailing text
799 it simply prints a warning to the screen.
800
801 <<subroutine <tt>chk_comment</tt> >>=
802 sub chk_comment {
803 my $lc = $_[0];
804
805 s/\s*//;
806 if (/^%/) {
807 # do nothing!
808 }
809 elsif (/^[^%]/) {
810 PrintWarningMessage("Trailing text is ignored",$lc);
811 }
812 }
813
814 @ The subroutine [[print_headers]] prints a header to the output file, as
815 well as a header to the LOG file.
816 The header contains information regarding the version of the
817 program, a copyright notice, the command line, date and time information,
818 and the names of the various files processed/generated.
819
820 <<subroutine <tt>print_headers</tt> >>=
821 sub print_headers
822 {
823 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
824 $year+=1900;
825 $mon+=1;
826 $now_string = "$year/" . ($mon>9 ? "$mon/" : "0$mon/") .
827 ($mday>9 ? "$mday " : "0$mday ") .
828 ($hour>9 ? "$hour:" : "0$hour:") .
829 ($min>9 ? "$min:" : "0$min:") .
830 ($sec>9 ? "$sec" : "0$sec");
831 print OUT "%* -----------------------------------------------\n";
832 print OUT "%* mathspic (Perl version $version_number)\n";
833 print OUT "%* A filter program for use with PiCTeX\n";
834 print OUT "%* Copyright (c) 2005-2010 A Syropoulos & RWD Nickalls \n";
835 print OUT "%* Command line: $0 $commandLineArgs\n";
836 print OUT "%* Input filename : $source_file\n";
837 print OUT "%* Output filename: $out_file\n";
838 print OUT "%* Date & time: $now_string\n";
839 print OUT "%* -----------------------------------------------\n";
840 #
841 print LOG "----\n";
842 print LOG "$now_string\n";
843 print LOG "mathspic (Perl version $version_number)\n";
844 print LOG "Copyright (c) 2005-2010 A Syropoulos & RWD Nickalls \n";
845 print LOG "Input file = $source_file\n";
846 print LOG "Output file = $out_file\n";
847 print LOG "Log file = $log_file\n";
848 print LOG "----\n";
849 }
850
851 @ The subroutine [[get_point]] parses an individual point name.
852 If the next token is also a point name then it returns the point name
853 (but only if the only if
854 the point name exists in the PointTable). In all other cases it returns
855 the string [[_undef_]] to indicate that something is wrong.
856
857 <<subroutine <tt>get_point</tt> >>=
858 sub get_point {
859
860 my ($lc) = $_[0];
861 my ($PointName);
862
863 if (s/^([^\W\d_]\d{0,4})\s*//i) { #point name
864 $PointName = $1;
865 if (!exists($PointTable{lc($PointName)})) {
866 PrintErrorMessage("Undefined point $PointName",$lc);
867 return "_undef_";
868 }
869 else {
870 return lc($PointName);
871 }
872 }
873 else {
874 PrintErrorMessage("Point name expected",$lc);
875 return "_undef_";
876 }
877 }
878
879 @ The subroutine [[perpendicular]] has 6 parameters that correspond to the
880 coordinates of some point P and to the coordinates of two points A and
881 B that define a line. The subroutine returns
882 a pair of numbers that correspond to the coordinates of a point that lies
883 at the foot of the perpendicular to the line AB that passes through point P.
884 The slope of line AB is m<sub>1</sub> and so its equation is
885 y=m<sub>1</sub>x+c<sub>1</sub>. Similarly, the slope of the line PF is
886 m<sub>2</sub>=-1/m<sub>1</sub> and its equation is
887 y=m<sub>2</sub>x+c<sub>2</sub>. Since the line AB passes through A, then
888 c<sub>1</sub>=y<sub>A</sub>-m<sub>1</sub>x<sub>A</sub>. Similarly, as P is
889 on line PF, then c<sub>2</sub>=y<sub>P</sub>-m<sub>2</sub>x<sub>P</sub>.
890 Now point F is on both lines, therefore
891 y<sub>F</sub>=m<sub>2</sub>x<sub>F</sub>+c<sub>2</sub> and
892 y<sub>F</sub>=m<sub>1</sub>x<sub>F</sub>+c<sub>1</sub>. Solving these
893 equations for x<sub>F</sub> and y<sub>F</sub> gives:
894 <center>
895 x<sub>F</sub>=(c<sub>2</sub>-c<sub>1</sub>)/(m<sub>1</sub>-m<sub>2</sub>)<br>
896 y<sub>F</sub>=(m<sub>1</sub>c<sub>2</sub>-m<sub>2</sub>c<sub>1</sub>)/
897 (m<sub>1</sub>-m<sub>2</sub>)
898 </center>
899
900 <<subroutine <tt>perpendicular</tt> >>=
901 sub perpendicular {
902 my ($xP, $yP, $xA, $yA, $xB, $yB) = @_;
903 my ($xF, $yF, $deltax, $deltay, $m1, $m2, $c1, $c2, $factor);
904
905 $deltax = $xA - $xB;
906 return ($xA, $yP) if abs($deltax) < 0.0000001;
907 $deltay = $yA - $yB;
908 return ($xP, $yA) if abs($deltay) < 0.0000001;
909 $m1 = $deltay / $deltax;
910 eval { $m2 = (-1) / $m1;};
911 PrintFatalError("Division by zero",$lc) if $@;
912 $c1 = $yA - $m1 * $xA;
913 $c2 = $yP - $m2 * $xP;
914 eval { $factor = 1 / ($m1 - $m2)};
915 PrintFatalError("Division by zero",$lc) if $@;
916 return (($c2 - $c1) * $factor, ($m1 * $c2 - $m2 * $c1) * $factor);
917 }
918
919 @ The subroutine [[Length]] computes the distance between two points A and B.
920 Notice, that the name of the subroutine starts with a capital L, just
921 to avoid conflict with the predefined Perl function. The subroutine
922 requires four parameters which are the coordinates of the two points.
923
924 <<subroutine <tt>Length</tt> >>=
925 sub Length {
926 my ($xA, $yA, $xB, $yB)=@_;
927 return sqrt(($xB - $xA)**2 + ($yB - $yA)**2);
928 }
929
930 @ The subroutine [[triangleArea]] computes the area of a triangle by using
931 Heron's formula, i.e., given a triangle ABC, we first compute
932 s=(AB+BC+CA)/2 and then the area of the triangle is equal to the
933 square root of s times (s-AB) times (s-BC) times (s-BA), where AB, BC, and CA
934 are the lengths of the three sides of the triangle. The subroutine accepts 6
935 parameters, which correspond to the coordinates of three points that define
936 the triangle.
937
938 <<subroutine <tt>triangleArea</tt> >>=
939 sub triangleArea {
940 my ($xA, $yA, $xB, $yB, $xC, $yC)=@_;
941 my ($lenAB, $lenBC, $lenCA, $s);
942
943 $lenAB = Length($xA,$yA,$xB,$yB);
944 $lenBC = Length($xB,$yB,$xC,$yC);
945 $lenCA = Length($xC,$yC,$xA,$yA);
946 $s = ($lenAB + $lenBC + $lenCA) / 2;
947 return sqrt($s * ($s - $lenAB)*($s - $lenBC)*($s - $lenCA));
948 }
949
950 @ The subroutine [[poinOnLine]] accepts five arguments: the coordinates of two
951 points and the decimal number which corresponds to the distance from the
952 first point towards the second one. The way we compute the coordinates of
953 the point is fairly simple.
954
955 <<subroutine <tt>pointOnLine</tt> >>=
956 sub pointOnLine {
957 my ($xA, $yA, $xB, $yB, $dist)=@_;
958 my ($deltax, $deltay, $xPol, $yPol);
959
960 $deltax = $xB - $xA;
961 $deltay = $yB - $yA;
962 $xPol = $xA + ($dist * $deltax / &Length($xA,$yA,$xB,$yB));
963 $yPol = $yA + ($dist * $deltay / &Length($xA,$yA,$xB,$yB));
964 return ($xPol, $yPol);
965 }
966
967
968 @ As we have mentioned above the subroutine [[circumCircleCenter]] takes six
969 arguments that correspond to the coordinates of three points that
970 define a triangle. The subroutine computes the coordinates of
971 the center of a circle that passes through these three points, and the radius of
972 the circle. We now describe how the subroutine computes the center
973 of the circle and its radius. Let the triangle points be [[t1]], [[t2]]
974 and [[t3]]. We use the two pairs of points to define two sides,
975 i.e., [[t1t2]] and [[t2t3]]. For each
976 side we locate the midpoints and get the their coordinates. We check
977 whether either of these two lines is either vertical or horizontal. If this
978 is true, we know that one of the coordinates of the center of the circumcircle
979 is the same as that of the midpoints of the horizontal or vertical line.
980 Next, we determine the slopes of the lines [[t1t2]] and [[t2t3]].
981 We now determine the slope of lines at right-angles to these lines. We solve the
982 resulting equations and obtain the center of the circumcircle. Now we get the
983 radius, and then we are done.
984
985 <<subroutine <tt>circumCircleCenter</tt> >>=
986
987 sub circumCircleCenter {
988 my ($xA, $yA, $xB, $yB, $xC, $yC, $lc)=@_;
989 my ($deltay12, $deltax12, $xs12, $ys12);
990 my ($deltay23, $deltax23, $xs23, $ys23);
991 my ($xcc, $ycc);
992 my ($m23, $mr23, $c23, $m12, $mr12, $c12);
993 my ($sideA, $sideB, $sideC, $a, $radius);
994
995 if (abs(triangleArea($xA, $yA, $xB, $yB, $xC, $yC)) < 0.0000001)
996 {
997 PrintErrorMessage("Area of triangle is zero!",$lc);
998 return (0,0,0);
999 }
1000 $deltay12 = $yB - $yA;
1001 $deltax12 = $xB - $xA;
1002 $xs12 = $xA + $deltax12 / 2;
1003 $ys12 = $yA + $deltay12 / 2;
1004 #
1005 $deltay23 = $yC - $yB;
1006 $deltax23 = $xC - $xB;
1007 $xs23 = $xB + $deltax23 / 2;
1008 $ys23 = $yB + $deltay23 / 2;
1009 #
1010 CCXYLINE:{
1011 if (abs($deltay12) < 0.0000001)
1012 {
1013 $xcc = $xs12;
1014 if (abs($deltax23) < 0.0000001)
1015 {
1016 $ycc = $ys23;
1017 last CCXYLINE;
1018 }
1019 else
1020 {
1021 $m23 = $deltay23 / $deltax23;
1022 $mr23 = -1 / $m23;
1023 $c23 = $ys23 - $mr23 * $xs23;
1024 $ycc = $mr23 * $xs12 + $c23;
1025 last CCXYLINE;
1026 }
1027 }
1028 if (abs($deltax12) < 0.0000001)
1029 {
1030 $ycc = $ys12;
1031 if (abs($deltay23) < 0.0000001)
1032 {
1033 $xcc = $xs23;
1034 last CCXYLINE;
1035 }
1036 else
1037 {
1038 $m23 = $deltay23 / $deltax23;
1039 $mr23 = -1 / $m23;
1040 $c23 = $ys23 - $mr23 * $xs23;
1041 $xcc = ($ys12 - $c23) / $mr23;
1042 last CCXYLINE;
1043 }
1044 }
1045 if (abs($deltay23) < 0.0000001)
1046 {
1047 $xcc = $xs23;
1048 if (abs($deltax12) < 0.0000001)
1049 {
1050 $ycc = $ys12;
1051 last CCXYLINE;
1052 }
1053 else
1054 {
1055 $m12 = $deltay12 / $deltax12;
1056 $mr12 = -1 / $m12;
1057 $c12 = $ys12 - $mr12 * $xs12;
1058 $ycc = $mr12 * $xcc + $c12;
1059 last CCXYLINE;
1060 }
1061 }
1062 if (abs($deltax23) < 0.0000001)
1063 {
1064 $ycc = $ys23;
1065 if (abs($deltay12) < 0.0000001)
1066 {
1067 $xcc = $xs12;
1068 last CCXYLINE;
1069 }
1070 else
1071 {
1072 $m12 = $deltay12 / $deltax12;
1073 $mr12 = -1 / $m12;
1074 $c12 = $ys12 - $mr12 * $xs12;
1075 $xcc = ($ycc - $c12) / $mr12;
1076 last CCXYLINE;
1077 }
1078 }
1079 $m12 = $deltay12 / $deltax12;
1080 $mr12 = -1 / $m12;
1081 $c12 = $ys12 - $mr12 * $xs12;
1082 #-----
1083 $m23 = $deltay23 / $deltax23;
1084 $mr23 = -1 / $m23;
1085 $c23 = $ys23 - $mr23 * $xs23;
1086 $xcc = ($c23 - $c12) / ($mr12 - $mr23);
1087 $ycc = ($c23 * $mr12 - $c12 * $mr23) / ($mr12 - $mr23);
1088 }
1089 #
1090 $sideA = &Length($xA,$yA,$xB,$yB);
1091 $sideB = &Length($xB,$yB,$xC,$yC);
1092 $sideC = &Length($xC,$yC,$xA,$yA);
1093 $a = triangleArea($xA, $yA, $xB, $yB, $xC, $yC);
1094 $radius = ($sideA * $sideB * $sideC) / (4 * $a);
1095 #
1096 return ($xcc, $ycc, $radius);
1097 }
1098
1099 @ The subroutine [[ComputeDist]] is used to compute a distance that is
1100 specified by either a float number, a pair of points, or a variable
1101 name. In case we have a pair of identifiers, we check whether the first
1102 one is a point. If it isn't a point we assume we have a variable followed
1103 by a keyword. Otherwise, i.e., if it is a point name, we check whether
1104 the second identifier is also a point name. If it is, we simply return
1105 the distance between them, otherwise we issue an error message.
1106 If we have only a single identifier, we check whether it is a
1107 variable that has already been defined, and if so we return its value.
1108 Since, this
1109 subroutine is heavily used, it actually returns a pair of numbers:
1110 the first one being the computed distance and the second one being an
1111 error indicator. If the value of this indicator is 0, then there is no
1112 error. If its value is 1, then there is an error. Moreover, in case there
1113 is an error the distance is assumed to be equal to zero.
1114
1115 <<subroutine <tt>ComputeDist</tt> >>=
1116 sub ComputeDist {
1117 my ($lc) = $_[0];
1118 my ($v1, $v2);
1119
1120 if (s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//) #is it a number?
1121 {
1122 return ($1, 1);
1123 }
1124 elsif (/^[^\W\d_]\d{0,4}[^\W\d_]\d{0,4}/) #it is a pair of IDs?
1125 {
1126 s/^([^\W\d_]\d{0,4})//i;
1127 $v1 = $1;
1128 if (!exists($PointTable{lc($v1)})) {
1129 if (exists($VarTable{lc($v1)})) {
1130 return ($VarTable{lc($v1)}, 1);
1131 }
1132 PrintErrorMessage("Point $v1 has not been defined", $lc);
1133 s/^\s*[^\W\d_]\d{0,4}//i;
1134 return (0,0);
1135 }
1136 $v1 = lc($v1);
1137 s/^\s*([^\W\d_]\d{0,4})//i;
1138 $v2 = $1;
1139 if (!exists($PointTable{lc($v2)}))
1140 {
1141 PrintErrorMessage("Point $v2 has not been defined", $lc);
1142 return (0,0);
1143 }
1144 $v2 = lc($v2);
1145 my ($x1,$y1,$pSV1,$pS1) = unpack("d3A*",$PointTable{$v1});
1146 my ($x2,$y2,$pSV2,$pS2) = unpack("d3A*",$PointTable{$v2});
1147 return (Length($x1,$y1,$x2,$y2), 1);
1148 }
1149 elsif (s/^([^\W\d_]\d{0,4})//i) # it is a single id
1150 {
1151 $v1 = $1;
1152 if (!exists($VarTable{lc($v1)})) #it isn't a variable
1153 {
1154 PrintErrorMessage("Variable $v1 has not been defined", $lc);
1155 return (0,0);
1156 }
1157 return ($VarTable{lc($v1)}, 1);
1158 }
1159 else
1160 {
1161 PrintErrorMessage("Unexpected token", $lc);
1162 return (0,0);
1163 }
1164 }
1165
1166 @ The subroutine [[intersection4points]] has 8 parameters that correspond to the
1167 coordinates of four points that uniquely determine two lines, and computes the
1168 the point of intersection of these two lines.
1169
1170 <<subroutine <tt>intersection4points</tt> >>=
1171 sub intersection4points {
1172 my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = @_;
1173 my ($deltay12, $deltax12, $deltay34, $deltax34);
1174 my ($xcc, $ycc, $m34, $c34, $m12, $c12);
1175
1176 $deltay12 = $y2 - $y1;
1177 $deltax12 = $x2 - $x1;
1178 #
1179 $deltay34 = $y4 - $y3;
1180 $deltax34 = $x4 - $x3;
1181 I4PXYLINE:{
1182 if (abs($deltay12) < 0.0000001)
1183 {
1184 $ycc = $y1;
1185 if (abs($deltax34) < 0.0000001)
1186 {
1187 $xcc = $x3;
1188 last I4PXYLINE;
1189 }
1190 else
1191 {
1192 $m34 = $deltay34 / $deltax34;
1193 $c34 = $y3 - $m34 * $x3;
1194 $xcc = ($ycc - $c34) / $m34;
1195 last I4PXYLINE;
1196 }
1197 }
1198 if (abs($deltax12) < 0.0000001)
1199 {
1200 $xcc = $x1;
1201 if (abs($deltay34) < 0.0000001)
1202 {
1203 $ycc = $y3;
1204 last I4PXYLINE;
1205 }
1206 else
1207 {
1208 $m34 = $deltay34 / $deltax34;
1209 $c34 = $y3 - $m34 * $x3;
1210 $ycc = $m34 * $xcc + $c34;
1211 last I4PXYLINE;
1212 }
1213 }
1214 if (abs($deltay34) < 0.0000001)
1215 {
1216 $ycc = $y3;
1217 if (abs($deltax12) < 0.0000001)
1218 {
1219 $xcc = $x1;
1220 last I4PXYLINE;
1221 }
1222 else
1223 {
1224 $m12 = $deltay12 / $deltax12;
1225 $c12 = $y1 - $m12 * $x1;
1226 $xcc = ($ycc - $c12) / $m12;
1227 last I4PXYLINE;
1228 }
1229 }
1230 if (abs($deltax34) < 0.0000001)
1231 {
1232 $xcc = $x3;
1233 if (abs($deltay12) < 0.0000001)
1234 {
1235 $ycc = $y1;
1236 last I4PXYLINE;
1237 }
1238 else
1239 {
1240 $m12 = $deltay12 / $deltax12;
1241 $c12 = $y1 - $m12 * $x1;
1242 $ycc = $m12 * $xcc + $c12;
1243 last I4PXYLINE;
1244 }
1245 }
1246 $m12 = $deltay12 / $deltax12;
1247 $c12 = $y1 - $m12 * $x1;
1248 $m34 = $deltay34 / $deltax34;
1249 $c34 = $y3 - $m34 * $x3;
1250 $xcc = ($c34 - $c12) / ($m12 - $m34);
1251 $ycc = ($c34 * $m12 - $c12 * $m34) / ($m12 - $m34);
1252 }
1253 return ($xcc, $ycc);
1254 }
1255
1256 @ The subroutine [[IncircleCenter]] computes the center and the
1257 radius of the circle that is inside a triangle and touches the sides of
1258 the triangle. The subroutine has six arguments that correspond to the
1259 coordinates of three points that uniquely determine the triangle. Here are
1260 the details:
1261 <ul>
1262 <li> Let the triangle points be A, B, C and sides a, b, c, where side B
1263 is opposite angle B, etc. </li>
1264 <li> Use angles A and B only.</li>
1265 <li> Let the bisector of angle A meet side a in point A1, and let the
1266 distance of A1 from B be designated BA1</li>
1267 <li> Using the sine rule, one gets: BA1/c = a/(b+c), that is
1268 BA1 = c * a/(b+c).</li>
1269 <li> Now do the same for side b, and determine equivalent point B1.
1270 CB1/a = b/(b+c), that is CB1 = a * b/(b+c).</li>
1271 <li> We can now find the intersection of the line from point A to point A1,
1272 and the line from point B to point B1. We have four points, so we use the
1273 mathspic internal [[intersection4points]] subroutine to return the
1274 coordinates of the intersection X<sub>i</sub>, Y<sub>i</sub>.</li>
1275 <li> Now get the radius: R=(area of triangle)/(a+b+c)/2</li>
1276 <li>Finally, return the radius and the coordinates of the center.
1277 </ul>
1278
1279 <<subroutine <tt>IncircleCenter</tt> >>=
1280 sub IncircleCenter {
1281 my ($Ax, $Ay, $Bx, $By, $Cx, $Cy) = @_;
1282 my ($sideA, $sideB, $sideC);
1283 my ($ba1, $xA1, $yA1, $cb1, $ac1, $xB1, $yB1, $xC1, $yC1, $a, $s, $r);
1284
1285 #determine the lengths of the sides
1286 $sideA = Length($Bx, $By, $Cx, $Cy);
1287 $sideB = Length($Cx, $Cy, $Ax, $Ay);
1288 $sideC = Length($Ax, $Ay, $Bx, $By);
1289 #
1290 $ba1 = ($sideC * $sideA) / ($sideB + $sideC);
1291 ($xA1, $yA1) = pointOnLine($Bx, $By, $Cx, $Cy, $ba1);
1292 $cb1 = ($sideA * $sideB) / ($sideC + $sideA);
1293 ($xB1, $yB1) = pointOnLine($Cx, $Cy, $Ax, $Ay, $cb1);
1294 $ac1 = ($sideB * $sideC) / ($sideA + $sideB);
1295 ($xC1, $yC1) = pointOnLine($Ax, $Ay, $Bx, $By, $ac1);
1296 ($xcenter, $ycenter) = &intersection4points($Ax, $Ay, $xA1, $yA1,
1297 $Bx, $By, $xB1, $yB1);
1298 # get radius
1299 $a = &triangleArea($Ax, $Ay, $Bx, $By, $Cx, $Cy);
1300 $s = ($sideA + $sideB +$sideC) / 2;
1301 $r = $a / $s;
1302 return ($xcenter, $ycenter, $r);
1303 }
1304
1305 @ The subroutine [[Angle]] takes six arguments which correspond to the
1306 coordinates of three points that define an angle. The subroutine computes
1307 the opening of the angle in degrees. In case there is an error it returns
1308 the number -500. ****EXPLAIN THE ALGORITHM****
1309
1310 <<subroutine <tt>Angle</tt> >>=
1311 sub Angle {
1312 my ($Ax, $Ay, $Bx, $By, $Cx, $Cy) = @_;
1313 my ($RAx, $RAy, $RBx, $RBy, $RCx, $RCy, $deltax, $deltay);
1314 my ($lineBA, $lineBC, $lineAC, $k, $kk, $angle);
1315 my ($T, $cosT, $sinT) = (0.3, cos(0.3), sin(0.3));
1316
1317 $RAx = $Ax * $cosT + $Ay * $sinT;
1318 $RAy = -$Ax * $sinT + $Ay * $cosT;
1319 $RBx = $Bx * $cosT + $By * $sinT;
1320 $RBy = -$Bx * $sinT + $By * $cosT;
1321 $RCx = $Cx * $cosT + $Cy * $sinT;
1322 $RCy = -$Cx * $sinT + $Cy * $cosT;
1323 $deltax = $RBx - $RAx;
1324 $deltay = $RBy - $RAy;
1325 $lineBA = sqrt($deltax*$deltax + $deltay*$deltay);
1326 if ($lineBA < 0.0000001)
1327 {
1328 return -500;
1329 }
1330 $deltax = $RBx - $RCx;
1331 $deltay = $RBy - $RCy;
1332 $lineBC = sqrt($deltax*$deltax + $deltay*$deltay);
1333 if ($lineBC < 0.0000001)
1334 {
1335 return -500;
1336 }
1337 $deltax = $RAx - $RCx;
1338 $deltay = $RAy - $RCy;
1339 $lineAC = sqrt($deltax*$deltax + $deltay*$deltay);
1340 if ($lineAC < 0.0000001)
1341 {
1342 return -500;
1343 }
1344 $k = ($lineBA*$lineBA + $lineBC*$lineBC - $lineAC*$lineAC ) /
1345 (2 * $lineBA * $lineBC);
1346 $k = -1 if $k < -0.99999;
1347 $k = 1 if $k > 0.99999;
1348 $kk = $k * $k;
1349 if (($kk * $kk) == 1)
1350 {
1351 $angle = PI if $k == -1;
1352 $angle = 0 if $k == 1;
1353 }
1354 else
1355 {
1356 $angle = (PI / 2) - atan2($k / sqrt(1 - $kk),1);
1357 }
1358 return $angle * 180 / PI;
1359 }
1360
1361 @ The subroutine [[excircle]] computes the center and the radius of a circle that
1362 externally touches a given side (4th and 5th arguments) of triangle (determined
1363 by the 1rst, the 2nd and 3rd argument). Here are the details:
1364 <ul>
1365 <li> Let the triangle points be A, B, C, and the given side be BC.</li>
1366 <li> Now calculate the radius of Excircle = (triangle area)/(s - side length),
1367 where s = (a+b+c)/2</li>
1368 <li>Calculate the distance from the angle (A) (opposite the given side BC)
1369 to the excircle center = radius/sin(A/2)</li>
1370 <li> Now determine the the Excircle center by locating it on the angle bisector
1371 (i.e., same line that the IncircleCenter is on), but at distance d further
1372 away from angle A. So, we now have the Incircle center (I),
1373 determine deltaX and deltaY from I to A, calculate the distance AI,
1374 and then extend the line from I by distance d to Excenter Xc, Yc.</li>
1375 </ul>
1376
1377 <<subroutine <tt>excircle</tt> >>=
1378 sub excircle {
1379 my ($A, $B, $C, $D, $E) = @_;
1380 my ($Ax,$Ay,$Bx,$By,$Dx,$Dy,$Ex,$Ey,$ASVA,$ASA);
1381 ($Ax,$Ay,$ASVA,$ASA)=unpack("d3A*",$PointTable{$A});
1382 ($Bx,$By,$ASVA,$ASA)=unpack("d3A*",$PointTable{$B});
1383 ($Cx,$Cy,$ASVA,$ASA)=unpack("d3A*",$PointTable{$C});
1384 ($Dx,$Dy,$ASVA,$ASA)=unpack("d3A*",$PointTable{$D});
1385 ($Ex,$Ey,$ASVA,$ASA)=unpack("d3A*",$PointTable{$E});
1386 my ($sideA, $sideB, $sideC, $s, $R, $theAdeg, $d);
1387 my ($Xmypoint, $Ymypoint, $deltax, $deltay, $mylength, $xc, $yc);
1388
1389 $sideA = &Length($Bx, $By, $Cx, $Cy);
1390 $sideB = &Length($Cx, $Cy, $Ax, $Ay);
1391 $sideC = &Length($Ax, $Ay, $Bx, $By);
1392 $s = ($sideA + $sideB + $sideC) / 2;
1393 $R = triangleArea($Ax, $Ay, $Bx, $By, $Cx, $Cy) /
1394 ($s - &Length($Dx, $Dy, $Ex, $Ey));
1395 if (($D eq $A && $E eq $B) || ($D eq $B && $E eq $A))
1396 {
1397 $theAdeg = &Angle($Bx, $By, $Cx, $Cy, $Ax, $Ay);
1398 $Xmypoint = $Cx;
1399 $Ymypoint = $Cy;
1400 }
1401 elsif (($D eq $B && $E eq $C) || ($D eq $C && $E eq $B))
1402 {
1403 $theAdeg = &Angle($Cx, $Cy, $Ax, $Ay, $Bx, $By);
1404 $Xmypoint = $Ax;
1405 $Ymypoint = $Ay;
1406 }
1407 elsif (($D eq $C && $E eq $A) || ($D eq $A && $E eq $C))
1408 {
1409 $theAdeg = &Angle($Ax, $Ay, $Bx, $By, $Cx, $Cy);
1410 $Xmypoint = $Bx;
1411 $Ymypoint = $By;
1412 }
1413 else
1414 {
1415 return (0,0,0);
1416 }
1417 $d = $R / sin($theAdeg * PI / 180 / 2);
1418 my ($xIn, $yIn, $rin) = &IncircleCenter($Ax, $Ay, $Bx, $By, $Cx, $Cy);
1419 $deltax = $xIn - $Xmypoint;
1420 $deltay = $yIn - $Ymypoint;
1421 $mylength = sqrt($deltax*$deltax + $deltay*$deltay);
1422 $xc = $Xmypoint + $d * $deltax / $mylength;
1423 $yc = $Ymypoint + $d * $deltay / $mylength;
1424 return ($xc, $yc, $R);
1425 }
1426
1427 @ The [[DrawLineOrArrow]] subroutine is used to parse the arguments of the commands
1428 [[drawline]], [[drawthickline]], [[drawarrow]], [[drawthickarrow]] and
1429 [[drawCurve]]. In general, these commands have as arguments a list of points separated by
1430 commas that are used to draw a set of lines. The list of points is
1431 enclosed in parentheses. Here we give only the syntax of the [[drawline]]
1432 comma, as the syntax of the other commands is identical:
1433 <pre>
1434 drawline ::= "drawline" "(" Points { "," Points } ")"
1435 Points ::= Point { separator Point}
1436 separator ::= blank | empty
1437 </pre>
1438 In the following code we
1439 scan a list of points (possibly separated by blanks) and we stop when
1440 we encounter either a comma or some other character. In case we have found
1441 a comma, we check whether we have a [[drawline]] command and if this is
1442 the case we plot the list of points. We continue with the next list of points,
1443 until there are no more points. The inner while-loop is used to control the
1444 consumption of point tokens and the external to reset the array [[PP]] which
1445 holds the point names.
1446
1447 <<subroutine <tt>DrawLineOrArrow</tt> >>=
1448 sub DrawLineOrArrow {
1449 my $draw_Line = shift;
1450 my $lc = shift;
1451 my $lineLength = -1;
1452 my $stacklen = 0;
1453 my @PP = ();
1454 # if ($draw_Line != 2) {
1455 # s/\s*//;
1456 # if (s/^\[\s*//) { # optional length specifier
1457 # $lineLength = expr($lc);
1458 # if ($lineLength <= 0) {
1459 # PrintErrorMessage("length must greater than zero",$lc);
1460 # $lineLength = -1;
1461 # }
1462 # chk_rsb("optional part",$lc);
1463 # }
1464 # }
1465 chk_lparen("$cmd",$lc);
1466 DRAWLINES:while(1) {
1467 @PP = () ;
1468 while(1) {
1469 if (s/^([^\W\d_]\d{0,4})\s*//i) { #point name
1470 $P = $1;
1471 if (!exists($PointTable{lc($P)})) {
1472 PrintErrorMessage("Undefined point $P",$lc);
1473 }
1474 else {
1475 push (@PP,$P);
1476 }
1477 }
1478 else {
1479 $stacklen = @PP;
1480 if ($draw_Line != 2) {
1481 if ($stacklen <= 1) {
1482 PrintErrorMessage("Wrong number of points",$lc);
1483 }
1484 else {
1485 push(@PP,$lc);
1486 if ($draw_Line == 0) {
1487 drawarrows(@PP);
1488 }
1489 elsif ($draw_Line == 1) {
1490 drawlines(@PP);
1491 }
1492 }
1493 }
1494 if (s/^,\s*// and $draw_Line != 2) {
1495 next DRAWLINES;
1496 }
1497 else {
1498 last DRAWLINES;
1499 }
1500 }
1501 }
1502 }
1503 if ($draw_Line == 2) {
1504 $stacklen = @PP;
1505 if ($stacklen < 2) {
1506 PrintErrorMessage("Wrong number of points",$lc);
1507 }
1508 elsif ($stacklen % 2 == 0) {
1509 PrintErrorMessage("Number of points must be odd",$lc);
1510 }
1511 else {
1512 drawCurve(@PP);
1513 }
1514 }
1515 chk_rparen("arguments of $cmd",$lc);
1516 chk_comment($lc);
1517 }
1518
1519 @ The subroutine [[drawarrows]] is used to draw one or more lines. The subroutine
1520 accepts as argument an array which contains the names of the points which
1521 define the lines, plus the current program line number. Each arrow is printed
1522 using the following code:
1523 <center>
1524 <tt>\arrow < </tt>ArrowLength <tt> mm> [</tt> beta <tt>,</tt> gamma <tt>] from
1525 x1 y1 to x2 y2 </tt>
1526 </center>
1527 where beta is equal to tan([[$arrowAngleB]] * [[d2r]] /2) and gamma is equal to
1528 2*tan([[$arrowAngleC]] * [[d2r]] / 2).
1529
1530 <<subroutine <tt>drawarrows</tt> >>=
1531 sub drawarrows {
1532 my ($NoArgs);
1533 $NoArgs = @_;
1534 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
1535 my ($NumberOfPoints, $p, $q, $r12, $d12);
1536 my ($px,$py,$pSV,$pS, $qx,$qy,$qSV,$qS);
1537
1538 $NumberOfPoints = $NoArgs - 1;
1539 LOOP: for(my $i=0; $i < $NumberOfPoints - 1; $i++)
1540 {
1541 $p = $_[$i];
1542 $q = $_[$i+1];
1543 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
1544 ($qx,$qy,$qSV,$qS) = unpack("d3A*",$PointTable{lc($q)});
1545 $pSV = $defaultLFradius if $pSV == 0;
1546 $qSV = $defaultLFradius if $qSV == 0;
1547 $r12 = $pSV + $qSV;
1548 $d12 = Length($px,$py,$qx,$qy);
1549 if ($d12 <= $r12)
1550 {
1551 if($d12 == 0)
1552 {
1553 PrintErrorMessage("points $p and $q are the same", $lc);
1554 next LOOP;
1555 }
1556 PrintWarningMessage("arrow $p$q not drawn: points too close or ".
1557 "radii too big", $lc);
1558 next LOOP;
1559 }
1560 ($px, $py) = pointOnLine($px, $py, $qx, $qy, $pSV) if $pSV > 0;
1561 ($qx, $qy) = pointOnLine($qx, $qy, $px, $py, $qSV) if $qSV > 0;
1562 my ($beta, $gamma);
1563 $beta = tan($arrowAngleB * D2R / 2);
1564 $gamma = 2 * tan($arrowAngleC * D2R / 2);
1565 printf OUT "\\arrow <%.5f%s> [%.5f,%.5f] from %.5f %.5f to %.5f %.5f\n",
1566 $arrowLength, $arrowLengthUnits, $beta, $gamma, $px, $py, $qx, $qy;
1567 }
1568 }
1569
1570 @ The subroutine [[drawlines]] is used to draw one or more lines. The subroutine
1571 accepts as argument an array which contains the names of the points which
1572 define the lines, plus the current program line number. If there are only
1573 two points (i.e., only one line), then we output the following PiCTeX code:
1574 <center>
1575 <tt> \plot x1 y1 x2 y2 / %% pointname1 pointname2</tt>
1576 </center>
1577 If there are more than two points, then we need to write the PiCTeX code in
1578 pairs with two points on each line (just to keep things simple) as follows:
1579 <center>
1580 <tt> \plot x1 y1 x2 y2 / %% pointname1 pointname2</tt>
1581 <tt> \plot x2 y2 x3 y3 / %% pointname2 pointname3</tt>
1582 <tt> \plot x3 y3 x4 y4 / %% pointname3 pointname4</tt>
1583 </center>
1584 An important part of the subroutine is devoted to checking whether either
1585 or both of the pairs of points are associated with a line-free zone, and if
1586 so, then we must take care not to draw the line inside the line-free zone. If
1587 a point does have a line-free zone, then we use the [[pointOnLine]]
1588 subroutine to determine the point on the line which is just on the line-free
1589 boundary, and draw the line to the that point instead of to the exact
1590 point-location.
1591
1592 <<subroutine <tt>drawlines</tt> >>=
1593 sub drawlines {
1594 my ($NoArgs);
1595 $NoArgs = @_;
1596 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
1597 my ($NumberOfPoints, $p, $q, $r12, $d12);
1598 my ($px,$py,$pSV,$pS, $qx,$qy,$qSV,$qS);
1599
1600 $NumberOfPoints = $NoArgs - 1;
1601 LOOP: for(my $i=0; $i < $NumberOfPoints - 1; $i++)
1602 {
1603 $p = $_[$i];
1604 $q = $_[$i+1];
1605 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
1606 ($qx,$qy,$qSV,$qS) = unpack("d3A*",$PointTable{lc($q)});
1607 $pSV = $defaultLFradius if $pSV == 0;
1608 $qSV = $defaultLFradius if $qSV == 0;
1609 $r12 = $pSV + $qSV;
1610 $d12 = Length($px,$py,$qx,$qy);
1611 if ($d12 <= $r12)
1612 {
1613 if($d12 == 0)
1614 {
1615 PrintErrorMessage("points $p and $q are the same", $lc);
1616 next LOOP;
1617 }
1618 PrintWarningMessage("line $p$q not drawn: points too close or ".
1619 "radii too big", $lc);
1620 next LOOP;
1621 }
1622 ($px, $py) = pointOnLine($px, $py, $qx, $qy, $pSV) if $pSV > 0;
1623 ($qx, $qy) = pointOnLine($qx, $qy, $px, $py, $qSV) if $qSV > 0;
1624 if ($px == $qx || $py == $qy)
1625 {
1626 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f %%%% %s%s\n",
1627 $px,$py,$qx,$qy,$p,$q;
1628 }
1629 else
1630 {
1631 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / %%%% %s%s\n",
1632 $px, $py,$qx,$qy,$p,$q;
1633 }
1634 }
1635 }
1636
1637 @ The subroutine [[drawCurve]] is used to draw a curve that passes through an odd
1638 number of points. The subroutine has as argument an array which contains the names of the
1639 points which define the lines plus the current program line number. The subroutine
1640 emits code that has the following general form:
1641 <pre>
1642 \setquadratic
1643 \plot
1644 X1 Y1
1645 X2 Y2
1646 X3 Y3
1647 \setlinear
1648 </pre>
1649
1650
1651 <<subroutine <tt>drawCurve</tt> >>=
1652 sub drawCurve {
1653 my ($NoArgs);
1654 $NoArgs = @_;
1655 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
1656 my ($NumberOfPoints, $p);
1657
1658 $NumberOfPoints = $NoArgs - 1;
1659 print OUT "\\setquadratic\n\\plot\n";
1660 for(my $i=0; $i <= $NumberOfPoints; $i++)
1661 {
1662 $p = $_[$i];
1663 my ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
1664 printf OUT "\t%0.5f %0.5f", $px, $py;
1665 print OUT (($i == $NumberOfPoints) ? " / %$p\n" : " %$p\n");
1666 }
1667 print OUT "\\setlinear\n";
1668 }
1669
1670 @ The subroutine [[drawpoints]] is used to draw one or more points. The subroutine
1671 has as arguments a list of points. For each point we produce code that has
1672 the following general form:
1673 <center>
1674 <tt> \put {SYMBOL} at Px PY</tt>
1675 </center>
1676 where [[SYMBOL]] is either the default plot symbol, i.e., [[$\bullet$]],
1677 whatever the user has set with the [[PointSymbol]] command, or the plot
1678 symbol specified in the definition of the point.
1679
1680 <<subroutine <tt>drawpoints</tt> >>=
1681 sub drawpoints {
1682 my ($NumberOfPoints,$p);
1683 $NumberOfPoints = @_;
1684 my ($px,$py,$pSV,$pS);
1685
1686 for($i=0; $i < $NumberOfPoints; $i++)
1687 {
1688 $p = $_[$i];
1689 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
1690 if ($pS eq "" and $defaultsymbol =~ /circle|square/) {
1691 $pS = $defaultsymbol;
1692 }
1693 POINTSWITCH: {
1694 if ($pS eq "") # no plot symbol specified
1695 {
1696 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n",
1697 $defaultsymbol, $px, $py, $p;
1698 last POINTSWITCH;
1699 }
1700 if ($pS eq "circle") # plot symbol is a circle
1701 {
1702 my $radius = (defined($DimOfPoint{lc($p)})) ? $DimOfPoint{lc($p)} :
1703 $GlobalDimOfPoints;
1704 if ($radius > 0) # draw a circle using the current units
1705 {
1706 if ($radius == 1.5) # use \bigcirc
1707 {
1708 printf OUT "\\put {\$\\bigcirc\$} at %.5f %.5f %%%% %s\n",
1709 $px, $py, $p;
1710 }
1711 else
1712 {
1713 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f %%%% %s\n",
1714 $px+$radius, $py, $px, $py, $p;
1715 }
1716 }
1717 else #use \circ symbol
1718 {
1719 printf OUT "\\put {\$\\circ\$} at %.5f %.5f %%%% %s\n",
1720 $px,$py,$p;
1721 }
1722 last POINTSWITCH;
1723 }
1724 if ($pS eq "square")
1725 {
1726 my $side = (defined($DimOfPoint{lc($p)})) ? $DimOfPoint{lc($p)} :
1727 $GlobalDimOfPoints;
1728 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n",
1729 drawsquare($side), $px, $py, $p;
1730 last POINTSWITCH;
1731 }
1732 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n", $pS,$px,$py,$p;
1733 }
1734 }
1735 }
1736
1737 @ The subroutine [[drawAngleArc]] gets six arguments which correspond to
1738 three points defining an angle (variables [[$P1]], [[$P2]] and [[$P3]]),
1739 the radius, the internal/external specification and the direction
1740 specification (clockwise or anticlockwise).
1741 Depending on the values of these arguments, the subroutine
1742 returns the corresponding PiCTeX code, the general format of
1743 which is <pre>
1744 \circulararc Angle degrees from x y center at x2 y2
1745 </pre>
1746 where [[Angle]] is the angle that the three points P1 P2 P3 define
1747 (computed by subroutine [[Angle]]),
1748 and [[x]] and [[y]] are the coordinates of a point
1749 residing on line P2P1 at distance equal to a [[$radius]] from
1750 point [[$P2]]; and [[x2]], [[y2]] are the coordinates of the
1751 center of the circle about which the arc is drawn,
1752 i.e., point [[$P2]].
1753
1754 <<subroutine <tt>drawAngleArc</tt> >>=
1755 sub drawAngleArc {
1756 my ($P1, $P2, $P3, $radius, $inout, $direction) = @_;
1757 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
1758 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
1759 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
1760
1761 my $internalAngle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
1762 my $externalAngle = 360 - $internalAngle;
1763 my ($x, $y) = pointOnLine($x2, $y2, $x1, $y1, $radius);
1764 my $code = "";
1765 if ($inout eq "internal" and $direction eq "clockwise" ) {
1766 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1767 -1 * $internalAngle, $x, $y, $x2, $y2;
1768 }
1769 elsif ($inout eq "internal" and $direction eq "anticlockwise" ) {
1770 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1771 $internalAngle, $x, $y, $x2, $y2;
1772 }
1773 elsif ($inout eq "external" and $direction eq "clockwise" ) {
1774 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1775 -1 * $externalAngle, $x, $y, $x2, $y2;
1776 }
1777 elsif ($inout eq "external" and $direction eq "anticlockwise" ) {
1778 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1779 $externalAngle, $x, $y, $x2, $y2;
1780 }
1781 return $code;
1782 }
1783
1784 @ The subroutine [[drawAngleArrow]] gets six arguments which correspond to
1785 three points defining an angle (variables [[$P1]], [[$P2]] and [[$P3]]),
1786 the radius, the internal/external specification and the direction
1787 specification. The subroutine mainly draws the arrowhead, and
1788 calls the subroutine [[drawAngleArc]] to draw the
1789 arc part of the arrow.
1790
1791 <<subroutine <tt>drawAngleArrow</tt> >>=
1792 sub drawAngleArrow {
1793 my ($P1, $P2, $P3, $radius, $inout, $direction) = @_;
1794 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
1795 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
1796 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
1797
1798 my $code = drawAngleArc($P1, $P2, $P3, $radius, $inout, $direction);
1799
1800 my ($xqp, $yqp) = pointOnLine($x2, $y2, $x1, $y1, $radius);
1801 my ($deltax, $deltay) = ($x1 - $x2, $y1 - $y2);
1802 my $AL;
1803
1804 if ($xunits =~ /mm/) {
1805 $AL = 1;
1806 }
1807 elsif ($xunits =~ /cm/) {
1808 $AL = 0.1;
1809 }
1810 elsif ($xunits =~ /pt/) {
1811 $AL = 2.845;
1812 }
1813 elsif ($xunits =~ /bp/) {
1814 $AL = 2.835;
1815 }
1816 elsif ($xunits =~ /pc/) {
1817 $AL = 0.2371;
1818 }
1819 elsif ($xunits =~ /in/) {
1820 $AL = 0.03937;
1821 }
1822 elsif ($xunits =~ /dd/) {
1823 $AL = 2.659;
1824 }
1825 elsif ($xunits =~ /cc/) {
1826 $AL = 0.2216;
1827 }
1828 elsif ($xunits =~ /sp/) {
1829 $AL = 186467.98;
1830 }
1831 my $halfAL = $AL / 2;
1832 my $d = sqrt($radius * $radius - $halfAL * $halfAL);
1833 my $alpha = atan2($d / $halfAL, 1) * R2D;
1834 my $beta = 2 * (90 - $alpha);
1835 my $thetaqr;
1836 if (abs($deltay) < 0.00001) {
1837 if ($deltax > 0 ) {$thetaqr = 0 }
1838 elsif ($deltax < 0) {$thetaqr = -180}
1839 }
1840 else {
1841 if (abs($deltax) < 0.00001) {
1842 $thetaqr = 90;
1843 }
1844 else {
1845 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1846 }
1847 }
1848 my ($xqr, $yqr) = pointOnLine($x2, $y2, $x3, $y3, $radius);
1849 $deltax = $x3 - $x2;
1850 $deltay = $y3 - $y2;
1851 $alpha = atan2(sqrt($radius * $radius - $halfAL * $halfAL) / $halfAL, 1) /
1852 D2R;
1853 $beta = 2 * (90 - $alpha);
1854 LINE2 : {
1855 if (abs($deltax) < 0.00001) {
1856 if ($deltay > 0) { $thetaqr = 90 }
1857 elsif ($deltay < 0) { $thetaqr = - 90 }
1858 last LINE2;
1859 }
1860 else {
1861 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1862 }
1863 if (abs($deltay) < 0.00001) {
1864 if ($deltax > 0) { $thetaqr = 0 }
1865 elsif ($deltax < 0) { $thetaqr = -180 }
1866 last LINE2;
1867 }
1868 else {
1869 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1870 }
1871 if ($deltax < 0 and $deltay > 0) { $thetaqr += 180 }
1872 elsif ($deltax < 0 and $deltay < 0) { $thetaqr += 180 }
1873 elsif ($deltax > 0 and $deltay < 0) { $thetaqr += 360 }
1874 }
1875 my $xqrleft = $x2 + $radius * cos(($thetaqr + $beta) * D2R);
1876 my $yqrleft = $y2 + $radius * sin(($thetaqr + $beta) * D2R);
1877 my $xqrright = $x2 + $radius * cos(($thetaqr - $beta) * D2R);
1878 my $yqrright = $y2 + $radius * sin(($thetaqr - $beta) * D2R);
1879 if ($inout eq "internal" and $direction eq "clockwise") {
1880 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1881 $xqrleft, $yqrleft, $xqr, $yqr;
1882 }
1883 elsif ($inout eq "internal" and $direction eq "anticlockwise") {
1884 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1885 $xqrright, $yqrright, $xqr, $yqr;
1886 }
1887 elsif ($inout eq "external" and $direction eq "clockwise") {
1888 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1889 $xqrleft, $yqrleft, $xqr, $yqr;
1890 }
1891 elsif ($inout eq "external" and $direction eq "anticlockwise") {
1892 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1893 $xqrright, $yqrright, $xqr, $yqr;
1894 }
1895 return $code;
1896 }
1897
1898 @ The subroutine [[expr]] is used to parse an expression. We are using a
1899 recursive descent parser to parse and evaluate an expression. The
1900 general syntax of an expression is as follows:
1901 <pre>
1902 expr ::= term { addop term }
1903 addop ::= "+" | "-"
1904 term ::= factor { mulop factor }
1905 mulop ::= "*" | "/" | "rem"
1906 factor ::= primitive [ ** factor ]
1907 primitive ::= [ "+" | "-"] primitive | number | variable |
1908 pair-of-points | "(" expr ")" |
1909 "sin (" expr ")" | "cos (" expr ")" | "area (" ThreePoints ")" |
1910 "tan (" expr ")" | "exp (" expr ")" | "int" "(" expr ")" |
1911 "log (" expr ")" | "atan (" expr ")" | "sgn" "(" expr ")" |
1912 "sqrt (" expr ")" | "acos (" expr ")" | "asin (" expr ")" |
1913 "atan (" expr ")" | "_pi_" | "_e_" |
1914 "xcoord (" point ")" | "ycoord (" point ")" | "angle "(" ThreePoints ")"|
1915 "angledeg" "(" ThreePoints ")" | "direction" "(" TwoPoints ")" |
1916 "directiondeg" "(" TwoPoints ")" | "_linethickness_"
1917 </pre>
1918 Note that [[_pi_]] and [[_e_]] can be used to access the value of the constants
1919 Pi and e.
1920
1921 <<subroutine <tt>expr</tt> >>=
1922 sub expr {
1923 my $lc = $_[0];
1924 my($left,$op,$right);
1925
1926 $left = term($lc);
1927 while ($op = addop()) {
1928 $right = term($lc);
1929 if ($op eq '+')
1930 { $left += $right }
1931 else
1932 { $left -= $right }
1933 }
1934 return $left;
1935 }
1936
1937 sub addop {
1938 s/^([+-])// && $1;
1939 }
1940
1941 sub term {
1942 my $lc = $_[0];
1943 my ($left, $op, $right);
1944 $left = factor($lc);
1945 while ($op = mulop()) {
1946 $right = factor($lc);
1947 if ($op eq '*')
1948 { $left *= $right }
1949 elsif ($op =~ /rem/i) {
1950 eval {$left %= $right};
1951 PrintFatalError("Division by zero", $lc) if $@;
1952 }
1953 else {
1954 eval {$left /= $right};
1955 PrintFatalError("Division by zero", $lc) if $@;
1956 }
1957 }
1958 return $left;
1959 }
1960
1961 sub mulop {
1962 (s#^([*/])## || s/^(rem)//i) && lc($1);
1963 }
1964
1965 sub factor {
1966 my $lc = $_[0];
1967 my ($left);
1968
1969 $left = primitive($lc);
1970 if (s/^\*\*//) {
1971 $left **= factor($lc);
1972 }
1973 return $left;
1974 }
1975
1976 sub primitive {
1977 my $lc = $_[0];
1978 my $val;
1979 s/\s*//;
1980 if (s/^\(//) { #is it an expr in parentheses
1981 $val = expr($lc);
1982 s/^\)// || PrintErrorMessage("Missing right parenthesis", $lc);
1983 }
1984 elsif (s/^-//) { # is it a negated primitive
1985 $val = - primitive();
1986 }
1987 elsif (s/^\+//) { # is it a positive primitive
1988 $val = primitive();
1989 }
1990 elsif (s/^angledeg//i) {
1991 chk_lparen("angledeg",$lc);
1992 my $point_1 = get_point($lc);
1993 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1994 my $point_2 = get_point($lc);
1995 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1996 my $point_3 = get_point($lc);
1997 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
1998 my $d12 = Length($x1, $y1, $x2, $y2);
1999 my $d23 = Length($x2, $y2, $x3, $y3);
2000 my $d31 = Length($x3, $y3, $x1, $y1);
2001 if ( $d12 == 0 ) {
2002 PrintErrorMessage("points `$point_1' and `$point_2' are the same", $lc);
2003 $val = 0;
2004 }
2005 elsif ( $d23 == 0 ) {
2006 PrintErrorMessage("points `$point_2' and `$point_3' are the same", $lc);
2007 $val = 0;
2008 }
2009 elsif ( $d31 == 0 ) {
2010 PrintErrorMessage("points `$point_1' and `$point_3' are the same", $lc);
2011 $val = 0;
2012 }
2013 else {
2014 $val = Angle($x1, $y1, $x2, $y2, $x3, $y3);
2015 $val = 0 if $val == -500;
2016 }
2017 chk_rparen("Missing right parenthesis", $lc);
2018 }
2019 elsif (s/^angle//i) {
2020 chk_lparen("angle",$lc);
2021 my $point_1 = get_point($lc);
2022 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
2023 my $point_2 = get_point($lc);
2024 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
2025 my $point_3 = get_point($lc);
2026 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
2027 my $d12 = Length($x1, $y1, $x2, $y2);
2028 my $d23 = Length($x2, $y2, $x3, $y3);
2029 my $d31 = Length($x3, $y3, $x1, $y1);
2030 if ( $d12 == 0 ) {
2031 PrintErrorMessage("points `$point_1' and `$point_2' are the same", $lc);
2032 $val = 0;
2033 }
2034 elsif ( $d23 == 0 ) {
2035 PrintErrorMessage("points `$point_2' and `$point_3' are the same", $lc);
2036 $val = 0;
2037 }
2038 elsif ( $d31 == 0 ) {
2039 PrintErrorMessage("points `$point_1' and `$point_3' are the same", $lc);
2040 $val = 0;
2041 }
2042 else {
2043 $val = Angle($x1, $y1, $x2, $y2, $x3, $y3);
2044 if ($val == -500) {
2045 $val = 0;
2046 }
2047 else {
2048 $val = D2R * $val;
2049 }
2050 }
2051 chk_rparen("Missing right parenthesis", $lc);
2052 }
2053 elsif (s/^area//i) {
2054 chk_lparen("angledeg",$lc);
2055 my $point_1 = get_point($lc);
2056 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
2057 my $point_2 = get_point($lc);
2058 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
2059 my $point_3 = get_point($lc);
2060 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
2061 $val = triangleArea($x1, $y1, $x2, $y2, $x3, $y3);
2062 chk_rparen("Missing right parenthesis", $lc);
2063 }
2064 elsif (s/^asin//i) {
2065 chk_lparen("asin");
2066 $val = expr();
2067 PrintFatalError("Can't take asin of $val", $lc) if $val < -1 || $val > 1;
2068 $val = asin($val);
2069 chk_rparen("Missing right parenthesis", $lc);
2070 }
2071 elsif (s/^acos//i) {
2072 chk_lparen("acos");
2073 $val = expr();
2074 PrintFatalError("Can't take acos of $val", $lc) if $val < -1 || $val > 1 ;
2075 $val = acos($val);
2076 chk_rparen("Missing right parenthesis", $lc);
2077 }
2078 elsif (s/^atan//i) {
2079 chk_lparen("atan");
2080 $val = expr();
2081 $val = atan($val);
2082 chk_rparen("Missing right parenthesis", $lc);
2083 }
2084 elsif (s/^cos//i) {
2085 chk_lparen("cos");
2086 $val = expr();
2087 $val = cos($val);
2088 chk_rparen("Missing right parenthesis", $lc);
2089 }
2090 elsif (s/^directiondeg//i) {
2091 chk_lparen("directiondeg",$lc);
2092 my $point_1 = get_point($lc);
2093 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
2094 my $point_2 = get_point($lc);
2095 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
2096 my $x3 = $x1+1;
2097 if ( ($y2 - $y1) >= 0) {
2098 $val = Angle($x3, $y1, $x1, $y1, $x2, $y2);
2099 $val = 0 if $val == -500;
2100 }
2101 else {
2102 $val = 360 - Angle($x3, $y1, $x1, $y1, $x2, $y2);
2103 $val = 0 if $val == -500;
2104 }
2105 chk_rparen("Missing right parenthesis", $lc);
2106 }
2107 elsif (s/^direction//i) {
2108 chk_lparen("direction",$lc);
2109 my $point_1 = get_point($lc);
2110 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
2111 my $point_2 = get_point($lc);
2112 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
2113 my $x3 = $x1+1;
2114 if ( ($y2 - $y1) >= 0) {
2115 $val = Angle($x3, $y1, $x1, $y1, $x2, $y2);
2116 $val = 0 if $val == -500;
2117 $val = D2R * $val;
2118 }
2119 else {
2120 $val = 360 - Angle($x3, $y1, $x1, $y1, $x2, $y2);
2121 $val = 0 if $val == -500;
2122 $val = D2R * $val;
2123 }
2124 chk_rparen("Missing right parenthesis", $lc);
2125 }
2126 elsif (s/^exp//i) {
2127 chk_lparen("exp");
2128 $val = expr();
2129 $val = exp($val);
2130 chk_rparen("Missing right parenthesis", $lc);
2131 }
2132 elsif (s/^int//i) {
2133 chk_lparen("int");
2134 $val = expr();
2135 $val = int($val);
2136 chk_rparen("Missing right parenthesis", $lc);
2137 }
2138 elsif (s/^log//i) {
2139 chk_lparen("log");
2140 $val = expr();
2141 PrintFatalError("Can't take log of $val", $lc) if $val <= 0;
2142 $val = log($val);
2143 chk_rparen("Missing right parenthesis", $lc);
2144 }
2145 elsif (s/^sin//i) {
2146 chk_lparen("sin");
2147 $val = expr();
2148 $val = sin($val);
2149 chk_rparen("Missing right parenthesis", $lc);
2150 }
2151 elsif (s/^sgn//i) {
2152 chk_lparen("sgn");
2153 $val = expr();
2154 if ($val > 0) {
2155 $val = 1;
2156 }
2157 elsif ($val == 0) {
2158 $val = 0;
2159 }
2160 else {
2161 $val = -1;
2162 }
2163 chk_rparen("Missing right parenthesis", $lc);
2164 }
2165 elsif (s/^sqrt//i) {
2166 chk_lparen("sqrt");
2167 $val = expr();
2168 $val = sqrt($val);
2169 chk_rparen("Missing right parenthesis", $lc);
2170 }
2171 elsif (s/^tan//i) {
2172 chk_lparen("tan");
2173 $val = expr();
2174 $val = sin($val)/cos($val);
2175 chk_rparen("Missing right parenthesis", $lc);
2176 }
2177 elsif (s/^xcoord//i) {
2178 chk_lparen("xcoord");
2179 my $point_name = get_point;
2180 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_name});
2181 $val = $x1;
2182 chk_rparen("Missing right parenthesis", $lc);
2183 }
2184 elsif (s/^ycoord//i) {
2185 chk_lparen("ycoord");
2186 my $point_name = get_point;
2187 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_name});
2188 $val = $y1;
2189 chk_rparen("Missing right parenthesis", $lc);
2190 }
2191 elsif (s/^_pi_//i) {
2192 $val = PI;
2193 }
2194 elsif (s/^_e_//i) {
2195 $val = 2.71828182845905;
2196 }
2197 elsif (s/^_linethickness_//i) {
2198 $val = $LineThickness / $xunits;
2199 }
2200 else {
2201 my $err_code;
2202 ($val,$err_code) = ComputeDist($lc);
2203 }
2204 s/\s*//;
2205 return $val;
2206 }
2207
2208 @ The subroutine [[memberOf]] is used to check whether a string is part of
2209 a list of strings. We assume that the first argument is the string in
2210 question. We compare each list element against the string in question and
2211 if we find it we stop and return the value [[1]] (denoting truth). Otherwise,
2212 we simply return the value [[0]] (denoting false).
2213
2214 <<subroutine <tt>memberOf</tt> >>=
2215 sub memberOf {
2216 my $elem = shift(@_);
2217
2218 my $found = 0;
2219 foreach $item (@_){
2220 if ($item eq $elem){
2221 $found = 1;
2222 last;
2223 }
2224 }
2225 return $found;
2226 }
2227
2228 @ The subroutine [[midpoint]] computes the coordinates of the midpoint of two points
2229 by means of the simple formula:
2230 <center>
2231 m<sub>x</sub>=x<sub>1</sub>+(y<sub>2</sub> - y<sub>1</sub>)/2 <br>
2232 m<sub>y</sub>=y<sub>1</sub>+(x<sub>2</sub> - x<sub>1</sub>)/2
2233 </center>
2234
2235 <<subroutine <tt>midpoint</tt> >>=
2236 sub midpoint {
2237 my ($x1, $y1, $x2, $y2)=@_;
2238 return ($x1 + ($x2 - $x1)/2,
2239 $y1 + ($y2 - $y1)/2);
2240 }
2241
2242
2243 @ The subroutine [[tand]] computes the tangent of an angle. The angle is
2244 supposed to be in degrees. We simply transform it into radians and then
2245 compute the actual result.
2246
2247 <<subroutine <tt>tand</tt> >>=
2248 sub tand {
2249 my $d = $_[0];
2250 $d = $d * PI / 180;
2251 return sin($d)/cos($d);
2252 }
2253
2254 @ The subroutine [[get_string]] is used to extract a leading valid mathspic string
2255 from the input line. A string must start with a quotation mark, i.e., [["]],
2256 and must end with the same symbol. A string may contain quotation marks which
2257 must be escaped with a backslash, i.e., [[\]]. Initially, we remove all
2258 leading white space. If the next character of the string is not a quotation
2259 mark we print an error message and stop. Otherwise, we split the string into
2260 an array of characters and store the characters up to the next quotation
2261 mark to the array [[@cmd]]. In case the next character is a backslash and
2262 we aren't at the end of the input string and the next character is a
2263 quotation mark, we have an escape sequence. This means that we store these
2264 two characters in the [[@cmd]] array and skip to characters after the quotation
2265 mark. Otherwise, we simply store the character in the [[@cmd]] array and
2266 skip to the next character. This process is repeated until either we consume
2267 all the characters of the string or until we find a sole quotation mark.
2268 Since we are not sure what has forced the loop to exit, we check whether
2269 there are still characters in the input string and we check whether this
2270 is a quotation mark. If these tests fail we have a string without a
2271 closing quotation mark. In all cases we return a triplet consisting of
2272 a number denoting success (1) or failure (0) and what we have consumed
2273 from the input string, and what is left from the input string.
2274
2275 <<subroutine <tt>get_string</tt> >>=
2276 sub get_string {
2277 my $string = shift;
2278 my $lc = shift;
2279
2280 $string =~ s/^\s+//;
2281 if ($string !~ s/^\"//) {
2282 PrintErrorMessage("No starting \" found",$lc);
2283 return (1,$string,$string);
2284 }
2285 my @ch = split //,$string;
2286 my @cmd;
2287 while (@ch and $ch[0] ne "\"") {
2288 if ($ch[0] eq "\\" and (defined $ch[1]) and $ch[1] eq "\"") {
2289 shift @ch;
2290 push @cmd, $ch[0];
2291 shift @ch;
2292 }
2293 else {
2294 push @cmd, $ch[0];
2295 shift @ch;
2296 }
2297 }
2298 if (! defined $ch[0]) {
2299 PrintErrorMessage("No closing \" found",$lc);
2300 return (1,join("",@cmd), join("",@ch))
2301 }
2302 else {
2303 shift @ch;
2304 return (0, join("",@cmd), join("",@ch))
2305 }
2306 }
2307
2308 @ The definition as well as an explanation of the functionality of the
2309 following subroutine can be found in "Programming Perl", 3rd edition.
2310
2311 <<subroutine <tt>is_tainted</tt> >>=
2312 sub is_tainted {
2313 my $arg = shift;
2314 my $nada = substr($arg,0,0);
2315 local $@;
2316 eval { eval "# $nada"};
2317 return length($@) != 0;
2318 }
2319
2320 @ The subroutine [[noOfDigits]] has one argument which is a number and returns
2321 the number of decimal digits it has. If the number matches the regular
2322 expression [[^\d+(?!\.)]] (a series of digits <i>not</i> followed by a
2323 period), then the number of decimal digits is zero. If the
2324 number matches the
2325 regular expression [[^\d+\.(\d+)?]], then number of decimal digits equals
2326 [[length($1)]]. Naturally, it maybe zero!
2327
2328 <<subroutine <tt>noOfDigits</tt> >>=
2329 sub noOfDigits {
2330 my $num = $_[0];
2331
2332 if ($num =~ /^[\+-]?\d+(?!\.)/) {
2333 return 0;
2334 }
2335 elsif ($num =~ /^[\+-]\d+\.(\d+)?/) {
2336 return length($1);
2337 }
2338 }
2339
2340 @ Subroutine [[drawsquare]] is use by the [[drawpoints]] routine to plot a
2341 point whose point symbol is a square. The subroutine has one argument, which is
2342 equal to the radius of the point. From this argument it computes the side of
2343 the square.
2344
2345 <<subroutine <tt>drawsquare</tt> >>=
2346 sub drawsquare {
2347 my $s = $_[0];
2348 #$s *= sqrt(2);
2349 $s = sprintf "%.5f", $s;
2350 my $code = "\\setlength{\\unitlength}{$xunits}%\n";
2351 $code .= "\\begin{picture}($s,$s)\\put(0,0)" .
2352 "{\\framebox($s,$s){}}\\end{picture}";
2353 return $code;
2354 }
2355
2356 @ Subroutine [[X2sp]] has two arguments: a number and a length unit. It returns
2357 the length expresssed in sp units.
2358
2359 <<subroutine <tt>X2sp</tt> >>=
2360 sub X2sp {
2361 my $LT = shift;
2362 my $units = shift;
2363
2364 if ($units eq "pc") {
2365 return $LT * 786432;
2366 }
2367 elsif ($units eq "pt") {
2368 return $LT * 65536;
2369 }
2370 elsif ($units eq "in") {
2371 return $LT * 4736286.72;
2372 }
2373 elsif ($units eq "bp") {
2374 return $LT * 65781.76;
2375 }
2376 elsif ($units eq "cm") {
2377 return $LT * 1864679.811023622;
2378 }
2379 elsif ($units eq "mm") {
2380 return $LT * 186467.981102362;
2381 }
2382 elsif ($units eq "dd") {
2383 return $LT * 70124.086430424;
2384 }
2385 elsif ($units eq "cc") {
2386 return $LT * 841489.037165082;
2387 }
2388 elsif ($units eq "sp") {
2389 return $LT;
2390 }
2391 }
2392
2393
2394 @ Subroutine [[sp2X]] has two arguments: a number that denotes a length in sp units
2395 and a length unit. It returns the length expresssed in units that are specified by
2396 the second argument.
2397
2398 <<subroutine <tt>sp2X</tt> >>=
2399 sub sp2X {
2400 my $LT = shift;
2401 my $units = shift;
2402
2403 if ($units eq "pc") {
2404 return $LT / 786432;
2405 }
2406 elsif ($units eq "pt") {
2407 return $LT / 65536;
2408 }
2409 elsif ($units eq "in") {
2410 return $LT / 4736286.72;
2411 }
2412 elsif ($units eq "bp") {
2413 return $LT / 65781.76;
2414 }
2415 elsif ($units eq "cm") {
2416 return $LT / 1864679.811023622;
2417 }
2418 elsif ($units eq "mm") {
2419 return $LT / 186467.981102362;
2420 }
2421 elsif ($units eq "dd") {
2422 return $LT / 70124.086430424;
2423 }
2424 elsif ($units eq "cc") {
2425 return $LT / 841489.037165082;
2426 }
2427 elsif ($units eq "sp") {
2428 return $LT;
2429 }
2430 }
2431
2432 @ Subroutine [[setLineThickness]] takes two arguments: the value of the variable
2433 [[$xunits]] and a string denoting the linethickness. It returns the linthickness
2434 expressed in the units of the [[$xunits]].
2435
2436 <<subroutine <tt>setLineThickness</tt> >>=
2437 sub setLineThickness {
2438 my $Xunits = shift;
2439 my $LT = shift;
2440 $Xunits =~ s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//;
2441 my $xlength = "$1";
2442 $Xunits =~ s/^\s*($units)//;
2443 my $x_in_units = $1;
2444 $LT =~ s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//;
2445 my $LTlength = "$1";
2446 $LT =~ s/^\s*($units)//;
2447 my $LT_in_units = $1;
2448 $LTlength = X2sp($LTlength,$LT_in_units);
2449 $LTlength = sp2X($LTlength,$x_in_units);
2450 return $LTlength;
2451 }
2452
2453 @ The subroutine [[process_input]] accepts one argument which is a file handle
2454 that corresponds to the file that the subroutine is supposed to process.
2455 The processing cycle is fairly simple: we input one line at the time, remove
2456 any leading space characters and the trailing new line character, and then
2457 start the actual processing. The variable [[$INFILE]] contains the name of
2458 the input file and the variable [[$lc]] is the local line counter. The
2459 commands [[beginSkip]] and [[endSkip]] can be used to ignore blocks
2460 of code and so we need to process them here. The variable [[$no_output]]
2461 is used as a switch to toggle from process mode to no-precess mode.
2462 If the first token is [[beginSkip]], we set the variable [[$no_output]] to 1,
2463 print a comment to the output file and continue with the next input line.
2464 If the first token is [[endSkip]], we check whether we are in a no-process
2465 mode. If this is the case, we revert to process mode; otherwise we print
2466 an error message. Finally, depending on whether we are in process or no-process
2467 mode we process the input text or simply printed commented out to the output
2468 file. Note, that we don't allow nested comment blocks, as this makes really
2469 no sense!
2470
2471 <<subroutine <tt>process_input</tt> >>=
2472 sub process_input {
2473 my ($INFILE,$currInFile) = @_;
2474 my $lc = 0;
2475 my $no_output = 0;
2476 $curr_in_file = $currInFile;
2477 LINE: while(<$INFILE>) {
2478 $lc++;
2479 chomp($command = $_);
2480 s/^\s+//;
2481 if (/^beginSkip\s*/i) {
2482 $no_output = 1;
2483 print OUT "%%$_" if $comments_on;
2484 next LINE;
2485 }
2486 elsif (/^endSkip\s*/i) {
2487 if ($no_output == 0) {
2488 PrintErrorMessage("endSkip without beginSkip",$lc);
2489 }
2490 else {
2491 $no_output = 0;
2492 }
2493 print OUT "%%$_" if $comments_on and !$no_output;
2494 next LINE;
2495 }
2496 elsif ($no_output == 1) {
2497 next LINE;
2498 }
2499 else {
2500 if (/^[^\\]/) {
2501 my $out_line = mpp($command,$lc) unless /^\\/; #call macro pre-processor
2502 $_ = "$out_line\n";
2503 }
2504 <<process input line>>
2505 }
2506 }
2507 }
2508
2509 @ Each command line starts with a particular <i>token</i> and depending on
2510 which one we have we perform different actions. If the first character
2511 is [[%]] we have a comment line, and depending on the value of the variable
2512 [[$comments_on]] we either output the comment on the output file (default
2513 action) or just ignore it and continue with the next input line. In case the
2514 first token is the name of a valid command we process the command and
2515 output the corresponding code. Otherwise, we print an error message to
2516 the screen and to the log file and continue with the next input line.
2517 Note that the input language is case-insensitive and so one is free to write a
2518 command name using any combination of upper and lower case
2519 letters, e.g., the tokens [[lAtEx]],
2520 [[LaTeX]], and [[latex]] are considered exactly the same.
2521 The valid <i>MathsPIC</i> commands are the following (don't pay attention
2522 to the case!):
2523 <ul>
2524 <li>
2525 Commands [[drawAngleArc]] and [[drawAngleArrow]] are used to draw an arc and an
2526 arrow, respectively. Since, their user interface is identical, we process
2527 them as if they were identical commands.
2528 </li>
2529 <li>
2530 Command [[drawcircle]] is used to draw a circle with a specified radius.
2531 </li>
2532 <li>
2533 Command [[drawCircumCircle]] is used to draw the circumcircle of triangle
2534 specified by three points.
2535 </li>
2536 <li>
2537 Command [[drawexcircle]] is used to draw the excircle of triangle
2538 relative to a given side of the triangle.
2539 </li>
2540 <li>
2541 Command [[drawincircle]] is used to draw the incircle of triangle.
2542 </li>
2543 <li>
2544 Command [[drawincurve]] is used to draw a curve that passes through a number of points.
2545 </li>
2546 <li> Command [[drawline]] is used to draw either
2547 a line (not necessarily a straight one) or a number of lines from a list
2548 or lists of points. The lines are specified as pairs of points that can
2549 be separated by blank spaces.
2550 <li> Command [[drawthickline]] is used to draw either
2551 a thick line (not necessarily a straight one) or a number of lines from a list
2552 or lists of points. The lines are specified as pairs of points that can
2553 be separated by blank spaces.
2554 </li>
2555 <li>
2556 Command [[drawPerpendicular]] draws a perpendicular line from point A to
2557 line BC.
2558 </li>
2559 <li> Command [[drawpoint]] is used to draw one, two or more points.
2560 The point names can be separated by blanks.
2561 </li>
2562 <li>
2563 Command [[drawRightAngle]] draws an angle, specified by three points,
2564 of a size specified by a side length.
2565 </li>
2566 <li>
2567 Command [[drawsquare]] draws a square, centered at the coordinates of the
2568 first arguments, which is assumed to be a point, with side equal to the
2569 second argument.
2570 </li>
2571 <li>
2572 Command [[inputfile*]] is used to verbatim include a file into the output
2573 file.
2574 </li>
2575 <li>
2576 Command [[inputfile]] is used to include a <i>MathsPIC</i> program file
2577 into the main file.
2578 </li>
2579 <li>
2580 Command [[linethickness]] should be used to set the thickness of lines.
2581 </li>
2582 <li>
2583 The [[paper]] command sets the paper scale, size, axes, etc. The most
2584 general format of the command follows:
2585 <center>
2586 <tt>paper{units(mm), xrange(0,120), yrange(0,100),axes(LRTB)}</tt>
2587 </center>
2588 Note, that one may opt not to write the commas between the different
2589 parts of command.
2590 </li>
2591 <li>
2592 Command [[point*]] allocates <i>new</i> co-ordinates and optionally
2593 a T<sub>E</sub>X point-name, to an existing point-name.
2594 Command [[point]] allocates co-ordinates and, optionally a T<sub>E</sub>X
2595 point character, to a <i>new</i> point-name. Since, both commands have
2596 identical syntax, we handle them together.
2597 </li>
2598 <li> Command [[PointSymbol]] is used to set or reset the default
2599 point symbol, i.e., when one plots a point this is the symbol that will
2600 appear on the final DVI/PostScript file.
2601 </li>
2602 <li>
2603 In the original DOS version of <tt>mathspic</tt> the command
2604 [[setPointNumber]] was used to set the length of the arrays that keep the
2605 various point related information. Since, in Perl arrays are dynamic objects
2606 and one can push as many objects as he/she wants, the command is implemented
2607 as an no-op. For reasons of compatibility, we only check the syntax of the
2608 command.
2609 </li>
2610 <li>
2611 Commands [[showAngle]] and [[showArea]] can be used to get
2612 the angle or the area determined by three points. In addition, the command
2613 [[showLenght]] can be used to get the length between two points. These three
2614 commands produce a comment to the output file.
2615 </li>
2616 <li> The [[system]] command provides a shell escape.
2617 </li>
2618 <li>
2619 The [[text]] command is used to put a symbol/text at a
2620 particular point location.
2621 </li>
2622 <li>
2623 Command [[var]] is used to store a numeric value into a comma separated
2624 list of variables.
2625 </li>
2626 <li>
2627 Command [[const]] is used to store a numeric value into a comma separated
2628 list of variables, whose value cannot be altered.
2629 </li>
2630 <li>
2631 If a line starts with a backslash, [[\]], then we copy verbatim this
2632 line to the output file. In case the second character is a space character,
2633 then we simply output a copy of the line without the leading backslash.
2634 </li>
2635 </ul>
2636 Empty lines are always ignored.
2637
2638 <<process input line>>=
2639
2640 if (/^\s*%/)
2641 {
2642 print OUT "$_" if $comments_on;
2643 }
2644 elsif (s/^\s*(beginloop(?=\W))//i) {
2645 s/\s+//;
2646 my $times = expr($lc);
2647 print OUT "%% BEGINLOOP $times\n" if $comments_on;
2648 my @C = ();
2649 REPEATCOMMS: while (<$INFILE>) {
2650 if (/^\s*endloop/i) {
2651 last REPEATCOMMS;
2652 }
2653 else {
2654 push @C, $_;
2655 }
2656 }
2657 if (! /^\s*endloop/i) {
2658 PrintFatalError("unexpected end of file",$lc);
2659 }
2660 else {
2661 s/^\s*endloop//i;
2662 for(my $i=1; $i<=$times; $i++) {
2663 tie *DUMMY, 'DummyFH', \@C;
2664 process_input(DUMMY, $currInFile);
2665 untie *DUMMY;
2666 }
2667 print OUT "%% ENDLOOP\n" if $comments_on;
2668 }
2669 }
2670 elsif (s/^\s*(ArrowShape(?=\W))//i)
2671 {
2672 my $cmd = $1;
2673 print OUT "%% $cmd$_" if $comments_on;
2674 <<process <tt>ArrowShape</tt> command>>
2675 }
2676 elsif (s/^\s*(const(?=\W))//i)
2677 {
2678 print OUT "%% $1$_" if $comments_on;
2679 <<process <tt>const</tt> command>>
2680 }
2681 elsif (s/^\s*(dasharray(?=\W))//i)
2682 {
2683 my ($cmd) = $1;
2684 print OUT "%% $cmd$_" if $comments_on;
2685 <<process <tt>dasharray</tt> command>>
2686 }
2687 elsif (s/^\s*(drawAngleArc(?=\W))//i or s/^\s*(drawAngleArrow(?=\W))//i )
2688 {
2689 my $cmd = $1;
2690 print OUT "%% $cmd$_" if $comments_on;
2691 <<process <tt>drawAngleArcOrArrow</tt> command>>
2692 }
2693 elsif (s/^\s*(drawArrow(?=\W))//i)
2694 {
2695 my ($cmd) = $1;
2696 print OUT "%% $cmd$_" if $comments_on;
2697 DrawLineOrArrow(0,$lc);
2698 }
2699 elsif (s/^\s*(drawcircle(?=\W))//i)
2700 {
2701 my ($cmd) = $1;
2702 print OUT "%% $cmd$_" if $comments_on;
2703 <<process <tt>drawcircle</tt> command>>
2704 }
2705 elsif (s/^\s*(drawcurve(?=\W))//i)
2706 {
2707 my ($cmd) = $1;
2708 print OUT "%% $cmd$_" if $comments_on;
2709 DrawLineOrArrow(2,$lc);
2710 }
2711 elsif (s/^\s*(drawcircumcircle(?=\W))//i)
2712 {
2713 my ($cmd) = $1;
2714 print OUT "%% $cmd$_" if $comments_on;
2715 <<process <tt>drawcircumcircle</tt> command>>
2716 }
2717 elsif (s/^\s*(drawexcircle(?=\W))//i)
2718 {
2719 my ($cmd) = $1;
2720 print OUT "%% $cmd$_" if $comments_on;
2721 <<process <tt>drawexcircle</tt> command>>
2722 }
2723 elsif (s/^\s*(drawincircle(?=\W))//i)
2724 {
2725 my ($cmd) = $1;
2726 print OUT "%% $cmd$_" if $comments_on;
2727 <<process <tt>drawincircle</tt> command>>
2728 }
2729 elsif (s/^\s*(drawline(?=\W))//i)
2730 {
2731 my ($cmd) = $1;
2732 print OUT "%% $cmd$_" if $comments_on;
2733 DrawLineOrArrow(1,$lc);
2734 }
2735 elsif (s/^\s*(drawthickarrow(?=\W))//i)
2736 {
2737 my ($cmd) = $1;
2738 print OUT "%% $cmd$_" if $comments_on;
2739 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\large .})%\n";
2740 print OUT "{\\setbox1=\\hbox{\\usefont{OT1}{cmr}{m}{n}\\large .}%\n";
2741 print OUT " \\global\\linethickness=0.31\\wd1}%\n";
2742 DrawLineOrArrow(0,$lc);
2743 print OUT "\\setlength{\\linethickness}{0.4pt}%\n";
2744 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
2745 }
2746 elsif (s/^\s*(drawthickline(?=\W))//i)
2747 {
2748 my ($cmd) = $1;
2749 print OUT "%% $cmd$_" if $comments_on;
2750 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\large .})%\n";
2751 print OUT "{\\setbox1=\\hbox{\\usefont{OT1}{cmr}{m}{n}\\large .}%\n";
2752 print OUT " \\global\\linethickness=0.31\\wd1}%\n";
2753 DrawLineOrArrow(1,$lc);
2754 print OUT "\\setlength{\\linethickness}{0.4pt}%\n";
2755 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
2756 }
2757 elsif (s/^\s*(drawperpendicular(?=\W))//i)
2758 {
2759 my ($cmd) = $1;
2760 print OUT "%% $cmd$_" if $comments_on;
2761 <<process <tt>drawPerpendicular</tt> command>>
2762 }
2763 elsif (s/^\s*(drawpoint(?=\W))//i)
2764 {
2765 my ($cmd) = $1;
2766 print OUT "%% $cmd$_" if $comments_on;
2767 <<process <tt>drawpoint</tt> command>>
2768 }
2769 elsif (s/^\s*(drawRightAngle(?=\W))//i)
2770 {
2771 my ($cmd) = $1;
2772 print OUT "%% $cmd$_" if $comments_on;
2773 <<process <tt>drawRightAngle</tt> command>>
2774 }
2775 elsif (s/^\s*(drawsquare(?=\W))//i)
2776 {
2777 my ($cmd) = $1;
2778 print OUT "%% $cmd$_" if $comments_on;
2779 <<process <tt>drawsquare</tt> command>>
2780 }
2781 elsif (s/^\s*inputfile\*//i)
2782 {
2783 <<process <tt>inputfile*</tt> command>>
2784 }
2785 elsif (s/^\s*(inputfile(?=\W))//i)
2786 {
2787 my ($cmd) = $1;
2788 print OUT "%% $cmd$_" if $comments_on;
2789 <<process <tt>inputfile</tt> command>>
2790 }
2791 elsif (s/^\s*(linethickness(?=\W))//i)
2792 {
2793 my $cmd = $1;
2794 print OUT "%% $cmd$_" if $comments_on;
2795 <<process <tt>linethickness</tt> command>>
2796 }
2797 elsif (s/^\s*(paper(?=\W))//i)
2798 {
2799 my ($cmd) = $1;
2800 print OUT "%% $cmd$_" if $comments_on;
2801 <<process <tt>paper</tt> command>>
2802 }
2803 elsif (s/^\s*(PointSymbol(?=\W))//i)
2804 {
2805 my $cmd = $1;
2806 print OUT "%% $cmd$_" if $comments_on;
2807 <<process <tt>PointSymbol</tt> command>>
2808 }
2809 elsif (s/^\s*point(?=\W)//i)
2810 {
2811 my ($Point_Line);
2812 chomp($Point_Line=$_);
2813 <<process <tt>point/point*</tt> commands>>
2814 }
2815 elsif (/^\s*setPointNumber(?=\W)/i)
2816 {
2817 PrintWarningMessage("Command setPointNumber is ignored",$lc);
2818 next LINE;
2819 }
2820 elsif (s/^\s*(showAngle(?=\W))//i)
2821 {
2822 <<process <tt>showAngle</tt> command>>
2823 }
2824 elsif (s/^\s*(showArea(?=\W))//i)
2825 {
2826 <<process <tt>showArea</tt> command>>
2827 }
2828 elsif (s/^\s*(showLength(?=\W))//i)
2829 {
2830 <<process <tt>showLength</tt> command>>
2831 }
2832 elsif (/^\s*showPoints(?=\W)/i)
2833 {
2834 print OUT "%%-------------------------------------------------\n";
2835 print OUT "%% L I S T O F P O I N T S \n";
2836 print OUT "%%-------------------------------------------------\n";
2837 foreach my $p (keys(%PointTable)) {
2838 my ($x, $y, $pSV, $pS) = unpack("d3A*",$PointTable{$p});
2839 printf OUT "%%%%\t%s\t= ( %.5f, %.5f ), LF-radius = %.5f, symbol = %s\n",
2840 $p, $x, $y, $pSV, $pS;
2841 }
2842 print OUT "%%-------------------------------------------------\n";
2843 print OUT "%% E N D O F L I S T O F P O I N T S \n";
2844 print OUT "%%-------------------------------------------------\n";
2845 next LINE;
2846 }
2847 elsif (/^\s*showVariables(?=\W)/i)
2848 {
2849 print OUT "%%-------------------------------------------------\n";
2850 print OUT "%% L I S T O F V A R I A B L E S \n";
2851 print OUT "%%-------------------------------------------------\n";
2852 foreach my $var (keys(%VarTable)) {
2853 print OUT "%%\t", $var, "\t=\t", $VarTable{$var}, "\n";
2854 }
2855 print OUT "%%-------------------------------------------------\n";
2856 print OUT "%% E N D O F L I S T O F V A R I A B L E S \n";
2857 print OUT "%%-------------------------------------------------\n";
2858 next LINE;
2859 }
2860 elsif (s/^\s*(system(?=\W))//i)
2861 {
2862 print OUT "%% $1$_" if $comments_on;
2863 <<process <tt>system</tt> command>>
2864 }
2865 elsif (s/^\s*(text(?=\W))//i)
2866 {
2867 print OUT "%% $1$_" if $comments_on;
2868 <<process <tt>text</tt> command>>
2869 }
2870 elsif (s/^\s*(var(?=\W))//i)
2871 {
2872 print OUT "%% $1$_" if $comments_on;
2873 <<process <tt>var</tt> command>>
2874 }
2875 elsif (/^\s*\\(.+)/)
2876 {
2877 my $line = $1;
2878 if ($line =~ /^\s+(.+)/)
2879 {
2880 print OUT " $line\n";
2881 }
2882 else
2883 {
2884 print OUT "\\$line\n";
2885 }
2886 next LINE;
2887 }
2888 elsif (0==length) #empty line
2889 {
2890 next LINE;
2891 }
2892 else {
2893 PrintErrorMessage("command not recognized",$lc);
2894 next LINE;
2895 }
2896
2897 @ Command [[dasharray]] takes an arbitrary number of arguments that are used to
2898 specify a dash pattern. Its general syntax follows:
2899 <center>
2900 <tt> "dasharray" "(" d<sub>1</sub> "," g<sub>1</sub> "," d<sub>2</sub> ","
2901 g<sub>2</sub> "," ... ")"</tt>
2902 </center>
2903 where <tt>d<sub>i</sub></tt> denotes the length of a dash and <tt>g<sub>i</sub></tt>
2904 denotes the length of gap between two consecutive dashes. Each <tt>d<sub>i</sub></tt>
2905 and <tt>g<sub>i</sub></tt> is a length (i.e., a number accompanied by a length of unit).
2906 Since we do not a priori know the number of arguments, we push them onto a stack and
2907 then we produce a command of the form
2908 <center>
2909 <tt> \setdashpattern &lt; d<sub>1</sub>, g<sub>1</sub>, d<sub>2</sub>,
2910 g<sub>2</sub>, ...&gt;</tt>
2911 </center>
2912
2913 <<process <tt>dasharray</tt> command>>=
2914 chk_lparen($cmd,$lc);
2915 my @DashArray = ();
2916 my $dash = "";
2917 my $dashpattern = "";
2918 PATTERN: while (1) {
2919 $dash = sprintf("%.5f", expr($lc));
2920 if (s/^\s*($units)//i) {
2921 push (@DashArray, "$dash$1");
2922 }
2923 else {
2924 PrintErrorMessage("Did not found unit after expression", $lc);
2925 }
2926 s/\s*//;
2927 if (/^[^,]/) {
2928 last PATTERN;
2929 }
2930 else {
2931 s/^,\s*//;
2932 }
2933 }
2934 print OUT "\\setdashpattern <";
2935 while (@DashArray) {
2936 $dashpattern .= shift @DashArray;
2937 $dashpattern .= ",";
2938 }
2939 $dashpattern =~ s/,$//;
2940 print OUT $dashpattern, ">\n";
2941 chk_rparen("arguments of $cmd",$lc);
2942 chk_comment($lc);
2943
2944 @ The command [[drawAngleArc]] draws an arc in the specified angle, a
2945 distance <i>radius</i> from the angle. The angle is either <i>internal</i>
2946 (<= 180 degrees) or <i>external</i> (>180 degrees). The direction of the
2947 arc is either <i>clockwise</i> or <i>anticlockwise</i>. The command
2948 [[drawAngleArrow]] draws an arrow just like the command [[drawAngleArc]]
2949 draws an arc. The syntax of these commands is as follows:
2950 <pre>
2951 cmds ::= ( "drawAngleArc" | "drawAngleArrow" ) args
2952 args ::= "{" angle comma radius comma internal comma clockwise "}"
2953 angle ::= "angle" "(" three-points ")"
2954 radius ::= "radius" "(" distance ")"
2955 distance ::= expression
2956 internal ::= "internal" | "external"
2957 clockwise ::= "clockwise" | "anticlockwise"
2958 comma ::= "," | empty
2959 </pre>
2960 We first collect all relevant information by parsing the [[args]] and then
2961 call the either the subroutine [[drawAngleArc]] or the subroutine
2962 [[drawAngleArrow]] to produce the actual code
2963 which is then printed into the output file. In order to be able to distinguish
2964 which command we are dealing with we simply use the variable [[$cmd]].
2965 We now start parsing the input line. We first check whether there is a
2966 left curly bracket. Next, we parse the [[angle]], the [[distance]], the
2967 [[internal]] and the [[clockwise]] parts of the command. Finally, we check
2968 for right curly bracket and a trailing comment. Depending on
2969 the value of
2970 the variable [[$cmd]] we call either the subroutine [[drawAngleArc]] or the
2971 subroutine [[drawAngleArrow]]. These subroutines return the code that will be
2972 finally output to the output file.
2973
2974 <<process <tt>drawAngleArcOrArrow</tt> command>>=
2975
2976 chk_lcb($cmd,$lc);
2977 <<process <tt>angle</tt> part of command>>
2978 s/^,\s*// or s/\s*//; #parse optional comma
2979 <<process <tt>radius</tt> part of command>>
2980 s/^,\s*// or s/\s*//; #parse optional comma
2981 my $inout = "";
2982 if (s/^(internal(?=\W))//i or s/^(external(?=\W))//i) {
2983 $inout = $1;
2984 }
2985 else {
2986 PrintErrorMessage("Did not find expected 'internal' specifier", $lc);
2987 next LINE;
2988 }
2989 s/^,\s*// or s/\s*//; #parse optional comma
2990 my $direction = "";
2991 if (s/^(clockwise(?=\W))//i or s/^(anticlockwise(?=\W))//i) {
2992 $direction = $1;
2993 }
2994 else {
2995 PrintErrorMessage("Did not find expected 'direction' specifier", $lc);
2996 next LINE;
2997 }
2998 chk_rcb("arguments of $cmd",$lc);
2999 chk_comment($lc);
3000 my $code;
3001 if (lc($cmd) eq "drawanglearc") {
3002 $code = drawAngleArc($P1, $P2, $P3, $radius, $inout, $direction);
3003 }
3004 else {
3005 $code = drawAngleArrow($P1, $P2, $P3, $radius, $inout, $direction);
3006 }
3007 print OUT $code if $code ne "";
3008
3009 @ We first check whether the first token is the word [[angle]]. In case it
3010 isn't, this yields an unrecoverable error. In case the expected word is
3011 there, we check for a left parenthesis. Next, we parse the three points that
3012 must follow. For this purpose we use the user-defined subroutine
3013 [[get_point]]. Now we check that the angle has a reasonable value, i.e., if
3014 it is less than -400 or equal to zero, the value yields an unrecoverable error.
3015 We finish by checking whether there is a right parenthesis.
3016
3017 <<process <tt>angle</tt> part of command>>=
3018 my ($P1, $P2, $P3);
3019 if (s/^angle(?=\W)//i) {
3020 chk_lparen("token angle of command $cmd",$lc);
3021 $P1 = get_point($lc);
3022 next LINE if $P1 eq "_undef_";
3023 $P2 = get_point($lc);
3024 next LINE if $P2 eq "_undef_";
3025 $P3 = get_point($lc);
3026 next LINE if $P3 eq "_undef_";
3027 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
3028 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
3029 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
3030 my $Angle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
3031 if ($Angle <= 0) {
3032 if ($Angle == 0) {
3033 PrintErrorMessage("Angle is equal to zero",$lc);
3034 next LINE;
3035 }
3036 elsif ($Angle < -400) {
3037 PrintErrorMessage("Something is wrong with the points",$lc);
3038 next LINE;
3039 }
3040 }
3041 chk_rparen("angle part of command $cmd",$lc);
3042 }
3043 else {
3044 PrintErrorMessage("Did not find expected angle part",$lc);
3045 next LINE;
3046 }
3047
3048 @ In this section we parse the [[radius]] part of the [[drawAngleArc]] or the
3049 [[drawAngleArrow]] command. We first check whether the next token is the word
3050 [[radius]]. If it is not, then we continue with the next line.
3051
3052 <<process <tt>radius</tt> part of command>>=
3053
3054 my $radius;
3055 if (s/^radius(?=\W)//i) {
3056 chk_lparen("token radius of command $cmd",$lc);
3057 $radius = expr($lc);
3058 chk_rparen("radius part of command $cmd",$lc);
3059 }
3060 else {
3061 PrintErrorMessage("Did not found expected angle part",$lc);
3062 next LINE;
3063 }
3064
3065 @ Command [[drawcircle]] accepts two arguments--a point name that is
3066 used to specify the center of the circle and the radius of the circle.
3067 The radius is simply an expression, whose value must be greater than zero.
3068 Otherwise, we print an error message and continue with the next input line.
3069 The general syntax of the command is as follows:
3070 <pre>
3071 "drawcircle" "(" point-name "," rad ")"
3072 </pre>
3073 The code we emit for a point with coordinates [[x]] and [[y]] and for radius
3074 equal to [[R]] is:
3075 <pre>
3076 \circulararc 360 degrees from X y center at x y
3077 </pre>
3078 where [[X = x+R]].<p>
3079 Initially, we check whether there is an opening left parenthesis. Next,
3080 we get the point name by using the subroutine [[get_point]] which
3081 issues an error message if the point hasn't been defined. In this
3082 case we stop processing the command, as there is absolutely no reason to
3083 do otherwise. Next, we parse the comma and then the radius by using
3084 the subroutine [[ComputeDist]]. If there is no problem, we emit the code
3085 and finally we check for a closing right parenthesis and for
3086 possible garbage that may follow the command.
3087
3088 <<process <tt>drawcircle</tt> command>>=
3089 chk_lparen("drawcircle",$lc);
3090 my $Point = get_point($lc);
3091 next LINE if $Point eq "_undef_";
3092 chk_comma($lc);
3093 my $R = expr($lc);
3094 if ($R <= 0) {
3095 PrintErrorMessage("Radius must be greater than zero",$lc);
3096 next LINE;
3097 }
3098 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{lc($Point)});
3099 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
3100 $x+$R, $y, $x, $y;
3101 chk_rparen("arguments of $cmd",$lc);
3102 chk_comment($lc);
3103
3104 @ Command [[drawcircumcircle]] is used to draw the circumcircle of triangle
3105 specified by three points which are the arguments of the command. We start
3106 by parsing the opening left parenthesis. Next, we get the three points
3107 that define the triangle. We are now able to compute the center and
3108 the radius of the circumcircle by calling the subroutine [[circumCircleCenter]].
3109 If the triangle area is equal to zero, then this subroutine will return
3110 the array [[(0,0,0)]] to indicate this fact.
3111 We now have all necessary information to draw the circumcircle. We use the
3112 following code to do the job:
3113 <pre>
3114 \circulararc 360 degrees from X y center x y
3115 </pre>
3116 where [[x]] and [[y]] are the coordinates of the center, [[R]] its
3117 radius and [[X=x+R]]. What is left is to check whether there is a
3118 closing right parenthesis and any trailing garbage.
3119
3120
3121 <<process <tt>drawcircumcircle</tt> command>>=
3122 chk_lparen("drawcircumcircle",$lc);
3123 my $point1 = get_point($lc);
3124 next LINE if $point1 eq "_undef_";
3125 my $point2 = get_point($lc);
3126 next LINE if $point2 eq "_undef_";
3127 my $point3 = get_point($lc);
3128 next LINE if $point3 eq "_undef_";
3129 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
3130 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
3131 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
3132 my ($xc, $yc,$r) = circumCircleCenter($x1,$y1,$x2,$y2,$x3,$y3,$lc);
3133 next LINE if $xc == 0 and $yc == 0 and $r == 0;
3134 print OUT "%% circumcircle center = ($xc,$yc), radius = $r\n" if $comments_on;
3135 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
3136 $xc+$r, $yc, $xc, $yc;
3137 chk_rparen("arguments of $cmd",$lc);
3138 chk_comment($lc);
3139
3140 @ The syntax of the [[drawexcircle]] command is as follows:
3141 <pre>
3142 drawexcircle ::= "drawexcircle" "(" ThreePoints "," TwoPoints ")"
3143 [ modifier ]
3144 modifier ::= "[" expr "]"
3145
3146 </pre>
3147 The [[modifier]] is an expression that is used to modify the radius of the
3148 excicle. We start by checking whether there is a left parenthesis. Then we
3149 get names of the three points. In case any of the points is not defined
3150 we issue an error message and continue with the next input line. Next, we
3151 check whether there is a comma that separates the three points defining the
3152 triangle from the two points defining a side of the triangle (variables
3153 [[$point1]], [[$point2]], and [[$point3]]). Moreover, we must ensure that
3154 the area of the area defined by these points is not equal to
3155 zero. If it is we issue an error message and we continue with the next
3156 input line. Now, we are ready to get the two
3157 point names that define the side of the triangle (variables [[$point3]] and
3158 [[$point5]]). At this point we must make sure that these points are different
3159 points and that they are members of the list of points that define the triangle.
3160 We make this check by calling the subroutine [[memberOf]]. Next, we check
3161 whether there is a closing right parenthesis. We now compute the center
3162 and the radius of the excircle by calling the subroutine [[excircle]]. The
3163 coordinates of the center are stored in the variables [[$xc]] and [[$yc]],
3164 while the radius is stored in the variable [[$r]]. If the next
3165 non-blank input character is a left square bracket, then we know the user has
3166 specified the optional part. We use the subroutine [[expr]] to get the value of
3167 the optional part. The value of the optional part is stored in the variable [[$R]].
3168 At this point we check whether the sum of the radius
3169 plus the optional part is equal to zero and if it is we continue with the
3170 next input line. Next, we check for a closing right square bracket. We are
3171 now ready to emit the source code. The first thing we must check is that
3172 the radius is not too big for PiCTeX, i.e., not greater than 500/2.845.
3173 Then we print some informative text to the output file and of course the
3174 actual code. We use the following code to do the job:
3175 <pre>
3176 \circulararc 360 degrees from (xc+R) yc center xc yc
3177 </pre>
3178 The last thing we check is whether there is some trailing garbage.
3179
3180 <<process <tt>drawexcircle</tt> command>>=
3181 chk_lparen("drawexcircle",$lc);
3182 my $point1 = get_point($lc);
3183 next LINE if $point1 eq "_undef_";
3184 my $point2 = get_point($lc);
3185 next LINE if $point2 eq "_undef_";
3186 my $point3 = get_point($lc);
3187 next LINE if $point3 eq "_undef_";
3188 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
3189 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
3190 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
3191 if (triangleArea($x1, $y1, $x2, $y2, $x3, $y3) < 0.0001) {
3192 PrintErrorMessage("Area of triangle is zero!",$lc);
3193 next LINE;
3194 }
3195 chk_comma($lc);
3196 my $point4 = get_point($lc);
3197 if (!memberOf($point4, $point1, $point2, $point3)) {
3198 PrintErrorMessage("Current point isn't a side point",$lc);
3199 next LINE;
3200 }
3201 next LINE if $point4 eq "_undef_";
3202 my $point5 = get_point($lc);
3203 next LINE if $point5 eq "_undef_";
3204 if (!memberOf($point5, $point1, $point2, $point3)) {
3205 PrintErrorMessage("Current point isn't a side point",$lc);
3206 next LINE;
3207 }
3208 if ($point4 eq $point5) {
3209 PrintErrorMessage("Side points are identical",$lc);
3210 next LINE;
3211 }
3212 chk_rparen("arguments of $cmd",$lc);
3213 my ($xc, $yc, $r) = excircle($point1, $point2, $point3,
3214 $point4, $point5);
3215 my $R=$r;
3216 if (s/^\s*\[\s*//) {
3217 $R += expr($lc);
3218 if ($R < 0.0001) {
3219 PrintErrorMessage("Radius has become equal to zero!",$lc);
3220 next LINE;
3221 }
3222 chk_rsb($lc);
3223 }
3224 if ($R > (500 / 2.845)) {
3225 PrintErrorMessage("Radius is greater than 175mm!",$lc);
3226 next LINE;
3227 }
3228 print OUT "%% excircle center = ($xc,$yc) radius = $R\n" if $comments_on;
3229 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
3230 $xc+$R, $yc, $xc, $yc;
3231 chk_comment($lc);
3232
3233 @ The syntax of the [[drawincircle]] command is as follows:
3234 <pre>
3235 drawincircle ::= "drawincircle" "(" ThreePoints ")" [ modifier]
3236 modifier ::= "[" expr "]"
3237 </pre>
3238 where [[ThreePoints]] correspond to the points defining the triangle and
3239 [[modifier]] is an optional modification factor.
3240 The first thing we do is to check whether
3241 there is an opening left parenthesis. Then we get the names of the three
3242 points that define the triangle (variables [[$point1]], [[$point2]],
3243 and [[$point3]]). Next, we make sure that the area of the
3244 triangle defined by these three points is not equal to zero. If it is, then
3245 we issue an error message and continue with the next input line. Now, we
3246 compute the center and the radius of the incircle (variables [[$xc]], [[$yc]],
3247 and [[$r]]). If the next non-blank input character is a left square bracket,
3248 then we now the user has specified the optional part. We use subroutine
3249 [[expr]] to get the value of the optional part. The value of
3250 the optional part
3251 is stored in the variable [[$R]]. At this point we check whether the sum of the
3252 radius plus the optional part is equal to zero and if it is we continue with
3253 the next input line. Next, we check for a closing right square bracket.
3254 We are now ready to emit the source code. The first thing we must check is
3255 that the radius is not too big for PiCTeX, i.e., not greater than 500/2.845.
3256 Then we print some informative text to the output file and of course the
3257 actual code. We use the following code to do the job:
3258 <pre>
3259 \circulararc 360 degrees from (xc+R) yc center xc yc
3260 </pre>
3261 The last thing we check is whether there is some trailing garbage.
3262
3263 <<process <tt>drawincircle</tt> command>>=
3264 chk_lparen("drawincircle",$lc);
3265 my $point1 = get_point($lc);
3266 next LINE if $point1 eq "_undef_";
3267 my $point2 = get_point($lc);
3268 next LINE if $point2 eq "_undef_";
3269 my $point3 = get_point($lc);
3270 next LINE if $point3 eq "_undef_";
3271 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
3272 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
3273 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
3274 if (triangleArea($x1, $y1, $x2, $y2, $x3, $y3) < 0.0001) {
3275 PrintErrorMessage("Area of triangle is zero!",$lc);
3276 next LINE;
3277 }
3278 my ($xc, $yc, $r) = IncircleCenter($x1,$y1,$x2,$y2,$x3,$y3);
3279 my $R=$r;
3280 if (s/^\s*\[\s*//) {
3281 $R += expr($lc);
3282 if ($R < 0.0001) {
3283 PrintErrorMessage("Radius has become equal to zero!",$lc);
3284 next LINE;
3285 }
3286 chk_rsb($lc);
3287 }
3288 if ($R > (500 / 2.845)) {
3289 PrintErrorMessage("Radius is greater than 175mm!",$lc);
3290 next LINE;
3291 }
3292 print OUT "%% incircle center = ($xc,$yc) radius = $R\n" if $comments_on;
3293 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
3294 $xc+$R, $yc, $xc, $yc;
3295 chk_rparen("arguments of $cmd",$lc);
3296 chk_comment($lc);
3297
3298 @ The command [[drawPerpendicular]] command draws a line from point A to line
3299 BC, such that it is perpendicular to line BC. The general syntax of the
3300 command is as follows:
3301 <pre>
3302 drawPenpedicular ::= "drawPenpedicular" "(" Point "," TwoPoints ")"
3303 </pre>
3304 The first thing we do is to parse the left parenthesis. Then we parse
3305 the name of the first point, namely [[$A$]]. If this point is undefined
3306 we print an error message and continue with the next line. Next, we parse
3307 the expected leading comma and the names of the other two points. Certainly,
3308 in case either of these two points has not been defined, we simply print an
3309 error message and continue with the next input line. Finally, we check for
3310 a closing right parenthesis and a possible trailing comment. Now we are
3311 ready to compute the coordinates of the foot of the
3312 perpendicular line. We do so my calling subroutine
3313 [[perpendicular]]. Certainly, before we do this we have to get the
3314 coordinates of the points that we have parsed. Finally, we output the
3315 PiCTeX code:
3316 <pre>
3317 \plot x1 y1 xF xY /
3318 </pre>
3319 where [[x1]] and [[y1]] are coordinates of the point A and [[xF]] and [[yF]]
3320 the coordinates of the foot.
3321
3322 <<process <tt>drawPerpendicular</tt> command>>=
3323
3324 chk_lparen($cmd,$lc);
3325 my $A = get_point($lc);
3326 next LINE if $A eq "_undef_";
3327 chk_comma($lc);
3328 my $B = get_point($lc);
3329 next LINE if $A eq "_undef_";
3330 s/\s*//; #ignore white space
3331 my $C = get_point($lc);
3332 next LINE if $A eq "_undef_";
3333 chk_rparen("arguments of $cmd",$lc);
3334 chk_comment($lc);
3335 #
3336 #start actual computation
3337 #
3338 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$A});
3339 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$B});
3340 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$C});
3341 my ($xF, $yF) = perpendicular($x1, $y1, $x2, $y2, $x3, $y3);
3342 printf OUT "\\plot %.5f %.5f %.5f %.5f /\n",
3343 $x1, $y1, $xF, $yF;
3344
3345 @ The [[drawpoint]] command has a number of points as arguments and produces
3346 PiCTeX code that draws a plot symbol at the coordinates of each point. The
3347 syntax of the command is as follows:
3348 <pre>
3349 drawpoint ::= "drawpoint" "(" Point { separator Point } ")"
3350 </pre>
3351 The [[while]] loop is used to consume all points that are
3352 between an opening left parenthesis and a closing right parenthesis. All
3353 points are pushed on the local array [[PP]]. When we have parsed the lists
3354 of points, we call the subroutine [[drawpoints]] to emit the actual PiCTeX code.
3355 Finally, we check whether there is a closing parenthesis
3356 parenthesis, and whether
3357 there is some trailing text that makes no sense. In case there are no points
3358 between the parentheses, then we issue an appropriate error message and
3359 we continue with the next input line.
3360
3361 <<process <tt>drawpoint</tt> command>>=
3362 my ($stacklen);
3363 chk_lparen("$cmd",$lc);
3364 if (/^\)/) {
3365 PrintErrorMessage("There are no point to draw",$lc);
3366 next LINE;
3367 }
3368 my(@PP);
3369 DRAWPOINTS:while(1) {
3370 if (s/^([^\W\d_]\d{0,4})//i) { #point name
3371 $P = $1;
3372 if (!exists($PointTable{lc($P)})) {
3373 PrintErrorMessage("Undefined point $P",$lc);
3374 next DRAWPOINTS;
3375 }
3376 else {
3377 push (@PP,$P);
3378 s/\s*//;
3379 }
3380 }
3381 else {
3382 last DRAWPOINTS;
3383 }
3384 }
3385 drawpoints(@PP);
3386 chk_rparen("arguments of $cmd",$lc);
3387 chk_comment($lc);
3388
3389 @ The syntax of the [[drawRightAngle]] command is as follows:
3390 <pre>
3391 drawRightAngle "(" ThreePoints "," dist ")"
3392 dist ::= expr | TwoPoints
3393 </pre>
3394 Before we proceed with the actual computation we parse the left parenthesis,
3395 the three points, the comma, the [[dist]], and the right parenthesis. In case
3396 we have neither three points nor a [[dist]] we print an error message and
3397 continue with the next input line, i.e., these errors are irrecoverable.
3398 The names of the three points are stored in variables [[$point1]],
3399 [[$point2]], and [[$point3]]. The value of the distance is stored
3400 in the variable [[$dist]].
3401 Let's now explain the semantics of this command.<p>
3402
3403 Our aim is to draw lines S<sub>1</sub>-S, S<sub>2</sub>-S (S<sub>1</sub>
3404 and S<sub>2</sub> are at distance d from B). All the relevant points are
3405 depicted in the following figure:
3406 <center>
3407 <img src="fig1.jpg">
3408 </center>
3409 Some notes are in order:
3410 <ol>
3411 <li> BS bisects angle ABC, and meets AC in Q, so start by determining point
3412 Q, then determine S, and then S<sub>1</sub> and S<sub>2</sub>, and then
3413 draw S<sub>1</sub>-S and S<sub>2</sub>-S.</li>
3414 <li> Distance AQ is given by AC/(1+tan(BCA))</li>
3415 <li> The coordinates of Q are computed using the subroutine [[pointOnLine]].</li>
3416 <li> Now we compute the coordinates of S on line BQ.</li>
3417 <li> We compute the coordinates of S<sub>1</sub> and S<sub>2</sub> by using
3418 The subroutine [[pointOnLine]].</li>
3419 </ol>
3420 In order to implement the above steps we first compute the length of the line
3421 AB. Note that A is [[$point1]], etc. Next we compute the angle BAC. Now
3422 we compute the distance AQ (variable [[$line1]]). The coordinates of point
3423 Q are stored in variables [[$xQ]] and [[$yQ]]. The coordinates of point
3424 S are stored in variables [[$xS]] and [[$yS]]. Now we have to determine the
3425 coordinates of points S<sub>1</sub> and S<sub>2</sub>. These coordinates
3426 are stored in variables [[$xS1]], [[$yS1]] and [[$xS2]], [[$yS2]],
3427 respectively. Finally, we emit the PiCTeX target code.
3428
3429 <<process <tt>drawRightAngle</tt> command>>=
3430
3431 chk_lparen("drawRightAngle",$lc);
3432 my $point1 = get_point($lc);
3433 next LINE if $point1 eq "_undef_";
3434 my $point2 = get_point($lc);
3435 next LINE if $point2 eq "_undef_";
3436 my $point3 = get_point($lc);
3437 next LINE if $point3 eq "_undef_";
3438 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
3439 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
3440 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
3441 chk_comma($lc);
3442 my $dist = expr($lc);
3443 chk_rparen("arguments of $cmd",$lc);
3444 chk_comment($lc);
3445 #
3446 #actual computation
3447 #
3448 my ($Px, $Py) = pointOnLine($x2, $y2, $x1, $y1, $dist);
3449 my ($Qx, $Qy) = pointOnLine($x2, $y2, $x3, $y3, $dist);
3450 my ($Tx, $Ty) = midpoint($Px, $Py, $Qx, $Qy);
3451 my ($Ux, $Uy) = pointOnLine($x2, $y2, $Tx, $Ty, 2*Length($x2, $y2, $Tx, $Ty));
3452 if ($Px == $Ux || $Py == $Uy) {
3453 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f \n", $Px,$Py,$Ux,$Uy;
3454 }
3455 else {
3456 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / \n", $Px, $Py,$Ux,$Uy;
3457 }
3458 if ($Ux == $Qx || $Uy == $Qy) {
3459 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f \n", $Ux,$Uy,$Qx,$Qy;
3460 }
3461 else {
3462 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / \n", $Ux, $Uy,$Qx,$Qy;
3463 }
3464
3465
3466 @ The command [[drawsquare]] has two arguments: a point, which specifies the
3467 coordinates of the point where the square will be placed, and a number, which
3468 specifies the length of the side of the square. The syntax of the command is as follows:
3469 <center>
3470 <tt> "drawSquare" "(" Point "," expression ")" </tt>
3471 </center>
3472 Note that RWDN has suggested to alter the value of the [[$side]] variable (see the
3473 line with [[RWDN]] comment).
3474
3475 <<process <tt>drawsquare</tt> command>>=
3476 chk_lparen("drawSquare",$lc);
3477 my $p = get_point($lc);
3478 chk_comma($lc);
3479 my $side = expr($lc);
3480 $side = $side - (1.1 * $LineThickness/$xunits); #Suggested by RWDN
3481 my ($x,$y,$pSV,$pS) = unpack("d3A*",$PointTable{$p});
3482 printf OUT "\\put {%s} at %.5f %.5f %%drawsquare\n", drawsquare($side), $x, $y;
3483 chk_rparen("arguments of $cmd",$lc);
3484 chk_comment($lc);
3485
3486 @ The argument of the [[inputfile*]] command is a file name that is always
3487 enclosed in parentheses:
3488 <pre>
3489 starred-input-file ::= "inputfile*" "(" file-name ")"
3490 file-name ::= (alpha | period) { alpha | period }
3491 alpha ::= letter | digit | "_" | "-"
3492 </pre>
3493 Note, that the input file is assumed to contain TeX code.
3494 We first check to see if there is a left parenthesis. Then we consume
3495 the file name. We check if the file exists and then we copy verbatim the
3496 input file to the output file. Next, we check for the closing parenthesis.
3497 Now, if there is a trailing comment we copy it to the output file depending
3498 on the value of the variable [[$comments_on]], else if there is some other
3499 text we simply ignore it and issue a warning message.
3500
3501 <<process <tt>inputfile*</tt> command>>=
3502 chk_lparen("inputfile*",$lc);
3503 my $row_in = "";
3504 if (s/^((\w|-|\.)+)//) {
3505 $row_in = $1;
3506 }
3507 else {
3508 PrintErrorMessage("No input file name found",$lc);
3509 next LINE;
3510 }
3511 if (!(-e $row_in)) {
3512 PrintErrorMessage("File $row_in does not exist",$lc);
3513 next LINE;
3514 }
3515 open(ROW, "$row_in")|| die "Can't open file $row_in\n";
3516 while (defined($in_line=<ROW>)) { print OUT $in_line; }
3517 print OUT "%% ... end of input file <$row_in>\n";
3518 close ROW;
3519 chk_rparen("input file name",$lc);
3520 chk_comment($lc);
3521
3522
3523 @ The [[inputfile]] command has at most two arguments, second being
3524 optional: a file name enclosed in curly brackets and the number of
3525 times this file should be included in square brackets:
3526 <pre>
3527 inputfile ::= "inputfile" "(" file-name ")" [ Times ]
3528 Times ::= "[" expr "]"
3529 </pre>
3530 Note that the input file is assumed to contain mathspic commands. In addition, if
3531 the expression is equal to a decimal number, it is truncated.
3532 As in the case of the [[inputfile*]] command we parse the left parenthesis,
3533 the file name, the right parenthesis and the optional argument if it exists.
3534 In order to process the commands contained in the input file, we call
3535 The subroutine [[process_input]].
3536
3537 <<process <tt>inputfile</tt> command>>=
3538
3539 chk_lparen("inputfile",$lc);
3540 my $comm_in = "";
3541 if (s/^((\w|-|\.)+)//) {
3542 $comm_in = $1;
3543 }
3544 else {
3545 PrintErrorMessage("No input file name found",$lc);
3546 next LINE;
3547 }
3548 if (!(-e $comm_in)) {
3549 PrintErrorMessage("File $comm_in does not exist",$lc);
3550 next LINE;
3551 }
3552 chk_rparen("input file name",$lc);
3553 my $input_times = 1; #default value
3554 if (s/^\[//) {
3555 $input_times = expr($lc);
3556 chk_rsb("optional argument",$lc);
3557 }
3558 print OUT "%% ... start of file <$comm_in> loop [$input_times]\n";
3559 for (my $i=0; $i<int($input_times); $i++) {
3560 open(COMM,"$comm_in") or die "Can't open file $comm_in\n";
3561 print OUT "%%% Iteration number: ",$i+1,"\n";
3562 my $old_file_name = $curr_in_file;
3563 process_input(COMM,"File $comm_in, ");
3564 $curr_in_file = $old_file_name;
3565 close COMM;
3566 }
3567 print OUT "%% ... end of file <$comm_in> loop [$input_times]\n";
3568 chk_comment($lc);
3569
3570 @ The [[linethickness]] command should be used to set the thickness of lines.
3571 The command has one argument, which is a length or the word [[default]].
3572 The default line thickness is 0.4 pt.
3573
3574 <<process <tt>linethickness</tt> command>>=
3575 chk_lparen("linethickness", $lc);
3576 if (s/^default//i) {
3577 print OUT "\\linethickness=0.4pt\\Linethickness{0.4pt}%%\n";
3578 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
3579 $LineThickness = setLineThickness($xunits,"0.4pt");
3580 }
3581 else {
3582 my $length = expr($lc);
3583 if (s/^\s*($units)//i) {
3584 my $units = $1;
3585 printf OUT "\\linethickness=%.5f%s\\Linethickness{%.5f%s}%%\n",
3586 $length, $units, $length, $units;
3587 $LineThickness = setLineThickness($xunits,"$length$units");
3588 my $mag;
3589 if ($units eq "pc") {
3590 $mag = $length * 12;
3591 }
3592 elsif ($units eq "in") {
3593 $mag = $length * 72.27;
3594 }
3595 elsif ($units eq "bp") {
3596 $mag = $length * 1.00375;
3597 }
3598 elsif ($units eq "cm") {
3599 $mag = $length * 28.45275;
3600 }
3601 elsif ($units eq "mm") {
3602 $mag = $length * 2.845275;
3603 }
3604 elsif ($units eq "dd") {
3605 $mag = $length * 1.07001;
3606 }
3607 elsif ($units eq "cc") {
3608 $mag = $length * 0.08917;
3609 }
3610 elsif ($units eq "sp") {
3611 $mag = $length * 0.000015259;
3612 }
3613 elsif ($units eq "pt") {
3614 $mag = $length;
3615 }
3616 $mag = 10 * $mag / 1.00278219;
3617 printf OUT "\\font\\CM=cmr10 at %.5fpt%%\n", $mag;
3618 print OUT "\\setplotsymbol ({\\CM .})%\n";
3619 }
3620 else {
3621 PrintErrorMessage("Did not found expect units part",$lc);
3622 }
3623 }
3624 chk_rparen("linethickness", $lc);
3625 chk_comment($lc);
3626
3627
3628 @ We first output the input line as a comment into the output file. Now,
3629 after the [[paper]] token we look for an opening brace. Then we process
3630 the [[units]] part of the command, if the token [[units]] is present. Note
3631 that the [[units]] part is optional. Next we process the [[xrange]] and the
3632 [[yrange]] part of the command, which are also optional parts of the command.
3633 We are now ready to process the [[axis]] part. Note, that the user is allowed
3634 to alternatively specify this part with the word [[axes]].
3635 The variable [[$axis]]
3636 is supposed to hold the various data relate to the [[axis]] part. The last
3637 thing we check is the [[ticks]] part. In case the user has not specified
3638 this part we assume that both ticks are equal to zero. If everything is
3639 according to the language syntax, we expect a closing right curly bracket.
3640 Now, that we have all relevant information we can output the rest of the code,
3641 as some parts of it have already been output during parsing. The last thing we
3642 do is to check whether there is any trailing comment.
3643
3644 <<process <tt>paper</tt> command>>=
3645 chk_lcb("paper", $lc);
3646 if (s/^units(?=\W)//i)
3647 {
3648 <<process <tt>unit</tt> part>>
3649 $nounits = 0;
3650 }
3651 else
3652 {
3653 $nounits = 1;
3654 }
3655 s/^,\s*// or s/\s*//;
3656 if (s/^xrange//i)
3657 {
3658 <<process <tt>xrange</tt> part>>
3659 $noxrange = 0;
3660 }
3661 else
3662 {
3663 $noxrange = 1;
3664 }
3665 s/^,\s*// or s/\s*//;
3666 if (s/^yrange//i)
3667 {
3668 <<process <tt>yrange</tt> part>>
3669 $noyrange = 0;
3670 }
3671 else
3672 {
3673 $noyrange = 1;
3674 }
3675 <<generate plot area related commands>>
3676 s/^,\s*// or s/\s*//;
3677 $axis = "";
3678 if (s/^ax[ei]s(?=\W)//i)
3679 {
3680 <<process <tt>axis</tt> part>>
3681 }
3682 $axis = uc($axis);
3683 s/^,\s*// or s/\s*//;
3684 if (s/^ticks(?=\W)//i)
3685 {
3686 <<process <tt>ticks</tt> part>>
3687 }
3688 else
3689 {
3690 $xticks = $yticks = 0;
3691 }
3692 chk_rcb("paper", $lc);
3693 <<generate the rest of the code for the <tt>paper</tt> command>>
3694 chk_comment($lc);
3695
3696 @ We first check whether there is a left parenthesis. Next, we check
3697 whether there is decimal number or a variable name. In case there isn't one we assume it
3698 is the number 1. Now, we get the units. If there is no valid unit, we issue
3699 an error and the x-unit is set to its default value. In case, there is
3700 a trailing comma, we assume the user wants also to specify the y-unit and
3701 we process this part just like we did with the x-unit part. Finally, we
3702 output the corresponding PiCTeX command. In case there is no y-unit
3703 we assume it is equal to the x-unit.
3704
3705 <<process <tt>unit</tt> part>>=
3706
3707 chk_lparen("units",$lc);
3708 if(s/^\)//)
3709 {
3710 PrintWarningMessage("Missing value in \"units\"--default is 1pt",
3711 $lc);
3712 $xunits = "1pt";
3713 }
3714 else {
3715 $xunits = expr($lc);
3716 s/\s*//;
3717 if (s/^($units)//i) {
3718 $xunits .= "$1";
3719 $LineThickness = setLineThickness($xunits,"0.4pt");
3720 }
3721 elsif(s/^(\w)+//i) {
3722 PrintErrorMessage("$1 is not a valid mathspic unit",$lc);
3723 $xunits = "1pt";
3724 }
3725 else {
3726 PrintErrorMessage("No x-units found",$lc);
3727 $xunits = "1pt";
3728 }
3729 s/\s*//; #ignore white space
3730 if (s/^,//) { # there is a comma so expect an y-units
3731 s/\s*//; #ignore white space
3732 $yunits = expr($lc);
3733 s/\s*//; #ignore white space
3734 if (s/^($units)//i) {
3735 $yunits .= "$1";
3736 }
3737 elsif(s/^(\w)+//i) {
3738 PrintErrorMessage("$1 is not a valid mathspic unit",$lc);
3739 $yunits = "1pt";
3740 }
3741 else {
3742 PrintErrorMessage("No y-units found",$lc);
3743 $yunits = $xunits;
3744 }
3745 }
3746 else {
3747 $yunits = $xunits;
3748 }
3749 chk_rparen("units",$lc);
3750 }
3751
3752 @ The [[xrange]] token must be followed by a left parenthesis, so we
3753 check whether the next token is a left parenthesis. We store in the variables
3754 [[$xlow]] and [[$xhigh]] the values of the range. The range is specified
3755 as pair of decimal numbers/variable/pair of points, separated by a
3756 comma. We use the subroutine [[ComputeDist]] to get the value of the lower
3757 end and the upper end of the range. The last thing we check is whether
3758 the lower end is less than the upper end. If this isn't the case we
3759 issue an error message and we skip into the next input line.
3760
3761 <<process <tt>xrange</tt> part>>=
3762
3763 chk_lparen("xrange",$lc);
3764 my $ec;
3765 ($xlow,$ec) = ComputeDist($lc);
3766 next LINE if $ec == 0;
3767 chk_comma($lc);
3768 ($xhigh,$ec) = ComputeDist($lc);
3769 next LINE if $ec == 0;
3770 if ($xlow >= $xhigh)
3771 {
3772 PrintErrorMessage("xlow >= xhigh in xrange",$lc);
3773 next LINE;
3774 }
3775 chk_rparen("$xhigh",$lc);
3776
3777 @ The [[yrange]] token must be followed by a left parenthesis, so we
3778 check whether the next token is a left parenthesis. We store in the variables
3779 [[$ylow]] and [[$yhigh]] the values of the range. The range is specified
3780 as pair of decimal numbers/variable/pair of points, separated by a
3781 comma. We use the subroutine [[ComputeDist]] to get the value of the lower
3782 end and the upper end of the range. The last thing we check is whether
3783 the lower end is less than the upper end. If this isn't the case we
3784 issue an error message and we skip into the next input line.
3785
3786 <<process <tt>yrange</tt> part>>=
3787
3788 chk_lparen("yrange",$lc);
3789 my $ec;
3790 ($ylow,$ec) = ComputeDist($lc);
3791 next LINE if $ec == 0;
3792 chk_comma($lc);
3793 ($yhigh,$ec) = ComputeDist($lc);
3794 next LINE if $ec == 0;
3795 if ($ylow >= $yhigh)
3796 {
3797 PrintErrorMessage("ylow >= yhigh in yrange",$lc);
3798 next LINE;
3799 }
3800 chk_rparen("$yhigh",$lc);
3801
3802 @ The [[showAngle]] command has three arguments that correspond to three distinct
3803 points and emits a comment of the form:
3804 <center>
3805 <tt>%% angle(ABC) = 45</tt>
3806 </center>
3807 Note that the computed angle is expressed in degrees.
3808
3809 <<process <tt>showAngle</tt> command>>=
3810 chk_lparen("showangle",$lc);
3811 my $point_1 = get_point($lc);
3812 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
3813 my $point_2 = get_point($lc);
3814 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
3815 my $point_3 = get_point($lc);
3816 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
3817 my $angle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
3818 $angle = 0 if $angle == -500;
3819 printf OUT "%%%% angle(%s%s%s) = %.5f deg ( %.5f rad)\n", $point_1,
3820 $point_2, $point_3, $angle, $angle*D2R;
3821 chk_rparen("Missing right parenthesis", $lc);
3822
3823 @ The [[showArea]] command has three arguments that correspond to three distinct
3824 points and emits a comment of the form:
3825 <center>
3826 <tt>%% area(ABC) = 45</tt>
3827 </center>
3828 Note that the computed angle is expressed in degrees.
3829
3830 <<process <tt>showArea</tt> command>>=
3831 chk_lparen("showarea",$lc);
3832 my $point_1 = get_point($lc);
3833 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
3834 my $point_2 = get_point($lc);
3835 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
3836 my $point_3 = get_point($lc);
3837 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
3838 print OUT "%% area($point_1$point_2$point_3) = ",
3839 triangleArea($x1, $y1, $x2, $y2, $x3, $y3), "\n";
3840 chk_rparen("Missing right parenthesis", $lc);
3841
3842 @ The [[showLength]] command has two arguments that correspond to two distinct
3843 points and emits a comment of the form:
3844 <center>
3845 <tt>%% length(AB) = 45</tt>
3846 </center>
3847 Note that the computed angle is expressed in degrees.
3848
3849 <<process <tt>showLength</tt> command>>=
3850 chk_lparen("showlength",$lc);
3851 my $point_1 = get_point($lc);
3852 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
3853 my $point_2 = get_point($lc);
3854 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
3855 print OUT "%% length($point_1$point_2) = ",
3856 Length($x1, $y1, $x2, $y2), "\n";
3857 chk_rparen("Missing right parenthesis", $lc);
3858
3859
3860 @ If the user hasn't specified units then we use the previous values to
3861 set the coordinate system. If the user hasn't specified either the
3862 [[xunits]] part or the [[yunits]], then we don't emit code. In case he/she
3863 has specified both parts we generate the command that sets the plot area.
3864
3865 <<generate plot area related commands>>=
3866
3867 if (!$nounits)
3868 {
3869 printf OUT "\\setcoordinatesystem units <%s,%s>\n",
3870 $xunits,$yunits;
3871 }
3872 if(!$noxrange && !$noyrange)
3873 {
3874 printf OUT "\\setplotarea x from %.5f to %.5f, y from %.5f to %.5f\n",
3875 $xlow, $xhigh, $ylow, $yhigh;
3876
3877 }
3878
3879 @ We first check to see whether there is an opening left parenthesis. Next
3880 we get the various options the user may have entered. The valid options
3881 are the letters L, R, T, B, X, and Y. These letters may be followed by
3882 an optional star [[*]] with space characters between the letter and the star.
3883 We use a loop, that stops when a right parenthesis is found, to
3884 go through all
3885 possible arguments and append each argument in the string [[$axis]]. Note
3886 one can have blank space between different arguments. The last thing we do is
3887 to check for the closing right parenthesis.
3888
3889 <<process <tt>axis</tt> part>>=
3890
3891 chk_lparen("axis",$lc);
3892 while(/^[^\)]/)
3893 {
3894 if (s/^([lrtbxy]{1}\*?)//i)
3895 {
3896 $axis .= $1;
3897 }
3898 elsif (s/^([^lrtbxy])//i)
3899 {
3900 PrintErrorMessage("Non-valid character \"$1\" in axis()",$lc);
3901 }
3902 s/\s*//;
3903 }
3904 chk_rparen("axis(arguments",$lc);
3905
3906 @ As usual we start by skipping white space. Next we check whether there is
3907 an opening left parenthesis. Now, we expect two numbers/variables/pair of
3908 point representing the [[ticks]] increment value. These [[ticks]] increment
3909 values must be separated by a comma (and possibly some white space around
3910 them). We use the subroutine [[ComputeDist]] to get the value of the [[ticks]]
3911 increment value and we assign to the variables [[$xticks]] and [[$yticks]]
3912 the value of x-ticks and y-ticks increment value. In case there is a
3913 problem we issue an error message and continue with the next line. The last
3914 thing we check is whether there is a closing right parenthesis.
3915
3916 <<process <tt>ticks</tt> part>>=
3917 chk_lparen("ticks",$lc);
3918 my $ec;
3919 ($xticks,$ec) = ComputeDist($lc);
3920 next LINE if $ec == 0;
3921 chk_comma($lc);
3922 ($yticks,$ec) = ComputeDist($lc);
3923 next LINE if $ec == 0;
3924 chk_rparen("ticks(arguments",$lc);
3925
3926 @ We actually emit code if the user has specified either the [[X]] or
3927 [[Y]] option in the [[axis]] part. If the user has specified the
3928 [[Y*]] or the [[X*]] option in the axis part, we just emit the commands
3929 [[\axis left shiftedto x=0]] or [[\axis bottom shiftedto y=0]] respectively
3930 and exit. If the use has specified ticks, then, depending on the options
3931 he had supplied with the [[axis]] part, we emit code that
3932 implements the user's wishes.
3933 **** HERE WE MUST EXPLAIN THE MEANING OF THE CODE EMITTED!!! *****
3934
3935 <<generate the rest of the code for the <tt>paper</tt> command>>=
3936 YBRANCH: {
3937 if (index($axis, "Y")>-1)
3938 {
3939 if (index($axis, "Y*")>-1)
3940 {
3941 print OUT "\\axis left shiftedto x=0 / \n";
3942 last YBRANCH;
3943 }
3944 if ($yticks > 0)
3945 {
3946 if (index($axis, "T")>-1 && index($axis, "B")==-1)
3947 {
3948 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
3949 print OUT "$ylow to -$yticks by $yticks\n from $yticks to ";
3950 print OUT $yhigh-$yticks," by $yticks /\n";
3951 }
3952 elsif (index($axis, "T")==-1 && index($axis, "B")>-1)
3953 {
3954 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
3955 print OUT $ylow+$yticks," to -$yticks by $yticks\n from ";
3956 print OUT "$yticks to $yhigh by $yticks /\n";
3957 }
3958 elsif (index($axis, "T")>-1 && index($axis, "B")>-1)
3959 {
3960 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
3961 print OUT $ylow+$yticks," to -$yticks by $yticks\n from ";
3962 print OUT "$yticks to ",$yhigh-$yticks," by $yticks /\n";
3963 }
3964 else
3965 {
3966 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
3967 print OUT "$ylow to -$yticks by $yticks\n from ";
3968 print OUT "$yticks to $yhigh by $yticks /\n";
3969 }
3970 }
3971 else
3972 {
3973 print OUT "\\axis left shiftedto x=0 /\n";
3974 }
3975 }
3976 }
3977 XBRANCH: { if (index($axis, "X")>-1)
3978 {
3979 if (index($axis, "X*")>-1)
3980 {
3981 print OUT "\\axis bottom shiftedto y=0 /\n";
3982 last XBRANCH;
3983 }
3984 if ($xticks > 0)
3985 {
3986 if (index($axis, "L")>-1 && index($axis, "R")==1)
3987 {
3988 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
3989 print OUT $xlow + $xticks," to -$xticks by $xticks\n from";
3990 print OUT " $xticks to $xhigh by $xticks /\n";
3991 }
3992 elsif (index($axis, "L")==-1 && index($axis, "R")>-1)
3993 {
3994 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
3995 print OUT "$xlow to -$xticks by $xticks\n from ";
3996 print OUT "$xticks to ",$xhigh-$xticks," by $xticks /\n";
3997 }
3998 elsif (index($axis, "L")>-1 && index($axis, "R")>-1)
3999 {
4000 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
4001 print OUT $xlow + $xticks," to -$xticks by $xticks\n from ";
4002 print OUT "$xticks to ",$xhigh - $xticks," by $xticks /\n";
4003 }
4004 else
4005 {
4006 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
4007 print OUT "$xlow to -$xticks by $xticks\n from ";
4008 print OUT "$xticks to $xhigh by $xticks /\n";
4009 }
4010 }
4011 else
4012 {
4013 print OUT "\\axis bottom shiftedto y=0 /\n";
4014 }
4015 } }
4016 LBRANCH: {if (index($axis, "L")>-1)
4017 {
4018 if (index($axis, "L")>-1)
4019 {
4020 if (index($axis, "L*")>-1)
4021 {
4022 print OUT "\\axis left /\n";
4023 last LBRANCH;
4024 }
4025 if ($yticks > 0)
4026 {
4027 print OUT "\\axis left ticks numbered from ";
4028 print OUT "$ylow to $yhigh by $yticks /\n";
4029 }
4030 else
4031 {
4032 print OUT "\\axis left /\n";
4033 }
4034 }
4035 } }
4036 RBRANCH: { if (index($axis, "R")>-1)
4037 {
4038 if (index($axis, "R*")>-1)
4039 {
4040 print OUT "\\axis right /\n";
4041 last RBRANCH;
4042 }
4043 if ($yticks > 0)
4044 {
4045 print OUT "\\axis right ticks numbered from $ylow to $yhigh by ";
4046 print OUT "$yticks /\n";
4047 }
4048 else
4049 {
4050 print OUT "\\axis right /\n";
4051 }
4052 } }
4053 TBRANCH: { if (index($axis, "T")>-1)
4054 {
4055 if (index($axis, "T*")>-1)
4056 {
4057 print OUT "\\axis top /\n";
4058 last TBRANCH;
4059 }
4060 if ($xticks > 0)
4061 {
4062 print OUT "\\axis top ticks numbered from $xlow to $xhigh by ";
4063 print OUT "$xticks /\n";
4064 }
4065 else
4066 {
4067 print OUT "\\axis top /\n";
4068 }
4069 } }
4070 BBRANCH: { if (index($axis, "B")>-1)
4071 {
4072 if (index($axis, "B*")>-1)
4073 {
4074 print OUT "\\axis bottom /\n";
4075 last BBRANCH;
4076 }
4077 if ($xticks > 0)
4078 {
4079 print OUT "\\axis bottom ticks numbered from $xlow to $xhigh by ";
4080 print OUT "$xticks /\n";
4081 }
4082 else
4083 {
4084 print OUT "\\axis bottom /\n";
4085 }
4086 } }
4087
4088
4089 @ The syntax of the [[point]] commands follows:
4090 <pre>
4091 point[*](PointName){Coordinates}[PointSymbol]
4092 </pre>
4093 where [[PointName]] is valid point name, [[Coordinates]] is either a
4094 pair of numbers denoting the coordinates of the point or an expression
4095 by means of which the system computes the coordinates of the point, and
4096 the [[PointSymbol]] is a valid T<sub><font size=+1>E</font></sub>X
4097 command denoting a point symbol. A valid point name consists of a
4098 letter and at most two trailing digits. That is, the names [[a11]],
4099 [[b2]] and [[c]] are valid names while [[qw]] and [[s123]] are not.
4100 The first thing we do is to set the point shape to the default symbol
4101 (this has been initialized in the main program). Next, we check whether
4102 we have a [[point]]command or a [[point*]] simply by inspecting the very
4103 next token. Note that there must be no blank spaces between the token
4104 [[point]] and the star symbol. Next, we get the point name: remember that
4105 the point name is surrounded by parentheses. In case we don't find a valid
4106 point name we issue an error message and continue with the next line of
4107 input. Suppose the point name was a valid one. If we have a [[point*]]
4108 command we must ensure that the this particular point name has been defined.
4109 If we have a [[point]] command we must ensure that this particular point
4110 name has not been defined. Point names are stored in the hash [[%PointTable]].
4111 We are now ready to process the coordinates part and the optional
4112 plot symbol part.
4113
4114 <<process <tt>point/point*</tt> commands>>=
4115 my ($pointStar, $PointName, $origPN);
4116 $pointStar = 0; # default value: he have a point command
4117 $pointStar = 1 if s/^\*//;
4118 chk_lparen("point" . (($pointStar)?"*":""),$lc);
4119 if (s/^([^\W\d_](?![^\W\d_])\d{0,4})//i) {
4120 #
4121 # Note: the regular expression (foo)(?!bar) means that we are
4122 # looking a foo not followed by a bar. Moreover, the regular
4123 # expression [^\W\d_] means that we are looking for letter.
4124 #
4125 $origPN = $1;
4126 $PointName = lc($1);
4127 }
4128 else {
4129 PrintErrorMessage("Invalid point name",$lc);
4130 next LINE;
4131 }
4132 #if ($pointStar and !exists($PointTable{$PointName})) {
4133 # PrintWarningMessage("Point $origPN has not been defined",$lc);
4134 #}
4135 if (!$pointStar and exists($PointTable{$PointName})) {
4136 PrintWarningMessage("Point $origPN has been used already",$lc);
4137 }
4138 chk_rparen("point" . (($pointStar)?"*":""). "($origPN",$lc);
4139 chk_lcb("point" . (($pointStar)?"*":""). "($origPN)",$lc);
4140 my ($Px, $Py);
4141 <<process coordinates>>
4142 chk_rcb("coordinates part",$lc);
4143 my $sv = $defaultsymbol;
4144 my $sh = $defaultLFradius;
4145 my $side_or_radius = undef;
4146 if (s/^\[\s*//) { # the user has opted to specify the optional part
4147 <<process optional point shape part>>
4148 chk_rsb("optional part",$lc);
4149 }
4150 # to avoid truncation problems introduced by the pack function, we
4151 # round each number up to five decimal digits
4152 $Px = sprintf("%.5f", $Px);
4153 $Py = sprintf("%.5f", $Py);
4154 print OUT "%% point$Point_Line \t$origPN = ($Px, $Py)\n" if $comments_on;
4155 chk_comment($lc);
4156 $PointTable{$PointName} = pack("d3A*",$Px,$Py,$sh,$sv);
4157 if (defined($side_or_radius)) {
4158 $DimOfPoint{$PointName} = $side_or_radius;
4159 }
4160
4161 @ In this section we parse the [[Coordinates]] part of the [[point]] command.
4162 The complete syntax of the [[Coordinates]] part follows:
4163 <pre>
4164 Coordinates ::= Variable |
4165 Distance "," Distance |
4166 "midpoint" "(" Point-Name Point-Name ")" |
4167 "pointOnLine" "(" Two-Points "," Distance ")" |
4168 "intersection" "(" Two-Points "," Two-Points ")" |
4169 "perpendicular" "(" Point-Name "," Two-Points ")" |
4170 "circumCircleCenter" "(" Three-Points ") |
4171 "incircleCenter" "(" Three-Points ")" |
4172 "excircleCenter" "(" Three-Points "," Two-Points ")" |
4173 Point-Name [ "," Modifier ]
4174
4175 Modifier ::= "shift" "(" Distance "," Distance ")" |
4176 "polar" "(" Distance, Distance [ "deg" | "rad" ] ")" |
4177 "rotate" "(" Point-Name, Distance [ "deg" | "rad" ] ")" |
4178 "vector" "(" Two-Points ")"
4179
4180 Distance ::= expression
4181
4182 Two-Points ::= Point-Name Point-Name
4183
4184 Three-Points ::= Point-Name Two-Points
4185 </pre>
4186 We now briefly explain the functionality of each option:
4187 <ul>
4188 <li>midpoint(AB): the midpoint between points A and B</li>
4189 <li>pointOnLine(AB,d): point at distance d from A towards B</li>
4190 <li>intersection(AB,CD): intersection of lines defined by AB and CD</li>
4191 <li>perpendicular(A,BC): point of the foot of the perpendicular from A to line BC</li>
4192 <li>circumCircleCenter(ABC): center of circumcircle of triangle ABC</li>
4193 <li>incircleCenter(ABC):center of incircle of triangle ABC</li>
4194 <li>excircleCenter(ABC,BC): center of excircle of triangle ABC, touching
4195 side BC</li>
4196 <li>A, shift(x,y): Point displaced from A by x and y along the X and Y
4197 axes</li>
4198 <li>A, polar(r,d): Point displaced from A by distance r in direction d</li>
4199 <li>A, rotate(B,d): Rotate A about B by d</li>
4200 </ul>
4201 We now explain how the following piece of code operates. In case the first
4202 token is a number, we assume that the coordinates are specified by a
4203 number and another number, a variable or a pair of points. So, we check
4204 whether there is a comma and use the subroutine [[ComputeDist]] to get the
4205 second coordinate. In case the next token is one of the words
4206 [[perpendicular]], [[intersection]], [[midpoint]], [[pointonline]],
4207 [[circumcircleCenter]], [[IncircleCenter]], or [[ExcircleCenter]]
4208 we consume the corresponding token and process the corresponding case.
4209 In case the first two tokens are two identifiers, then we assume that we
4210 have a pair of numbers. We compute their distance, check whether there is
4211 a leading comma and compute the y-coordinate by calling subroutine
4212 [[ComputeDist]]. In case the next token is a single identifier, we store
4213 its name in the variable [[$PointA]]. If this identifier is a defined point name,
4214 we check whether the next token is a comma. In case it is, we check whether
4215 he token after the comma is either the token [[shift]], [[polar]], or
4216 [[rotate]] and process each case accordingly. If it is
4217 none of these tokens we issue an error message and continue with the next
4218 input line. Now, if the token after the identifier isn't a comma, we assume
4219 that the coordinates of the point will be identical to those of the point
4220 whose name has been stored in the variable [[$PointA]]. If the identifier is a
4221 variable name, we assume that the x-coordinate is the value of this variable.
4222 We check whether the next token is a comma, and compute the y-coordinate by
4223 calling the subroutine [[ComputeDist]]. The x-coordinate is stored in the variable
4224 [[$Px]] and the y-coordinate in the variable [[$Py]].
4225
4226 <<process coordinates>>=
4227 if (s/^perpendicular(?=\W)//i) {
4228 <<process <tt>perpendicular</tt> case>>
4229 }
4230 elsif (s/^intersection(?=\W)//i) {
4231 <<process <tt>intersection</tt> case>>
4232 }
4233 elsif (s/^midpoint(?=\W)//i) {
4234 <<process <tt>midpoint</tt> case>>
4235 }
4236 elsif (s/^pointonline(?=\W)//i) {
4237 <<process <tt>pointonline</tt> case>>
4238 }
4239 elsif (s/^circumcircleCenter(?=\W)//i) {
4240 <<process <tt>circumcircleCenter</tt> case>>
4241 }
4242 elsif (s/^IncircleCenter(?=\W)//i) {
4243 <<process <tt>IncircleCenter</tt> case>>
4244 }
4245 elsif (s/^ExcircleCenter(?=\W)//i) {
4246 <<process <tt>ExcircleCenter</tt> case>>
4247 }
4248 elsif (/^[^\W\d_]\d{0,4}\s*[^,\w]/) {
4249 m/^([^\W\d_]\d{0,4})\s*/i;
4250 if (exists($PointTable{lc($1)})) {
4251 my $Tcoord = get_point($lc);
4252 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{$Tcoord});
4253 $Px = $x;
4254 $Py = $y;
4255 }
4256 else {
4257 $Px = expr();
4258 chk_comma($lc);
4259 $Py = expr();
4260 }
4261 }
4262 elsif (/[^\W\d_]\d{0,4}\s*,\s*shift|polar|rotate|vector/i) { #a point?
4263 s/^([^\W\d_]\d{0,4})//i;
4264 my $PointA = $1;
4265 if (exists($PointTable{lc($PointA)})) {
4266 s/\s*//;
4267 if (s/^,//) {
4268 s/\s*//;
4269 if (s/^shift(?=\W)//i) {
4270 <<process <tt>shift</tt> case>>
4271 }
4272 elsif (s/^polar(?=\W)//i) {
4273 <<process <tt>polar</tt> case>>
4274 }
4275 elsif (s/^rotate(?=\W)//i) {
4276 <<process <tt>rotate</tt> case>>
4277 }
4278 elsif (s/^vector(?=\W)//i) {
4279 <<process <tt>vector</tt> case>>
4280 }
4281 else {
4282 PrintErrorMessage("unexpected token",$lc);
4283 next LINE;
4284 }
4285 }
4286 else {
4287 my ($xA,$yA,$pSVA,$pSA)=unpack("d3A*",$PointTable{lc($PointA)});
4288 $Px = $xA;
4289 $Py = $yA;
4290 }
4291 }
4292 else {
4293 PrintErrorMessage("Undefined point $PointA",$lc);
4294 next LINE;
4295 }
4296 }
4297 else {
4298 $Px = expr();
4299 chk_comma($lc);
4300 $Py = expr();
4301 }
4302
4303 @ In the following piece of code we process the [[perpendicular]]
4304 case of the [[point]] specification. We first check whether there is an
4305 opening left parenthesis. Next, we get the first point name. In case
4306 there is no point name, we simply abandon the processing of this
4307 line and continue with the next one. Then we see whether there is
4308 a trailing comma. Omitting this token yields a non-fatal error.
4309 Then we get two more points. As before, if we can't find any of these
4310 points this yields a fatal-error. Note, that each time we check that the
4311 point names correspond to existing point names. Then, we call subroutine
4312 [[perpendicular]] to calculate the coordinates of the point.
4313
4314 <<process <tt>perpendicular</tt> case>>=
4315 chk_lparen("perpendicular",$lc);
4316 my $FirstPoint = &get_point($lc);
4317 next LINE if $FirstPoint eq "_undef_";
4318 chk_comma($lc);
4319 my $SecondPoint = &get_point($lc);
4320 next LINE if $SecondPoint eq "_undef_";
4321 my $ThirdPoint = &get_point($lc);
4322 next LINE if $ThirdPoint eq "_undef_";
4323 chk_rparen("No closing parenthesis found",$lc);
4324 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4325 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4326 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
4327 ($Px, $Py) = perpendicular($x1,$y1,$x2,$y2,$x3,$y3);
4328
4329 @ In the following piece of code we process the [[intersection]] case of the
4330 [[point]] specification. We get the four point names and if there is
4331 no error we compute the intersection point by calling subroutine
4332 [[intersection]].
4333
4334 <<process <tt>intersection</tt> case>>=
4335 chk_lparen("intersection",$lc);
4336 my $FirstPoint = get_point($lc);
4337 next LINE if $FirstPoint eq "_undef_";
4338 my $SecondPoint = get_point($lc);
4339 next LINE if $SecondPoint eq "_undef_";
4340 chk_comma($lc);
4341 my $ThirdPoint = get_point($lc);
4342 next LINE if $ThirdPoint eq "_undef_";
4343 my $ForthPoint = get_point($lc);
4344 next LINE if $ForthPoint eq "_undef_";
4345 chk_rparen("No closing parenthesis found",$lc);
4346 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4347 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4348 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
4349 my ($x4,$y4,$pSV4,$pS4)=unpack("d3A*",$PointTable{$ForthPoint});
4350 ($Px, $Py) = intersection4points($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4);
4351
4352
4353 @ Given two points A and B, the midpoint option computes the coordinates
4354 of a third point that lies on the middle of the line segment defined by
4355 these two points. We get the the two points, and then we compute the
4356 coordinates of the midpoint with function [[midpoint]].
4357
4358 <<process <tt>midpoint</tt> case>>=
4359 chk_lparen("midpoint",$lc);
4360 my $FirstPoint = &get_point($lc);
4361 next LINE if $FirstPoint eq "_undef_";
4362 my $SecondPoint = &get_point($lc);
4363 next LINE if $SecondPoint eq "_undef_";
4364 chk_rparen("No closing parenthesis found",$lc);
4365 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4366 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4367 ($Px, $Py) = midpoint($x1, $y1, $x2, $y2);
4368
4369 @ Given two points A and B and length d, the [[PointOnLine]] option
4370 computes the coordinates of a point that lies d units in the direction from
4371 A towards B. We first get the coordinates of the two points that define
4372 the line and then we get the distance, which can be a number, a variable,
4373 or a pair of points.
4374
4375 <<process <tt>pointonline</tt> case>>=
4376 chk_lparen("pointonline",$lc);
4377 my $FirstPoint = &get_point($lc);
4378 next LINE if $FirstPoint eq "_undef_";
4379 my $SecondPoint = &get_point($lc);
4380 next LINE if $SecondPoint eq "_undef_";
4381 chk_comma($lc);
4382 # now get the distance
4383 my $distance = expr($lc);
4384 chk_rparen("No closing parenthesis found",$lc);
4385 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4386 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4387 ($Px, $Py) = pointOnLine($x1,$y1,$x2,$y2,$distance);
4388
4389 @ The [[circumcircleCenter]] is used when one wants to compute the coordinates
4390 of the center of circle that passes through the three points
4391 of a triangle defined
4392 by the three arguments of the option. All we do is get the coordinates
4393 of the three points and then we call the subroutine [[circumCircleCenter]]
4394 to compute the center.
4395
4396 <<process <tt>circumcircleCenter</tt> case>>=
4397 chk_lparen("circumCircleCenter",$lc);
4398 my $FirstPoint = &get_point($lc);
4399 next LINE if $FirstPoint eq "_undef_";
4400 my $SecondPoint = &get_point($lc);
4401 next LINE if $SecondPoint eq "_undef_";
4402 my $ThirdPoint = &get_point($lc);
4403 next LINE if $ThirdPoint eq "_undef_";
4404 chk_rparen("No closing parenthesis found",$lc);
4405 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4406 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4407 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
4408 ($Px, $Py,$r) = &circumCircleCenter($x1,$y1,$x2,$y2,$x3,$y3,$lc);
4409 next LINE if $Px == 0 and $Py == 0 and $r == 0;
4410
4411 @ The [[IncircleCenter]] option is to determine the coordinates of a point
4412 that is the center of circle that internally touches the sides
4413 of a triangle defined by three given points.
4414 The coordinates are computed by the subroutine [[IncircleCenter]].
4415
4416 <<process <tt>IncircleCenter</tt> case>>=
4417 chk_lparen("IncircleCenter",$lc);
4418 my $FirstPoint = &get_point($lc);
4419 next LINE if $FirstPoint eq "_undef_";
4420 my $SecondPoint = &get_point($lc);
4421 next LINE if $SecondPoint eq "_undef_";
4422 my $ThirdPoint = &get_point($lc);
4423 next LINE if $ThirdPoint eq "_undef_";
4424 chk_rparen("No closing parenthesis found",$lc);
4425 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
4426 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
4427 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
4428 ($Px, $Py, $r) = IncircleCenter($x1,$y1,$x2,$y2,$x3,$y3);
4429
4430 @ The [[ExcircleCenter]] option is used to define the coordinates of point
4431 that is the center of an excircle of a triangle. We first check
4432 whether there is an opening left parenthesis. Next, we get the names of the
4433 three points that define the triangle. Then, we
4434 check whether there is a comma. Now we get the names of the two points that
4435 define one side of the triangle. We check whether the two points we
4436 get are of the set of the triangle points. If not we issue
4437 an error message and continue with the next input line. Then we make sure
4438 that these two points are not identical. We compute the actual
4439 coordinates by calling the subroutine [[excircle]]. Finally, we
4440 make sure there is a closing right parenthesis.
4441
4442 <<process <tt>ExcircleCenter</tt> case>>=
4443 chk_lparen("ExcircleCenter",$lc);
4444 my $PointA = get_point($lc);
4445 next LINE if $PointA eq "_undef_";
4446 my $PointB = get_point($lc);
4447 next LINE if $PointB eq "_undef_";
4448 my $PointC = get_point($lc);
4449 next LINE if $PointC eq "_undef_";
4450 chk_comma($lc);
4451 my $PointD = &get_point($lc);
4452 next LINE if $PointD eq "_undef_";
4453 if (!memberOf($PointD, $PointA, $PointB, $PointC)) {
4454 PrintErrorMessage("Current point isn't a side point",$lc);
4455 next LINE;
4456 }
4457 my $PointE = get_point($lc);
4458 next LINE if $PointE eq "_undef_";
4459 if (!memberOf($PointE, $PointA, $PointB, $PointC)) {
4460 PrintErrorMessage("Current point isn't a side point",$lc);
4461 next LINE;
4462 }
4463 if ($PointD eq $PointE) {
4464 PrintErrorMessage("Side points are identical",$lc);
4465 next LINE;
4466 }
4467 ($Px, $Py, $r) = excircle($PointA, $PointB, $PointC,
4468 $PointD, $PointE);
4469 chk_rparen("after coordinates part",$lc);
4470
4471 @ The [[shift]] option allows us to define a point's coordinates relative
4472 to the coordinates of an existing point by using two shift parameters. Each
4473 parameter can be either a float, a variable name, or a pair of points.
4474
4475 <<process <tt>shift</tt> case>>=
4476
4477 chk_lparen("shift",$lc);
4478 my $dist1 = expr($lc);
4479 chk_comma($lc);
4480 my $dist2 = expr($lc);
4481 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
4482 $Px = $x1 + $dist1;
4483 $Py = $y1 + $dist2;
4484 chk_rparen("shift part",$lc);
4485
4486 @ The [[polar]] option allows us to define a point's coordinates relative
4487 to the coordinates of an existing point using the polar coordinates of some
4488 other point. We first check whether there is a left parenthesis,
4489 Then we parse the various parts of the [[polar]] option.
4490 In case the user has specified the angle in degrees, we have
4491 to transform it into radians, as all trigonometric function expect their
4492 arguments to be radians. Next, we compute the coordinates of the point.
4493 We conclude by checking whether there is a closing parenthesis.
4494
4495 <<process <tt>polar</tt> case>>=
4496 chk_lparen("polar",$lc);
4497 my ($R1, $Theta1);
4498 $R1 = expr($lc);
4499 chk_comma($lc);
4500 $Theta1 = expr($lc);
4501 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
4502 s/\s*//;
4503 if (s/^rad(?=\W)//i) {
4504 # do nothing!
4505 }
4506 elsif (s/^deg(?=\W)//i) {
4507 $Theta1 = $Theta1 * PI / 180;
4508 }
4509 else {
4510 #$Theta1 = $Theta1 * PI / 180;
4511 }
4512 $Px = $x1 + $R1 * cos($Theta1);
4513 $Py = $y1 + $R1 * sin($Theta1);
4514 chk_rparen("after polar part",$lc);
4515
4516 @ The [[rotate]] option allows us to define a point's coordinates by
4517 rotating an existing point, Q, about a third point, P, by a
4518 specified angle.
4519 The method to achieve this is to first get the coordinates of points
4520 P and Q and then
4521 <ol>
4522 <li> translate origin to P</li>
4523 <li> rotate about P</li>
4524 <li> translate from P back to origin, etc</li>
4525 </ol>
4526 As in the case of the [[polar]] option, we check for an opening parenthesis.
4527 Next, we parse the point name and the angle. At this point we are able to
4528 compute the coordinates of the rotated point. We conclude by checking
4529 whether there is a closing parenthesis.
4530
4531 <<process <tt>rotate</tt> case>>=
4532 chk_lparen("rotate",$lc);
4533 my $Q = lc($PointA);
4534 my $P = get_point($lc);
4535 next LINE if $P eq "_undef_";
4536 chk_comma($lc);
4537 my $Theta1 = expr($lc);
4538 my ($xP,$yP,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P});
4539 my ($xQ,$yQ,$pSV2,$pS2)=unpack("d3A*",$PointTable{$Q});
4540 s/\s*//;
4541 if (s/^rad(?=\W)//i)
4542 {
4543 # do nothing!
4544 }
4545 elsif (s/^deg(?=\W)//i)
4546 {
4547 $Theta1 = $Theta1 * PI / 180;
4548 }
4549 else
4550 {
4551 $Theta1 = $Theta1 * PI / 180;
4552 }
4553 # shift origin to P
4554 $xQ -= $xP;
4555 $yQ -= $yP;
4556 # do the rotation
4557 $Px = $xQ * cos($Theta1) - $yQ * sin($Theta1);
4558 $Py = $xQ * sin($Theta1) + $yQ * cos($Theta1);
4559 # return origin back to original origin
4560 $Px += $xP;
4561 $Py += $yP;
4562 chk_rparen("after rotate part",$lc);
4563
4564 @ [[vector(PQ)]] is actually is a shorthand of [[shift(xQ-xP,yQ-yP)]]. Thus, it
4565 is implemented by borrowing code from the [[shift]] modifier.
4566
4567 <<process <tt>vector</tt> case>>=
4568 chk_lparen("vector",$lc);
4569 my ($x0,$y0,$pSV0,$pS0) = unpack("d3A*",$PointTable{lc($PointA)});
4570 my $P = get_point($lc);
4571 my $Q = get_point($lc);
4572 my ($x1,$y1,$pSV1,$pS1) = unpack("d3A*",$PointTable{$P});
4573 my ($x2,$y2,$pSV2,$pS2) = unpack("d3A*",$PointTable{$Q});
4574 $Px = $x0 + $x2 - $x1;
4575 $Py = $y0 + $y2 - $y1;
4576 chk_rparen("vector part",$lc);
4577
4578
4579 @ When lines are drawn to a point, the line will (unless otherwise
4580 specified) extend to the point location. However, this can be prevented by
4581 allocating an optional circular line-free zone to a point by specifying the
4582 radius (in square brackets) of the optional point shape part. Currently, in this part
4583 we are allowed to describe the point shape and the radius value. If only the
4584 radius is specified, e.g., <tt>[radius=5]</tt>, then the line-free zone will be
4585 applied to the default point character, i.e., <tt>$\bullet$</tt> or whatever it
4586 has been set to. Here is the syntax we employ:
4587 <pre>
4588 Optional_point_shape_part ::= "[" [ symbol_part ] [","] [ radius_part ]"
4589 symbol_part ::= "symbol" "=" symbol
4590 symbol ::= "circle" "(" expression ")" |
4591 "square" "(" expression ")" |
4592 LaTeX_Code
4593 radius_part ::= "radius" "=" expression
4594 </pre>
4595 Note that it is possible to have right square bracket in the <tt>LaTeX_Code</tt> but it
4596 has to be escaped (i.e., <tt>\]</tt>).
4597
4598 <<process optional point shape part>>=
4599 if (/^(symbol|radius|side)\s*/i) {
4600 my @previous_options = ();
4601 my $number_of_options = 1;
4602 my $symbol_set = 0;
4603 while (s/^(symbol|radius)\s*//i and $number_of_options <= 2) {
4604 my $option = lc($1);
4605 if (s/^=\s*//) {
4606 if (memberOf($option,@previous_options)) {
4607 PrintErrorMessage("Option \"$option\" has been already defined", $lc);
4608 my $dummy = expr($lc);
4609 }
4610 elsif ($option eq "radius") {
4611 $sh = expr($lc);
4612 $sv = $defaultsymbol if ! $symbol_set;
4613 }
4614 elsif ($option eq "symbol") {
4615 if (s/^circle\s*//i) {
4616 $sv = "circle";
4617 chk_lparen("after token circle",$lc);
4618 $side_or_radius = expr($lc);
4619 chk_rparen("expression",$lc);
4620 }
4621 elsif (s/^square\s*//i) {
4622 $sv = "square";
4623 chk_lparen("after token square",$lc);
4624 $side_or_radius = expr($lc);
4625 chk_rparen("expression",$lc);
4626 }
4627 elsif (s/^(((\\\]){1}|(\\,){1}|(\\\s){1}|[^\],\s])+)//) {
4628 $sv = $1;
4629 $sv =~ s/\\\]/\]/g;
4630 $sv =~ s/\\,/,/g;
4631 $sv =~ s/\\ / /g;
4632 s/\s*//;
4633 }
4634 $symbol_set = 1;
4635 }
4636 }
4637 else {
4638 PrintErrorMessage("unexpected token", $lc);
4639 next LINE;
4640 }
4641 $number_of_options++;
4642 push (@previous_options, $option);
4643 s/^,\s*//;
4644 }
4645 }
4646 else {
4647 PrintErrorMessage("unexpected token", $lc);
4648 next LINE;
4649 }
4650
4651 @ The [[ArrowShape]] command has either one or three arguments. If the only argument of
4652 the command is the token [[default]], then the parameters associated with the
4653 arrow shape resume their default values. Now, if there are three arguments, these are
4654 used to specify the shape of an arrow. The command actually sets the three global variables
4655 [[$arrowLength]], [[$arrowAngleB]] and [[$arrowAngleC]]. Arguments whose value is equal
4656 to zero, do not affect the value of the corresponding global variables. To reset the
4657 values of the global variables one should use the commane with [[default]] as it
4658 only argument. The syntax of the command is as follows:
4659 <center>
4660 <tt>"ArrowShape" "(" expr [ units ] "," expr "," expr ")"</tt> or<br>
4661 <tt>"ArrowShape" "(" "default" ")" </tt>
4662 </center>>
4663 Here [[units]] is any valid TeX unit (e.g., "mm", "cm", etc.). Note that if
4664 any of the three expressions is equal to zero, the default value is taken
4665 instead. As direct consequence, if the value of the first expression is zero,
4666 the units part is actually ignored.
4667
4668 <<process <tt>ArrowShape</tt> command>>=
4669
4670 chk_lparen("$cmd",$lc);
4671 if (s/^default//i) {
4672 $arrowLength = 2;
4673 $arrowLengthUnits = "mm";
4674 $arrowAngleB = 30;
4675 $arrowAngleC = 40;
4676 }
4677 else {
4678 my ($LocalArrowLength, $LocalArrowAngleB ,$LocalArrowAngleC) = (0,0,0);
4679 $LocalArrowLength = expr($lc);
4680 if (s/^\s*($units)//i) {
4681 $arrowLengthUnits = "$1";
4682 }
4683 else {
4684 $xunits =~ /(\d+(\.\d+)?)\s*($units)/;
4685 $LocalArrowLength *= $1;
4686 $arrowLengthUnits = "$3";
4687 }
4688 chk_comma($lc);
4689 $LocalArrowAngleB = expr($lc);
4690 chk_comma($lc);
4691 $LocalArrowAngleC = expr($lc);
4692 $arrowLength = ($LocalArrowLength == 0 ? 2 : $LocalArrowLength);
4693 $arrowLengthUnits = ($LocalArrowLength == 0 ? "mm" : $arrowLengthUnits);
4694 $arrowAngleB = ($LocalArrowAngleB == 0 ? 30 : $LocalArrowAngleB);
4695 $arrowAngleC = ($LocalArrowAngleC == 0 ? 40 : $LocalArrowAngleC);
4696 }
4697 chk_rparen("after $cmd arguments",$lc);
4698 chk_comment("after $cmd command",$lc);
4699 print OUT "%% arrowLength = $arrowLength$arrowLengthUnits, ",
4700 "arrowAngleB = $arrowAngleB ",
4701 "and arrowAngleC = $arrowAngleC\n" if $comments_on;
4702
4703 @ The [[PointSymbol]] command is used to set the point symbol and possibly its
4704 line-free radius. The point symbol can be either a LaTeX symbol or the word [[default]]
4705 which corresponds to the default point symbol, i.e., <tt>$\bullet$</tt>. The line-free
4706 radius can be an expression. Here is the complete syntax:
4707 <pre>
4708 pointsymbol ::= "pointsymbol" ( symbol [ "," radius])
4709 symbol ::= "default" | circle | square | LaTeX_Code
4710 circle ::= "circle" "(" expression ")"
4711 square ::= "square" "(" expression ")"
4712 radius ::= expression
4713 </pre>
4714 Note that the <tt>LaTeX_Code</tt> can contain the symbols <tt>\,</tt> and
4715 <tt>\)</tt> which are escape sequences for a comma and right parenthesis, respectively.
4716
4717 <<process <tt>PointSymbol</tt> command>>=
4718
4719 chk_lparen("$cmd",$lc);
4720 if (s/^default//i) #default point symbol
4721 {
4722 $defaultsymbol = "\$\\bullet\$";
4723 }
4724 elsif (s/^(circle|square)//i) {
4725 $defaultsymbol = $1;
4726 chk_lparen($defaultsymbol, $lc);
4727 $GlobalDimOfPoints = expr($lc);
4728 chk_rparen("expression", $lc);
4729 }
4730 elsif (s/^(((\\,){1}|(\\\)){1}|(\\\s){1}|[^\),\s])+)//) #arbitrary LaTeX point
4731 {
4732 $defaultsymbol = $1;
4733 $defaultsymbol=~ s/\\\)/\)/g;
4734 $defaultsymbol=~ s/\\,/,/g;
4735 $defaultsymbol=~ s/\\ / /g;
4736 }
4737 else
4738 {
4739 PrintErrorMessage("unrecognized point symbol",$lc);
4740 }
4741 if (s/\s*,\s*//) {
4742 $defaultLFradius = expr($lc);
4743 }
4744 chk_rparen("after $cmd arguments",$lc);
4745 chk_comment("after $cmd command",$lc);
4746
4747 @ The [[system]] command provides a shell escape. However, we use a subroutine
4748 to check whether the argument of the command contains tainted data. If this
4749 is the case, then we simply ignore this command. The syntax of the command
4750 is as follows:
4751 <pre>
4752 system-cmd ::= "system" "(" string ")"
4753 </pre>
4754 where string is just a sequence of characters enclosed in quotation marks.
4755 We start by parsing a left parenthesis and then we get the command by
4756 calling the subroutine [[get_string]]. If there is an error we skip this
4757 command. Otherwise, we assign to the variable [[$_]] what is left. Now we check
4758 if the variable [[$command]] contains any tainted data. If it doesn't, we
4759 execute the command, otherwise we print an error message and skip to the
4760 next input line. Next, we check for closing right parenthesis and a possible
4761 trailing comment.
4762
4763 <<process <tt>system</tt> command>>=
4764
4765 chk_lparen("$cmd",$lc);
4766 my ($error, $command, $rest) = get_string($_);
4767 next LINE if $error == 1;
4768 $_ = $rest;
4769 if (! is_tainted($command)) {
4770 system($command);
4771 }
4772 else {
4773 PrintErrorMessage("String \"$command\" has tainted data", $lc);
4774 next LINE;
4775 }
4776 chk_rparen("after $cmd arguments",$lc);
4777 chk_comment("after $cmd command",$lc);
4778
4779 @ The [[text]] command is used to put a piece of text or a symbol on
4780 a particular point of the resulting graph. The syntax of the command is
4781 as follows:
4782 <pre>
4783 text-comm ::= "text" "(" text ")" "{"coords"} "[" pos-code "]"
4784 text ::= ascii string
4785 coords ::= Coord "," Coord |
4786 Point-Name "," "shift" "(" Coord "," Coord ")" |
4787 Point-Name "," "polar" "(" Coord "," Coord [angle-unit] ")"
4788 Coord ::= decimal number | variable | pair-of-Point-Names
4789 pair-of-Point-Names ::= Point-Name Point-Name
4790 angle-unit ::= "deg" | "rad"
4791 pos-code ::= lr-code [tb-code] | tb-code [lr-code]
4792 lr-code ::= "l" | "r"
4793 tb-code ::= "t" | "b" | "B"
4794 </pre>
4795 Initially, we parse the [[text]]. Since the text may contain parentheses
4796 we assume that the user enters pairs of matching parentheses. Note, that
4797 this is a flaw in the original design of the language, which may be remedied
4798 in future releases of the software. Then, we check the [[coords]] part. Next,
4799 if there is a left square bracket, we assume the user has specified the
4800 [[pos-code]]. We conclude by checking a possible trailing comment.
4801 The next thing we do is to generate the PiCTeX code. The two possible
4802 forms follow:
4803 <center>
4804 <tt>\put {TEXT} [POS] at Px Py</tt><br>
4805 <tt>\put {TEXT} at Px Py</tt><br>
4806 </center>
4807
4808 <<process <tt>text</tt> command>>=
4809
4810 chk_lparen("text",$lc);
4811 my ($level,$text)=(1,"");
4812 TEXTLOOP: while (1)
4813 {
4814 $level++ if /^\(/;
4815 $level-- if /^\)/;
4816 s/^(.)//;
4817 last TEXTLOOP if $level==0;
4818 $text .= $1;
4819 }
4820 chk_lcb("text part",$lc);
4821 my ($Px, $Py,$dummy,$pos);
4822 $pos="";
4823 s/\s*//;
4824 <<process coordinates part of text command>>
4825 chk_rcb("coordinates part of text command",$lc);
4826 if (s/^\[//)
4827 {
4828 s/\s*//;
4829 <<process optional part of text command>>
4830 s/\s*//;
4831 chk_rsb("optional part of text command",$lc);
4832 }
4833 chk_comment($lc);
4834 if ($pos eq "")
4835 {
4836 printf OUT "\\put {%s} at %f %f\n", $text, $Px, $Py;
4837 }
4838 else
4839 {
4840 printf OUT "\\put {%s} [%s] at %f %f\n", $text, $pos, $Px, $Py;
4841 }
4842
4843 @ In this section we define the code that handles the coordinates part
4844 of the [[text]] command. The code just implements the grammar given above.
4845 If the first token is a number, we assume this is the x-coordinate. If
4846 it is a variable, we assume its value is the x-coordinate. However, if
4847 it is a point name, we check whether the next token is another point name.
4848 In this case we compute the distance between the two points. In case we
4849 have a single point followed by a comma, we expect to have either a polar
4850 or a shift part, which we process the same we processed them in the point
4851 command.
4852
4853 <<process coordinates part of text command>>=
4854
4855 if (/^[^\W\d_]\d{0,4}\s*[^,\w]/) {
4856 my $Tcoord = get_point($lc);
4857 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{$Tcoord});
4858 $Px = $x;
4859 $Py = $y;
4860 }
4861 elsif (/[^\W\d_]\d{0,4}\s*,\s*shift|polar/i) {
4862 s/^([^\W\d_]\d{0,4})//i;
4863 my $PointA = $1;
4864 if (exists($PointTable{lc($PointA)})) {
4865 s/\s*//;
4866 if (s/^,//) {
4867 s/\s*//;
4868 if (s/^shift(?=\W)//i) {
4869 <<process <tt>shift</tt> case>>
4870 }
4871 elsif (s/^polar(?=\W)//i) {
4872 <<process <tt>polar</tt> case>>
4873 }
4874 }
4875 }
4876 else {
4877 PrintErrorMessage("undefined point/var",$lc);
4878 next LINE;
4879 }
4880 }
4881 else {
4882 $Px = expr();
4883 chk_comma($lc);
4884 $Py = expr();
4885 }
4886
4887 @ In this section we process the optional part of the [[text]] command.
4888 The general rule is that we are allowed to have up to two options one
4889 from the characters [[l]] and [[r]] and one from the the characters
4890 [[B]], [[b]], and [[t]]. We first check whether the next character is
4891 letter, if it isn't we issue an error message and continue with the next
4892 input line. If it is a letter we check whether it belongs to one of the
4893 two groups and if it doesn't we issue an error message and continue with the
4894 next input line. If the next character belongs to first group, i.e., it is
4895 either [[l]] or [[r]], we store this character into the variable [[$pos]]. Next,
4896 we check whether there is another letter. If it is a letter, we store it
4897 in the variable [[$np]]. Now we make sure that this character belongs to the
4898 other group, i.e., it is either [[b]], [[B]], or [[t]]. In case it belongs
4899 to the other group, we append the value of [[$np]] to the string stored in
4900 the variable [[$pos]]. Otherwise we issue an error message and continue with the
4901 next input line. We work similarly for the other case. In order to check
4902 whether a character belongs to some group of characters, we use the user
4903 defined function [[memberOf]].
4904
4905 <<process optional part of text command>>=
4906
4907 if (s/^(\w{1})\s*//) {
4908 $pos .= $1;
4909 if (memberOf($pos, "l", "r")) {
4910 if (s/^(\w{1})\s*//) {
4911 my $np = $1;
4912 if (memberOf($np, "t", "b", "B")) {
4913 $pos .= $np;
4914 }
4915 else {
4916 if (memberOf($np, "l", "r")) {
4917 PrintErrorMessage("$np can't follow 'l' or 'r'", $lc);
4918 }
4919 else {
4920 PrintErrorMessage("$np is not a valid positioning option", $lc);
4921 }
4922 next LINE;
4923 }
4924 }
4925 }
4926 elsif (memberOf($pos, "t", "b", "B")) {
4927 if (s/^(\w{1})\s*//) {
4928 my $np = $1;
4929 if (memberOf($np, "l", "r")) {
4930 $pos .= $np;
4931 }
4932 else {
4933 if (memberOf($np, "t", "b", "B")) {
4934 PrintErrorMessage("$np can't follow 't', 'b', or 'B'", $lc);
4935 }
4936 else {
4937 PrintErrorMessage("$np is not a valid positioning option", $lc);
4938 }
4939 next LINE;
4940 }
4941 }
4942 }
4943 else {
4944 PrintErrorMessage("$pos is not a valid positioning option", $lc);
4945 next LINE;
4946 }
4947 }
4948 else {
4949 PrintErrorMessage("illegal token in optional part of text command",$lc);
4950 next LINE;
4951 }
4952
4953 @ The [[const]] command is used to store values into a comma separated
4954 list of named constants. Constant names have the same format as point names,
4955 i.e., they start with a letter and are followed by up to two digits. The
4956 whole operation is performed by a [[do-while]] construct that checks that
4957 there is a valid constant name, a [[=]] sign, and an expression. The
4958 [[do-while]] construct terminates if the next token isn't a comma. Variable
4959 [[$Constname]] is used to store the initial variable name, while we store
4960 in variable [[$varname]] the lowercase version of the variable name. In addition,
4961 we make sure a constant is not redefined (or else it wouldn't be a constant:-).
4962 The last thing we do is to check whether there is a trailing comment.
4963 In case there, we simply ignore itl; otherwise we print a warning message.
4964
4965 <<process <tt>const</tt> command>>=
4966 do{
4967 s/\s*//;
4968 PrintErrorMessage("no identifier found after token const",$lc)
4969 if $_ !~ s/^([^\W\d_]\d{0,4})//i;
4970 my $Constname = $1;
4971 my $constname = lc($Constname);
4972 if (exists $ConstTable{$constname}) {
4973 PrintErrorMessage("Redefinition of constant $constname",$lc);
4974 }
4975 s/\s*//; #remove leading white space
4976 PrintErrorMessage("did not find expected = sign",$lc)
4977 if $_ !~ s/^[=]//i;
4978 my $val = expr($lc);
4979 $VarTable{$constname} = $val;
4980 $ConstTable{$constname} = 1;
4981 print OUT "%% $Constname = $val\n" if $comments_on;
4982 }while (s/^,//);
4983 chk_comment($lc);
4984 s/\s*//;
4985 if (/^[^%]/) {
4986 PrintWarningMessage("Trailing text is ignored",$lc);
4987 }
4988
4989 @ The [[var]] command is used to store values into a comma separated
4990 list of named variables. Variable names have the same format as point names,
4991 i.e., they start with a letter and are followed by up to two digits. The
4992 whole operation is performed by a [[do-while]] construct that checks that
4993 there is a valid variable name, a [[=]] sign, and an expression. The
4994 [[do-while]] construct terminates if the next token isn't a comma. The variable
4995 [[$Varname]] is used to store the initial variable name, while we store
4996 in the variable [[$varname]] the lowercase version of the variable name.
4997 The last thing we do is to check whether there is a trailing comment.
4998 In case there, we simply ignore itl; otherwise we print a warning message.
4999
5000 <<process <tt>var</tt> command>>=
5001 do{
5002 s/\s*//;
5003 PrintErrorMessage("no identifier found after token var",$lc)
5004 if $_ !~ s/^([^\W\d_]\d{0,4})//i;
5005 my $Varname = $1;
5006 my $varname = lc($Varname);
5007 if (exists $ConstTable{$varname}) {
5008 PrintErrorMessage("Redefinition of constant $varname",$lc);
5009 }
5010 s/\s*//; #remove leading white space
5011 PrintErrorMessage("did not find expected = sign",$lc)
5012 if $_ !~ s/^[=]//i;
5013 my $val = expr($lc);
5014 $VarTable{$varname} = $val;
5015 print OUT "%% $Varname = $val\n" if $comments_on;
5016 }while (s/^,//);
5017 chk_comment($lc);
5018 s/\s*//;
5019 if (/^[^%]/) {
5020 PrintWarningMessage("Trailing text is ignored",$lc);
5021 }