forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFigures.js
More file actions
5924 lines (5409 loc) · 262 KB
/
Copy pathFigures.js
File metadata and controls
5924 lines (5409 loc) · 262 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
'use strict';
/*
* Copyright (C) 1998-2019 by Northwoods Software Corporation. All Rights Reserved.
*/
// This file holds definitions of all standard shape figures -- string values for Shape.figure.
// The following functions and variables are used throughout this file:
/**
* @constructor
* @param {string} name
* @param {number} def
* @param {number=} min defaults to zero
* @param {number=} max defaults to Infinity
* @class
* This FigureParameter class describes various properties each parameter uses in figures.
*/
function FigureParameter(name, def, min, max) {
if (min === undefined/*notpresent*/) min = 0.0;
if (max === undefined/*notpresent*/) max = Infinity;
/** @type {string} */
this._name = name;
/** @type {number} */
this._defaultValue = def;
/** @type {number} */
this._minimum = min;
/** @type {number} */
this._maximum = max;
};
// Public properties
/**
* Gets or sets the name of the figure.
* @name FigureParamater#name
* @function.
* @return {string}
*/
Object.defineProperty(FigureParameter.prototype, "name", {
get: function() { return this._name; },
set: function(val) {
if (typeof val !== "string" || val === "") throw new Error("Shape name must be a valid string.");
this._name = val;
}
});
/**
* Gets or sets the default value for the parameter.
* @name FigureParameter#defaultValue
* @function
* @return {number}
*/
Object.defineProperty(FigureParameter.prototype, "defaultValue", {
get: function() { return this._defaultValue; },
set: function(val) {
if (typeof val !== "number" || isNaN(val)) throw new Error("The default value must be a real number, not: " + val);
this._defaultValue = val;
}
});
/**
* Gets or sets the minimum value allowed for the figure parameter.
* @name FigureParameter#minimum
* @function.
* @return {number}
*/
Object.defineProperty(FigureParameter.prototype, "minimum", {
get: function() { return this._minimum; },
set: function(val) {
if (typeof val !== "number" || isNaN(val)) throw new Error("Minimum must be a real number, not: " + val);
this._minimum = val;
}
});
/**
* Gets or sets the maximum value allowed for the figure parameter.
* @name FigureParameter#maximum
* @function.
* @return {number}
*/
Object.defineProperty(FigureParameter.prototype, "maximum", {
get: function() { return this._maximum; },
set: function(val) {
if (typeof val !== "number" || isNaN(val)) throw new Error("Maximum must be a real number, not: " + val);
this._maximum = val;
}
});
go.Shape._FigureParameters = {};
/*
* This static function gets a FigureParameter for a particular figure name.
* @param {String} figurename
* @param {number} index, currently must be either 0 or 1
* @return {FigureParameter}
*/
go.Shape.getFigureParameter = function(figurename, index) {
var arr = go.Shape._FigureParameters[figurename];
if (!arr) return null;
return /** @type {FigureParmeter} */ (arr[index]);
};
/*
* This static function sets a FigureParameter for a particular figure name.
* @param {String} figurename
* @param {number} index, currently must be either 0 or 1
* @param {FigureParameter} figparam
*/
go.Shape.setFigureParameter = function(figurename, index, figparam) {
if (!(figparam instanceof FigureParameter)) throw new Error("Third argument to Shape.setFigureParameter is not FigureParameter: " + figparam);
if (figparam.defaultValue < figparam.minimum || figparam.defaultValue > figparam.maximum) throw new Error("defaultValue must be between minimum and maximum, not: " + figparam.defaultValue);
var arr = go.Shape._FigureParameters[figurename];
if (!arr) {
arr = [];
go.Shape._FigureParameters[figurename] = arr;
}
arr[index] = figparam;
};
/** @ignore */
var _CachedPoints = [];
/**
* @ignore
* @param {number} x
* @param {number} y
* @return {Point}
*/
function tempPointAt(x, y) {
var temp = _CachedPoints.pop();
if (temp === undefined) return new go.Point(x, y);
temp.x = x;
temp.y = y;
return temp;
};
/**
* @ignore
* @return {Point}
*/
function tempPoint() {
var temp = _CachedPoints.pop();
if (temp === undefined) return new go.Point();
return temp;
};
/**
* @ignore
* @param {Point} temp
*/
function freePoint(temp) {
_CachedPoints.push(temp);
};
/**
* @ignore
* @param {number} p1x
* @param {number} p1y
* @param {number} p2x
* @param {number} p2y
* @param {number} q1x
* @param {number} q1y
* @param {number} q2x
* @param {number} q2y
* @param {Point} result
* @return {Point}
*/
function getIntersection(p1x, p1y, p2x, p2y, q1x, q1y, q2x, q2y, result) {
var dx1 = p1x - p2x;
var dx2 = q1x - q2x;
var x;
var y;
if (dx1 === 0 || dx2 === 0) {
if (dx1 === 0) {
var m2 = (q1y - q2y) / dx2;
var b2 = q1y - m2 * q1x;
x = p1x;
y = m2 * x + b2;
}
else {
var m1 = (p1y - p2y) / dx1;
var b1 = p1y - m1 * p1x;
x = q1x;
y = m1 * x + b1;
}
}
else {
var m1 = (p1y - p2y) / dx1;
var m2 = (q1y - q2y) / dx2;
var b1 = p1y - m1 * p1x;
var b2 = q1y - m2 * q1x;
x = (b2 - b1) / (m1 - m2);
y = m1 * x + b1;
}
result.x = x;
result.y = y;
return result;
};
/**
* @ignore
* @param {number} startx
* @param {number} starty
* @param {number} c1x
* @param {number} c1y
* @param {number} c2x
* @param {number} c2y
* @param {number} endx
* @param {number} endy
* @pararm {number} fraction
* @param {Point} curve1cp1 // modified result control point
* @param {Point} curve1cp2 // modified result control point
* @param {Point} midpoint // modified result
* @param {Point} curve2cp1 // modified result control point
* @param {Point} curve2cp2 // modified result control point
*/
function breakUpBezier(startx, starty, c1x, c1y, c2x, c2y, endx, endy, fraction,
curve1cp1, curve1cp2, midpoint, curve2cp1, curve2cp2) {
var fo = 1 - fraction;
var so = fraction;
var m1x = (startx * fo + c1x * so);
var m1y = (starty * fo + c1y * so);
var m2x = (c1x * fo + c2x * so);
var m2y = (c1y * fo + c2y * so);
var m3x = (c2x * fo + endx * so);
var m3y = (c2y * fo + endy * so);
var m12x = (m1x * fo + m2x * so);
var m12y = (m1y * fo + m2y * so);
var m23x = (m2x * fo + m3x * so);
var m23y = (m2y * fo + m3y * so);
var m123x = (m12x * fo + m23x * so);
var m123y = (m12y * fo + m23y * so);
curve1cp1.x = m1x;
curve1cp1.y = m1y;
curve1cp2.x = m12x;
curve1cp2.y = m12y;
midpoint.x = m123x;
midpoint.y = m123y;
curve2cp1.x = m23x;
curve2cp1.y = m23y;
curve2cp2.x = m3x;
curve2cp2.y = m3y;
};
var GeneratorEllipseSpot1 = new go.Spot(0.156, 0.156);
var GeneratorEllipseSpot2 = new go.Spot(0.844, 0.844);
var KAPPA = 4 * ((Math.sqrt(2) - 1) / 3);
// PREDEFINED figures, built into the v2.0 library:
go.Shape.defineFigureGenerator("Rectangle", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
});
go.Shape.defineFigureGenerator("Square", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
geo.defaultStretch = go.GraphObject.Uniform;
return geo;
});
go.Shape.setFigureParameter("RoundedRectangle", 0, new FigureParameter("CornerRounding", 5));
go.Shape.defineFigureGenerator("RoundedRectangle", function(shape, w, h) { // predefined in 2.0
var param1 = shape ? shape.parameter1 : NaN;
if (isNaN(param1) || param1 < 0) param1 = 5; // default corner
param1 = Math.min(param1, w / 3);
param1 = Math.min(param1, h / 3);
var cpOffset = param1 * KAPPA;
var geo = new go.Geometry()
.add(new go.PathFigure(param1, 0, true)
.add(new go.PathSegment(go.PathSegment.Line, w - param1, 0))
.add(new go.PathSegment(go.PathSegment.Bezier, w, param1, w - cpOffset, 0, w, cpOffset))
.add(new go.PathSegment(go.PathSegment.Line, w, h - param1))
.add(new go.PathSegment(go.PathSegment.Bezier, w - param1, h, w, h - cpOffset, w - cpOffset, h))
.add(new go.PathSegment(go.PathSegment.Line, param1, h))
.add(new go.PathSegment(go.PathSegment.Bezier, 0, h - param1, cpOffset, h, 0, h - cpOffset))
.add(new go.PathSegment(go.PathSegment.Line, 0, param1))
.add(new go.PathSegment(go.PathSegment.Bezier, param1, 0, 0, cpOffset, cpOffset, 0).close()));
if (cpOffset > 1) {
geo.spot1 = new go.Spot(0, 0, cpOffset, cpOffset);
geo.spot2 = new go.Spot(1, 1, -cpOffset, -cpOffset);
}
return geo;
});
go.Shape.defineFigureGenerator("Border", "RoundedRectangle"); // predefined in 2.0
go.Shape.defineFigureGenerator("Ellipse", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Ellipse);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
geo.spot1 = GeneratorEllipseSpot1;
geo.spot2 = GeneratorEllipseSpot2;
return geo;
});
go.Shape.defineFigureGenerator("Circle", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Ellipse);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
geo.spot1 = GeneratorEllipseSpot1;
geo.spot2 = GeneratorEllipseSpot2;
geo.defaultStretch = go.GraphObject.Uniform;
return geo;
});
go.Shape.defineFigureGenerator("TriangleRight", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0.5 * h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()))
.setSpots(0, 0.25, 0.5, 0.75);
});
go.Shape.defineFigureGenerator("TriangleDown", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, 0.5 * w, h).close()))
.setSpots(0.25, 0, 0.75, 0.5);
});
go.Shape.defineFigureGenerator("TriangleLeft", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(w, h)
.add(new go.PathSegment(go.PathSegment.Line, 0, 0.5 * h))
.add(new go.PathSegment(go.PathSegment.Line, w, 0).close()))
.setSpots(0.5, 0.25, 1, 0.75);
});
go.Shape.defineFigureGenerator("TriangleUp", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(w, h)
.add(new go.PathSegment(go.PathSegment.Line, 0, h))
.add(new go.PathSegment(go.PathSegment.Line, 0.5 * w, 0).close()))
.setSpots(0.25, 0.5, 0.75, 1);
});
go.Shape.defineFigureGenerator("Triangle", "TriangleUp"); // predefined in 2.0
go.Shape.defineFigureGenerator("Diamond", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(0.5 * w, 0)
.add(new go.PathSegment(go.PathSegment.Line, 0, 0.5 * h))
.add(new go.PathSegment(go.PathSegment.Line, 0.5 * w, h))
.add(new go.PathSegment(go.PathSegment.Line, w, 0.5 * h).close()))
.setSpots(0.25, 0.25, 0.75, 0.75);
});
go.Shape.defineFigureGenerator("LineH", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Line);
geo.startX = 0;
geo.startY = h / 2;
geo.endX = w;
geo.endY = h / 2;
return geo;
});
go.Shape.defineFigureGenerator("LineV", function(shape, w, h) { // predefined in 2.0
var geo = new go.Geometry(go.Geometry.Line);
geo.startX = w / 2;
geo.startY = 0;
geo.endX = w / 2;
geo.endY = h;
return geo;
});
go.Shape.defineFigureGenerator("BarH", "Rectangle"); // predefined in 2.0
go.Shape.defineFigureGenerator("BarV", "Rectangle"); // predefined in 2.0
go.Shape.defineFigureGenerator("MinusLine", "LineH"); // predefined in 2.0
go.Shape.defineFigureGenerator("PlusLine", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(0, h/2, false)
.add(new go.PathSegment(go.PathSegment.Line, w, h/2))
.add(new go.PathSegment(go.PathSegment.Move, w/2, 0))
.add(new go.PathSegment(go.PathSegment.Line, w/2, h)));
});
go.Shape.defineFigureGenerator("XLine", function(shape, w, h) { // predefined in 2.0
return new go.Geometry()
.add(new go.PathFigure(0, h, false)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Move, 0, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h)));
});
// OPTIONAL figures, not predefined in the v2.0 library:
go.Shape.defineFigureGenerator("AsteriskLine", function(shape, w, h) {
var offset = .2 / Math.SQRT2;
return new go.Geometry()
.add(new go.PathFigure(offset * w, (1 - offset) * h, false)
.add(new go.PathSegment(go.PathSegment.Line, (1 - offset) * w, offset * h))
.add(new go.PathSegment(go.PathSegment.Move, offset * w, offset * h))
.add(new go.PathSegment(go.PathSegment.Line, (1 - offset) * w, (1 - offset) * h))
.add(new go.PathSegment(go.PathSegment.Move, 0, h / 2))
.add(new go.PathSegment(go.PathSegment.Line, w, h / 2))
.add(new go.PathSegment(go.PathSegment.Move, w / 2, 0))
.add(new go.PathSegment(go.PathSegment.Line, w / 2, h)));
});
go.Shape.defineFigureGenerator("CircleLine", function(shape, w, h) {
var rad = w/2;
var geo = new go.Geometry()
.add(new go.PathFigure(w, w / 2, false) // clockwise
.add(new go.PathSegment(go.PathSegment.Arc, 0, 360, rad, rad, rad, rad).close()));
geo.spot1 = GeneratorEllipseSpot1;
geo.spot2 = GeneratorEllipseSpot2;
geo.defaultStretch = go.GraphObject.Uniform;
return geo;
});
go.Shape.defineFigureGenerator("Line1", function(shape, w, h) {
var geo = new go.Geometry(go.Geometry.Line);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
});
go.Shape.defineFigureGenerator("Line2", function(shape, w, h) {
var geo = new go.Geometry(go.Geometry.Line);
geo.startX = w;
geo.startY = 0;
geo.endX = 0;
geo.endY = h;
return geo;
});
go.Shape.defineFigureGenerator("Curve1", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(0, 0, false)
.add(new go.PathSegment(go.PathSegment.Bezier, w, h, KAPPA * w, 0, w, (1 - KAPPA) * h)));
});
go.Shape.defineFigureGenerator("Curve2", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(0, 0, false)
.add(new go.PathSegment(go.PathSegment.Bezier, w, h, 0, KAPPA * h, (1 - KAPPA) * w, h)));
});
go.Shape.defineFigureGenerator("Curve3", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(w, 0, false)
.add(new go.PathSegment(go.PathSegment.Bezier, 0, h, w, KAPPA * h, KAPPA * w, h)));
});
go.Shape.defineFigureGenerator("Curve4", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(w, 0, false)
.add(new go.PathSegment(go.PathSegment.Bezier, 0, h, (1 - KAPPA) * w, 0, 0, (1 - KAPPA) * h)));
});
go.Shape.defineFigureGenerator("TriangleDownLeft", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(0, 0, true)
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()))
.setSpots(0, 0.5, 0.5, 1);
});
go.Shape.defineFigureGenerator("TriangleDownRight", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(w, 0, true)
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()))
.setSpots(0.5, 0.5, 1, 1);
});
go.Shape.defineFigureGenerator("TriangleUpLeft", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(0, 0, true)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()))
.setSpots(0, 0, 0.5, 0.5);
});
go.Shape.defineFigureGenerator("TriangleUpRight", function(shape, w, h) {
return new go.Geometry()
.add(new go.PathFigure(0, 0, true)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h).close()))
.setSpots(0.5, 0, 1, 0.5);
});
go.Shape.defineFigureGenerator("RightTriangle", "TriangleDownLeft");
go.Shape.setFigureParameter("Parallelogram1", 0, new FigureParameter("Indent", .1, -.99, .99));
go.Shape.defineFigureGenerator("Parallelogram1", function(shape, w, h) {
var param1 = shape ? shape.parameter1 : NaN; // indent's percent distance
if (isNaN(param1)) param1 = 0.1;
else if (param1 < -1) param1 = -1;
else if (param1 > 1) param1 = 1;
var indent = Math.abs(param1) * w;
if (param1 === 0) {
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
} else {
var geo = new go.Geometry();
if (param1 > 0) {
geo.add(new go.PathFigure(indent, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w - indent, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()));
} else { // param1 < 0
geo.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w - indent, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, indent, h).close()));
}
if (indent < w / 2) {
geo.setSpots(indent / w, 0, (w - indent) / w, 1);
}
return geo;
}
});
go.Shape.defineFigureGenerator("Parallelogram", "Parallelogram1"); // alias
// Parallelogram with absolutes instead of scaling
go.Shape.setFigureParameter("Parallelogram2", 0, new FigureParameter("Indent", 10, -Infinity, Infinity));
go.Shape.defineFigureGenerator("Parallelogram2", function(shape, w, h) {
var param1 = shape ? shape.parameter1 : NaN; // indent's x distance
if (isNaN(param1)) param1 = 10;
else if (param1 < -w) param1 = -w;
else if (param1 > w) param1 = w;
var indent = Math.abs(param1);
if (param1 === 0) {
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
} else {
var geo = new go.Geometry();
if (param1 > 0) {
geo.add(new go.PathFigure(indent, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w - indent, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()));
} else { // param1 < 0
geo.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w - indent, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, indent, h).close()));
}
if (indent < w / 2) {
geo.setSpots(indent / w, 0, (w - indent) / w, 1);
}
return geo;
}
});
go.Shape.setFigureParameter("Trapezoid1", 0, new FigureParameter("Indent", .2, -.99, .99));
go.Shape.defineFigureGenerator("Trapezoid1", function(shape, w, h) {
var param1 = shape ? shape.parameter1 : NaN; // indent's percent distance
if (isNaN(param1)) param1 = 0.2;
else if (param1 < 0.5) param1 = -0.5;
else if (param1 > 0.5) param1 = 0.5;
var indent = Math.abs(param1) * w;
if (param1 === 0) {
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
} else {
var geo = new go.Geometry();
if (param1 > 0) {
geo.add(new go.PathFigure(indent, 0)
.add(new go.PathSegment(go.PathSegment.Line, w - indent, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()));
} else { // param1 < 0
geo.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w - indent, h))
.add(new go.PathSegment(go.PathSegment.Line, indent, h).close()));
}
if (indent < w / 2) {
geo.setSpots(indent / w, 0, (w - indent) / w, 1);
}
return geo;
}
});
go.Shape.defineFigureGenerator("Trapezoid", "Trapezoid1"); // alias
// Trapezoid with absolutes instead of scaling
go.Shape.setFigureParameter("Trapezoid2", 0, new FigureParameter("Indent", 20, -Infinity, Infinity));
go.Shape.defineFigureGenerator("Trapezoid2", function(shape, w, h) {
var param1 = shape ? shape.parameter1 : NaN; // indent's x distance
if (isNaN(param1)) param1 = 20; // default value
else if (param1 < -w) param1 = -w / 2;
else if (param1 > w) param1 = w / 2;
var indent = Math.abs(param1);
if (param1 === 0) {
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
} else {
var geo = new go.Geometry();
if (param1 > 0) {
geo.add(new go.PathFigure(indent, 0)
.add(new go.PathSegment(go.PathSegment.Line, w - indent, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()));
} else { // param1 < 0
geo.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w - indent, h))
.add(new go.PathSegment(go.PathSegment.Line, indent, h).close()));
}
if (indent < w / 2) {
geo.setSpots(indent / w, 0, (w - indent) / w, 1);
}
return geo;
}
});
go.Shape.setFigureParameter("ManualOperation", 0, new FigureParameter("Indent", 10, -Infinity, Infinity));
go.Shape.defineFigureGenerator("ManualOperation", function(shape, w, h) {
var param1 = shape ? shape.parameter1 : NaN;
// Distance from topleft of bounding rectangle,
// in % of the total width, of the topleft corner
if (isNaN(param1)) param1 = 10; // default value
else if (param1 < -w) param1 = -w / 2;
else if (param1 > w) param1 = w / 2;
var indent = Math.abs(param1);
if (param1 === 0) {
var geo = new go.Geometry(go.Geometry.Rectangle);
geo.startX = 0;
geo.startY = 0;
geo.endX = w;
geo.endY = h;
return geo;
} else {
var geo = new go.Geometry();
if (param1 > 0) {
geo.add(new go.PathFigure(0, 0)
.add(new go.PathSegment(go.PathSegment.Line, w, 0))
.add(new go.PathSegment(go.PathSegment.Line, w - indent, h))
.add(new go.PathSegment(go.PathSegment.Line, indent, h).close()));
} else { // param1 < 0
geo.add(new go.PathFigure(indent, 0)
.add(new go.PathSegment(go.PathSegment.Line, w - indent, 0))
.add(new go.PathSegment(go.PathSegment.Line, w, h))
.add(new go.PathSegment(go.PathSegment.Line, 0, h).close()));
}
if (indent < w / 2) {
geo.setSpots(indent / w, 0, (w - indent) / w, 1);
}
return geo;
}
});
// The following functions are used by a group of regular figures that are defined below:
/** @ignore */
var _CachedArrays = [];
/**
* @ignore
* @return {Array}
*/
function tempArray() {
var temp = _CachedArrays.pop();
if (temp === undefined) return [];
return temp;
};
/**
* @ignore
* @param {Array} a
*/
function freeArray(a) {
a.length = 0; // clear any references to objects
_CachedArrays.push(a);
};
/**
* @ignore
* This allocates a temporary Array that should be freeArray()'ed by the caller.
* @param {number} sides
* @return {Array}
*/
function createPolygon(sides) {
// Point[] points = new Point[sides + 1];
var points = tempArray();
var radius = .5;
var center = .5;
var offsetAngle = Math.PI * 1.5;
var angle = 0;
// Loop through each side of the polygon
for (var i = 0; i < sides; i++) {
angle = 2 * Math.PI / sides * i + offsetAngle;
points[i] = new go.Point((center + radius * Math.cos(angle)), (center + radius * Math.sin(angle)));
}
// Add the last line
// points[points.length - 1] = points[0];
points.push(points[0]);
return points;
};
/**
* @ignore
* This allocates a temporary Array that should be freeArray()'ed by the caller.
* @param {number} points
* @return {Array}
*/
function createBurst(points) {
var star = createStar(points);
var pts = tempArray(); // new Point[points * 3 + 1];
pts[0] = star[0];
for (var i = 1, count = 1; i < star.length; i += 2, count += 3) {
pts[count] = star[i];
pts[count + 1] = star[i];
pts[count + 2] = star[i + 1];
}
freeArray(star);
return pts;
};
/**
* @ignore
* This allocates a temporary Array that should be freeArray()'ed by the caller.
* @param {number} points
* @return {Array}
*/
function createStar(points) {
// First, create a regular polygon
var polygon = createPolygon(points);
// Calculate the points inbetween
var pts = tempArray(); // new Point[points * 2 + 1];
var half = Math.floor(polygon.length / 2);
var count = polygon.length - 1;
var offset = (points % 2 === 0) ? 2 : 1;
for (var i = 0; i < count; i++) {
// Get the intersection of two lines
var p0 = polygon[i];
var p1 = polygon[i + 1];
var q21 = polygon[(half + i - 1) % count];
var q2off = polygon[(half + i + offset) % count];
pts[i * 2] = p0;
pts[i * 2 + 1] = getIntersection(p0.x, p0.y,
q21.x, q21.y,
p1.x, p1.y,
q2off.x, q2off.y, new go.Point()); // ?? not currently managed
}
pts[pts.length] = pts[0];
freeArray(polygon);
return pts;
};
go.Shape.defineFigureGenerator("Pentagon", function(shape, w, h) {
var points = createPolygon(5);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 5; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.2, .22);
geo.spot2 = new go.Spot(.8, .9);
return geo;
});
go.Shape.defineFigureGenerator("Hexagon", function(shape, w, h) {
var points = createPolygon(6);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 6; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.07, .25);
geo.spot2 = new go.Spot(.93, .75);
return geo;
});
go.Shape.defineFigureGenerator("Heptagon", function(shape, w, h) {
var points = createPolygon(7);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 7; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.2, .15);
geo.spot2 = new go.Spot(.8, .85);
return geo;
});
go.Shape.defineFigureGenerator("Octagon", function(shape, w, h) {
var points = createPolygon(8);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 8; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.15, .15);
geo.spot2 = new go.Spot(.85, .85);
return geo;
});
go.Shape.defineFigureGenerator("Nonagon", function(shape, w, h) {
var points = createPolygon(9);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 9; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.17, .13);
geo.spot2 = new go.Spot(.82, .82);
return geo;
});
go.Shape.defineFigureGenerator("Decagon", function(shape, w, h) {
var points = createPolygon(10);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 10; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.16, .16);
geo.spot2 = new go.Spot(.84, .84);
return geo;
});
go.Shape.defineFigureGenerator("Dodecagon", function(shape, w, h) {
var points = createPolygon(12);
var geo = new go.Geometry();
var fig = new go.PathFigure(points[0].x * w, points[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 12; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, points[i].x * w, points[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, points[0].x * w, points[0].y * h).close());
freeArray(points);
geo.spot1 = new go.Spot(.16, .16);
geo.spot2 = new go.Spot(.84, .84);
return geo;
});
go.Shape.defineFigureGenerator("FivePointedStar", function(shape, w, h) {
var starPoints = createStar(5);
var geo = new go.Geometry();
var fig = new go.PathFigure(starPoints[0].x * w, starPoints[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 10; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[i].x * w, starPoints[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[0].x * w, starPoints[0].y * h).close());
freeArray(starPoints);
geo.spot1 = new go.Spot(.266, .333);
geo.spot2 = new go.Spot(.733, .733);
return geo;
});
go.Shape.defineFigureGenerator("SixPointedStar", function(shape, w, h) {
var starPoints = createStar(6);
var geo = new go.Geometry();
var fig = new go.PathFigure(starPoints[0].x * w, starPoints[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 12; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[i].x * w, starPoints[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[0].x * w, starPoints[0].y * h).close());
freeArray(starPoints);
geo.spot1 = new go.Spot(.17, .25);
geo.spot2 = new go.Spot(.83, .75);
return geo;
});
go.Shape.defineFigureGenerator("SevenPointedStar", function(shape, w, h) {
var starPoints = createStar(7);
var geo = new go.Geometry();
var fig = new go.PathFigure(starPoints[0].x * w, starPoints[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 14; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[i].x * w, starPoints[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[0].x * w, starPoints[0].y * h).close());
freeArray(starPoints);
geo.spot1 = new go.Spot(.222, .277);
geo.spot2 = new go.Spot(.777, .666);
return geo;
});
go.Shape.defineFigureGenerator("EightPointedStar", function(shape, w, h) {
var starPoints = createStar(8);
var geo = new go.Geometry();
var fig = new go.PathFigure(starPoints[0].x * w, starPoints[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 16; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[i].x * w, starPoints[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[0].x * w, starPoints[0].y * h).close());
freeArray(starPoints);
geo.spot1 = new go.Spot(.25, .25);
geo.spot2 = new go.Spot(.75, .75);
return geo;
});
go.Shape.defineFigureGenerator("NinePointedStar", function(shape, w, h) {
var starPoints = createStar(9);
var geo = new go.Geometry();
var fig = new go.PathFigure(starPoints[0].x * w, starPoints[0].y * h, true);
geo.add(fig);
for (var i = 1; i < 18; i++) {
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[i].x * w, starPoints[i].y * h));
}
fig.add(new go.PathSegment(go.PathSegment.Line, starPoints[0].x * w, starPoints[0].y * h).close());
freeArray(starPoints);
geo.spot1 = new go.Spot(.222, .277);
geo.spot2 = new go.Spot(.777, .666);
return geo;
});