-
Notifications
You must be signed in to change notification settings - Fork 0
/
Points.pm
1386 lines (1184 loc) · 42 KB
/
Points.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use warnings;
use strict;
############################ Vertex (point) Library ############################
package Vertex;
=head2 NAME
Vertex - a package for handling vertices.
=head2 USAGE
use Points;
my $vert = Vertex->new(42,"uplt",3,5,0,(interest=>"depot"));
=head2 METHODS
=over
=item new(class,identity,name,x,y,z,meta)
Returns a new Vertex object.
Takes these options, all optional:
class (if not using Vertex->new)
identity - numeric ID
name - human-friendly name
x,y,z - integer coordinates
meta - a hash of metadata for the particular type of point
=cut
sub new {
my ($class,$i,$n,$x1,$y1,$z1,%meta) = @_;
#print "Creating a vertex ($i,$n) at ($x1,$y1)...\n";
my $self = {
identity => ($i or 0),
moniker => ($n or 'Unnamed'),
origin_x => ($x1 or 0),
origin_y => ($y1 or 0),
origin_z => ($z1 or 0),
class => "point",
immobile => 0,
metadata => (%meta or {})
};
bless $self,$class;
return $self;
}
=item id(value)
Returns the Vertex's numeric ID, and sets it, if an argument is provided.
=cut
sub id {
my ($self,$value) = @_;
$self->{identity} = $value if defined($value);
return $self->{identity};
}
=item name(value)
Returns the Vertex's print-friendly name, and sets it, if an argument is provided.
=cut
sub name {
my ($self,$value) = @_;
$self->{moniker} = $value if defined($value);
return $self->{moniker};
}
=item x(value), y(value), z(value)
Returns the Vertex's X, Y, or Z coordinate, and sets it, if an argument is provided and the Vertex is not immobilized.
=cut
sub x {
my ($self,$value) = @_;
$self->{origin_x} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_x};
}
sub y {
my ($self,$value) = @_;
$self->{origin_y} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_y};
}
=item loc(showZ)
Takes an argument determining if the Z coordinate is included.
Returns the Vertex's location as an array of X, Y, and optionally Z coordinates.
=cut
sub loc {
my ($self,$use_z) = @_;
my @loc = ($self->{origin_x},$self->{origin_y});
if ($use_z) { push(@loc,$self->{origin_z}); }
return @loc;
}
sub z {
my ($self,$value) = @_;
$self->{origin_z} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_z};
}
=item move({X},{Y},Z)
Takes arguments for X, Y, and optionally Z coordinates.
Sets the position of the Vertex.
Returns 0 if the Vertex is moved, or 1 if a required parameter is missing, or 86 if the point is immobile.
=cut
sub move {
my ($self,$x,$y,$z) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile vertex $self";
return 86;
}
if (not defined $x or not defined $y) { return 1; }
$self->{origin_x} = $x;
$self->{origin_y} = $y;
if (defined $z) { $self->{origin_z} = $z; }
return 0;
}
=item wobble({variance})
Takes a required parameter for the amount of variance.
Varies the X, Y, and Z values of the Vertex.
Returns 0 if the Vertex is moved, or 86 if the point is immobile, or -1 if no variance value is given.
=cut
sub wobble {
use Common qw( vary );
my ($self,$variance) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile vertex $self";
return 86;
}
unless (defined $variance) {
warn "Variance omittted";
return -1;
}
$self->{origin_x} = Common::vary($self->{origin_x},$variance);
$self->{origin_y} = Common::vary($self->{origin_y},$variance);
unless ($self->{origin_z} == 0) { $self->{origin_z} = Common::vary($self->{origin_z},$variance); }
return 0;
}
=item roundLoc({precision})
Takes a required parameter for the precision in places.
Moves the X and Y values of the Vertex to the nearest point of the required precision.
Returns move result.
=cut
sub roundLoc {
my ($self,$prec) = @_;
defined $prec || return;
use Common qw( nround );
return $self->move(nround($prec,$self->{origin_x}),nround($prec,$self->{origin_y}));
if (0) { print "Moved to (" . $self->{origin_x} . "," . $self->{origin_y} . ").\n"; }
}
=item can_move(value)
Takes an optional parameter to set the Vertex's mobility.
Makes the Vertex mobile or immobile if the value parameter is set.
Returns 0 if the Vertex is immobile, or 1 if the point is mobile.
=cut
sub can_move {
my ($self,$value) = @_;
if (defined $value) {
$self->{immobile} = ($value == 0 ? 1 : 0);
}
if ($self->{immobile}) { return 0; }
return 1;
}
=item immobilize()
Takes no parameters.
Makes the Vertex immobile.
Returns 0.
=cut
sub immobilize {
my $self = shift;
$self->{immobile} = 1;
return 0;
}
=item Iama()
Takes no parameters.
Returns the object's class (Vertex).
=cut
sub Iama {
my $self = shift;
return $self->{class};
}
=item describe(verbose,showZ)
Takes an optional parameter for verbosity and one for showing th Z coordinat.
Makes no changes.
Returns the string describing the object.
=cut
sub describe {
my ($self,$vv,$showz) = @_;
unless (defined $vv) { $vv = 0 };
if ($vv == 0) { return $self->x(),$self->y(),$self->z(); } # 0
my $class = $self->Iama();
my $bio = sprintf("I am %s, a%smovable $class at (%.2f,%.2f%s).",$self->name(),( $self->can_move() ? " " : "n im"),$self->x(),$self->y(),($showz ? sprintf(",%.2f",$self->z()) : "" )); # 1
return $bio;
}
=item clip({minX,minY,maxX,maxY})
Takes required parameters for minimum X and Y, and maximum X and Y.
Makes changes to the point to bring it within the minimum and maximum values.
Returns 0 if the Vertex is moved or within the boundaries, or 1 if a required parameter is missing, or 86 if the point is immobile.
=cut
sub clip {
my ($self,$minx,$miny,$maxx,$maxy) = @_;
return 1 unless (defined $minx and defined $maxx and defined $miny and defined $maxy);
my $x = $self->{origin_x};
my $y = $self->{origin_y};
$x = ($x > $maxx ? $maxx : ($x < $minx ? $minx : $x));
$y = ($y > $maxy ? $maxy : ($y < $miny ? $miny : $y));
return $self->move($x,$y); # 86 and 0 are handled here.
}
=item getMeta({key})
Takes required parameter key.
Makes no changes.
Returns key value, if present, or undef if not defined.
=cut
sub getMeta {
my ($self,$key) = @_;
return $self->{metadata}{$key};
}
=item setMeta({key,value})
Takes required parameters key and value.
Makes changes: sets key to value given.
Returns 0 for successful set, 1 for failure, -1 for missing value or key.
=back
=cut
sub setMeta {
my ($self,$key,$value) = @_;
unless (defined $value and defined $key) { return -1; }
# print "Setting metadata $key to $value.\n";
($self->{metadata}{$key} = $value) or return 1;
return 0;
}
############################### Segment Library ################################
package Segment;
use Common qw ( between );
=head2 NAME
Segment - a package for handling lines (line segments).
=head2 USAGE
use Points;
my $line = Segment->new(42,"road",3,5,7,9,0,0,(cost=>35,speed=>40));
=head2 METHODS
=over
=item new(ID,NAME,Xo,Xe,Yo,Ye,Zo,Ze,(META HASH))
Takes the following arguments, all optional:
ID - numeric identifier
NAME - friendly name
Origin X, End X, Origin Y, End Y, Origin Z, End Z - coordinates for the line's position
META - Metadata in hash form
Returns a Segment object
=cut
sub new {
my ($class,$i,$n,$x1,$x2,$y1,$y2,$z1,$z2,%meta) = @_; # needs ID;
my $self = {
identity => ($i or 0),
moniker => ($n or 'Unnamed'),
origin_x => ($x1 or 0),
origin_y => ($y1 or 0),
origin_z => ($z1 or 0),
distance_x => ($x2-$x1 or 0),
distance_y => ($y2-$y1 or 0),
distance_z => ($z2-$z1 or 0),
metadata => (%meta or {}),
immobile => 0
};
bless $self,$class;
$self->move_endpoint($x2,$y2,$z2);
return $self;
}
=item id(VALUE)
Takes an optional value.
Sets Segment's numeric ID, if provided with a value.
Returns the Segment's numeric ID.
=cut
sub id {
my ($self,$value) = @_;
$self->{identity} = $value if defined($value);
return $self->{identity};
}
=item name(VALUE)
Takes an optional value.
Sets Segment's friendly name, if provided with a value.
Returns the Segment's friendly name.
=cut
sub name {
my ($self,$value) = @_;
$self->{moniker} = $value if defined($value);
return $self->{moniker};
}
=item slope()
Takes no arguments
Calculates the Segment's slope.
Returns the Segment's slope.
=cut
sub slope {
my $self = shift;
if ($self->{distance_x} == 0) { return undef; } # vertical line, even if length of line is 0, for the sake of simplicity and consistency in the program.
if ($self->{distance_y} == 0) { return 0; } # horizontal line
return $self->{distance_y} / $self->{distance_x};
}
=item y_intercept()
Takes no arguments.
Calculates the Segment's Y-intercept.
NB: This function returns the theoretical y-intercept of the segment even if it does not actually cross the y axis.
Returns the point where the line does or would cross the Y axis.
=cut
sub y_intercept {
my $self = shift;
my $m = $self->slope();
if (not defined $m) { return undef; } # undefined slope == vertical line. No Y-intercept!
elsif ($m == 0) { return $self->{origin_y}; } # horizontal line. Y is Y.
my $b = $self->{origin_y} - ($m * $self->{origin_x});
return $b;
}
=item f({x})
Takes a required argument X.
Calculates that X's Y-coordinate on the given line, even if it doesn't exist on this segment (useful for extending the line).
Returns the corresponding Y value.
=cut
sub f { # f(x)
my ($self,$x) = @_;
unless (defined $x) {
warn "No X value given";
return undef;
}
my $m = $self->slope();
if (not defined $m) { return undef; } # undefined slope == vertical line. Any y value will do.
elsif ($m == 0) { return $self->{origin_y}; } # horizontal line. Y is constant.
my $b = $self->{origin_y} - ($m * $self->{origin_x});
my $y = $m * $x + $b;
return $y;
}
=item finv({y})
Takes a required argument Y.
Calculates that Y's X-coordinate on the given line, even if it doesn't exist in this segment of the line.
Returns the corresponding X value.
=cut
sub finv { # f-1(y)
my ($self,$y) = @_;
unless (defined $y) {
warn "No Y value given";
return undef;
}
my $m = $self->slope();
if (not defined $m) { return $self->{origin_x}; } # undefined slope == vertical line. X is constant.
elsif ($m == 0) { return undef; } # horizontal line. Any Y will do.
my $b = $self->{origin_y} - ($m * $self->{origin_x});
my $x = ($y - $b)/$m;
return $x;
}
=item ox(value), oy(value), oz(value)
Take an optional argument for a new origin value.
Change the origin value for X, Y, or Z, depending on method chosen, if a value is given.
Return the (new) origin value.
=cut
sub ox {
my ($self,$value) = @_;
$self->{origin_x} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_x};
}
sub oy {
my ($self,$value) = @_;
$self->{origin_y} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_y};
}
sub oz {
my ($self,$value) = @_;
$self->{origin_z} = $value if (defined($value) and not $self->{immobile});
return $self->{origin_z};
}
=item xl(length), yl(length), zl(length)
Take an optional argument for a new length.
Change the length value for X, Y, or Z, depending on method chosen, if a value is given.
Return the (new) length along the given axis.
=cut
sub xl {
my ($self,$value) = @_;
$self->{distance_x} = $value if (defined($value) and not $self->{immobile});
return $self->{distance_x};
}
sub yl {
my ($self,$value) = @_;
$self->{distance_y} = $value if (defined($value) and not $self->{immobile});
return $self->{distance_y};
}
sub zl {
my ($self,$value) = @_;
$self->{distance_z} = $value if (defined($value) and not $self->{immobile});
return $self->{distance_z};
}
=item ex(), ey(), ez()
Take no arguments.
Does nothing.
Return the endpoint value.
=cut
sub ex {
my $self = shift;
return $self->{origin_x} + $self->{distance_x};
}
sub ey {
my $self = shift;
return $self->{origin_y} + $self->{distance_y};
}
sub ez {
my $self = shift;
return $self->{origin_z} + $self->{distance_z};
}
=item azimuth(whole)
Takes an optional argument to return a whole number as opposed to a precise number.
Uses getAzimuth to calculate the Segment's azimuth.
Returns the Segment's azimuth (rotation in degrees from north or the top of the screen).
=cut
sub azimuth {
my ($self,$whole) = @_;
return Points::getAzimuth($self->ex(),$self->ey(),$self->ox(),$self->oy(),$whole);
}
=item move({x,y},z)
Takes required arguments for new X and Y coordinates, and an optional argument for a Z coordinate.
Moves the line's origin, if not immobilized, to the given location.
Returns 0 if the line's new location matches the given location, 86 if the line could not be moved, or 1 if one of the required arguments is missing.
=cut
sub move {
my ($self,$x,$y,$z) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile line $self";
return 86;
}
if (not defined $x or not defined $y) { return 1; }
$self->{origin_x} = $x;
$self->{origin_y} = $y;
if (defined $z) { $self->{origin_z} = $z; }
return 0;
}
=item move_endpoint({x,y},z)
Takes required arguments for new X and Y coordinates, and an optional argument for a Z coordinate.
Moves the line's endpoint, if not immobilized, to the given location.
Returns 0 if the line's new endpoint matches the given location, 86 if the line could not be moved, or 1 if one of the required arguments is missing.
=cut
sub move_endpoint {
my ($self,$x,$y,$z) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile line $self";
return 86;
}
if (not defined $x or not defined $y) { return 1; }
$self->{distance_x} = 0 + $x - $self->{origin_x};
$self->{distance_y} = 0 + $y - $self->{origin_y};
if (defined $z) { $self->{distance_z} = 0 + $z - $self->{origin_z}; }
return 0;
}
=item double({w,h},nx,ny)
Takes required arguments for width and height of allowed area, and optional arguments for minimum x and y.
Makes the Segment twice as long, if that fits in the given boundaries.
Returns result of move_endpoint.
=cut
sub double {
my ($self,$w,$h,$minx,$miny) = @_;
return $self->stretch(2.0,$w,$h,$minx,$miny);
}
=item double({F,w,h},nx,ny)
Takes required arguments for width and height of allowed area, and optional arguments for minimum x and y.
Multiplies the Segment length by F, if that fits in the given boundaries, or adjusts it so it does.
Returns result of move_endpoint.
=cut
sub stretch {
# TODO: Make an option to Segment->stretch() that will keep the slope intact if the new end is out of bounds
my ($self,$factor,$w,$h,$minx,$miny) = @_;
$minx = 0 if not defined $minx;
$miny = 0 if not defined $miny;
# print "Min: $minx,$miny\n";
if (0) { printf("%d,%d =>",$self->ex(),$self->ey()); }
my $dx = $self->ex() - $self->ox();
my $dy = $self->ey() - $self->oy();
# my $x = ($self->ex() + $dx < ($minx or 0) ? ($minx or 0) : ($self->ex() + $dx > $w ? $w : $self->ex() + $dx));
my $x = ($self->ox() + ($dx * $factor));
# my $y = ($self->ey() + $dy < ($miny or 0) ? ($miny or 0) : ($self->ey() + $dy > $h ? $h : $self->ey() + $dy));
my $y = ($self->oy() + ($dy * $factor));
if (defined $w and $w > $minx and defined $h and $h > $miny) {
my ($setx,$sety);
if ($x < $minx) { $setx = $minx; } elsif ($x > $w) { $setx = $w; }
if (defined $setx) { $x = $setx; $y = $self->f($x); $setx = undef; }
if ($y < $miny) { $sety = $miny; } elsif ($y > $h) { $sety = $h; }
if (defined $sety) { $y = $sety; $x = $self->finv($y); }
if ($x < $minx) { $setx = $minx; } elsif ($x > $w) { $setx = $w; } # recheck in case the adjusted value is outside the boundaries.
if ($setx) { $x = $setx; } # This will mess up the line's slope, but it'll be within the given field.
}
my $rv = $self->move_endpoint($x,$y);
if (0) { printf("%d,%d\n",$self->ex(),$self->ey()); }
return $rv;
}
# TODO: Continue documentation
sub move_origin_only {
my ($self,$x,$y,$z) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile line $self";
return 86;
}
if (not defined $x or not defined $y) { return 1; }
$self->{distance_x} += $self->{origin_x} - $x;
$self->{distance_y} += $self->{origin_y} - $y;
$self->{origin_x} = $x;
$self->{origin_y} = $y;
if (defined $z) { $self->{distance_z} += $self->{origin_z} - $z; }
return 0;
}
sub can_move {
my ($self,$value) = @_;
if (defined $value) {
$self->{immobile} = ($value == 0 ? 1 : 0);
}
if ($self->{immobile}) { return 0; }
return 1;
}
sub getMeta {
my ($self,$key) = @_;
return $self->{metadata}{$key};
}
sub setMeta {
my ($self,$key,$value) = @_;
unless (defined $value) { return -1; }
# print "Setting metadata $key to $value.\n";
($self->{metadata}{$key} = $value) or return 1;
return 0;
}
sub immobilize {
my $self = shift;
$self->{immobile} = 1;
return 0;
}
sub set_ends {
my ($self,$x1,$x2,$y1,$y2,$z1,$z2) = @_;
if ($self->{immobile}) {
warn "Trying to move immobile line $self";
return 86;
}
if (not defined $x1 or not defined $y1 or not defined $x2 or not defined $y2) { return -1; }
my $rv = 0;
$rv += $self->move($x1,$y1,$z1);
$rv += $self->move_endpoint($x2,$y2,$z2);
return $rv;
}
sub roundLoc {
my ($self,$prec) = @_;
use Common qw( nround );
$self->set_ends(nround($prec,$self->{origin_x}),nround($prec,$self->ex()),nround($prec,$self->{origin_y}),nround($prec,$self->ey()),nround($prec,$self->{origin_z}),nround($prec,$self->ez()));
if (0) { print "Moved to (" . $self->{origin_x} . "," . $self->{origin_y} . "," . $self->{origin_z} . ")-(" . $self->ex() . "," . $self->ey() . "," . $self->ez() . ").\n"; }
}
sub length {
my $self = shift;
return Points::getDist($self->ox(),$self->oy(),$self->ex(),$self->ey());
}
sub fparts { # give the necessary function parts
my $self = shift;
return ($self->slope(),$self->y_intercept()); # (m,b)
}
sub intersects { #returns a theoretical intersection, even if neither segment contains it.
my ($self,$line) = @_;
unless (ref($self) eq 'Segment' and ref($line) eq 'Segment') { return undef; }
my ($a,$c) = $self->fparts();
my ($b,$d) = $line->fparts();
# print "Parts: $a+$c,$b+$d ... ";
unless (defined $a and defined $b) { # some vertical?
# print "~";
my ($x,$y);
unless (defined $a or defined $b) { # both vertical?
if (0) { print "[W] Vertical Parallel"; }
return undef;
} elsif (defined $a) {
# print "It's vertical.";
$x = $line->ox();
$y = $self->f($x);
} else {
# print "I'm vertical.";
$x = $self->ox();
$y = $line->f($x);
}
return $x,$y;
} elsif ($a == $b) { # same slope, parallel
# print "`";
if (0) { print "[W] Parallel"; }
unless ($c == $d) { #Not on same path
return undef;
}
# } else {
# print "^";
}
# after Wikipedia:Line-line_intersection#Intersection_of_two_lines_in_the_plane
my $x = ($d-$c)/($a-$b);
my $y = $a * $x + $c;
# print "((($x,$y)))";
unless (nround(10,$y) == nround(10,$b * $x + $d)) { # 10 digits of precision is sufficient for my^H^Hmost purposes.
if (1) { printf("\t%.6f =/= %.6f\t",$y,$b * $x + $d); }
return undef;
} else {
return ($x,$y);
}
}
sub partOfMe {
my $fuzziness = 0.01; # floating point fuzziness factor
my ($self,$v) = @_;
unless (ref($self) eq 'Segment' and ref($v) eq 'Vertex') { return 0; }
my ($m,$b) = $self->fparts();
# vertical line handler:
if (not defined $m and $v->x() - $self->ox() < $fuzziness) { return 1; }
if (between($v->x(),$self->ox(),$self->ex(),0,$fuzziness)
and abs($m * $v->x() + $b - $v->y()) < $fuzziness) {
return 1;
} else {
return 0;
}
}
sub touches { # returns an actual intersection, or undef if the lines don't touch.
my ($self,$line) = @_;
unless (ref($self) eq 'Segment' and ref($line) eq 'Segment') { return undef; }
my $p = Vertex->new(-1,"Junction",$self->intersects($line));
my $pointself = $self->partOfMe($p);
my $pointline = $line->partOfMe($p);
return ($pointself and $pointline,$p->x(),$p->y()); # does it touch?; intersect x; intersect y
# (intersect is not valid unless does it touch? == 1) Included in all returns in case caller wants to extend a lin to the intersection.
}
sub origin {
my ($self,$use_z) = @_;
my @loc = ($self->{origin_x},$self->{origin_y});
if ($use_z) { push(@loc,$self->{origin_z}); }
return @loc;
}
sub endpoint {
my ($self,$use_z) = @_;
my @loc = ($self->ex(),$self->ey());
if ($use_z) { push(@loc,$self->ez()); }
return @loc;
}
sub describe {
my ($self,$vv,$showz) = @_;
unless (defined $vv) { $vv = 0 };
if ($vv == 0) { return $self->ox(),$self->oy(),$self->oz(),$self->ex(),$self->ey(),$self->ez(); } # 0
my $bio = "I am ". $self->name() . ", a" . ( $self->can_move() ? " " : "n im") . "movable line segment from (" . $self->ox() . "," . $self->oy() . ($showz ? "," . $self->oz() : "" ) . ") to (" . $self->ex() . "," . $self->ey() . ($showz ? "," . $self->oz() : "" ) . ")."; # 1
if ($vv > 1) { $bio = "$bio I have a slope of " . $self->slope() . "."; # 2
if ($vv > 2) { $bio = "$bio If I am long enough in the right direction, I cross 0 at " . $self->y_intercept() . "."; # 3
if ($vv > 3) {
$bio = "$bio My length is " . Points::getDist($self->ox(),$self->oy(),$self->ex(),$self->ey()) . "."; # 4
}
}
}
return $bio;
}
sub orient { # orients the line generally in a particular direction. Useful for preparing a set of lines for duplicate checking.
#currently works in only x,y axes
my ($self,$dir) = @_;
$dir = 0 unless (defined $dir); # default to south/east
if ($dir == 0) { # s/e
if ($self->oy() > $self->ey()) { # unless ey > oy, line is upside down
$self->flip();
} elsif ($self->oy() == $self->ey() and $self->ox() > $self->ex()) { # unless ex > ox, line points west
$self->flip();
}
return;
} elsif ($dir == 1) { # n/e
if ($self->oy() < $self->ey()) { # unless ey > oy, line is upside down
$self->flip();
} elsif ($self->oy() == $self->ey() and $self->ox() > $self->ex()) { # unless ex > ox, line points west
$self->flip();
}
return;
} elsif ($dir == 2) { # n/w
if ($self->oy() < $self->ey()) { # unless ey > oy, line is upside down
$self->flip();
} elsif ($self->oy() == $self->ey() and $self->ox() < $self->ex()) { # unless ex > ox, line points west
$self->flip();
}
return;
} else { # s/w
if ($self->oy() > $self->ey()) { # unless ey > oy, line is upside down
$self->flip();
} elsif ($self->oy() == $self->ey() and $self->ox() < $self->ex()) { # unless ex > ox, line points west
$self->flip();
}
return;
}
}
sub flip {
my $self = shift;
my ($v,$w,$x,$y) = ($self->ex(),$self->ey(),$self->ox(),$self->oy());
return $self->set_ends($v,$x,$w,$y);
}
################################ Nodes Library #################################
package Node;
use parent -norequire, 'Vertex';
sub new {
my ($class,$i,$n,$x,$y,$z,$p) = @_;
my $self = Node->SUPER::new($i,$n,$x,$y,$z);
my $nodeself = {
parentID => ($p or -1),
cost2parent => 0,
children => [],
class => "node",
maximum => 0
};
@$self{ keys %$nodeself } = values %$nodeself;
bless $self, 'Node';
return $self;
}
sub describe {
my ($self,$vv,$showz) = @_;
if ($vv == 0) { return $self->SUPER::describe($vv,$showz),$self->{maximum},$self->{parentID},$self->{children}; } # just coords (plus my info: max, parent, children)
my $out = $self->SUPER::describe($vv,$showz);
$out = "$out My parent is " . $self->{parentID} . ". " . (scalar @{ $self->{children} } ? "My children are " . join(',',@{ $self->{children} }) . ". " : "" ) . "My maximum is " . $self->{maximum} . ".";
return $out;
}
################################# Mesh Library #################################
package Mesh; # a collection of nodes
sub new {
my ($class,$i,$n,%meta) = @_;
my $self = {
identity => ($i or -1),
members => [],
metadata => (%meta or {}),
moniker => ($n or "Unnamed"),
lastID => 0
};
bless $self, 'Mesh';
return $self;
}
sub setget {
my ($self,$op,$val) = @_;
# identity => ($i or -1),
# metadata => (%meta or {}),
if ($op == 0) {
$self->{identity} = $val if defined($val);
return $self->{identity};
} elsif ($op == 1) {
$self->{moniker} = $val if defined($val);
return $self->{moniker};
} else { # assume op is a key for the metadata hash -- give the user credit for using me properly.
unless (defined $val) { return $self->{metadata}{$op}; } # use op as key
# print "Setting metadata $key to $value.\n";
($self->{metadata}{$op} = $val) or return 1;
return 0;
}
}
sub addNode {
my ($self,$n,$x,$y,$z,$p) = @_;
# sanity check parent ID here?
my $member = Node->new(++$self->lastid,($n or "Anonymous"),$x,$y,$z,$p);
push(@{ $self->{members}},$member);
}
sub removeNode {
# find node in list of members
# find each child node in list of members
# set child's parent to grandparent
# if node is last in line, decrement {lastID}
# delete node
}
sub connectNodes {
my ($self,$parentID,$childID) = @_;
my $par = $self->getMember($parentID);
my $chi = $self->getMember($childID);
# If child has a parent, find its old parent in member list
# remove child from old parent
# set child's parent to parent
# add child to parent;s children
}
sub findPath {
}
############################### Points Library #################################
package Points;
use Common qw ( between nround getColorsbyName vary );
use List::Util qw( min );
use Math::Trig qw( tan pi acos asin );
use Math::Round qw( round );
use POSIX qw( floor );
my $debug = 1;
my $termcolors = 0;
my $basecolor = "";
my $funcolor = "";
my @cornerbearings = (0,0,0,0);
my $uid = 1; #time() % 1000; # runtime-unique ID counter for vertices
sub pointIsOnLine { # Don't remember the source of this algorithm, but it was given as a formula.
if ($debug) { print $funcolor . "pointIsOnLine($basecolor@_$funcolor)$basecolor\n"; }
my ($x0,$y0,$x1,$y1,$x2,$y2,$fuzziness,$checkrange) = @_; # point, line start, line end, max determinant
if (defined $checkrange and $checkrange == 1) {
unless (between($x0,$x1,$x2,0,$fuzziness)) { return 0; }
}
my $det = ($x2 - $x1) * ($y0 - $y1) - ($y2 - $y1) * ($x0 - $x1);
if ($debug) { print "=>$det\n"; }
return (abs($det) < ($fuzziness or 0));
}
sub findOnLine {
if ($debug > 2) { print $funcolor . "findOnLine(" . ($debug > 7 ? "$basecolor@_$funcolor" : "") . ")$basecolor\n"; }
my ($x1,$y1,$x2,$y2,$frac,$whole) = @_;
my $dx = $x1 - $x2;
my $dy = $y1 - $y2;
my $p = Vertex->new($uid++);
my $x = $x1 - ($dx * $frac);
my $y = $y1 - ($dy * $frac);
$p->move($x,$y);
if ($whole) { $p->roundLoc(0); }
return $p;
}
sub getDist {
if ($debug > 1) { print $funcolor . "getDist($basecolor@_$funcolor)$basecolor\n"; }
my ($x1,$y1,$x2,$y2,$sides) = @_; # point 1, point 2, return all distances?
my $dx = $x2 - $x1; # preserving sign for rise/run
my $dy = $y2 - $y1;
unless ($dx or $dy) { return ($sides ? (0,0,0) : 0); } # same point
# unless ($dx and $dy) { return ($dx ? $dx : $dy); } # horiz/vert line ## can't do this efficiently with sides variable
my $d = sqrt($dx**2 + $dy**2); # squaring makes values absolute
if ($debug > 1) { print "In: $x1,$y1 - $x2,$y2 -- $dy/$dx :: $d\n"; }
if ($sides) { return $d,$dy,$dx; } # dist, rise, run
return $d;
}
sub getClosest {
if ($debug > 1) { print $funcolor . "getClosest($basecolor@_$funcolor)$basecolor\n"; }
my ($ox,$oy,$ptlr,%exargs) = @_; # origin, reference of list of vertices, hash of extra arguments
my @ptlist = @$ptlr;
my $ex; my $ey;
if (defined $exargs{'exclude'}) {
my $xv = $exargs{'exclude'};
$ex = $xv->x();
$ey = $xv->y();
}
my $lowdex = 0;
my $lowdist = undef;
foreach my $i (0 .. $#ptlist) {
my ($d,$dy,$dx) = getDist($ptlist[$i]->x(),$ptlist[$i]->y(),$ox,$oy,1);
if (defined $ex and defined $ey and $ptlist[$i]->x() == $ex and $ptlist[$i]->y() == $ey) {
# do nothing # point is the excluded point
} elsif (not defined $lowdist or $d < $lowdist) {
if ($debug > 7) { print "Low: " . (defined $lowdist ? $lowdist : "undef") . "(#$lowdex) => $d (#$i) - - - $dy/$dx\n"; }
$lowdist = $d;
$lowdex = $i;
}
}
return $lowdex;
}
sub perpDist { # Algorithm source: Wikipedia/Distance_from_a_point_to_a_line
if ($debug > 1) { print $funcolor . "perpDist($basecolor@_$funcolor)$basecolor\n"; }
my ($x0,$y0,$x1,$y1,$x2,$y2) = @_; # point, line start, line end
my $dx = $x2 - $x1;
my $dy = $y2 - $y1;
return 0 if ($dx == 0 and $dy == 0);
my $d = (abs($dy*$x0 - $dx*$y0 - $x1*$y2 + $x2*$y1) / sqrt($dx**2 + $dy**2));
# print "($d)";
return $d;