Debian Codegraph @

texk/texlive/linked_scripts/mathspic/mathspic.pl

raw
1 #!/usr/bin/perl
2 #
3 #(c) Copyright 2005-2010 VERSION 1.13 April 26, 2010
4 # Apostolos Syropoulos & R.W.D. Nickalls
5 # [email protected] [email protected]
6 #
7 # This program can be redistributed and/or modified under the terms
8 # of the LaTeX Project Public License Distributed from CTAN
9 # archives in directory macros/latex/base/lppl.txt; either
10 # version 1 of the License, or any later version.
11 #
12 package DummyFH;
13 my $index = 0;
14 sub TIEHANDLE {
15 my $class = shift;
16 my $self= shift;
17 bless $self, $class;
18 }
19 sub READLINE {
20 my $self = shift;
21 #shift @$self;
22 if ($index > $#$self) {
23 $index = 0;
24 return undef;
25 }
26 else {
27 return $self->[$index++];
28 }
29 }
30
31 package main;
32 use Math::Trig;
33 our $version_number = "1.13 Apr 26, 2010";
34 our $commandLineArgs = join(" ", @ARGV);
35 our $command = "";
36 our $curr_in_file = "";
37 our %PointTable = ();
38 our %VarTable = ();
39 our %ConstTable = ();
40 our $no_errors = 0;
41 our $xunits = "1pt";
42 our $yunits = "1pt";
43 our $units = "pt|pc|in|bp|cm|mm|dd|cc|sp";
44 our $defaultsymbol = "\$\\bullet\$";
45 our $defaultLFradius = 0;
46 use constant PI => atan2(1,1)*4;
47 use constant R2D => 180 / PI;
48 use constant D2R => PI / 180;
49 our $arrowLength = 2;
50 our $arrowLengthUnits = "mm";
51 our $arrowAngleB = 30;
52 our $arrowAngleC = 40;
53 our %DimOfPoint = ();
54 our $GlobalDimOfPoints = 0;
55 our @Macros = ();
56 our $LineThickness = 0.4;
57
58 sub mpp {
59 my $in_line;
60 chomp($in_line = shift);
61 my $LC = shift;
62 my $out_line = $in_line;
63 my $macro_name = "";
64 my @macro_param = ();
65 my $macro_code = "";
66 if ($in_line =~ s/^%def\s*//) {
67 if ($in_line =~ s/^(\w+)\s*//){
68 $macro_name = $1;
69 }
70 else {
71 PrintErrorMessage("No macro name has been found",$LC);
72 return ""
73 }
74 if ($in_line =~ s/^\(\s*//) {
75 # do nothing
76 }
77 else {
78 PrintErrorMessage("No left parenthesis after macro name has been found",$LC);
79 return "";
80 }
81 if ($in_line =~ s/^\)//) {
82 # Macro has no parameters!
83 }
84 else {
85 MACROS: while (1) {
86 if ($in_line =~ s/^(\w+)\s*//) {
87 push (@macro_param, $1);
88 }
89 else {
90 PrintErrorMessage("No macro parameter name has been found",$LC);
91 return "";
92 }
93 if ($in_line =~ s/^,\s*//) {
94 next MACROS;
95 }
96 else {
97 last MACROS;
98 }
99 }
100 if ($in_line =~ s/^\)//) {
101 # do nothing!
102 }
103 else {
104 PrintErrorMessage("No closing parenthesis after macro parameters",$LC);
105 return "";
106 }
107 }
108 $in_line =~ s/([^%]+)(%.*)/$1/;
109 $macro_code = $in_line;
110 push ( @Macros , { 'macro_name' => $macro_name,
111 'macro_code' => $macro_code,
112 'macro_param' => \@macro_param });
113 return $out_line;
114 }
115 elsif ($in_line =~ s/^%undef\s*//) {
116 if ($in_line =~ s/^(\w+)//) {
117 my $undef_macro = $1;
118 for(my $i = $#Macros; $i >= 0; $i--) {
119 if ($Macros[$i]->{'macro_name'} eq $undef_macro) {
120 splice(@Macros,$i,1);
121 }
122 }
123 }
124 return $out_line;
125 }
126 elsif ($in_line =~ s/^\s*%//) {
127 return $out_line;
128 }
129 else {
130 my $comment = $2 if $in_line =~ s/([^%]+)(%.+)/$1/;
131 EXPANSIONLOOP: while () {
132 my $org_in_line = $in_line;
133 for(my $i = $#Macros; $i >= 0; $i--) {
134 my $macro_name = $Macros[$i]->{'macro_name'};
135 if ($in_line =~ /&$macro_name\b/) { ############################
136 my $num_of_macro_args = @{$Macros[$i]->{'macro_param'}};
137 if ( $num_of_macro_args > 0 ) {
138 # Macro with parameters
139 my $pattern = "&$macro_name\\(";
140 foreach my $p ( 1..$num_of_macro_args ) {
141 my $comma = ($p == $num_of_macro_args) ? "\\s*" : "\\s*,\\s*";
142 $pattern .= "\\s*[^\\s\\)]+$comma";
143 }
144 $pattern .= "\\)";
145 while($in_line =~ /&$macro_name\b/) {
146 if ($in_line =~ /$pattern/) {
147 my $before = $`;
148 my $after = $';
149 my $match = $&;
150 my $new_code = $Macros[$i]->{'macro_code'};
151 $match =~ s/^&$macro_name\(\s*//;
152 $match =~ s/\)$//;
153 foreach my $arg ( 0..($num_of_macro_args - 1) ) {
154 my $old = $Macros[$i]->{'macro_param'}->[$arg];
155 my $comma = ($arg == ($num_of_macro_args - 1)) ? "" : ",";
156 $match =~ s/^\s*([^\s,]+)\s*$comma//;
157 my $new = $1;
158 # 'g': Parameter may occur several times
159 # in $new_code.
160 # '\b': Substitute only whole words
161 # not x in xA
162 $new_code =~ s/\b$old\b/$new/g;
163 }
164 $in_line = "$before$new_code$after";
165 }
166 else {
167 PrintErrorMessage("Usage of macro &$macro_name does not " .
168 "match its definition", $LC);
169 return "";
170 }
171 }
172 }
173 else {
174 # Macro without parameters
175 my $replacement = $Macros[$i]->{'macro_code'};
176 # '\b': Substitute only whole words
177 # not x in xA
178 $in_line =~ s/&$macro_name\b/$replacement/g;
179 }
180 }
181 }
182 last EXPANSIONLOOP if ( $org_in_line eq $in_line );
183 }
184 return "$in_line$comment";
185 }
186 }
187
188 sub PrintErrorMessage {
189 my $errormessage = shift;
190 my $error_line = shift;
191 my ($l,$A);
192 $l = 1+length($command)-length;
193 $A = substr($command,0,$l);
194 $l += 7 +length($error_line);
195
196 for my $fh (STDOUT, LOG) {
197 print $fh "$curr_in_file", "Line $error_line: $A\n";
198 print $fh " " x $l ,$_,"***Error: $errormessage\n";
199 }
200 if ($comments_on) { #print to output file file
201 print OUT "%% *** $curr_in_file", "Line $error_line: $A\n";
202 print OUT "%% *** "," " x $l ,$_,"%% ... Error: $errormessage\n";
203 }
204 $no_errors++;
205 }
206
207 sub PrintWarningMessage {
208 my $warningMessage = shift;
209 my $warning_line = shift;
210 my ($l,$A);
211 $l = 1+length($command)-length;
212 $A = substr($command,0,$l);
213 $l += 7 +length($warning_line);
214
215 for my $fh (STDOUT, LOG) {
216 print $fh "$curr_in_file", "Line $warning_line: $A\n";
217 print $fh " " x $l ,$_,"***Warning: $warningMessage\n";
218 }
219 if ($comments_on) { #print to output file file
220 print OUT "%% *** $curr_in_file", "Line $warning_line: $A\n";
221 print OUT "%% *** "," " x $l ,$_,"%% ... Warning: $warningMessage\n";
222 }
223 }
224
225 sub PrintFatalError {
226 my $FatalMessage = shift;
227 my $fatal_line = shift;
228 my ($l,$A);
229 $l = 1+length($command)-length;
230 $A = substr($command,0,$l);
231 $l += 7 +length($fatal_line);
232
233 die "$curr_in_file", "Line $fatal_line: $A\n" .
234 (" " x $l) . $_ . "***Fatal Error: $FatalMessage\n";
235 }
236
237 sub chk_lparen {
238 my $token = $_[0];
239 my $lc = $_[1];
240 s/\s*//;
241 if (/^[^\(]/) {
242 PrintErrorMessage("Missing ( after $token",$lc);
243 }
244 else {
245 s/^\(\s*//;
246 }
247 }
248
249 sub chk_rparen {
250 my $token = $_[0];
251 my $lc = $_[1];
252 s/\s*//;
253 if (s/^\)//) {
254 s/\s*//;
255 }
256 else {
257 PrintErrorMessage("Missing ) after $token",$lc);
258 }
259 }
260
261
262 sub chk_lcb {
263 my $token = $_[0];
264 my $lc = $_[1];
265 s/\s*//;
266 if ($_ !~ /^\{/) {
267 PrintErrorMessage("Missing { after $token",$lc);
268 }
269 else {
270 s/^{\s*//;
271 }
272 }
273
274 sub chk_rcb {
275 my $token = $_[0];
276 my $lc = $_[1];
277 if ($_ !~ /^\s*\}/) {
278 PrintErrorMessage("Missing } after $token",$lc);
279 }
280 else {
281 s/^\s*}\s*//;
282 }
283 }
284
285 sub chk_lsb {
286 my $token = $_[0];
287 my $lc = $_[1];
288
289 s/\s*//;
290 if ($_ !~ /^\[/) {
291 PrintErrorMessage("Missing [ after $token",$lc);
292 }
293 else {
294 s/^\[\s*//;
295 }
296 }
297
298 sub chk_rsb {
299 my $token = $_[0];
300 my $lc = $_[1];
301
302 s/\s*//;
303 if ($_ !~ /^\]/) {
304 PrintErrorMessage("Missing ] after $token",$lc);
305 }
306 else {
307 s/^\]\s*//;
308 }
309 }
310
311 sub chk_comma {
312 my $lc = $_[0];
313
314 s/\s*//;
315 if (/^[^,]/) {
316 PrintErrorMessage("Did not find expected comma",$lc);
317 }
318 else {
319 s/^,\s*//;
320 }
321 }
322
323 sub chk_comment {
324 my $lc = $_[0];
325
326 s/\s*//;
327 if (/^%/) {
328 # do nothing!
329 }
330 elsif (/^[^%]/) {
331 PrintWarningMessage("Trailing text is ignored",$lc);
332 }
333 }
334
335 sub print_headers
336 {
337 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
338 $year+=1900;
339 $mon+=1;
340 $now_string = "$year/" . ($mon>9 ? "$mon/" : "0$mon/") .
341 ($mday>9 ? "$mday " : "0$mday ") .
342 ($hour>9 ? "$hour:" : "0$hour:") .
343 ($min>9 ? "$min:" : "0$min:") .
344 ($sec>9 ? "$sec" : "0$sec");
345 print OUT "%* -----------------------------------------------\n";
346 print OUT "%* mathspic (Perl version $version_number)\n";
347 print OUT "%* A filter program for use with PiCTeX\n";
348 print OUT "%* Copyright (c) 2005-2010 A Syropoulos & RWD Nickalls \n";
349 print OUT "%* Command line: $0 $commandLineArgs\n";
350 print OUT "%* Input filename : $source_file\n";
351 print OUT "%* Output filename: $out_file\n";
352 print OUT "%* Date & time: $now_string\n";
353 print OUT "%* -----------------------------------------------\n";
354 #
355 print LOG "----\n";
356 print LOG "$now_string\n";
357 print LOG "mathspic (Perl version $version_number)\n";
358 print LOG "Copyright (c) 2005-2010 A Syropoulos & RWD Nickalls \n";
359 print LOG "Input file = $source_file\n";
360 print LOG "Output file = $out_file\n";
361 print LOG "Log file = $log_file\n";
362 print LOG "----\n";
363 }
364
365 sub get_point {
366
367 my ($lc) = $_[0];
368 my ($PointName);
369
370 if (s/^([^\W\d_]\d{0,4})\s*//i) { #point name
371 $PointName = $1;
372 if (!exists($PointTable{lc($PointName)})) {
373 PrintErrorMessage("Undefined point $PointName",$lc);
374 return "_undef_";
375 }
376 else {
377 return lc($PointName);
378 }
379 }
380 else {
381 PrintErrorMessage("Point name expected",$lc);
382 return "_undef_";
383 }
384 }
385
386 sub perpendicular {
387 my ($xP, $yP, $xA, $yA, $xB, $yB) = @_;
388 my ($xF, $yF, $deltax, $deltay, $m1, $m2, $c1, $c2, $factor);
389
390 $deltax = $xA - $xB;
391 return ($xA, $yP) if abs($deltax) < 0.0000001;
392 $deltay = $yA - $yB;
393 return ($xP, $yA) if abs($deltay) < 0.0000001;
394 $m1 = $deltay / $deltax;
395 eval { $m2 = (-1) / $m1;};
396 PrintFatalError("Division by zero",$lc) if $@;
397 $c1 = $yA - $m1 * $xA;
398 $c2 = $yP - $m2 * $xP;
399 eval { $factor = 1 / ($m1 - $m2)};
400 PrintFatalError("Division by zero",$lc) if $@;
401 return (($c2 - $c1) * $factor, ($m1 * $c2 - $m2 * $c1) * $factor);
402 }
403
404 sub Length {
405 my ($xA, $yA, $xB, $yB)=@_;
406 return sqrt(($xB - $xA)**2 + ($yB - $yA)**2);
407 }
408
409 sub triangleArea {
410 my ($xA, $yA, $xB, $yB, $xC, $yC)=@_;
411 my ($lenAB, $lenBC, $lenCA, $s);
412
413 $lenAB = Length($xA,$yA,$xB,$yB);
414 $lenBC = Length($xB,$yB,$xC,$yC);
415 $lenCA = Length($xC,$yC,$xA,$yA);
416 $s = ($lenAB + $lenBC + $lenCA) / 2;
417 return sqrt($s * ($s - $lenAB)*($s - $lenBC)*($s - $lenCA));
418 }
419
420 sub pointOnLine {
421 my ($xA, $yA, $xB, $yB, $dist)=@_;
422 my ($deltax, $deltay, $xPol, $yPol);
423
424 $deltax = $xB - $xA;
425 $deltay = $yB - $yA;
426 $xPol = $xA + ($dist * $deltax / &Length($xA,$yA,$xB,$yB));
427 $yPol = $yA + ($dist * $deltay / &Length($xA,$yA,$xB,$yB));
428 return ($xPol, $yPol);
429 }
430
431
432
433 sub circumCircleCenter {
434 my ($xA, $yA, $xB, $yB, $xC, $yC, $lc)=@_;
435 my ($deltay12, $deltax12, $xs12, $ys12);
436 my ($deltay23, $deltax23, $xs23, $ys23);
437 my ($xcc, $ycc);
438 my ($m23, $mr23, $c23, $m12, $mr12, $c12);
439 my ($sideA, $sideB, $sideC, $a, $radius);
440
441 if (abs(triangleArea($xA, $yA, $xB, $yB, $xC, $yC)) < 0.0000001)
442 {
443 PrintErrorMessage("Area of triangle is zero!",$lc);
444 return (0,0,0);
445 }
446 $deltay12 = $yB - $yA;
447 $deltax12 = $xB - $xA;
448 $xs12 = $xA + $deltax12 / 2;
449 $ys12 = $yA + $deltay12 / 2;
450 #
451 $deltay23 = $yC - $yB;
452 $deltax23 = $xC - $xB;
453 $xs23 = $xB + $deltax23 / 2;
454 $ys23 = $yB + $deltay23 / 2;
455 #
456 CCXYLINE:{
457 if (abs($deltay12) < 0.0000001)
458 {
459 $xcc = $xs12;
460 if (abs($deltax23) < 0.0000001)
461 {
462 $ycc = $ys23;
463 last CCXYLINE;
464 }
465 else
466 {
467 $m23 = $deltay23 / $deltax23;
468 $mr23 = -1 / $m23;
469 $c23 = $ys23 - $mr23 * $xs23;
470 $ycc = $mr23 * $xs12 + $c23;
471 last CCXYLINE;
472 }
473 }
474 if (abs($deltax12) < 0.0000001)
475 {
476 $ycc = $ys12;
477 if (abs($deltay23) < 0.0000001)
478 {
479 $xcc = $xs23;
480 last CCXYLINE;
481 }
482 else
483 {
484 $m23 = $deltay23 / $deltax23;
485 $mr23 = -1 / $m23;
486 $c23 = $ys23 - $mr23 * $xs23;
487 $xcc = ($ys12 - $c23) / $mr23;
488 last CCXYLINE;
489 }
490 }
491 if (abs($deltay23) < 0.0000001)
492 {
493 $xcc = $xs23;
494 if (abs($deltax12) < 0.0000001)
495 {
496 $ycc = $ys12;
497 last CCXYLINE;
498 }
499 else
500 {
501 $m12 = $deltay12 / $deltax12;
502 $mr12 = -1 / $m12;
503 $c12 = $ys12 - $mr12 * $xs12;
504 $ycc = $mr12 * $xcc + $c12;
505 last CCXYLINE;
506 }
507 }
508 if (abs($deltax23) < 0.0000001)
509 {
510 $ycc = $ys23;
511 if (abs($deltay12) < 0.0000001)
512 {
513 $xcc = $xs12;
514 last CCXYLINE;
515 }
516 else
517 {
518 $m12 = $deltay12 / $deltax12;
519 $mr12 = -1 / $m12;
520 $c12 = $ys12 - $mr12 * $xs12;
521 $xcc = ($ycc - $c12) / $mr12;
522 last CCXYLINE;
523 }
524 }
525 $m12 = $deltay12 / $deltax12;
526 $mr12 = -1 / $m12;
527 $c12 = $ys12 - $mr12 * $xs12;
528 #-----
529 $m23 = $deltay23 / $deltax23;
530 $mr23 = -1 / $m23;
531 $c23 = $ys23 - $mr23 * $xs23;
532 $xcc = ($c23 - $c12) / ($mr12 - $mr23);
533 $ycc = ($c23 * $mr12 - $c12 * $mr23) / ($mr12 - $mr23);
534 }
535 #
536 $sideA = &Length($xA,$yA,$xB,$yB);
537 $sideB = &Length($xB,$yB,$xC,$yC);
538 $sideC = &Length($xC,$yC,$xA,$yA);
539 $a = triangleArea($xA, $yA, $xB, $yB, $xC, $yC);
540 $radius = ($sideA * $sideB * $sideC) / (4 * $a);
541 #
542 return ($xcc, $ycc, $radius);
543 }
544
545 sub ComputeDist {
546 my ($lc) = $_[0];
547 my ($v1, $v2);
548
549 if (s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//) #is it a number?
550 {
551 return ($1, 1);
552 }
553 elsif (/^[^\W\d_]\d{0,4}[^\W\d_]\d{0,4}/) #it is a pair of IDs?
554 {
555 s/^([^\W\d_]\d{0,4})//i;
556 $v1 = $1;
557 if (!exists($PointTable{lc($v1)})) {
558 if (exists($VarTable{lc($v1)})) {
559 return ($VarTable{lc($v1)}, 1);
560 }
561 PrintErrorMessage("Point $v1 has not been defined", $lc);
562 s/^\s*[^\W\d_]\d{0,4}//i;
563 return (0,0);
564 }
565 $v1 = lc($v1);
566 s/^\s*([^\W\d_]\d{0,4})//i;
567 $v2 = $1;
568 if (!exists($PointTable{lc($v2)}))
569 {
570 PrintErrorMessage("Point $v2 has not been defined", $lc);
571 return (0,0);
572 }
573 $v2 = lc($v2);
574 my ($x1,$y1,$pSV1,$pS1) = unpack("d3A*",$PointTable{$v1});
575 my ($x2,$y2,$pSV2,$pS2) = unpack("d3A*",$PointTable{$v2});
576 return (Length($x1,$y1,$x2,$y2), 1);
577 }
578 elsif (s/^([^\W\d_]\d{0,4})//i) # it is a single id
579 {
580 $v1 = $1;
581 if (!exists($VarTable{lc($v1)})) #it isn't a variable
582 {
583 PrintErrorMessage("Variable $v1 has not been defined", $lc);
584 return (0,0);
585 }
586 return ($VarTable{lc($v1)}, 1);
587 }
588 else
589 {
590 PrintErrorMessage("Unexpected token", $lc);
591 return (0,0);
592 }
593 }
594
595 sub intersection4points {
596 my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = @_;
597 my ($deltay12, $deltax12, $deltay34, $deltax34);
598 my ($xcc, $ycc, $m34, $c34, $m12, $c12);
599
600 $deltay12 = $y2 - $y1;
601 $deltax12 = $x2 - $x1;
602 #
603 $deltay34 = $y4 - $y3;
604 $deltax34 = $x4 - $x3;
605 I4PXYLINE:{
606 if (abs($deltay12) < 0.0000001)
607 {
608 $ycc = $y1;
609 if (abs($deltax34) < 0.0000001)
610 {
611 $xcc = $x3;
612 last I4PXYLINE;
613 }
614 else
615 {
616 $m34 = $deltay34 / $deltax34;
617 $c34 = $y3 - $m34 * $x3;
618 $xcc = ($ycc - $c34) / $m34;
619 last I4PXYLINE;
620 }
621 }
622 if (abs($deltax12) < 0.0000001)
623 {
624 $xcc = $x1;
625 if (abs($deltay34) < 0.0000001)
626 {
627 $ycc = $y3;
628 last I4PXYLINE;
629 }
630 else
631 {
632 $m34 = $deltay34 / $deltax34;
633 $c34 = $y3 - $m34 * $x3;
634 $ycc = $m34 * $xcc + $c34;
635 last I4PXYLINE;
636 }
637 }
638 if (abs($deltay34) < 0.0000001)
639 {
640 $ycc = $y3;
641 if (abs($deltax12) < 0.0000001)
642 {
643 $xcc = $x1;
644 last I4PXYLINE;
645 }
646 else
647 {
648 $m12 = $deltay12 / $deltax12;
649 $c12 = $y1 - $m12 * $x1;
650 $xcc = ($ycc - $c12) / $m12;
651 last I4PXYLINE;
652 }
653 }
654 if (abs($deltax34) < 0.0000001)
655 {
656 $xcc = $x3;
657 if (abs($deltay12) < 0.0000001)
658 {
659 $ycc = $y1;
660 last I4PXYLINE;
661 }
662 else
663 {
664 $m12 = $deltay12 / $deltax12;
665 $c12 = $y1 - $m12 * $x1;
666 $ycc = $m12 * $xcc + $c12;
667 last I4PXYLINE;
668 }
669 }
670 $m12 = $deltay12 / $deltax12;
671 $c12 = $y1 - $m12 * $x1;
672 $m34 = $deltay34 / $deltax34;
673 $c34 = $y3 - $m34 * $x3;
674 $xcc = ($c34 - $c12) / ($m12 - $m34);
675 $ycc = ($c34 * $m12 - $c12 * $m34) / ($m12 - $m34);
676 }
677 return ($xcc, $ycc);
678 }
679
680 sub IncircleCenter {
681 my ($Ax, $Ay, $Bx, $By, $Cx, $Cy) = @_;
682 my ($sideA, $sideB, $sideC);
683 my ($ba1, $xA1, $yA1, $cb1, $ac1, $xB1, $yB1, $xC1, $yC1, $a, $s, $r);
684
685 #determine the lengths of the sides
686 $sideA = Length($Bx, $By, $Cx, $Cy);
687 $sideB = Length($Cx, $Cy, $Ax, $Ay);
688 $sideC = Length($Ax, $Ay, $Bx, $By);
689 #
690 $ba1 = ($sideC * $sideA) / ($sideB + $sideC);
691 ($xA1, $yA1) = pointOnLine($Bx, $By, $Cx, $Cy, $ba1);
692 $cb1 = ($sideA * $sideB) / ($sideC + $sideA);
693 ($xB1, $yB1) = pointOnLine($Cx, $Cy, $Ax, $Ay, $cb1);
694 $ac1 = ($sideB * $sideC) / ($sideA + $sideB);
695 ($xC1, $yC1) = pointOnLine($Ax, $Ay, $Bx, $By, $ac1);
696 ($xcenter, $ycenter) = &intersection4points($Ax, $Ay, $xA1, $yA1,
697 $Bx, $By, $xB1, $yB1);
698 # get radius
699 $a = &triangleArea($Ax, $Ay, $Bx, $By, $Cx, $Cy);
700 $s = ($sideA + $sideB +$sideC) / 2;
701 $r = $a / $s;
702 return ($xcenter, $ycenter, $r);
703 }
704
705 sub Angle {
706 my ($Ax, $Ay, $Bx, $By, $Cx, $Cy) = @_;
707 my ($RAx, $RAy, $RBx, $RBy, $RCx, $RCy, $deltax, $deltay);
708 my ($lineBA, $lineBC, $lineAC, $k, $kk, $angle);
709 my ($T, $cosT, $sinT) = (0.3, cos(0.3), sin(0.3));
710
711 $RAx = $Ax * $cosT + $Ay * $sinT;
712 $RAy = -$Ax * $sinT + $Ay * $cosT;
713 $RBx = $Bx * $cosT + $By * $sinT;
714 $RBy = -$Bx * $sinT + $By * $cosT;
715 $RCx = $Cx * $cosT + $Cy * $sinT;
716 $RCy = -$Cx * $sinT + $Cy * $cosT;
717 $deltax = $RBx - $RAx;
718 $deltay = $RBy - $RAy;
719 $lineBA = sqrt($deltax*$deltax + $deltay*$deltay);
720 if ($lineBA < 0.0000001)
721 {
722 return -500;
723 }
724 $deltax = $RBx - $RCx;
725 $deltay = $RBy - $RCy;
726 $lineBC = sqrt($deltax*$deltax + $deltay*$deltay);
727 if ($lineBC < 0.0000001)
728 {
729 return -500;
730 }
731 $deltax = $RAx - $RCx;
732 $deltay = $RAy - $RCy;
733 $lineAC = sqrt($deltax*$deltax + $deltay*$deltay);
734 if ($lineAC < 0.0000001)
735 {
736 return -500;
737 }
738 $k = ($lineBA*$lineBA + $lineBC*$lineBC - $lineAC*$lineAC ) /
739 (2 * $lineBA * $lineBC);
740 $k = -1 if $k < -0.99999;
741 $k = 1 if $k > 0.99999;
742 $kk = $k * $k;
743 if (($kk * $kk) == 1)
744 {
745 $angle = PI if $k == -1;
746 $angle = 0 if $k == 1;
747 }
748 else
749 {
750 $angle = (PI / 2) - atan2($k / sqrt(1 - $kk),1);
751 }
752 return $angle * 180 / PI;
753 }
754
755 sub excircle {
756 my ($A, $B, $C, $D, $E) = @_;
757 my ($Ax,$Ay,$Bx,$By,$Dx,$Dy,$Ex,$Ey,$ASVA,$ASA);
758 ($Ax,$Ay,$ASVA,$ASA)=unpack("d3A*",$PointTable{$A});
759 ($Bx,$By,$ASVA,$ASA)=unpack("d3A*",$PointTable{$B});
760 ($Cx,$Cy,$ASVA,$ASA)=unpack("d3A*",$PointTable{$C});
761 ($Dx,$Dy,$ASVA,$ASA)=unpack("d3A*",$PointTable{$D});
762 ($Ex,$Ey,$ASVA,$ASA)=unpack("d3A*",$PointTable{$E});
763 my ($sideA, $sideB, $sideC, $s, $R, $theAdeg, $d);
764 my ($Xmypoint, $Ymypoint, $deltax, $deltay, $mylength, $xc, $yc);
765
766 $sideA = &Length($Bx, $By, $Cx, $Cy);
767 $sideB = &Length($Cx, $Cy, $Ax, $Ay);
768 $sideC = &Length($Ax, $Ay, $Bx, $By);
769 $s = ($sideA + $sideB + $sideC) / 2;
770 $R = triangleArea($Ax, $Ay, $Bx, $By, $Cx, $Cy) /
771 ($s - &Length($Dx, $Dy, $Ex, $Ey));
772 if (($D eq $A && $E eq $B) || ($D eq $B && $E eq $A))
773 {
774 $theAdeg = &Angle($Bx, $By, $Cx, $Cy, $Ax, $Ay);
775 $Xmypoint = $Cx;
776 $Ymypoint = $Cy;
777 }
778 elsif (($D eq $B && $E eq $C) || ($D eq $C && $E eq $B))
779 {
780 $theAdeg = &Angle($Cx, $Cy, $Ax, $Ay, $Bx, $By);
781 $Xmypoint = $Ax;
782 $Ymypoint = $Ay;
783 }
784 elsif (($D eq $C && $E eq $A) || ($D eq $A && $E eq $C))
785 {
786 $theAdeg = &Angle($Ax, $Ay, $Bx, $By, $Cx, $Cy);
787 $Xmypoint = $Bx;
788 $Ymypoint = $By;
789 }
790 else
791 {
792 return (0,0,0);
793 }
794 $d = $R / sin($theAdeg * PI / 180 / 2);
795 my ($xIn, $yIn, $rin) = &IncircleCenter($Ax, $Ay, $Bx, $By, $Cx, $Cy);
796 $deltax = $xIn - $Xmypoint;
797 $deltay = $yIn - $Ymypoint;
798 $mylength = sqrt($deltax*$deltax + $deltay*$deltay);
799 $xc = $Xmypoint + $d * $deltax / $mylength;
800 $yc = $Ymypoint + $d * $deltay / $mylength;
801 return ($xc, $yc, $R);
802 }
803
804 sub DrawLineOrArrow {
805 my $draw_Line = shift;
806 my $lc = shift;
807 my $lineLength = -1;
808 my $stacklen = 0;
809 my @PP = ();
810 # if ($draw_Line != 2) {
811 # s/\s*//;
812 # if (s/^\[\s*//) { # optional length specifier
813 # $lineLength = expr($lc);
814 # if ($lineLength <= 0) {
815 # PrintErrorMessage("length must greater than zero",$lc);
816 # $lineLength = -1;
817 # }
818 # chk_rsb("optional part",$lc);
819 # }
820 # }
821 chk_lparen("$cmd",$lc);
822 DRAWLINES:while(1) {
823 @PP = () ;
824 while(1) {
825 if (s/^([^\W\d_]\d{0,4})\s*//i) { #point name
826 $P = $1;
827 if (!exists($PointTable{lc($P)})) {
828 PrintErrorMessage("Undefined point $P",$lc);
829 }
830 else {
831 push (@PP,$P);
832 }
833 }
834 else {
835 $stacklen = @PP;
836 if ($draw_Line != 2) {
837 if ($stacklen <= 1) {
838 PrintErrorMessage("Wrong number of points",$lc);
839 }
840 else {
841 push(@PP,$lc);
842 if ($draw_Line == 0) {
843 drawarrows(@PP);
844 }
845 elsif ($draw_Line == 1) {
846 drawlines(@PP);
847 }
848 }
849 }
850 if (s/^,\s*// and $draw_Line != 2) {
851 next DRAWLINES;
852 }
853 else {
854 last DRAWLINES;
855 }
856 }
857 }
858 }
859 if ($draw_Line == 2) {
860 $stacklen = @PP;
861 if ($stacklen < 2) {
862 PrintErrorMessage("Wrong number of points",$lc);
863 }
864 elsif ($stacklen % 2 == 0) {
865 PrintErrorMessage("Number of points must be odd",$lc);
866 }
867 else {
868 drawCurve(@PP);
869 }
870 }
871 chk_rparen("arguments of $cmd",$lc);
872 chk_comment($lc);
873 }
874
875 sub drawarrows {
876 my ($NoArgs);
877 $NoArgs = @_;
878 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
879 my ($NumberOfPoints, $p, $q, $r12, $d12);
880 my ($px,$py,$pSV,$pS, $qx,$qy,$qSV,$qS);
881
882 $NumberOfPoints = $NoArgs - 1;
883 LOOP: for(my $i=0; $i < $NumberOfPoints - 1; $i++)
884 {
885 $p = $_[$i];
886 $q = $_[$i+1];
887 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
888 ($qx,$qy,$qSV,$qS) = unpack("d3A*",$PointTable{lc($q)});
889 $pSV = $defaultLFradius if $pSV == 0;
890 $qSV = $defaultLFradius if $qSV == 0;
891 $r12 = $pSV + $qSV;
892 $d12 = Length($px,$py,$qx,$qy);
893 if ($d12 <= $r12)
894 {
895 if($d12 == 0)
896 {
897 PrintErrorMessage("points $p and $q are the same", $lc);
898 next LOOP;
899 }
900 PrintWarningMessage("arrow $p$q not drawn: points too close or ".
901 "radii too big", $lc);
902 next LOOP;
903 }
904 ($px, $py) = pointOnLine($px, $py, $qx, $qy, $pSV) if $pSV > 0;
905 ($qx, $qy) = pointOnLine($qx, $qy, $px, $py, $qSV) if $qSV > 0;
906 my ($beta, $gamma);
907 $beta = tan($arrowAngleB * D2R / 2);
908 $gamma = 2 * tan($arrowAngleC * D2R / 2);
909 printf OUT "\\arrow <%.5f%s> [%.5f,%.5f] from %.5f %.5f to %.5f %.5f\n",
910 $arrowLength, $arrowLengthUnits, $beta, $gamma, $px, $py, $qx, $qy;
911 }
912 }
913
914 sub drawlines {
915 my ($NoArgs);
916 $NoArgs = @_;
917 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
918 my ($NumberOfPoints, $p, $q, $r12, $d12);
919 my ($px,$py,$pSV,$pS, $qx,$qy,$qSV,$qS);
920
921 $NumberOfPoints = $NoArgs - 1;
922 LOOP: for(my $i=0; $i < $NumberOfPoints - 1; $i++)
923 {
924 $p = $_[$i];
925 $q = $_[$i+1];
926 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
927 ($qx,$qy,$qSV,$qS) = unpack("d3A*",$PointTable{lc($q)});
928 $pSV = $defaultLFradius if $pSV == 0;
929 $qSV = $defaultLFradius if $qSV == 0;
930 $r12 = $pSV + $qSV;
931 $d12 = Length($px,$py,$qx,$qy);
932 if ($d12 <= $r12)
933 {
934 if($d12 == 0)
935 {
936 PrintErrorMessage("points $p and $q are the same", $lc);
937 next LOOP;
938 }
939 PrintWarningMessage("line $p$q not drawn: points too close or ".
940 "radii too big", $lc);
941 next LOOP;
942 }
943 ($px, $py) = pointOnLine($px, $py, $qx, $qy, $pSV) if $pSV > 0;
944 ($qx, $qy) = pointOnLine($qx, $qy, $px, $py, $qSV) if $qSV > 0;
945 if ($px == $qx || $py == $qy)
946 {
947 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f %%%% %s%s\n",
948 $px,$py,$qx,$qy,$p,$q;
949 }
950 else
951 {
952 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / %%%% %s%s\n",
953 $px, $py,$qx,$qy,$p,$q;
954 }
955 }
956 }
957
958 sub drawCurve {
959 my ($NoArgs);
960 $NoArgs = @_;
961 my ($lc) = $_[$NoArgs-1]; #line number is the last argument
962 my ($NumberOfPoints, $p);
963
964 $NumberOfPoints = $NoArgs - 1;
965 print OUT "\\setquadratic\n\\plot\n";
966 for(my $i=0; $i <= $NumberOfPoints; $i++)
967 {
968 $p = $_[$i];
969 my ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
970 printf OUT "\t%0.5f %0.5f", $px, $py;
971 print OUT (($i == $NumberOfPoints) ? " / %$p\n" : " %$p\n");
972 }
973 print OUT "\\setlinear\n";
974 }
975
976 sub drawpoints {
977 my ($NumberOfPoints,$p);
978 $NumberOfPoints = @_;
979 my ($px,$py,$pSV,$pS);
980
981 for($i=0; $i < $NumberOfPoints; $i++)
982 {
983 $p = $_[$i];
984 ($px,$py,$pSV,$pS) = unpack("d3A*",$PointTable{lc($p)});
985 if ($pS eq "" and $defaultsymbol =~ /circle|square/) {
986 $pS = $defaultsymbol;
987 }
988 POINTSWITCH: {
989 if ($pS eq "") # no plot symbol specified
990 {
991 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n",
992 $defaultsymbol, $px, $py, $p;
993 last POINTSWITCH;
994 }
995 if ($pS eq "circle") # plot symbol is a circle
996 {
997 my $radius = (defined($DimOfPoint{lc($p)})) ? $DimOfPoint{lc($p)} :
998 $GlobalDimOfPoints;
999 if ($radius > 0) # draw a circle using the current units
1000 {
1001 if ($radius == 1.5) # use \bigcirc
1002 {
1003 printf OUT "\\put {\$\\bigcirc\$} at %.5f %.5f %%%% %s\n",
1004 $px, $py, $p;
1005 }
1006 else
1007 {
1008 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f %%%% %s\n",
1009 $px+$radius, $py, $px, $py, $p;
1010 }
1011 }
1012 else #use \circ symbol
1013 {
1014 printf OUT "\\put {\$\\circ\$} at %.5f %.5f %%%% %s\n",
1015 $px,$py,$p;
1016 }
1017 last POINTSWITCH;
1018 }
1019 if ($pS eq "square")
1020 {
1021 my $side = (defined($DimOfPoint{lc($p)})) ? $DimOfPoint{lc($p)} :
1022 $GlobalDimOfPoints;
1023 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n",
1024 drawsquare($side), $px, $py, $p;
1025 last POINTSWITCH;
1026 }
1027 printf OUT "\\put {%s} at %.5f %.5f %%%% %s\n", $pS,$px,$py,$p;
1028 }
1029 }
1030 }
1031
1032 sub drawAngleArc {
1033 my ($P1, $P2, $P3, $radius, $inout, $direction) = @_;
1034 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
1035 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
1036 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
1037
1038 my $internalAngle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
1039 my $externalAngle = 360 - $internalAngle;
1040 my ($x, $y) = pointOnLine($x2, $y2, $x1, $y1, $radius);
1041 my $code = "";
1042 if ($inout eq "internal" and $direction eq "clockwise" ) {
1043 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1044 -1 * $internalAngle, $x, $y, $x2, $y2;
1045 }
1046 elsif ($inout eq "internal" and $direction eq "anticlockwise" ) {
1047 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1048 $internalAngle, $x, $y, $x2, $y2;
1049 }
1050 elsif ($inout eq "external" and $direction eq "clockwise" ) {
1051 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1052 -1 * $externalAngle, $x, $y, $x2, $y2;
1053 }
1054 elsif ($inout eq "external" and $direction eq "anticlockwise" ) {
1055 $code = sprintf "\\circulararc %.5f degrees from %.5f %.5f center at %.5f %.5f\n",
1056 $externalAngle, $x, $y, $x2, $y2;
1057 }
1058 return $code;
1059 }
1060
1061 sub drawAngleArrow {
1062 my ($P1, $P2, $P3, $radius, $inout, $direction) = @_;
1063 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
1064 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
1065 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
1066
1067 my $code = drawAngleArc($P1, $P2, $P3, $radius, $inout, $direction);
1068
1069 my ($xqp, $yqp) = pointOnLine($x2, $y2, $x1, $y1, $radius);
1070 my ($deltax, $deltay) = ($x1 - $x2, $y1 - $y2);
1071 my $AL;
1072
1073 if ($xunits =~ /mm/) {
1074 $AL = 1;
1075 }
1076 elsif ($xunits =~ /cm/) {
1077 $AL = 0.1;
1078 }
1079 elsif ($xunits =~ /pt/) {
1080 $AL = 2.845;
1081 }
1082 elsif ($xunits =~ /bp/) {
1083 $AL = 2.835;
1084 }
1085 elsif ($xunits =~ /pc/) {
1086 $AL = 0.2371;
1087 }
1088 elsif ($xunits =~ /in/) {
1089 $AL = 0.03937;
1090 }
1091 elsif ($xunits =~ /dd/) {
1092 $AL = 2.659;
1093 }
1094 elsif ($xunits =~ /cc/) {
1095 $AL = 0.2216;
1096 }
1097 elsif ($xunits =~ /sp/) {
1098 $AL = 186467.98;
1099 }
1100 my $halfAL = $AL / 2;
1101 my $d = sqrt($radius * $radius - $halfAL * $halfAL);
1102 my $alpha = atan2($d / $halfAL, 1) * R2D;
1103 my $beta = 2 * (90 - $alpha);
1104 my $thetaqr;
1105 if (abs($deltay) < 0.00001) {
1106 if ($deltax > 0 ) {$thetaqr = 0 }
1107 elsif ($deltax < 0) {$thetaqr = -180}
1108 }
1109 else {
1110 if (abs($deltax) < 0.00001) {
1111 $thetaqr = 90;
1112 }
1113 else {
1114 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1115 }
1116 }
1117 my ($xqr, $yqr) = pointOnLine($x2, $y2, $x3, $y3, $radius);
1118 $deltax = $x3 - $x2;
1119 $deltay = $y3 - $y2;
1120 $alpha = atan2(sqrt($radius * $radius - $halfAL * $halfAL) / $halfAL, 1) /
1121 D2R;
1122 $beta = 2 * (90 - $alpha);
1123 LINE2 : {
1124 if (abs($deltax) < 0.00001) {
1125 if ($deltay > 0) { $thetaqr = 90 }
1126 elsif ($deltay < 0) { $thetaqr = - 90 }
1127 last LINE2;
1128 }
1129 else {
1130 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1131 }
1132 if (abs($deltay) < 0.00001) {
1133 if ($deltax > 0) { $thetaqr = 0 }
1134 elsif ($deltax < 0) { $thetaqr = -180 }
1135 last LINE2;
1136 }
1137 else {
1138 $thetaqr = atan2($deltay / $deltax, 1) * R2D;
1139 }
1140 if ($deltax < 0 and $deltay > 0) { $thetaqr += 180 }
1141 elsif ($deltax < 0 and $deltay < 0) { $thetaqr += 180 }
1142 elsif ($deltax > 0 and $deltay < 0) { $thetaqr += 360 }
1143 }
1144 my $xqrleft = $x2 + $radius * cos(($thetaqr + $beta) * D2R);
1145 my $yqrleft = $y2 + $radius * sin(($thetaqr + $beta) * D2R);
1146 my $xqrright = $x2 + $radius * cos(($thetaqr - $beta) * D2R);
1147 my $yqrright = $y2 + $radius * sin(($thetaqr - $beta) * D2R);
1148 if ($inout eq "internal" and $direction eq "clockwise") {
1149 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1150 $xqrleft, $yqrleft, $xqr, $yqr;
1151 }
1152 elsif ($inout eq "internal" and $direction eq "anticlockwise") {
1153 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1154 $xqrright, $yqrright, $xqr, $yqr;
1155 }
1156 elsif ($inout eq "external" and $direction eq "clockwise") {
1157 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1158 $xqrleft, $yqrleft, $xqr, $yqr;
1159 }
1160 elsif ($inout eq "external" and $direction eq "anticlockwise") {
1161 $code .= sprintf "\\arrow <1.5mm> [0.5, 1] from %.5f %.5f to %.5f %.5f\n",
1162 $xqrright, $yqrright, $xqr, $yqr;
1163 }
1164 return $code;
1165 }
1166
1167 sub expr {
1168 my $lc = $_[0];
1169 my($left,$op,$right);
1170
1171 $left = term($lc);
1172 while ($op = addop()) {
1173 $right = term($lc);
1174 if ($op eq '+')
1175 { $left += $right }
1176 else
1177 { $left -= $right }
1178 }
1179 return $left;
1180 }
1181
1182 sub addop {
1183 s/^([+-])// && $1;
1184 }
1185
1186 sub term {
1187 my $lc = $_[0];
1188 my ($left, $op, $right);
1189 $left = factor($lc);
1190 while ($op = mulop()) {
1191 $right = factor($lc);
1192 if ($op eq '*')
1193 { $left *= $right }
1194 elsif ($op =~ /rem/i) {
1195 eval {$left %= $right};
1196 PrintFatalError("Division by zero", $lc) if $@;
1197 }
1198 else {
1199 eval {$left /= $right};
1200 PrintFatalError("Division by zero", $lc) if $@;
1201 }
1202 }
1203 return $left;
1204 }
1205
1206 sub mulop {
1207 (s#^([*/])## || s/^(rem)//i) && lc($1);
1208 }
1209
1210 sub factor {
1211 my $lc = $_[0];
1212 my ($left);
1213
1214 $left = primitive($lc);
1215 if (s/^\*\*//) {
1216 $left **= factor($lc);
1217 }
1218 return $left;
1219 }
1220
1221 sub primitive {
1222 my $lc = $_[0];
1223 my $val;
1224 s/\s*//;
1225 if (s/^\(//) { #is it an expr in parentheses
1226 $val = expr($lc);
1227 s/^\)// || PrintErrorMessage("Missing right parenthesis", $lc);
1228 }
1229 elsif (s/^-//) { # is it a negated primitive
1230 $val = - primitive();
1231 }
1232 elsif (s/^\+//) { # is it a positive primitive
1233 $val = primitive();
1234 }
1235 elsif (s/^angledeg//i) {
1236 chk_lparen("angledeg",$lc);
1237 my $point_1 = get_point($lc);
1238 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1239 my $point_2 = get_point($lc);
1240 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1241 my $point_3 = get_point($lc);
1242 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
1243 my $d12 = Length($x1, $y1, $x2, $y2);
1244 my $d23 = Length($x2, $y2, $x3, $y3);
1245 my $d31 = Length($x3, $y3, $x1, $y1);
1246 if ( $d12 == 0 ) {
1247 PrintErrorMessage("points `$point_1' and `$point_2' are the same", $lc);
1248 $val = 0;
1249 }
1250 elsif ( $d23 == 0 ) {
1251 PrintErrorMessage("points `$point_2' and `$point_3' are the same", $lc);
1252 $val = 0;
1253 }
1254 elsif ( $d31 == 0 ) {
1255 PrintErrorMessage("points `$point_1' and `$point_3' are the same", $lc);
1256 $val = 0;
1257 }
1258 else {
1259 $val = Angle($x1, $y1, $x2, $y2, $x3, $y3);
1260 $val = 0 if $val == -500;
1261 }
1262 chk_rparen("Missing right parenthesis", $lc);
1263 }
1264 elsif (s/^angle//i) {
1265 chk_lparen("angle",$lc);
1266 my $point_1 = get_point($lc);
1267 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1268 my $point_2 = get_point($lc);
1269 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1270 my $point_3 = get_point($lc);
1271 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
1272 my $d12 = Length($x1, $y1, $x2, $y2);
1273 my $d23 = Length($x2, $y2, $x3, $y3);
1274 my $d31 = Length($x3, $y3, $x1, $y1);
1275 if ( $d12 == 0 ) {
1276 PrintErrorMessage("points `$point_1' and `$point_2' are the same", $lc);
1277 $val = 0;
1278 }
1279 elsif ( $d23 == 0 ) {
1280 PrintErrorMessage("points `$point_2' and `$point_3' are the same", $lc);
1281 $val = 0;
1282 }
1283 elsif ( $d31 == 0 ) {
1284 PrintErrorMessage("points `$point_1' and `$point_3' are the same", $lc);
1285 $val = 0;
1286 }
1287 else {
1288 $val = Angle($x1, $y1, $x2, $y2, $x3, $y3);
1289 if ($val == -500) {
1290 $val = 0;
1291 }
1292 else {
1293 $val = D2R * $val;
1294 }
1295 }
1296 chk_rparen("Missing right parenthesis", $lc);
1297 }
1298 elsif (s/^area//i) {
1299 chk_lparen("angledeg",$lc);
1300 my $point_1 = get_point($lc);
1301 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1302 my $point_2 = get_point($lc);
1303 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1304 my $point_3 = get_point($lc);
1305 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
1306 $val = triangleArea($x1, $y1, $x2, $y2, $x3, $y3);
1307 chk_rparen("Missing right parenthesis", $lc);
1308 }
1309 elsif (s/^asin//i) {
1310 chk_lparen("asin");
1311 $val = expr();
1312 PrintFatalError("Can't take asin of $val", $lc) if $val < -1 || $val > 1;
1313 $val = asin($val);
1314 chk_rparen("Missing right parenthesis", $lc);
1315 }
1316 elsif (s/^acos//i) {
1317 chk_lparen("acos");
1318 $val = expr();
1319 PrintFatalError("Can't take acos of $val", $lc) if $val < -1 || $val > 1 ;
1320 $val = acos($val);
1321 chk_rparen("Missing right parenthesis", $lc);
1322 }
1323 elsif (s/^atan//i) {
1324 chk_lparen("atan");
1325 $val = expr();
1326 $val = atan($val);
1327 chk_rparen("Missing right parenthesis", $lc);
1328 }
1329 elsif (s/^cos//i) {
1330 chk_lparen("cos");
1331 $val = expr();
1332 $val = cos($val);
1333 chk_rparen("Missing right parenthesis", $lc);
1334 }
1335 elsif (s/^directiondeg//i) {
1336 chk_lparen("directiondeg",$lc);
1337 my $point_1 = get_point($lc);
1338 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1339 my $point_2 = get_point($lc);
1340 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1341 my $x3 = $x1+1;
1342 if ( ($y2 - $y1) >= 0) {
1343 $val = Angle($x3, $y1, $x1, $y1, $x2, $y2);
1344 $val = 0 if $val == -500;
1345 }
1346 else {
1347 $val = 360 - Angle($x3, $y1, $x1, $y1, $x2, $y2);
1348 $val = 0 if $val == -500;
1349 }
1350 chk_rparen("Missing right parenthesis", $lc);
1351 }
1352 elsif (s/^direction//i) {
1353 chk_lparen("direction",$lc);
1354 my $point_1 = get_point($lc);
1355 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
1356 my $point_2 = get_point($lc);
1357 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
1358 my $x3 = $x1+1;
1359 if ( ($y2 - $y1) >= 0) {
1360 $val = Angle($x3, $y1, $x1, $y1, $x2, $y2);
1361 $val = 0 if $val == -500;
1362 $val = D2R * $val;
1363 }
1364 else {
1365 $val = 360 - Angle($x3, $y1, $x1, $y1, $x2, $y2);
1366 $val = 0 if $val == -500;
1367 $val = D2R * $val;
1368 }
1369 chk_rparen("Missing right parenthesis", $lc);
1370 }
1371 elsif (s/^exp//i) {
1372 chk_lparen("exp");
1373 $val = expr();
1374 $val = exp($val);
1375 chk_rparen("Missing right parenthesis", $lc);
1376 }
1377 elsif (s/^int//i) {
1378 chk_lparen("int");
1379 $val = expr();
1380 $val = int($val);
1381 chk_rparen("Missing right parenthesis", $lc);
1382 }
1383 elsif (s/^log//i) {
1384 chk_lparen("log");
1385 $val = expr();
1386 PrintFatalError("Can't take log of $val", $lc) if $val <= 0;
1387 $val = log($val);
1388 chk_rparen("Missing right parenthesis", $lc);
1389 }
1390 elsif (s/^sin//i) {
1391 chk_lparen("sin");
1392 $val = expr();
1393 $val = sin($val);
1394 chk_rparen("Missing right parenthesis", $lc);
1395 }
1396 elsif (s/^sgn//i) {
1397 chk_lparen("sgn");
1398 $val = expr();
1399 if ($val > 0) {
1400 $val = 1;
1401 }
1402 elsif ($val == 0) {
1403 $val = 0;
1404 }
1405 else {
1406 $val = -1;
1407 }
1408 chk_rparen("Missing right parenthesis", $lc);
1409 }
1410 elsif (s/^sqrt//i) {
1411 chk_lparen("sqrt");
1412 $val = expr();
1413 $val = sqrt($val);
1414 chk_rparen("Missing right parenthesis", $lc);
1415 }
1416 elsif (s/^tan//i) {
1417 chk_lparen("tan");
1418 $val = expr();
1419 $val = sin($val)/cos($val);
1420 chk_rparen("Missing right parenthesis", $lc);
1421 }
1422 elsif (s/^xcoord//i) {
1423 chk_lparen("xcoord");
1424 my $point_name = get_point;
1425 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_name});
1426 $val = $x1;
1427 chk_rparen("Missing right parenthesis", $lc);
1428 }
1429 elsif (s/^ycoord//i) {
1430 chk_lparen("ycoord");
1431 my $point_name = get_point;
1432 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_name});
1433 $val = $y1;
1434 chk_rparen("Missing right parenthesis", $lc);
1435 }
1436 elsif (s/^_pi_//i) {
1437 $val = PI;
1438 }
1439 elsif (s/^_e_//i) {
1440 $val = 2.71828182845905;
1441 }
1442 elsif (s/^_linethickness_//i) {
1443 $val = $LineThickness / $xunits;
1444 }
1445 else {
1446 my $err_code;
1447 ($val,$err_code) = ComputeDist($lc);
1448 }
1449 s/\s*//;
1450 return $val;
1451 }
1452
1453 sub memberOf {
1454 my $elem = shift(@_);
1455
1456 my $found = 0;
1457 foreach $item (@_){
1458 if ($item eq $elem){
1459 $found = 1;
1460 last;
1461 }
1462 }
1463 return $found;
1464 }
1465
1466 sub midpoint {
1467 my ($x1, $y1, $x2, $y2)=@_;
1468 return ($x1 + ($x2 - $x1)/2,
1469 $y1 + ($y2 - $y1)/2);
1470 }
1471
1472
1473 sub tand {
1474 my $d = $_[0];
1475 $d = $d * PI / 180;
1476 return sin($d)/cos($d);
1477 }
1478
1479 sub get_string {
1480 my $string = shift;
1481 my $lc = shift;
1482
1483 $string =~ s/^\s+//;
1484 if ($string !~ s/^\"//) {
1485 PrintErrorMessage("No starting \" found",$lc);
1486 return (1,$string,$string);
1487 }
1488 my @ch = split //,$string;
1489 my @cmd;
1490 while (@ch and $ch[0] ne "\"") {
1491 if ($ch[0] eq "\\" and (defined $ch[1]) and $ch[1] eq "\"") {
1492 shift @ch;
1493 push @cmd, $ch[0];
1494 shift @ch;
1495 }
1496 else {
1497 push @cmd, $ch[0];
1498 shift @ch;
1499 }
1500 }
1501 if (! defined $ch[0]) {
1502 PrintErrorMessage("No closing \" found",$lc);
1503 return (1,join("",@cmd), join("",@ch))
1504 }
1505 else {
1506 shift @ch;
1507 return (0, join("",@cmd), join("",@ch))
1508 }
1509 }
1510
1511 sub is_tainted {
1512 my $arg = shift;
1513 my $nada = substr($arg,0,0);
1514 local $@;
1515 eval { eval "# $nada"};
1516 return length($@) != 0;
1517 }
1518
1519 sub noOfDigits {
1520 my $num = $_[0];
1521
1522 if ($num =~ /^[\+-]?\d+(?!\.)/) {
1523 return 0;
1524 }
1525 elsif ($num =~ /^[\+-]\d+\.(\d+)?/) {
1526 return length($1);
1527 }
1528 }
1529
1530 sub drawsquare {
1531 my $s = $_[0];
1532 #$s *= sqrt(2);
1533 $s = sprintf "%.5f", $s;
1534 my $code = "\\setlength{\\unitlength}{$xunits}%\n";
1535 $code .= "\\begin{picture}($s,$s)\\put(0,0)" .
1536 "{\\framebox($s,$s){}}\\end{picture}";
1537 return $code;
1538 }
1539
1540 sub X2sp {
1541 my $LT = shift;
1542 my $units = shift;
1543
1544 if ($units eq "pc") {
1545 return $LT * 786432;
1546 }
1547 elsif ($units eq "pt") {
1548 return $LT * 65536;
1549 }
1550 elsif ($units eq "in") {
1551 return $LT * 4736286.72;
1552 }
1553 elsif ($units eq "bp") {
1554 return $LT * 65781.76;
1555 }
1556 elsif ($units eq "cm") {
1557 return $LT * 1864679.811023622;
1558 }
1559 elsif ($units eq "mm") {
1560 return $LT * 186467.981102362;
1561 }
1562 elsif ($units eq "dd") {
1563 return $LT * 70124.086430424;
1564 }
1565 elsif ($units eq "cc") {
1566 return $LT * 841489.037165082;
1567 }
1568 elsif ($units eq "sp") {
1569 return $LT;
1570 }
1571 }
1572
1573
1574 sub sp2X {
1575 my $LT = shift;
1576 my $units = shift;
1577
1578 if ($units eq "pc") {
1579 return $LT / 786432;
1580 }
1581 elsif ($units eq "pt") {
1582 return $LT / 65536;
1583 }
1584 elsif ($units eq "in") {
1585 return $LT / 4736286.72;
1586 }
1587 elsif ($units eq "bp") {
1588 return $LT / 65781.76;
1589 }
1590 elsif ($units eq "cm") {
1591 return $LT / 1864679.811023622;
1592 }
1593 elsif ($units eq "mm") {
1594 return $LT / 186467.981102362;
1595 }
1596 elsif ($units eq "dd") {
1597 return $LT / 70124.086430424;
1598 }
1599 elsif ($units eq "cc") {
1600 return $LT / 841489.037165082;
1601 }
1602 elsif ($units eq "sp") {
1603 return $LT;
1604 }
1605 }
1606
1607 sub setLineThickness {
1608 my $Xunits = shift;
1609 my $LT = shift;
1610 $Xunits =~ s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//;
1611 my $xlength = "$1";
1612 $Xunits =~ s/^\s*($units)//;
1613 my $x_in_units = $1;
1614 $LT =~ s/^((\+|-)?\d+(\.\d+)?([eE](\+|-)?\d+)?)//;
1615 my $LTlength = "$1";
1616 $LT =~ s/^\s*($units)//;
1617 my $LT_in_units = $1;
1618 $LTlength = X2sp($LTlength,$LT_in_units);
1619 $LTlength = sp2X($LTlength,$x_in_units);
1620 return $LTlength;
1621 }
1622
1623 sub process_input {
1624 my ($INFILE,$currInFile) = @_;
1625 my $lc = 0;
1626 my $no_output = 0;
1627 $curr_in_file = $currInFile;
1628 LINE: while(<$INFILE>) {
1629 $lc++;
1630 chomp($command = $_);
1631 s/^\s+//;
1632 if (/^beginSkip\s*/i) {
1633 $no_output = 1;
1634 print OUT "%%$_" if $comments_on;
1635 next LINE;
1636 }
1637 elsif (/^endSkip\s*/i) {
1638 if ($no_output == 0) {
1639 PrintErrorMessage("endSkip without beginSkip",$lc);
1640 }
1641 else {
1642 $no_output = 0;
1643 }
1644 print OUT "%%$_" if $comments_on and !$no_output;
1645 next LINE;
1646 }
1647 elsif ($no_output == 1) {
1648 next LINE;
1649 }
1650 else {
1651 if (/^[^\\]/) {
1652 my $out_line = mpp($command,$lc) unless /^\\/; #call macro pre-processor
1653 $_ = "$out_line\n";
1654 }
1655
1656 if (/^\s*%/)
1657 {
1658 print OUT "$_" if $comments_on;
1659 }
1660 elsif (s/^\s*(beginloop(?=\W))//i) {
1661 s/\s+//;
1662 my $times = expr($lc);
1663 print OUT "%% BEGINLOOP $times\n" if $comments_on;
1664 my @C = ();
1665 REPEATCOMMS: while (<$INFILE>) {
1666 if (/^\s*endloop/i) {
1667 last REPEATCOMMS;
1668 }
1669 else {
1670 push @C, $_;
1671 }
1672 }
1673 if (! /^\s*endloop/i) {
1674 PrintFatalError("unexpected end of file",$lc);
1675 }
1676 else {
1677 s/^\s*endloop//i;
1678 for(my $i=1; $i<=$times; $i++) {
1679 tie *DUMMY, 'DummyFH', \@C;
1680 process_input(DUMMY, $currInFile);
1681 untie *DUMMY;
1682 }
1683 print OUT "%% ENDLOOP\n" if $comments_on;
1684 }
1685 }
1686 elsif (s/^\s*(ArrowShape(?=\W))//i)
1687 {
1688 my $cmd = $1;
1689 print OUT "%% $cmd$_" if $comments_on;
1690
1691 chk_lparen("$cmd",$lc);
1692 if (s/^default//i) {
1693 $arrowLength = 2;
1694 $arrowLengthUnits = "mm";
1695 $arrowAngleB = 30;
1696 $arrowAngleC = 40;
1697 }
1698 else {
1699 my ($LocalArrowLength, $LocalArrowAngleB ,$LocalArrowAngleC) = (0,0,0);
1700 $LocalArrowLength = expr($lc);
1701 if (s/^\s*($units)//i) {
1702 $arrowLengthUnits = "$1";
1703 }
1704 else {
1705 $xunits =~ /(\d+(\.\d+)?)\s*($units)/;
1706 $LocalArrowLength *= $1;
1707 $arrowLengthUnits = "$3";
1708 }
1709 chk_comma($lc);
1710 $LocalArrowAngleB = expr($lc);
1711 chk_comma($lc);
1712 $LocalArrowAngleC = expr($lc);
1713 $arrowLength = ($LocalArrowLength == 0 ? 2 : $LocalArrowLength);
1714 $arrowLengthUnits = ($LocalArrowLength == 0 ? "mm" : $arrowLengthUnits);
1715 $arrowAngleB = ($LocalArrowAngleB == 0 ? 30 : $LocalArrowAngleB);
1716 $arrowAngleC = ($LocalArrowAngleC == 0 ? 40 : $LocalArrowAngleC);
1717 }
1718 chk_rparen("after $cmd arguments",$lc);
1719 chk_comment("after $cmd command",$lc);
1720 print OUT "%% arrowLength = $arrowLength$arrowLengthUnits, ",
1721 "arrowAngleB = $arrowAngleB ",
1722 "and arrowAngleC = $arrowAngleC\n" if $comments_on;
1723
1724 }
1725 elsif (s/^\s*(const(?=\W))//i)
1726 {
1727 print OUT "%% $1$_" if $comments_on;
1728 do{
1729 s/\s*//;
1730 PrintErrorMessage("no identifier found after token const",$lc)
1731 if $_ !~ s/^([^\W\d_]\d{0,4})//i;
1732 my $Constname = $1;
1733 my $constname = lc($Constname);
1734 if (exists $ConstTable{$constname}) {
1735 PrintErrorMessage("Redefinition of constant $constname",$lc);
1736 }
1737 s/\s*//; #remove leading white space
1738 PrintErrorMessage("did not find expected = sign",$lc)
1739 if $_ !~ s/^[=]//i;
1740 my $val = expr($lc);
1741 $VarTable{$constname} = $val;
1742 $ConstTable{$constname} = 1;
1743 print OUT "%% $Constname = $val\n" if $comments_on;
1744 }while (s/^,//);
1745 chk_comment($lc);
1746 s/\s*//;
1747 if (/^[^%]/) {
1748 PrintWarningMessage("Trailing text is ignored",$lc);
1749 }
1750
1751 }
1752 elsif (s/^\s*(dasharray(?=\W))//i)
1753 {
1754 my ($cmd) = $1;
1755 print OUT "%% $cmd$_" if $comments_on;
1756 chk_lparen($cmd,$lc);
1757 my @DashArray = ();
1758 my $dash = "";
1759 my $dashpattern = "";
1760 PATTERN: while (1) {
1761 $dash = sprintf("%.5f", expr($lc));
1762 if (s/^\s*($units)//i) {
1763 push (@DashArray, "$dash$1");
1764 }
1765 else {
1766 PrintErrorMessage("Did not found unit after expression", $lc);
1767 }
1768 s/\s*//;
1769 if (/^[^,]/) {
1770 last PATTERN;
1771 }
1772 else {
1773 s/^,\s*//;
1774 }
1775 }
1776 print OUT "\\setdashpattern <";
1777 while (@DashArray) {
1778 $dashpattern .= shift @DashArray;
1779 $dashpattern .= ",";
1780 }
1781 $dashpattern =~ s/,$//;
1782 print OUT $dashpattern, ">\n";
1783 chk_rparen("arguments of $cmd",$lc);
1784 chk_comment($lc);
1785
1786 }
1787 elsif (s/^\s*(drawAngleArc(?=\W))//i or s/^\s*(drawAngleArrow(?=\W))//i )
1788 {
1789 my $cmd = $1;
1790 print OUT "%% $cmd$_" if $comments_on;
1791
1792 chk_lcb($cmd,$lc);
1793 my ($P1, $P2, $P3);
1794 if (s/^angle(?=\W)//i) {
1795 chk_lparen("token angle of command $cmd",$lc);
1796 $P1 = get_point($lc);
1797 next LINE if $P1 eq "_undef_";
1798 $P2 = get_point($lc);
1799 next LINE if $P2 eq "_undef_";
1800 $P3 = get_point($lc);
1801 next LINE if $P3 eq "_undef_";
1802 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P1});
1803 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$P2});
1804 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$P3});
1805 my $Angle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
1806 if ($Angle <= 0) {
1807 if ($Angle == 0) {
1808 PrintErrorMessage("Angle is equal to zero",$lc);
1809 next LINE;
1810 }
1811 elsif ($Angle < -400) {
1812 PrintErrorMessage("Something is wrong with the points",$lc);
1813 next LINE;
1814 }
1815 }
1816 chk_rparen("angle part of command $cmd",$lc);
1817 }
1818 else {
1819 PrintErrorMessage("Did not find expected angle part",$lc);
1820 next LINE;
1821 }
1822
1823 s/^,\s*// or s/\s*//; #parse optional comma
1824
1825 my $radius;
1826 if (s/^radius(?=\W)//i) {
1827 chk_lparen("token radius of command $cmd",$lc);
1828 $radius = expr($lc);
1829 chk_rparen("radius part of command $cmd",$lc);
1830 }
1831 else {
1832 PrintErrorMessage("Did not found expected angle part",$lc);
1833 next LINE;
1834 }
1835
1836 s/^,\s*// or s/\s*//; #parse optional comma
1837 my $inout = "";
1838 if (s/^(internal(?=\W))//i or s/^(external(?=\W))//i) {
1839 $inout = $1;
1840 }
1841 else {
1842 PrintErrorMessage("Did not find expected 'internal' specifier", $lc);
1843 next LINE;
1844 }
1845 s/^,\s*// or s/\s*//; #parse optional comma
1846 my $direction = "";
1847 if (s/^(clockwise(?=\W))//i or s/^(anticlockwise(?=\W))//i) {
1848 $direction = $1;
1849 }
1850 else {
1851 PrintErrorMessage("Did not find expected 'direction' specifier", $lc);
1852 next LINE;
1853 }
1854 chk_rcb("arguments of $cmd",$lc);
1855 chk_comment($lc);
1856 my $code;
1857 if (lc($cmd) eq "drawanglearc") {
1858 $code = drawAngleArc($P1, $P2, $P3, $radius, $inout, $direction);
1859 }
1860 else {
1861 $code = drawAngleArrow($P1, $P2, $P3, $radius, $inout, $direction);
1862 }
1863 print OUT $code if $code ne "";
1864
1865 }
1866 elsif (s/^\s*(drawArrow(?=\W))//i)
1867 {
1868 my ($cmd) = $1;
1869 print OUT "%% $cmd$_" if $comments_on;
1870 DrawLineOrArrow(0,$lc);
1871 }
1872 elsif (s/^\s*(drawcircle(?=\W))//i)
1873 {
1874 my ($cmd) = $1;
1875 print OUT "%% $cmd$_" if $comments_on;
1876 chk_lparen("drawcircle",$lc);
1877 my $Point = get_point($lc);
1878 next LINE if $Point eq "_undef_";
1879 chk_comma($lc);
1880 my $R = expr($lc);
1881 if ($R <= 0) {
1882 PrintErrorMessage("Radius must be greater than zero",$lc);
1883 next LINE;
1884 }
1885 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{lc($Point)});
1886 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
1887 $x+$R, $y, $x, $y;
1888 chk_rparen("arguments of $cmd",$lc);
1889 chk_comment($lc);
1890
1891 }
1892 elsif (s/^\s*(drawcurve(?=\W))//i)
1893 {
1894 my ($cmd) = $1;
1895 print OUT "%% $cmd$_" if $comments_on;
1896 DrawLineOrArrow(2,$lc);
1897 }
1898 elsif (s/^\s*(drawcircumcircle(?=\W))//i)
1899 {
1900 my ($cmd) = $1;
1901 print OUT "%% $cmd$_" if $comments_on;
1902 chk_lparen("drawcircumcircle",$lc);
1903 my $point1 = get_point($lc);
1904 next LINE if $point1 eq "_undef_";
1905 my $point2 = get_point($lc);
1906 next LINE if $point2 eq "_undef_";
1907 my $point3 = get_point($lc);
1908 next LINE if $point3 eq "_undef_";
1909 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
1910 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
1911 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
1912 my ($xc, $yc,$r) = circumCircleCenter($x1,$y1,$x2,$y2,$x3,$y3,$lc);
1913 next LINE if $xc == 0 and $yc == 0 and $r == 0;
1914 print OUT "%% circumcircle center = ($xc,$yc), radius = $r\n" if $comments_on;
1915 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
1916 $xc+$r, $yc, $xc, $yc;
1917 chk_rparen("arguments of $cmd",$lc);
1918 chk_comment($lc);
1919
1920 }
1921 elsif (s/^\s*(drawexcircle(?=\W))//i)
1922 {
1923 my ($cmd) = $1;
1924 print OUT "%% $cmd$_" if $comments_on;
1925 chk_lparen("drawexcircle",$lc);
1926 my $point1 = get_point($lc);
1927 next LINE if $point1 eq "_undef_";
1928 my $point2 = get_point($lc);
1929 next LINE if $point2 eq "_undef_";
1930 my $point3 = get_point($lc);
1931 next LINE if $point3 eq "_undef_";
1932 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
1933 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
1934 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
1935 if (triangleArea($x1, $y1, $x2, $y2, $x3, $y3) < 0.0001) {
1936 PrintErrorMessage("Area of triangle is zero!",$lc);
1937 next LINE;
1938 }
1939 chk_comma($lc);
1940 my $point4 = get_point($lc);
1941 if (!memberOf($point4, $point1, $point2, $point3)) {
1942 PrintErrorMessage("Current point isn't a side point",$lc);
1943 next LINE;
1944 }
1945 next LINE if $point4 eq "_undef_";
1946 my $point5 = get_point($lc);
1947 next LINE if $point5 eq "_undef_";
1948 if (!memberOf($point5, $point1, $point2, $point3)) {
1949 PrintErrorMessage("Current point isn't a side point",$lc);
1950 next LINE;
1951 }
1952 if ($point4 eq $point5) {
1953 PrintErrorMessage("Side points are identical",$lc);
1954 next LINE;
1955 }
1956 chk_rparen("arguments of $cmd",$lc);
1957 my ($xc, $yc, $r) = excircle($point1, $point2, $point3,
1958 $point4, $point5);
1959 my $R=$r;
1960 if (s/^\s*\[\s*//) {
1961 $R += expr($lc);
1962 if ($R < 0.0001) {
1963 PrintErrorMessage("Radius has become equal to zero!",$lc);
1964 next LINE;
1965 }
1966 chk_rsb($lc);
1967 }
1968 if ($R > (500 / 2.845)) {
1969 PrintErrorMessage("Radius is greater than 175mm!",$lc);
1970 next LINE;
1971 }
1972 print OUT "%% excircle center = ($xc,$yc) radius = $R\n" if $comments_on;
1973 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
1974 $xc+$R, $yc, $xc, $yc;
1975 chk_comment($lc);
1976
1977 }
1978 elsif (s/^\s*(drawincircle(?=\W))//i)
1979 {
1980 my ($cmd) = $1;
1981 print OUT "%% $cmd$_" if $comments_on;
1982 chk_lparen("drawincircle",$lc);
1983 my $point1 = get_point($lc);
1984 next LINE if $point1 eq "_undef_";
1985 my $point2 = get_point($lc);
1986 next LINE if $point2 eq "_undef_";
1987 my $point3 = get_point($lc);
1988 next LINE if $point3 eq "_undef_";
1989 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
1990 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
1991 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
1992 if (triangleArea($x1, $y1, $x2, $y2, $x3, $y3) < 0.0001) {
1993 PrintErrorMessage("Area of triangle is zero!",$lc);
1994 next LINE;
1995 }
1996 my ($xc, $yc, $r) = IncircleCenter($x1,$y1,$x2,$y2,$x3,$y3);
1997 my $R=$r;
1998 if (s/^\s*\[\s*//) {
1999 $R += expr($lc);
2000 if ($R < 0.0001) {
2001 PrintErrorMessage("Radius has become equal to zero!",$lc);
2002 next LINE;
2003 }
2004 chk_rsb($lc);
2005 }
2006 if ($R > (500 / 2.845)) {
2007 PrintErrorMessage("Radius is greater than 175mm!",$lc);
2008 next LINE;
2009 }
2010 print OUT "%% incircle center = ($xc,$yc) radius = $R\n" if $comments_on;
2011 printf OUT "\\circulararc 360 degrees from %.5f %.5f center at %.5f %.5f\n",
2012 $xc+$R, $yc, $xc, $yc;
2013 chk_rparen("arguments of $cmd",$lc);
2014 chk_comment($lc);
2015
2016 }
2017 elsif (s/^\s*(drawline(?=\W))//i)
2018 {
2019 my ($cmd) = $1;
2020 print OUT "%% $cmd$_" if $comments_on;
2021 DrawLineOrArrow(1,$lc);
2022 }
2023 elsif (s/^\s*(drawthickarrow(?=\W))//i)
2024 {
2025 my ($cmd) = $1;
2026 print OUT "%% $cmd$_" if $comments_on;
2027 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\large .})%\n";
2028 print OUT "{\\setbox1=\\hbox{\\usefont{OT1}{cmr}{m}{n}\\large .}%\n";
2029 print OUT " \\global\\linethickness=0.31\\wd1}%\n";
2030 DrawLineOrArrow(0,$lc);
2031 print OUT "\\setlength{\\linethickness}{0.4pt}%\n";
2032 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
2033 }
2034 elsif (s/^\s*(drawthickline(?=\W))//i)
2035 {
2036 my ($cmd) = $1;
2037 print OUT "%% $cmd$_" if $comments_on;
2038 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\large .})%\n";
2039 print OUT "{\\setbox1=\\hbox{\\usefont{OT1}{cmr}{m}{n}\\large .}%\n";
2040 print OUT " \\global\\linethickness=0.31\\wd1}%\n";
2041 DrawLineOrArrow(1,$lc);
2042 print OUT "\\setlength{\\linethickness}{0.4pt}%\n";
2043 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
2044 }
2045 elsif (s/^\s*(drawperpendicular(?=\W))//i)
2046 {
2047 my ($cmd) = $1;
2048 print OUT "%% $cmd$_" if $comments_on;
2049
2050 chk_lparen($cmd,$lc);
2051 my $A = get_point($lc);
2052 next LINE if $A eq "_undef_";
2053 chk_comma($lc);
2054 my $B = get_point($lc);
2055 next LINE if $A eq "_undef_";
2056 s/\s*//; #ignore white space
2057 my $C = get_point($lc);
2058 next LINE if $A eq "_undef_";
2059 chk_rparen("arguments of $cmd",$lc);
2060 chk_comment($lc);
2061 #
2062 #start actual computation
2063 #
2064 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$A});
2065 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$B});
2066 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$C});
2067 my ($xF, $yF) = perpendicular($x1, $y1, $x2, $y2, $x3, $y3);
2068 printf OUT "\\plot %.5f %.5f %.5f %.5f /\n",
2069 $x1, $y1, $xF, $yF;
2070
2071 }
2072 elsif (s/^\s*(drawpoint(?=\W))//i)
2073 {
2074 my ($cmd) = $1;
2075 print OUT "%% $cmd$_" if $comments_on;
2076 my ($stacklen);
2077 chk_lparen("$cmd",$lc);
2078 if (/^\)/) {
2079 PrintErrorMessage("There are no point to draw",$lc);
2080 next LINE;
2081 }
2082 my(@PP);
2083 DRAWPOINTS:while(1) {
2084 if (s/^([^\W\d_]\d{0,4})//i) { #point name
2085 $P = $1;
2086 if (!exists($PointTable{lc($P)})) {
2087 PrintErrorMessage("Undefined point $P",$lc);
2088 next DRAWPOINTS;
2089 }
2090 else {
2091 push (@PP,$P);
2092 s/\s*//;
2093 }
2094 }
2095 else {
2096 last DRAWPOINTS;
2097 }
2098 }
2099 drawpoints(@PP);
2100 chk_rparen("arguments of $cmd",$lc);
2101 chk_comment($lc);
2102
2103 }
2104 elsif (s/^\s*(drawRightAngle(?=\W))//i)
2105 {
2106 my ($cmd) = $1;
2107 print OUT "%% $cmd$_" if $comments_on;
2108
2109 chk_lparen("drawRightAngle",$lc);
2110 my $point1 = get_point($lc);
2111 next LINE if $point1 eq "_undef_";
2112 my $point2 = get_point($lc);
2113 next LINE if $point2 eq "_undef_";
2114 my $point3 = get_point($lc);
2115 next LINE if $point3 eq "_undef_";
2116 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point1});
2117 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point2});
2118 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point3});
2119 chk_comma($lc);
2120 my $dist = expr($lc);
2121 chk_rparen("arguments of $cmd",$lc);
2122 chk_comment($lc);
2123 #
2124 #actual computation
2125 #
2126 my ($Px, $Py) = pointOnLine($x2, $y2, $x1, $y1, $dist);
2127 my ($Qx, $Qy) = pointOnLine($x2, $y2, $x3, $y3, $dist);
2128 my ($Tx, $Ty) = midpoint($Px, $Py, $Qx, $Qy);
2129 my ($Ux, $Uy) = pointOnLine($x2, $y2, $Tx, $Ty, 2*Length($x2, $y2, $Tx, $Ty));
2130 if ($Px == $Ux || $Py == $Uy) {
2131 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f \n", $Px,$Py,$Ux,$Uy;
2132 }
2133 else {
2134 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / \n", $Px, $Py,$Ux,$Uy;
2135 }
2136 if ($Ux == $Qx || $Uy == $Qy) {
2137 printf OUT "\\putrule from %.5f %.5f to %.5f %.5f \n", $Ux,$Uy,$Qx,$Qy;
2138 }
2139 else {
2140 printf OUT "\\plot %.5f %.5f\t%.5f %.5f / \n", $Ux, $Uy,$Qx,$Qy;
2141 }
2142
2143
2144 }
2145 elsif (s/^\s*(drawsquare(?=\W))//i)
2146 {
2147 my ($cmd) = $1;
2148 print OUT "%% $cmd$_" if $comments_on;
2149 chk_lparen("drawSquare",$lc);
2150 my $p = get_point($lc);
2151 chk_comma($lc);
2152 my $side = expr($lc);
2153 $side = $side - (1.1 * $LineThickness/$xunits); #Suggested by RWDN
2154 my ($x,$y,$pSV,$pS) = unpack("d3A*",$PointTable{$p});
2155 printf OUT "\\put {%s} at %.5f %.5f %%drawsquare\n", drawsquare($side), $x, $y;
2156 chk_rparen("arguments of $cmd",$lc);
2157 chk_comment($lc);
2158
2159 }
2160 elsif (s/^\s*inputfile\*//i)
2161 {
2162 chk_lparen("inputfile*",$lc);
2163 my $row_in = "";
2164 if (s/^((\w|-|\.)+)//) {
2165 $row_in = $1;
2166 }
2167 else {
2168 PrintErrorMessage("No input file name found",$lc);
2169 next LINE;
2170 }
2171 if (!(-e $row_in)) {
2172 PrintErrorMessage("File $row_in does not exist",$lc);
2173 next LINE;
2174 }
2175 open(ROW, "$row_in")|| die "Can't open file $row_in\n";
2176 while (defined($in_line=<ROW>)) { print OUT $in_line; }
2177 print OUT "%% ... end of input file <$row_in>\n";
2178 close ROW;
2179 chk_rparen("input file name",$lc);
2180 chk_comment($lc);
2181
2182
2183 }
2184 elsif (s/^\s*(inputfile(?=\W))//i)
2185 {
2186 my ($cmd) = $1;
2187 print OUT "%% $cmd$_" if $comments_on;
2188
2189 chk_lparen("inputfile",$lc);
2190 my $comm_in = "";
2191 if (s/^((\w|-|\.)+)//) {
2192 $comm_in = $1;
2193 }
2194 else {
2195 PrintErrorMessage("No input file name found",$lc);
2196 next LINE;
2197 }
2198 if (!(-e $comm_in)) {
2199 PrintErrorMessage("File $comm_in does not exist",$lc);
2200 next LINE;
2201 }
2202 chk_rparen("input file name",$lc);
2203 my $input_times = 1; #default value
2204 if (s/^\[//) {
2205 $input_times = expr($lc);
2206 chk_rsb("optional argument",$lc);
2207 }
2208 print OUT "%% ... start of file <$comm_in> loop [$input_times]\n";
2209 for (my $i=0; $i<int($input_times); $i++) {
2210 open(COMM,"$comm_in") or die "Can't open file $comm_in\n";
2211 print OUT "%%% Iteration number: ",$i+1,"\n";
2212 my $old_file_name = $curr_in_file;
2213 process_input(COMM,"File $comm_in, ");
2214 $curr_in_file = $old_file_name;
2215 close COMM;
2216 }
2217 print OUT "%% ... end of file <$comm_in> loop [$input_times]\n";
2218 chk_comment($lc);
2219
2220 }
2221 elsif (s/^\s*(linethickness(?=\W))//i)
2222 {
2223 my $cmd = $1;
2224 print OUT "%% $cmd$_" if $comments_on;
2225 chk_lparen("linethickness", $lc);
2226 if (s/^default//i) {
2227 print OUT "\\linethickness=0.4pt\\Linethickness{0.4pt}%%\n";
2228 print OUT "\\setplotsymbol ({\\usefont{OT1}{cmr}{m}{n}\\tiny .})%\n";
2229 $LineThickness = setLineThickness($xunits,"0.4pt");
2230 }
2231 else {
2232 my $length = expr($lc);
2233 if (s/^\s*($units)//i) {
2234 my $units = $1;
2235 printf OUT "\\linethickness=%.5f%s\\Linethickness{%.5f%s}%%\n",
2236 $length, $units, $length, $units;
2237 $LineThickness = setLineThickness($xunits,"$length$units");
2238 my $mag;
2239 if ($units eq "pc") {
2240 $mag = $length * 12;
2241 }
2242 elsif ($units eq "in") {
2243 $mag = $length * 72.27;
2244 }
2245 elsif ($units eq "bp") {
2246 $mag = $length * 1.00375;
2247 }
2248 elsif ($units eq "cm") {
2249 $mag = $length * 28.45275;
2250 }
2251 elsif ($units eq "mm") {
2252 $mag = $length * 2.845275;
2253 }
2254 elsif ($units eq "dd") {
2255 $mag = $length * 1.07001;
2256 }
2257 elsif ($units eq "cc") {
2258 $mag = $length * 0.08917;
2259 }
2260 elsif ($units eq "sp") {
2261 $mag = $length * 0.000015259;
2262 }
2263 elsif ($units eq "pt") {
2264 $mag = $length;
2265 }
2266 $mag = 10 * $mag / 1.00278219;
2267 printf OUT "\\font\\CM=cmr10 at %.5fpt%%\n", $mag;
2268 print OUT "\\setplotsymbol ({\\CM .})%\n";
2269 }
2270 else {
2271 PrintErrorMessage("Did not found expect units part",$lc);
2272 }
2273 }
2274 chk_rparen("linethickness", $lc);
2275 chk_comment($lc);
2276
2277
2278 }
2279 elsif (s/^\s*(paper(?=\W))//i)
2280 {
2281 my ($cmd) = $1;
2282 print OUT "%% $cmd$_" if $comments_on;
2283 chk_lcb("paper", $lc);
2284 if (s/^units(?=\W)//i)
2285 {
2286
2287 chk_lparen("units",$lc);
2288 if(s/^\)//)
2289 {
2290 PrintWarningMessage("Missing value in \"units\"--default is 1pt",
2291 $lc);
2292 $xunits = "1pt";
2293 }
2294 else {
2295 $xunits = expr($lc);
2296 s/\s*//;
2297 if (s/^($units)//i) {
2298 $xunits .= "$1";
2299 $LineThickness = setLineThickness($xunits,"0.4pt");
2300 }
2301 elsif(s/^(\w)+//i) {
2302 PrintErrorMessage("$1 is not a valid mathspic unit",$lc);
2303 $xunits = "1pt";
2304 }
2305 else {
2306 PrintErrorMessage("No x-units found",$lc);
2307 $xunits = "1pt";
2308 }
2309 s/\s*//; #ignore white space
2310 if (s/^,//) { # there is a comma so expect an y-units
2311 s/\s*//; #ignore white space
2312 $yunits = expr($lc);
2313 s/\s*//; #ignore white space
2314 if (s/^($units)//i) {
2315 $yunits .= "$1";
2316 }
2317 elsif(s/^(\w)+//i) {
2318 PrintErrorMessage("$1 is not a valid mathspic unit",$lc);
2319 $yunits = "1pt";
2320 }
2321 else {
2322 PrintErrorMessage("No y-units found",$lc);
2323 $yunits = $xunits;
2324 }
2325 }
2326 else {
2327 $yunits = $xunits;
2328 }
2329 chk_rparen("units",$lc);
2330 }
2331
2332 $nounits = 0;
2333 }
2334 else
2335 {
2336 $nounits = 1;
2337 }
2338 s/^,\s*// or s/\s*//;
2339 if (s/^xrange//i)
2340 {
2341
2342 chk_lparen("xrange",$lc);
2343 my $ec;
2344 ($xlow,$ec) = ComputeDist($lc);
2345 next LINE if $ec == 0;
2346 chk_comma($lc);
2347 ($xhigh,$ec) = ComputeDist($lc);
2348 next LINE if $ec == 0;
2349 if ($xlow >= $xhigh)
2350 {
2351 PrintErrorMessage("xlow >= xhigh in xrange",$lc);
2352 next LINE;
2353 }
2354 chk_rparen("$xhigh",$lc);
2355
2356 $noxrange = 0;
2357 }
2358 else
2359 {
2360 $noxrange = 1;
2361 }
2362 s/^,\s*// or s/\s*//;
2363 if (s/^yrange//i)
2364 {
2365
2366 chk_lparen("yrange",$lc);
2367 my $ec;
2368 ($ylow,$ec) = ComputeDist($lc);
2369 next LINE if $ec == 0;
2370 chk_comma($lc);
2371 ($yhigh,$ec) = ComputeDist($lc);
2372 next LINE if $ec == 0;
2373 if ($ylow >= $yhigh)
2374 {
2375 PrintErrorMessage("ylow >= yhigh in yrange",$lc);
2376 next LINE;
2377 }
2378 chk_rparen("$yhigh",$lc);
2379
2380 $noyrange = 0;
2381 }
2382 else
2383 {
2384 $noyrange = 1;
2385 }
2386
2387 if (!$nounits)
2388 {
2389 printf OUT "\\setcoordinatesystem units <%s,%s>\n",
2390 $xunits,$yunits;
2391 }
2392 if(!$noxrange && !$noyrange)
2393 {
2394 printf OUT "\\setplotarea x from %.5f to %.5f, y from %.5f to %.5f\n",
2395 $xlow, $xhigh, $ylow, $yhigh;
2396
2397 }
2398
2399 s/^,\s*// or s/\s*//;
2400 $axis = "";
2401 if (s/^ax[ei]s(?=\W)//i)
2402 {
2403
2404 chk_lparen("axis",$lc);
2405 while(/^[^\)]/)
2406 {
2407 if (s/^([lrtbxy]{1}\*?)//i)
2408 {
2409 $axis .= $1;
2410 }
2411 elsif (s/^([^lrtbxy])//i)
2412 {
2413 PrintErrorMessage("Non-valid character \"$1\" in axis()",$lc);
2414 }
2415 s/\s*//;
2416 }
2417 chk_rparen("axis(arguments",$lc);
2418
2419 }
2420 $axis = uc($axis);
2421 s/^,\s*// or s/\s*//;
2422 if (s/^ticks(?=\W)//i)
2423 {
2424 chk_lparen("ticks",$lc);
2425 my $ec;
2426 ($xticks,$ec) = ComputeDist($lc);
2427 next LINE if $ec == 0;
2428 chk_comma($lc);
2429 ($yticks,$ec) = ComputeDist($lc);
2430 next LINE if $ec == 0;
2431 chk_rparen("ticks(arguments",$lc);
2432
2433 }
2434 else
2435 {
2436 $xticks = $yticks = 0;
2437 }
2438 chk_rcb("paper", $lc);
2439 YBRANCH: {
2440 if (index($axis, "Y")>-1)
2441 {
2442 if (index($axis, "Y*")>-1)
2443 {
2444 print OUT "\\axis left shiftedto x=0 / \n";
2445 last YBRANCH;
2446 }
2447 if ($yticks > 0)
2448 {
2449 if (index($axis, "T")>-1 && index($axis, "B")==-1)
2450 {
2451 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
2452 print OUT "$ylow to -$yticks by $yticks\n from $yticks to ";
2453 print OUT $yhigh-$yticks," by $yticks /\n";
2454 }
2455 elsif (index($axis, "T")==-1 && index($axis, "B")>-1)
2456 {
2457 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
2458 print OUT $ylow+$yticks," to -$yticks by $yticks\n from ";
2459 print OUT "$yticks to $yhigh by $yticks /\n";
2460 }
2461 elsif (index($axis, "T")>-1 && index($axis, "B")>-1)
2462 {
2463 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
2464 print OUT $ylow+$yticks," to -$yticks by $yticks\n from ";
2465 print OUT "$yticks to ",$yhigh-$yticks," by $yticks /\n";
2466 }
2467 else
2468 {
2469 print OUT "\\axis left shiftedto x=0 ticks numbered from ";
2470 print OUT "$ylow to -$yticks by $yticks\n from ";
2471 print OUT "$yticks to $yhigh by $yticks /\n";
2472 }
2473 }
2474 else
2475 {
2476 print OUT "\\axis left shiftedto x=0 /\n";
2477 }
2478 }
2479 }
2480 XBRANCH: { if (index($axis, "X")>-1)
2481 {
2482 if (index($axis, "X*")>-1)
2483 {
2484 print OUT "\\axis bottom shiftedto y=0 /\n";
2485 last XBRANCH;
2486 }
2487 if ($xticks > 0)
2488 {
2489 if (index($axis, "L")>-1 && index($axis, "R")==1)
2490 {
2491 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
2492 print OUT $xlow + $xticks," to -$xticks by $xticks\n from";
2493 print OUT " $xticks to $xhigh by $xticks /\n";
2494 }
2495 elsif (index($axis, "L")==-1 && index($axis, "R")>-1)
2496 {
2497 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
2498 print OUT "$xlow to -$xticks by $xticks\n from ";
2499 print OUT "$xticks to ",$xhigh-$xticks," by $xticks /\n";
2500 }
2501 elsif (index($axis, "L")>-1 && index($axis, "R")>-1)
2502 {
2503 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
2504 print OUT $xlow + $xticks," to -$xticks by $xticks\n from ";
2505 print OUT "$xticks to ",$xhigh - $xticks," by $xticks /\n";
2506 }
2507 else
2508 {
2509 print OUT "\\axis bottom shiftedto y=0 ticks numbered from ";
2510 print OUT "$xlow to -$xticks by $xticks\n from ";
2511 print OUT "$xticks to $xhigh by $xticks /\n";
2512 }
2513 }
2514 else
2515 {
2516 print OUT "\\axis bottom shiftedto y=0 /\n";
2517 }
2518 } }
2519 LBRANCH: {if (index($axis, "L")>-1)
2520 {
2521 if (index($axis, "L")>-1)
2522 {
2523 if (index($axis, "L*")>-1)
2524 {
2525 print OUT "\\axis left /\n";
2526 last LBRANCH;
2527 }
2528 if ($yticks > 0)
2529 {
2530 print OUT "\\axis left ticks numbered from ";
2531 print OUT "$ylow to $yhigh by $yticks /\n";
2532 }
2533 else
2534 {
2535 print OUT "\\axis left /\n";
2536 }
2537 }
2538 } }
2539 RBRANCH: { if (index($axis, "R")>-1)
2540 {
2541 if (index($axis, "R*")>-1)
2542 {
2543 print OUT "\\axis right /\n";
2544 last RBRANCH;
2545 }
2546 if ($yticks > 0)
2547 {
2548 print OUT "\\axis right ticks numbered from $ylow to $yhigh by ";
2549 print OUT "$yticks /\n";
2550 }
2551 else
2552 {
2553 print OUT "\\axis right /\n";
2554 }
2555 } }
2556 TBRANCH: { if (index($axis, "T")>-1)
2557 {
2558 if (index($axis, "T*")>-1)
2559 {
2560 print OUT "\\axis top /\n";
2561 last TBRANCH;
2562 }
2563 if ($xticks > 0)
2564 {
2565 print OUT "\\axis top ticks numbered from $xlow to $xhigh by ";
2566 print OUT "$xticks /\n";
2567 }
2568 else
2569 {
2570 print OUT "\\axis top /\n";
2571 }
2572 } }
2573 BBRANCH: { if (index($axis, "B")>-1)
2574 {
2575 if (index($axis, "B*")>-1)
2576 {
2577 print OUT "\\axis bottom /\n";
2578 last BBRANCH;
2579 }
2580 if ($xticks > 0)
2581 {
2582 print OUT "\\axis bottom ticks numbered from $xlow to $xhigh by ";
2583 print OUT "$xticks /\n";
2584 }
2585 else
2586 {
2587 print OUT "\\axis bottom /\n";
2588 }
2589 } }
2590
2591
2592 chk_comment($lc);
2593
2594 }
2595 elsif (s/^\s*(PointSymbol(?=\W))//i)
2596 {
2597 my $cmd = $1;
2598 print OUT "%% $cmd$_" if $comments_on;
2599
2600 chk_lparen("$cmd",$lc);
2601 if (s/^default//i) #default point symbol
2602 {
2603 $defaultsymbol = "\$\\bullet\$";
2604 }
2605 elsif (s/^(circle|square)//i) {
2606 $defaultsymbol = $1;
2607 chk_lparen($defaultsymbol, $lc);
2608 $GlobalDimOfPoints = expr($lc);
2609 chk_rparen("expression", $lc);
2610 }
2611 elsif (s/^(((\\,){1}|(\\\)){1}|(\\\s){1}|[^\),\s])+)//) #arbitrary LaTeX point
2612 {
2613 $defaultsymbol = $1;
2614 $defaultsymbol=~ s/\\\)/\)/g;
2615 $defaultsymbol=~ s/\\,/,/g;
2616 $defaultsymbol=~ s/\\ / /g;
2617 }
2618 else
2619 {
2620 PrintErrorMessage("unrecognized point symbol",$lc);
2621 }
2622 if (s/\s*,\s*//) {
2623 $defaultLFradius = expr($lc);
2624 }
2625 chk_rparen("after $cmd arguments",$lc);
2626 chk_comment("after $cmd command",$lc);
2627
2628 }
2629 elsif (s/^\s*point(?=\W)//i)
2630 {
2631 my ($Point_Line);
2632 chomp($Point_Line=$_);
2633 my ($pointStar, $PointName, $origPN);
2634 $pointStar = 0; # default value: he have a point command
2635 $pointStar = 1 if s/^\*//;
2636 chk_lparen("point" . (($pointStar)?"*":""),$lc);
2637 if (s/^([^\W\d_](?![^\W\d_])\d{0,4})//i) {
2638 #
2639 # Note: the regular expression (foo)(?!bar) means that we are
2640 # looking a foo not followed by a bar. Moreover, the regular
2641 # expression [^\W\d_] means that we are looking for letter.
2642 #
2643 $origPN = $1;
2644 $PointName = lc($1);
2645 }
2646 else {
2647 PrintErrorMessage("Invalid point name",$lc);
2648 next LINE;
2649 }
2650 #if ($pointStar and !exists($PointTable{$PointName})) {
2651 # PrintWarningMessage("Point $origPN has not been defined",$lc);
2652 #}
2653 if (!$pointStar and exists($PointTable{$PointName})) {
2654 PrintWarningMessage("Point $origPN has been used already",$lc);
2655 }
2656 chk_rparen("point" . (($pointStar)?"*":""). "($origPN",$lc);
2657 chk_lcb("point" . (($pointStar)?"*":""). "($origPN)",$lc);
2658 my ($Px, $Py);
2659 if (s/^perpendicular(?=\W)//i) {
2660 chk_lparen("perpendicular",$lc);
2661 my $FirstPoint = &get_point($lc);
2662 next LINE if $FirstPoint eq "_undef_";
2663 chk_comma($lc);
2664 my $SecondPoint = &get_point($lc);
2665 next LINE if $SecondPoint eq "_undef_";
2666 my $ThirdPoint = &get_point($lc);
2667 next LINE if $ThirdPoint eq "_undef_";
2668 chk_rparen("No closing parenthesis found",$lc);
2669 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2670 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2671 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
2672 ($Px, $Py) = perpendicular($x1,$y1,$x2,$y2,$x3,$y3);
2673
2674 }
2675 elsif (s/^intersection(?=\W)//i) {
2676 chk_lparen("intersection",$lc);
2677 my $FirstPoint = get_point($lc);
2678 next LINE if $FirstPoint eq "_undef_";
2679 my $SecondPoint = get_point($lc);
2680 next LINE if $SecondPoint eq "_undef_";
2681 chk_comma($lc);
2682 my $ThirdPoint = get_point($lc);
2683 next LINE if $ThirdPoint eq "_undef_";
2684 my $ForthPoint = get_point($lc);
2685 next LINE if $ForthPoint eq "_undef_";
2686 chk_rparen("No closing parenthesis found",$lc);
2687 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2688 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2689 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
2690 my ($x4,$y4,$pSV4,$pS4)=unpack("d3A*",$PointTable{$ForthPoint});
2691 ($Px, $Py) = intersection4points($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4);
2692
2693
2694 }
2695 elsif (s/^midpoint(?=\W)//i) {
2696 chk_lparen("midpoint",$lc);
2697 my $FirstPoint = &get_point($lc);
2698 next LINE if $FirstPoint eq "_undef_";
2699 my $SecondPoint = &get_point($lc);
2700 next LINE if $SecondPoint eq "_undef_";
2701 chk_rparen("No closing parenthesis found",$lc);
2702 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2703 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2704 ($Px, $Py) = midpoint($x1, $y1, $x2, $y2);
2705
2706 }
2707 elsif (s/^pointonline(?=\W)//i) {
2708 chk_lparen("pointonline",$lc);
2709 my $FirstPoint = &get_point($lc);
2710 next LINE if $FirstPoint eq "_undef_";
2711 my $SecondPoint = &get_point($lc);
2712 next LINE if $SecondPoint eq "_undef_";
2713 chk_comma($lc);
2714 # now get the distance
2715 my $distance = expr($lc);
2716 chk_rparen("No closing parenthesis found",$lc);
2717 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2718 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2719 ($Px, $Py) = pointOnLine($x1,$y1,$x2,$y2,$distance);
2720
2721 }
2722 elsif (s/^circumcircleCenter(?=\W)//i) {
2723 chk_lparen("circumCircleCenter",$lc);
2724 my $FirstPoint = &get_point($lc);
2725 next LINE if $FirstPoint eq "_undef_";
2726 my $SecondPoint = &get_point($lc);
2727 next LINE if $SecondPoint eq "_undef_";
2728 my $ThirdPoint = &get_point($lc);
2729 next LINE if $ThirdPoint eq "_undef_";
2730 chk_rparen("No closing parenthesis found",$lc);
2731 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2732 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2733 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
2734 ($Px, $Py,$r) = &circumCircleCenter($x1,$y1,$x2,$y2,$x3,$y3,$lc);
2735 next LINE if $Px == 0 and $Py == 0 and $r == 0;
2736
2737 }
2738 elsif (s/^IncircleCenter(?=\W)//i) {
2739 chk_lparen("IncircleCenter",$lc);
2740 my $FirstPoint = &get_point($lc);
2741 next LINE if $FirstPoint eq "_undef_";
2742 my $SecondPoint = &get_point($lc);
2743 next LINE if $SecondPoint eq "_undef_";
2744 my $ThirdPoint = &get_point($lc);
2745 next LINE if $ThirdPoint eq "_undef_";
2746 chk_rparen("No closing parenthesis found",$lc);
2747 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$FirstPoint});
2748 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$SecondPoint});
2749 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$ThirdPoint});
2750 ($Px, $Py, $r) = IncircleCenter($x1,$y1,$x2,$y2,$x3,$y3);
2751
2752 }
2753 elsif (s/^ExcircleCenter(?=\W)//i) {
2754 chk_lparen("ExcircleCenter",$lc);
2755 my $PointA = get_point($lc);
2756 next LINE if $PointA eq "_undef_";
2757 my $PointB = get_point($lc);
2758 next LINE if $PointB eq "_undef_";
2759 my $PointC = get_point($lc);
2760 next LINE if $PointC eq "_undef_";
2761 chk_comma($lc);
2762 my $PointD = &get_point($lc);
2763 next LINE if $PointD eq "_undef_";
2764 if (!memberOf($PointD, $PointA, $PointB, $PointC)) {
2765 PrintErrorMessage("Current point isn't a side point",$lc);
2766 next LINE;
2767 }
2768 my $PointE = get_point($lc);
2769 next LINE if $PointE eq "_undef_";
2770 if (!memberOf($PointE, $PointA, $PointB, $PointC)) {
2771 PrintErrorMessage("Current point isn't a side point",$lc);
2772 next LINE;
2773 }
2774 if ($PointD eq $PointE) {
2775 PrintErrorMessage("Side points are identical",$lc);
2776 next LINE;
2777 }
2778 ($Px, $Py, $r) = excircle($PointA, $PointB, $PointC,
2779 $PointD, $PointE);
2780 chk_rparen("after coordinates part",$lc);
2781
2782 }
2783 elsif (/^[^\W\d_]\d{0,4}\s*[^,\w]/) {
2784 m/^([^\W\d_]\d{0,4})\s*/i;
2785 if (exists($PointTable{lc($1)})) {
2786 my $Tcoord = get_point($lc);
2787 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{$Tcoord});
2788 $Px = $x;
2789 $Py = $y;
2790 }
2791 else {
2792 $Px = expr();
2793 chk_comma($lc);
2794 $Py = expr();
2795 }
2796 }
2797 elsif (/[^\W\d_]\d{0,4}\s*,\s*shift|polar|rotate|vector/i) { #a point?
2798 s/^([^\W\d_]\d{0,4})//i;
2799 my $PointA = $1;
2800 if (exists($PointTable{lc($PointA)})) {
2801 s/\s*//;
2802 if (s/^,//) {
2803 s/\s*//;
2804 if (s/^shift(?=\W)//i) {
2805
2806 chk_lparen("shift",$lc);
2807 my $dist1 = expr($lc);
2808 chk_comma($lc);
2809 my $dist2 = expr($lc);
2810 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
2811 $Px = $x1 + $dist1;
2812 $Py = $y1 + $dist2;
2813 chk_rparen("shift part",$lc);
2814
2815 }
2816 elsif (s/^polar(?=\W)//i) {
2817 chk_lparen("polar",$lc);
2818 my ($R1, $Theta1);
2819 $R1 = expr($lc);
2820 chk_comma($lc);
2821 $Theta1 = expr($lc);
2822 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
2823 s/\s*//;
2824 if (s/^rad(?=\W)//i) {
2825 # do nothing!
2826 }
2827 elsif (s/^deg(?=\W)//i) {
2828 $Theta1 = $Theta1 * PI / 180;
2829 }
2830 else {
2831 #$Theta1 = $Theta1 * PI / 180;
2832 }
2833 $Px = $x1 + $R1 * cos($Theta1);
2834 $Py = $y1 + $R1 * sin($Theta1);
2835 chk_rparen("after polar part",$lc);
2836
2837 }
2838 elsif (s/^rotate(?=\W)//i) {
2839 chk_lparen("rotate",$lc);
2840 my $Q = lc($PointA);
2841 my $P = get_point($lc);
2842 next LINE if $P eq "_undef_";
2843 chk_comma($lc);
2844 my $Theta1 = expr($lc);
2845 my ($xP,$yP,$pSV1,$pS1)=unpack("d3A*",$PointTable{$P});
2846 my ($xQ,$yQ,$pSV2,$pS2)=unpack("d3A*",$PointTable{$Q});
2847 s/\s*//;
2848 if (s/^rad(?=\W)//i)
2849 {
2850 # do nothing!
2851 }
2852 elsif (s/^deg(?=\W)//i)
2853 {
2854 $Theta1 = $Theta1 * PI / 180;
2855 }
2856 else
2857 {
2858 $Theta1 = $Theta1 * PI / 180;
2859 }
2860 # shift origin to P
2861 $xQ -= $xP;
2862 $yQ -= $yP;
2863 # do the rotation
2864 $Px = $xQ * cos($Theta1) - $yQ * sin($Theta1);
2865 $Py = $xQ * sin($Theta1) + $yQ * cos($Theta1);
2866 # return origin back to original origin
2867 $Px += $xP;
2868 $Py += $yP;
2869 chk_rparen("after rotate part",$lc);
2870
2871 }
2872 elsif (s/^vector(?=\W)//i) {
2873 chk_lparen("vector",$lc);
2874 my ($x0,$y0,$pSV0,$pS0) = unpack("d3A*",$PointTable{lc($PointA)});
2875 my $P = get_point($lc);
2876 my $Q = get_point($lc);
2877 my ($x1,$y1,$pSV1,$pS1) = unpack("d3A*",$PointTable{$P});
2878 my ($x2,$y2,$pSV2,$pS2) = unpack("d3A*",$PointTable{$Q});
2879 $Px = $x0 + $x2 - $x1;
2880 $Py = $y0 + $y2 - $y1;
2881 chk_rparen("vector part",$lc);
2882
2883
2884 }
2885 else {
2886 PrintErrorMessage("unexpected token",$lc);
2887 next LINE;
2888 }
2889 }
2890 else {
2891 my ($xA,$yA,$pSVA,$pSA)=unpack("d3A*",$PointTable{lc($PointA)});
2892 $Px = $xA;
2893 $Py = $yA;
2894 }
2895 }
2896 else {
2897 PrintErrorMessage("Undefined point $PointA",$lc);
2898 next LINE;
2899 }
2900 }
2901 else {
2902 $Px = expr();
2903 chk_comma($lc);
2904 $Py = expr();
2905 }
2906
2907 chk_rcb("coordinates part",$lc);
2908 my $sv = $defaultsymbol;
2909 my $sh = $defaultLFradius;
2910 my $side_or_radius = undef;
2911 if (s/^\[\s*//) { # the user has opted to specify the optional part
2912 if (/^(symbol|radius|side)\s*/i) {
2913 my @previous_options = ();
2914 my $number_of_options = 1;
2915 my $symbol_set = 0;
2916 while (s/^(symbol|radius)\s*//i and $number_of_options <= 2) {
2917 my $option = lc($1);
2918 if (s/^=\s*//) {
2919 if (memberOf($option,@previous_options)) {
2920 PrintErrorMessage("Option \"$option\" has been already defined", $lc);
2921 my $dummy = expr($lc);
2922 }
2923 elsif ($option eq "radius") {
2924 $sh = expr($lc);
2925 $sv = $defaultsymbol if ! $symbol_set;
2926 }
2927 elsif ($option eq "symbol") {
2928 if (s/^circle\s*//i) {
2929 $sv = "circle";
2930 chk_lparen("after token circle",$lc);
2931 $side_or_radius = expr($lc);
2932 chk_rparen("expression",$lc);
2933 }
2934 elsif (s/^square\s*//i) {
2935 $sv = "square";
2936 chk_lparen("after token square",$lc);
2937 $side_or_radius = expr($lc);
2938 chk_rparen("expression",$lc);
2939 }
2940 elsif (s/^(((\\\]){1}|(\\,){1}|(\\\s){1}|[^\],\s])+)//) {
2941 $sv = $1;
2942 $sv =~ s/\\\]/\]/g;
2943 $sv =~ s/\\,/,/g;
2944 $sv =~ s/\\ / /g;
2945 s/\s*//;
2946 }
2947 $symbol_set = 1;
2948 }
2949 }
2950 else {
2951 PrintErrorMessage("unexpected token", $lc);
2952 next LINE;
2953 }
2954 $number_of_options++;
2955 push (@previous_options, $option);
2956 s/^,\s*//;
2957 }
2958 }
2959 else {
2960 PrintErrorMessage("unexpected token", $lc);
2961 next LINE;
2962 }
2963
2964 chk_rsb("optional part",$lc);
2965 }
2966 # to avoid truncation problems introduced by the pack function, we
2967 # round each number up to five decimal digits
2968 $Px = sprintf("%.5f", $Px);
2969 $Py = sprintf("%.5f", $Py);
2970 print OUT "%% point$Point_Line \t$origPN = ($Px, $Py)\n" if $comments_on;
2971 chk_comment($lc);
2972 $PointTable{$PointName} = pack("d3A*",$Px,$Py,$sh,$sv);
2973 if (defined($side_or_radius)) {
2974 $DimOfPoint{$PointName} = $side_or_radius;
2975 }
2976
2977 }
2978 elsif (/^\s*setPointNumber(?=\W)/i)
2979 {
2980 PrintWarningMessage("Command setPointNumber is ignored",$lc);
2981 next LINE;
2982 }
2983 elsif (s/^\s*(showAngle(?=\W))//i)
2984 {
2985 chk_lparen("showangle",$lc);
2986 my $point_1 = get_point($lc);
2987 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
2988 my $point_2 = get_point($lc);
2989 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
2990 my $point_3 = get_point($lc);
2991 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
2992 my $angle = Angle($x1, $y1, $x2, $y2, $x3, $y3);
2993 $angle = 0 if $angle == -500;
2994 printf OUT "%%%% angle(%s%s%s) = %.5f deg ( %.5f rad)\n", $point_1,
2995 $point_2, $point_3, $angle, $angle*D2R;
2996 chk_rparen("Missing right parenthesis", $lc);
2997
2998 }
2999 elsif (s/^\s*(showArea(?=\W))//i)
3000 {
3001 chk_lparen("showarea",$lc);
3002 my $point_1 = get_point($lc);
3003 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
3004 my $point_2 = get_point($lc);
3005 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
3006 my $point_3 = get_point($lc);
3007 my ($x3,$y3,$pSV3,$pS3)=unpack("d3A*",$PointTable{$point_3});
3008 print OUT "%% area($point_1$point_2$point_3) = ",
3009 triangleArea($x1, $y1, $x2, $y2, $x3, $y3), "\n";
3010 chk_rparen("Missing right parenthesis", $lc);
3011
3012 }
3013 elsif (s/^\s*(showLength(?=\W))//i)
3014 {
3015 chk_lparen("showlength",$lc);
3016 my $point_1 = get_point($lc);
3017 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{$point_1});
3018 my $point_2 = get_point($lc);
3019 my ($x2,$y2,$pSV2,$pS2)=unpack("d3A*",$PointTable{$point_2});
3020 print OUT "%% length($point_1$point_2) = ",
3021 Length($x1, $y1, $x2, $y2), "\n";
3022 chk_rparen("Missing right parenthesis", $lc);
3023
3024
3025 }
3026 elsif (/^\s*showPoints(?=\W)/i)
3027 {
3028 print OUT "%%-------------------------------------------------\n";
3029 print OUT "%% L I S T O F P O I N T S \n";
3030 print OUT "%%-------------------------------------------------\n";
3031 foreach my $p (keys(%PointTable)) {
3032 my ($x, $y, $pSV, $pS) = unpack("d3A*",$PointTable{$p});
3033 printf OUT "%%%%\t%s\t= ( %.5f, %.5f ), LF-radius = %.5f, symbol = %s\n",
3034 $p, $x, $y, $pSV, $pS;
3035 }
3036 print OUT "%%-------------------------------------------------\n";
3037 print OUT "%% E N D O F L I S T O F P O I N T S \n";
3038 print OUT "%%-------------------------------------------------\n";
3039 next LINE;
3040 }
3041 elsif (/^\s*showVariables(?=\W)/i)
3042 {
3043 print OUT "%%-------------------------------------------------\n";
3044 print OUT "%% L I S T O F V A R I A B L E S \n";
3045 print OUT "%%-------------------------------------------------\n";
3046 foreach my $var (keys(%VarTable)) {
3047 print OUT "%%\t", $var, "\t=\t", $VarTable{$var}, "\n";
3048 }
3049 print OUT "%%-------------------------------------------------\n";
3050 print OUT "%% E N D O F L I S T O F V A R I A B L E S \n";
3051 print OUT "%%-------------------------------------------------\n";
3052 next LINE;
3053 }
3054 elsif (s/^\s*(system(?=\W))//i)
3055 {
3056 print OUT "%% $1$_" if $comments_on;
3057
3058 chk_lparen("$cmd",$lc);
3059 my ($error, $command, $rest) = get_string($_);
3060 next LINE if $error == 1;
3061 $_ = $rest;
3062 if (! is_tainted($command)) {
3063 system($command);
3064 }
3065 else {
3066 PrintErrorMessage("String \"$command\" has tainted data", $lc);
3067 next LINE;
3068 }
3069 chk_rparen("after $cmd arguments",$lc);
3070 chk_comment("after $cmd command",$lc);
3071
3072 }
3073 elsif (s/^\s*(text(?=\W))//i)
3074 {
3075 print OUT "%% $1$_" if $comments_on;
3076
3077 chk_lparen("text",$lc);
3078 my ($level,$text)=(1,"");
3079 TEXTLOOP: while (1)
3080 {
3081 $level++ if /^\(/;
3082 $level-- if /^\)/;
3083 s/^(.)//;
3084 last TEXTLOOP if $level==0;
3085 $text .= $1;
3086 }
3087 chk_lcb("text part",$lc);
3088 my ($Px, $Py,$dummy,$pos);
3089 $pos="";
3090 s/\s*//;
3091
3092 if (/^[^\W\d_]\d{0,4}\s*[^,\w]/) {
3093 my $Tcoord = get_point($lc);
3094 my ($x,$y,$pSV,$pS)=unpack("d3A*",$PointTable{$Tcoord});
3095 $Px = $x;
3096 $Py = $y;
3097 }
3098 elsif (/[^\W\d_]\d{0,4}\s*,\s*shift|polar/i) {
3099 s/^([^\W\d_]\d{0,4})//i;
3100 my $PointA = $1;
3101 if (exists($PointTable{lc($PointA)})) {
3102 s/\s*//;
3103 if (s/^,//) {
3104 s/\s*//;
3105 if (s/^shift(?=\W)//i) {
3106
3107 chk_lparen("shift",$lc);
3108 my $dist1 = expr($lc);
3109 chk_comma($lc);
3110 my $dist2 = expr($lc);
3111 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
3112 $Px = $x1 + $dist1;
3113 $Py = $y1 + $dist2;
3114 chk_rparen("shift part",$lc);
3115
3116 }
3117 elsif (s/^polar(?=\W)//i) {
3118 chk_lparen("polar",$lc);
3119 my ($R1, $Theta1);
3120 $R1 = expr($lc);
3121 chk_comma($lc);
3122 $Theta1 = expr($lc);
3123 my ($x1,$y1,$pSV1,$pS1)=unpack("d3A*",$PointTable{lc($PointA)});
3124 s/\s*//;
3125 if (s/^rad(?=\W)//i) {
3126 # do nothing!
3127 }
3128 elsif (s/^deg(?=\W)//i) {
3129 $Theta1 = $Theta1 * PI / 180;
3130 }
3131 else {
3132 #$Theta1 = $Theta1 * PI / 180;
3133 }
3134 $Px = $x1 + $R1 * cos($Theta1);
3135 $Py = $y1 + $R1 * sin($Theta1);
3136 chk_rparen("after polar part",$lc);
3137
3138 }
3139 }
3140 }
3141 else {
3142 PrintErrorMessage("undefined point/var",$lc);
3143 next LINE;
3144 }
3145 }
3146 else {
3147 $Px = expr();
3148 chk_comma($lc);
3149 $Py = expr();
3150 }
3151
3152 chk_rcb("coordinates part of text command",$lc);
3153 if (s/^\[//)
3154 {
3155 s/\s*//;
3156
3157 if (s/^(\w{1})\s*//) {
3158 $pos .= $1;
3159 if (memberOf($pos, "l", "r")) {
3160 if (s/^(\w{1})\s*//) {
3161 my $np = $1;
3162 if (memberOf($np, "t", "b", "B")) {
3163 $pos .= $np;
3164 }
3165 else {
3166 if (memberOf($np, "l", "r")) {
3167 PrintErrorMessage("$np can't follow 'l' or 'r'", $lc);
3168 }
3169 else {
3170 PrintErrorMessage("$np is not a valid positioning option", $lc);
3171 }
3172 next LINE;
3173 }
3174 }
3175 }
3176 elsif (memberOf($pos, "t", "b", "B")) {
3177 if (s/^(\w{1})\s*//) {
3178 my $np = $1;
3179 if (memberOf($np, "l", "r")) {
3180 $pos .= $np;
3181 }
3182 else {
3183 if (memberOf($np, "t", "b", "B")) {
3184 PrintErrorMessage("$np can't follow 't', 'b', or 'B'", $lc);
3185 }
3186 else {
3187 PrintErrorMessage("$np is not a valid positioning option", $lc);
3188 }
3189 next LINE;
3190 }
3191 }
3192 }
3193 else {
3194 PrintErrorMessage("$pos is not a valid positioning option", $lc);
3195 next LINE;
3196 }
3197 }
3198 else {
3199 PrintErrorMessage("illegal token in optional part of text command",$lc);
3200 next LINE;
3201 }
3202
3203 s/\s*//;
3204 chk_rsb("optional part of text command",$lc);
3205 }
3206 chk_comment($lc);
3207 if ($pos eq "")
3208 {
3209 printf OUT "\\put {%s} at %f %f\n", $text, $Px, $Py;
3210 }
3211 else
3212 {
3213 printf OUT "\\put {%s} [%s] at %f %f\n", $text, $pos, $Px, $Py;
3214 }
3215
3216 }
3217 elsif (s/^\s*(var(?=\W))//i)
3218 {
3219 print OUT "%% $1$_" if $comments_on;
3220 do{
3221 s/\s*//;
3222 PrintErrorMessage("no identifier found after token var",$lc)
3223 if $_ !~ s/^([^\W\d_]\d{0,4})//i;
3224 my $Varname = $1;
3225 my $varname = lc($Varname);
3226 if (exists $ConstTable{$varname}) {
3227 PrintErrorMessage("Redefinition of constant $varname",$lc);
3228 }
3229 s/\s*//; #remove leading white space
3230 PrintErrorMessage("did not find expected = sign",$lc)
3231 if $_ !~ s/^[=]//i;
3232 my $val = expr($lc);
3233 $VarTable{$varname} = $val;
3234 print OUT "%% $Varname = $val\n" if $comments_on;
3235 }while (s/^,//);
3236 chk_comment($lc);
3237 s/\s*//;
3238 if (/^[^%]/) {
3239 PrintWarningMessage("Trailing text is ignored",$lc);
3240 }
3241 }
3242 elsif (/^\s*\\(.+)/)
3243 {
3244 my $line = $1;
3245 if ($line =~ /^\s+(.+)/)
3246 {
3247 print OUT " $line\n";
3248 }
3249 else
3250 {
3251 print OUT "\\$line\n";
3252 }
3253 next LINE;
3254 }
3255 elsif (0==length) #empty line
3256 {
3257 next LINE;
3258 }
3259 else {
3260 PrintErrorMessage("command not recognized",$lc);
3261 next LINE;
3262 }
3263
3264 }
3265 }
3266 }
3267
3268
3269 our $alarm="";
3270 our $comments_on=1;
3271 our $out_file="default";
3272 our $argc=@ARGV;
3273 if ($argc == 0 || $argc > 5 ){ # no command line arguments or more than 4
3274 # arguments
3275 die "\nmathspic version $version_number\n" .
3276 "Usage: mathspic [-h] [-b] [-c] [-o <out file>] <in file>\n\n";
3277 }
3278 else {
3279 our $file = "";
3280 SWITCHES:
3281 while($_ = $ARGV[0]) {
3282 shift;
3283 if (/^-h$/) {
3284 die "\nThis is mathspic version $version_number\n" .
3285 "Type \"man mathspic\" for detailed help\n".
3286 "Usage:\tmathspic [-h] [-b] [-c] [-o <out file>] <in file>\n" .
3287 "\twhere,\n" .
3288 "\t[-b]\tenables bell sound if error exists\n" .
3289 "\t[-c]\tdisables comments in output file\n" .
3290 "\t[-h]\tgives this help listing\n" .
3291 "\t[-o]\tcreates specified output file\n\n";
3292 }
3293 elsif (/^-b$/) {
3294 $alarm = chr(7);
3295 }
3296 elsif (/^-c$/) {
3297 $comments_on = 0;
3298 }
3299 elsif (/^-o$/) {
3300 die "No output file specified!\n" if !@ARGV;
3301 $out_file = $ARGV[0];
3302 shift;
3303 }
3304 elsif (/^-\w+/) {
3305 die "$_: Illegal command line switch!\n";
3306 }
3307 else {
3308 $file = $_;
3309 }
3310 }my ($xA, $yA, $xB, $yB, $dist)=@_;
3311 die "No input file specified!\n" if $file eq "";
3312
3313 print "This is mathspic version $version_number\n";
3314 }
3315 our ($source_file, $log_file);
3316 if (! -e $file) {
3317 die "$file: no such file!\n" if (! (-e "$file.m"));
3318 $source_file = "$file.m";
3319 }
3320 else {
3321 $source_file = $file;
3322 $file = $1 if $file =~ /(\w[\w-\.]+)\.\w+/;
3323 }
3324 $out_file= "$file.mt" if $out_file eq "default";
3325 $log_file= "$file.mlg";
3326
3327
3328 open(IN,"$source_file")||die "Can't open source file: $source_file\n";
3329 open(OUT,">$out_file")||die "Can't open output file: $out_file\n";
3330 open(LOG,">$log_file")||die "Can't open log file: $log_file\n";
3331 print_headers;
3332 process_input(IN,"");
3333
3334 print $alarm if $no_errors > 0;
3335 __END__
3336