-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmath2_unit.pas
More file actions
3770 lines (2661 loc) · 144 KB
/
math2_unit.pas
File metadata and controls
3770 lines (2661 loc) · 144 KB
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
(*
================================================================================
This file is part of OpenTemplot2024, a computer program for the design of model railway track.
Copyright (C) 2024 Martin Wynne. email: martin@85a.uk
This program is free software: you may redistribute it and/or modify
it under the terms of the GNU General Public Licence as published by
the Free Software Foundation, either version 3 of the Licence, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public Licence for more details.
You should have received a copy of the GNU General Public Licence
along with this program. See the file: licence.txt
Or if not, refer to the web site: https://www.gnu.org/licenses/
================================================================================
This file was saved from Delphi5
This file was derived from Templot2 version 244d
*)
unit math2_unit;
// 0.93.a maths unit for make diamond-crossing at intersection mod 228a
// and extend to meet 213b
// and tangential link 223c
// and make Y-symm 227a
{$MODE Delphi}
{$ALIGN OFF}
interface
uses
StdCtrls, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, ComCtrls, MaskEdit, FileCtrl, Math,
pad_unit;
type
Tintersect_result=record
angle:extended;
code:integer;
end;
var
intersect_cancel_clicked:boolean=False; // 227a
function do_notch_on_intersection(making_diamond,move_notch:boolean; rail_offset_control,rail_offset_bgnd:integer; top_str,next_str:string):boolean;
procedure make_diamond_crossing_at_intersection;
function notch_on_intersection(move_notch:boolean; control_rail_offset,bgnd_rail_offset:integer):integer;
function get_circle_intersections(x1,y1,r1, x2,y2,r2:extended; var qx1,qy1,k1_r1,k1_r2, qx2,qy2,k2_r1,k2_r2:extended):integer;
function check_if_inside_4_corners(p,corner1,corner2,corner3,corner4:Tpex):boolean;
function check_if_control_template_on_screen:boolean;
function extend_to_boundary(index,boundary:integer; grow,ignore_error:boolean):boolean; // 213b
procedure make_tangential_link; // 223c
function peg_on_intersection(index,bgnd_rail,control_rail:integer; bgnd_peg,allow_pegging,making_diamond:boolean):Tintersect_result; // 228a
procedure make_symm_y_turnout(len:integer); // 227a 0=short, 1=long, -1=very short
function force_symmetry(click:boolean; limit_rad:extended):boolean; // 227a set curviform and adjust radii to equal
implementation
uses
control_room, math_unit, info_unit, keep_select, bgkeeps_unit, shove_timber, alert_unit, help_sheet, grid_unit, rail_options_unit, wait_message, gaps_unit, intersect_unit;
var
dummy:extended=0;
tangential_help_str:string;
which_one:integer=1; // which_one = +1 or -1 to select which intersection
xing_angle:extended=0; // radians
x_for_notch:extended=0;
y_for_notch:extended=0;
fail_code:integer=0;
saved_rad,saved_radx,saved_rady:extended; // 226c
//______________________________________________________________________________
function check_if_control_template_on_screen:boolean;
// return True if any part of the control template enclosing rectangle will be on the screen (central 80%).
// part of intersection selector for the user.
var
ctx_max:extended;
ctx_min:extended;
cty_max:extended;
cty_min:extended;
scx_max:extended;
scx_min:extended;
scy_max:extended;
scy_min:extended;
begin
RESULT:=False; // init
// 80% screen corners around intersection...
scx_max:=x_for_notch+screenx/2.5;
scx_min:=x_for_notch-screenx/2.5;
scy_max:=y_for_notch+screeny/2.5;
scy_min:=y_for_notch-screeny/2.5;
// control template extents in 1/100th mm, convert to mm ...
ctx_max:=xy_max[0]/100; // top right corner
cty_max:=y_datum+(xy_max[1]/100);
ctx_min:=xy_min[0]/100; // bottom left corner
cty_min:=y_datum+xy_min[1]/100;
if ctx_max<scx_min then EXIT; // control template entirely to left of screen
if ctx_min>scx_max then EXIT; // control template entirely to right of screen
if cty_max<scy_min then EXIT; // control template entirely below screen
if cty_min>scy_max then EXIT; // control template entirely above screen
RESULT:=True; // must be some overlap of rectangles
end;
//______________________________________________________________________________
function calc_radial_angle(xc,yc,x,y:extended):extended;
// return all positive angles (acw), so we can do arithmetic on them easily
var
k:extended;
begin
RESULT:=0; // init
k:=0;
if ABS(x-xc)<minfp
then begin
if y>=yc // north from centre
then RESULT:=Pi/2
else RESULT:=Pi+Pi/2; // south from centre
EXIT;
end;
if ABS(y-yc)<minfp
then begin
if x>=xc // east from centre
then RESULT:=0
else RESULT:=Pi; // west from centre
EXIT;
end;
k:=ARCTAN(ABS(y-yc)/ABS(x-xc));
RESULT:=k; // north-east
if (x>xc) and (y<yc) then RESULT:=Pi*2-k; // south-east
if (x<xc) and (y>yc) then RESULT:=Pi-k; // north-west
if (x<xc) and (y<yc) then RESULT:=Pi+k; // south-west
end;
//______________________________________________________________________________
function check_if_inside_4_corners(p,corner1,corner2,corner3,corner4:Tpex):boolean;
// return True if p is inside 4-sided polygon
// (maths works for higher-sided polygon)
var
corners:array[0..3] of Tpex;
n,i:integer;
ydiff:extended;
begin
RESULT:=False; // init...
corners[0]:=corner1;
corners[1]:=corner2;
corners[2]:=corner3;
corners[3]:=corner4;
// find intersections of horizontal line stretching to the right of p with any side...
try
n:=3; // any two different corners...
for i:=0 to 3 do begin
if ((p.y>corners[i].y) and (p.y<corners[n].y)) or ((p.y>corners[n].y) and (p.y<corners[i].y))
then begin
ydiff:=corners[n].y-corners[i].y;
if ABS(ydiff)<minfp then CONTINUE; // prevent div zero - horizontal side can't intersect
if p.x<((corners[n].x-corners[i].x)*(p.y-corners[i].y)/ydiff+corners[i].x) // intersection?
then RESULT:= NOT RESULT; // needs an odd number of intersections for p to be inside
end;
n:=i; // do all 4 sides
end;
except
RESULT:=False;
end;//try
end;
//______________________________________________________________________________
function notch_on_intersection(move_notch:boolean; control_rail_offset,bgnd_rail_offset:integer):integer;
// return code:
// 2 = OK, two usable intersections
// 1 = OK, but only one intersection is usable, which_one ignored
// 0 = OK, but neither intersection is usable
// -1 = FAIL, one circle is completely outside the other
// -2 = FAIL, one circle is completely inside the other
// -3 = FAIL, circles are identical copies
// -4 = FAIL, calculation exception
// -5 = FAIL, transition template
// -6 = FAIL, slew template
// -7 = FAIL, invalid control template
// returns also the angle difference at the notch
// move_notch=False means here for calcs only, no visual change
var
saved_control:Ttemplate_info;
new_notch_data:Tnotch;
x1,y1,r1:extended;
x2,y2,r2:extended;
xi,yi,xj,yj:extended;
k1_i,k1_j,k2_i,k2_j:extended;
k:extended;
begin
RESULT:=0-4; // default fail init.
xing_angle:=0;
x_for_notch:=0;
y_for_notch:=0;
with new_notch_data do begin
notch_x:=0;
notch_y:=0;
notch_k:=0;
end;//with
if (clicked_keep_index<0) or (clicked_keep_index>(keeps_list.Count-1)) or (keeps_list.Count<1) then EXIT;
if check_control_template_is_valid('intersection')=False
then begin
RESULT:=0-7;
EXIT; // zero length
end;
if spiral=True
then begin
RESULT:=0-5;
EXIT;
end;
if slewing=True
then begin
RESULT:=0-6;
EXIT;
end;
// save the control ...
saved_control:=hold_the_control; // 227a
try // finally
try // except
// get data for current control template ...
gocalc(0,0);
x1:=rad1_orgx;
y1:=rad1_orgy;
r1:=ABS(nomrad)+control_rail_offset*g/2;
// now get the background template, and repeat...
list_position:=clicked_keep_index; // make it current in the keeps box.
copy_keep_to_current(False,False,True,False); // copy to pad.
// get data for the bgnd template ...
gocalc(0,0);
if check_control_template_is_valid('intersection')=False
then begin
RESULT:=0-7;
EXIT; // zero length
end;
if spiral=True
then begin
RESULT:=0-5;
EXIT;
end;
if slewing=True
then begin
RESULT:=0-6;
EXIT;
end;
x2:=rad1_orgx;
y2:=rad1_orgy;
r2:=ABS(nomrad)+bgnd_rail_offset*g/2; // (bgnd template may be n.g. but is now the control)
// 226c used in make diamond-crossing at intersection...
saved_rad:=ABS(nomrad);
saved_radx:=x2;
saved_rady:=y2;
// 226c end
RESULT:=get_circle_intersections(x1,y1,r1, x2,y2,r2, xi,yi,k1_i,k2_i, xj,yj,k1_j,k2_j);
if RESULT<1 then EXIT; // no usable intersections
if which_one=1 // if RESULT=1 both of these are the same...
then begin
with new_notch_data do begin
notch_x:=xi;
notch_y:=yi;
notch_k:=k1_i-Pi/2; // align with control template
end;//with
k:=ABS(k1_i-k2_i); // angle difference
end
else begin
with new_notch_data do begin
notch_x:=xj;
notch_y:=yj;
notch_k:=k1_j-Pi/2; // align with control template
end;//with
k:=ABS(k1_j-k2_j); // angle difference
end;
// return data (globals) ...
if k>Pi then k:=Pi*2-k; // adjust angle ..
if k>(Pi/2) then k:=Pi-k;
xing_angle:=ABS(k);
x_for_notch:=new_notch_data.notch_x;
y_for_notch:=new_notch_data.notch_y;
if move_notch=True
then begin
pad_form.notch_unlinked_from_current_menu_entry.Click; // cancel any moving the notch in mouse actions.
new_notch(new_notch_data,True); // new data, and link group if wanted.
redraw_pad(True,False); // no need to put this change in rollback register on redraw.
end;
except
RESULT:=0-4;
EXIT;
end;//try
finally
unhold_the_control(saved_control); // 227a restore the original control
end;//try
end;
//______________________________________________________________________________
function get_circle_intersections(x1,y1,r1, x2,y2,r2:extended; var qx1,qy1,k1_r1,k1_r2, qx2,qy2,k2_r1,k2_r2:extended):integer;
// return code:
// 2 = OK, two usable intersections
// 1 = OK, but only one intersection is usable
// 0 = OK, but neither intersection is usable
// -1 = FAIL, one circle is completely outside the other
// -2 = FAIL, one circle is completely inside the other
// -3 = FAIL, circles are identical copies
// -4 = FAIL, calculation exception
// return intersections at q1 and q2 // k is radial angle to intersection, not rail angle.
// derived from public-domain code by Tim Voght
var
a,dx,dy,d,h,rx,ry,xp,yp:extended;
limit_dim:extended;
begin
RESULT:=2; // init good result
// fail inits for returned vars ...
qx1:=0;
qy1:=0;
qy2:=0;
qx2:=0;
k1_r1:=0;
k1_r2:=0;
k2_r1:=0;
k2_r2:=0;
r1:=ABS(r1); // prevent exceptions
r2:=ABS(r2);
if (r1<scale) or (r2<scale) // sensible limits
then begin
RESULT:=0-4;
EXIT;
end;
try
if (x1=x2) and (y1=y2) and (r1=r2)
then begin
RESULT:=0-3; // circles identical
EXIT;
end;
// X and Y distances between the circle centres..
dx:=x2-x1;
dy:=y2-y1;
d:=SQRT(SQR(dy)+SQR(dx)); // diagonal distance between the centres
if d>(r1+r2) // no intersection, gap between circles
then begin
if (d-(r1+r2))<1E-9 // if by tiny amount, adjust r2 in the calcs. 1E-9 arbitrary. templates not changed.
then begin
r2:=r2+1E-9;
RESULT:=1; // only one intersect
end
else begin
RESULT:=0-1;
EXIT; // circles do not intersect
end;
end
else begin
if d<ABS(r1-r2) // one within other
then begin
if (ABS(r1-r2)-d)<1E-9 // if by tiny amount, adjust r2 in the calcs. 1E-9 arbitrary. templates not changed.
then begin
if r2>r1 then r2:=r2-1E-9
else r2:=r2+1E-9;
RESULT:=1;
end
else begin
RESULT:=0-2;
EXIT; // one circle is contained in the other
end;
end;
end;
// xp,yp is the point where the line through the circle intersection points
// crosses the line between the circle centres ...
a:=(SQR(r1)-SQR(r2)+SQR(d))/2/d; // distance point 0 to point 2
xp:=x1+(dx*a/d); // coordinates of point p
yp:=y1+(dy*a/d);
// get the distance from point p to either of the intersection points ...
if (SQR(r1)-SQR(a))<=0 // no neg
then h:=0
else h:=SQRT(SQR(r1)-SQR(a));
// get the offsets of the intersection points from point 2 ...
rx:=0-dy*h/d;
ry:=dx*h/d;
// return the intersection points q ...
qx1:=xp+rx; // 1st intersect..
qy1:=yp+ry;
qy2:=yp-ry; // 2nd intersect..
qx2:=xp-rx;
if (ABS(qx1-qx2)<1E-4) and (ABS(qy1-qy2)<1E-4) then RESULT:=1; // coincident, effectively only one intersection. 1E-4 arbitrary.
limit_dim:=max_rad_test/10; // arbitrary screen limit for sensible display
if ( (ABS(qx1)>limit_dim) or (ABS(qy1)>limit_dim) )
and ( (ABS(qx2)>limit_dim) or (ABS(qy2)>limit_dim) ) // neither can sensibly be displayed
then begin
RESULT:=0;
EXIT;
end;
if (ABS(qx1)>limit_dim) or (ABS(qy1)>limit_dim) // q1 can't sensibly be displayed
then begin
RESULT:=1;
qx1:=qx2; // so duplicate q2
qy1:=qy2;
end;
if (ABS(qx2)>limit_dim) or (ABS(qy2)>limit_dim) // q2 can't sensibly be displayed
then begin
RESULT:=1;
qx2:=qx1; // so duplicate q1
qy2:=qy1;
end;
// radial angles from centres ...
k1_r1:=calc_radial_angle(x1,y1,qx1,qy1); // to 1st intersect..
k1_r2:=calc_radial_angle(x2,y2,qx1,qy1);
k2_r1:=calc_radial_angle(x1,y1,qx2,qy2); // to 2nd intersect..
k2_r2:=calc_radial_angle(x2,y2,qx2,qy2);
except
RESULT:=0-4;
EXIT;
end;//try
end;
//______________________________________________________________________________
function do_notch_on_intersection(making_diamond,move_notch:boolean; rail_offset_control,rail_offset_bgnd:integer; top_str,next_str:string):boolean;
// rail offsets from centre-line: 0=none, 1=outer, -1=inner
// return True if the notch is now on an intersection
// 0.93.a
const
notch_intersection_help_str:string='php/109 `0Notch on Intersection`9';
var
i,code:integer;
angle_str,hd_str:string;
target_showing:boolean;
temp:extended;
begin
RESULT:=False; // init
which_one:=1; // init
i:=0; // keep compiler happy
target_showing:=False;
code:=notch_on_intersection(move_notch,rail_offset_control,rail_offset_bgnd);
if code=2 // 2 intersections found. Show firstly the one with the control template on screen.
then begin
if check_if_control_template_on_screen=False
then begin
which_one:=0-1;
code:=notch_on_intersection(move_notch,rail_offset_control,rail_offset_bgnd); // code shouldn't change.
end;
end;
if ABS(xing_angle-Pi/2)<1E-2 // 2 decimal places shown.
then angle_str:='90 degrees'
else if ABS(xing_angle)<1E-2
then angle_str:='0'
else begin
temp:=TAN(xing_angle);
if ABS(temp)>minfp then angle_str:=round_str(xing_angle*180/Pi,2)+' degrees ( 1: '+round_str(1/temp,2)+' RAM )'
else angle_str:='0';
end;
case code of
-7: EXIT; // invalid zero-length template -- already alerted in notch_on_intersection()
-6: i:=alert(6,'php/109 notch on intersection',
top_str+'Sorry, this function is not available because one or both templates contains a slew.'
+'||If the intersection is not within the slewing zone, try again after using the `0TOOLS > MAKE SPLIT >`1 menu options accordingly.'
+'||If the intersection is within the slewing zone, you may be able to perform this operation manually by moving the fixing peg along the rails (`0CTRL+F8`2 mouse action).',
'','','','more information','cancel','',4);
-5: i:=alert(6,'php/109 notch on intersection',
top_str+'Sorry, this function is not available because one or both templates contains a transition curve.'
+'||If the intersection is not within the transition zone, try again after using the TOOLS > MAKE SPLIT > menu options accordingly.'
+'||If the intersection is within the transition zone, you may be able to perform this operation manually by moving the fixing peg along the rails (CTRL+F8 mouse action).',
'','','','more information','cancel','',4);
-4: alert(2,'php/109 notch on intersection',
top_str+'Sorry, the intersect calculations have failed to produce a result',
'','','','','cancel','',0);
-3: i:=alert(6,'php/109 notch on intersection',
top_str+'There is no intersection because the control template and background template are both on the same alignment.',
'','','','more information','cancel','',4);
-2,-1: i:=alert(6,'php/109 notch on intersection',
top_str+'The control template does not intersect the background template.',
'','','','more information','cancel','',4);
0: i:=alert(3,'php/109 notch on intersection',
top_str+'The template intersections are too far off-screen to be usable.',
'','','','more information','cancel','',4);
1: begin // only one usable intersect. one or both straight templates, or very large radius
target_showing:=pad_form.show_zoom_target_menu_entry.Checked;
pad_form.show_zoom_target_menu_entry.Checked:=True; // show the zooming ring at centre
pad_form.pad_on_notch_menu_entry.Click; // centre on it
redraw_pad(False,False); // and show it
if making_diamond=True // called from make_diamond_crossing
then begin
pad_form.show_zoom_target_menu_entry.Checked:=target_showing; // reset
RESULT:=True;
end
else begin
repeat
with alert_box do begin // 205d
left_panbutton.Visible:=True;
right_panbutton.Visible:=True;
down_panbutton.Visible:=True;
up_panbutton.Visible:=True;
end;//with
alert_option2a_click_code:=1; // zoom in 205d
alert_option2b_click_code:=2; // zoom out
i:=alert(3,'php/109 notch on intersection',
top_str+next_str+'There is only one usable intersection, now shown by the position of the notch at the centre of the trackpad.'
+'||The intersection angle is '+angle_str+'.| ',
'','_2a_+ zoom in_2b_- zoom out','','more information','cancel - reset notch','continue',4);
if i=4 then alert_help(0,notch_intersection_help_str,'');
until i>4;
pad_form.show_zoom_target_menu_entry.Checked:=target_showing; // reset
if i=5 then begin
pad_form.reset_notch_menu_entry.Click;
EXIT;
end;
if i=6 then RESULT:=True;
end;
end;
2: begin // normal result, 2 curved templates ...
target_showing:=pad_form.show_zoom_target_menu_entry.Checked;
pad_form.show_zoom_target_menu_entry.Checked:=True; // show the zooming ring at centre
pad_form.pad_on_notch_menu_entry.Click; // centre on it
redraw_pad(False,False); // and show it
repeat
with alert_box do begin // 205d
left_panbutton.Visible:=True;
right_panbutton.Visible:=True;
down_panbutton.Visible:=True;
up_panbutton.Visible:=True;
end;//with
alert_option2a_click_code:=1; // zoom in 205d
alert_option2b_click_code:=2; // zoom out
if making_diamond=True
then begin
i:=alert(7,'php/109 make diamond - crossing at intersection',
'This function uses the pegging notch.'
+'||There are two places where the templates intersect, or would intersect if sufficiently extended in length. Templot needs to know which intersection is the one you want.'
+'||If the place now shown by the position of the notch (ringed in yellow) is not the place where you want to make a diamond-crossing, please click|TRY OTHER INTERSECTION.'
+'||If the trackpad appears to be blank or you do not understand what you are seeing, please click|TRY OTHER INTERSECTION.'
+'||For some explanations and diagrams, please click|MORE INFORMATION.'
+'||The intersection angle is '+angle_str+'.',
'','_2a_+ zoom in_2b_- zoom out','more information','try other intersection','cancel diamond - crossing - reset notch ','continue - make diamond - crossing at notch ',3);
end
else begin
i:=alert(4,'php/109 notch on intersection',
'There are two places where the templates intersect, or would intersect if sufficiently extended in length.'
+'||Is the notch on the required intersection, now showing at the centre of the trackpad?'
+'||The intersection angle is '+angle_str+'.| ',
'','_2a_+ zoom in_2b_- zoom out','more information','no - try other intersection instead','cancel - reset notch','yes - continue',3);
end;
case i of
3: alert_help(0,notch_intersection_help_str,'');
4: begin
which_one:=0-which_one;
notch_on_intersection(move_notch,rail_offset_control,rail_offset_bgnd); // don't need result, same dims as before
pad_form.pad_on_notch_menu_entry.Click; // centre on it
redraw_pad(False,False); // and show it
end;
5: begin
pad_form.reset_notch_menu_entry.Click;
pad_form.show_zoom_target_menu_entry.Checked:=target_showing; // reset
EXIT;
end;
6: RESULT:=True;
end;//case
until i=6;
pad_form.show_zoom_target_menu_entry.Checked:=target_showing; // reset
end;
else run_error(40);
end;//case
if i=4 then help(0,notch_intersection_help_str,'');
// extra ...
if (RESULT=True) and (plain_track=False) and (move_notch=True)
and (rail_offset_control<>0) and (rail_offset_bgnd<>0) and (ABS(TAN(xing_angle))>minfp)
then begin
if 1/ABS(TAN(xing_angle))<1.5 then EXIT;
if half_diamond=True
then hd_str:='half-diamond'
else hd_str:='turnout';
repeat
with alert_box do begin // 205d
left_panbutton.Visible:=True;
right_panbutton.Visible:=True;
down_panbutton.Visible:=True;
up_panbutton.Visible:=True;
end;//with
alert_option2a_click_code:=1; // zoom in 205d
alert_option2b_click_code:=2; // zoom out
i:=alert(3,'php/111 notch now on rail intersection',
'The pegging notch is now on the requested rail intersection.'
+'||Do you want to set the '+hd_str+' in the control template to a V-crossing angle of '+angle_str
+' and peg the V-crossing onto the notch?'
+'||The fixing peg will be moved to the FP position (`0CTRL-4`2).'
+'||The turnout-road exit will be set to a minimum (`0GEOMETRY > TURNOUT-ROAD EXIT LENGTH >`1 menu options).'
+'||Select the `0CURVIFORM`z option below unless both tracks are straight or curved to the same radius in the same direction.'
+'||rp.gif This function will work correctly only if the '+hd_str+' in the control template has been previously set to the required hand and is facing in the required direction.'
+' It may be necessary to rotate the '+hd_str+' by 180 degrees after pegging. Click the `0GEOMETRY > SHIFT/ROTATE > ROTATE 180 DEGREES`1 menu item.',
'','_2a_+ zoom in_2b_- zoom out','more information','yes please - with REGULAR V - crossing','no thanks','yes please - with CURVIFORM V - crossing',3);
if i=3 then alert_help(0,notch_intersection_help_str,'');
if i=5 then EXIT;
until i>3;
k3n:=1/ABS(TAN(xing_angle));
if i=4 then xing_type_i:=0
else xing_type_i:=1;
turnout_road_i:=3; // set minimum length to minimize mis-match
gocalc(0,0);
if peg_code<>4 // not already on main side peg angle.
then pad_form.peg_on_fp_menu_entry.Click;
gocalc(0,0);
shift_onto_notch(False,False);
redraw_pad(True,True);
end;
end;
//______________________________________________________________________________
procedure make_diamond_crossing_at_intersection; // 226c re-write...
// 228a mods
var
was_turnout:boolean;
was_half_diamond:boolean;
was_xorg:extended;
was_mvjpx:extended;
was_minendx:extended;
was_turnoutx:extended;
saved_control:Ttemplate_info;
k3n1,k3n2:extended;
k_angle:extended;
info_showing:boolean;
short_angle_help_str:string;
//////////////////////////////////////////////////////////////////////////
function diamond_calcs:boolean; // 226c re-write...
// 228a now plain track only
var
temp_control:Ttemplate_info;
dist0,dist9,xing_size:extended;
no_split:boolean;
no_split_str,split6_str,split4_str:string;
i,stored:integer;
v1_turnoutx,v2_turnoutx:extended;
org_cenx,org_ceny:extended;
min_match:extended;
thdpx1,thdpx2:extended;
hdkn1:extended;
iii:integer;
intersect_result:Tintersect_result;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function convert_to_unit_angle(k:extended):extended;
begin
try
RESULT:=ABS(1/TAN(k));
except
RESULT:=0;
end;//try
end;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function test_thdp_fit:extended; // 226c
// generated THDP to original curve centre, return difference from radius
var
thd_loc_x,thd_loc_y:extended;
thd_on_pad_x,thd_on_pad_y:extended;
dummy1,dummy2:extended;
begin
gocalc(0,0);
try
docurving(True,True,thdpx,aq25offset(thdpx,dummy1),thd_loc_x,thd_loc_y,dummy1,dummy2); // curve and transform THDP to get position on pad.
thd_on_pad_x:=thd_loc_x;
thd_on_pad_y:=thd_loc_y*hand_i+y_datum;
RESULT:=ABS(SQRT(SQR(thd_on_pad_x-saved_radx)+SQR(thd_on_pad_y-saved_rady))-saved_rad);
except
RESULT:=maxfp;
end;//try
end;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function adjust_k3n:boolean; // 226c adjust k3n until THDP closest fit to original curve ...
// 228a mods
var
k3n_mod:extended;
k3n_calc:extended;
k3n_org:extended;
proximity:extended;
proximity_old:extended;
begin
RESULT:=False; // init
k3n_org:=k3n; // in case fail
k3n_calc:=0.3; // init
k3n_mod:=0.25; // init
proximity_old:=50000; // init very large start
try
repeat
k3n_calc:=k3n_calc+k3n_mod; // first run 0.55
if (k3n_calc>100) or (k3n_calc<0.3) then EXIT; // failed, out of range
k3n:=k3n_calc;
proximity:=test_thdp_fit;
if proximity=maxfp then EXIT; // failed
if proximity>proximity_old // gone past the closest fit
then k3n_mod:=0-k3n_mod/10; // creep back again
proximity_old:=proximity;
until (ABS(proximity)<0.0001) or (ABS(k3n_mod)<0.000001); // arbitrary precision (needed for good fit)
if ABS(proximity)<0.002 then RESULT:=True; // arbitrary 1/500th mm
finally
if (RESULT=True) and (k3n_calc<200) and (k3n_calc>0.3) // arbitrary
then k3n:=k3n_calc
else begin
k3n:=k3n_org; // restore if no usable success
RESULT:=False;
end;
gocalc(0,0);
end;
end;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
begin
RESULT:=False; // init
fail_code:=0; // ok
min_match:=0.001; // arbitrary radial match
info_showing:=info_form.Showing;
wait_form.waiting_label.Caption:='fitting half-diamonds...';
wait_form.waiting_label.Width:=wait_form.Canvas.TextWidth(wait_form.waiting_label.Caption); // 205b bug fix for Wine
wait_form.wait_progressbar.Visible:=False;
info_form.Hide; // prevent flickering results
wait_form.Show;
Screen.Cursor:=crHourGlass;
Application.ProcessMessages;
try
org_cenx:=rad1_orgx; // radial centre for control template - half-diamonds must match
org_ceny:=rad1_orgy;
intersect_cancel_clicked:=False; // 236b bug-fix
intersect_result:=peg_on_intersection(clicked_keep_index,24,24,False,False,True);
if intersect_result.code=-2
then begin
fail_code:=-2; // templates not overlaid?
EXIT;
end;
k_angle:=intersect_result.angle; // 228a 24=MS centre-line
if k_angle<1E-5
then begin