plotboilerplate 1.12.10 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +5 -1
  2. package/changelog.md +50 -0
  3. package/dist/index.esm.js +999 -510
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.esm.min.js +1 -1
  6. package/dist/index.esm.min.js.map +1 -1
  7. package/dist/plotboilerplate.browser.min.js +1 -1
  8. package/dist/plotboilerplate.browser.min.js.map +1 -1
  9. package/dist/plotboilerplate.js +1013 -509
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/BezierPath.d.ts +45 -2
  13. package/src/cjs/BezierPath.js +114 -18
  14. package/src/cjs/BezierPath.js.map +1 -1
  15. package/src/cjs/Circle.d.ts +25 -10
  16. package/src/cjs/Circle.js +38 -33
  17. package/src/cjs/Circle.js.map +1 -1
  18. package/src/cjs/CircleSector.d.ts +27 -13
  19. package/src/cjs/CircleSector.js +38 -23
  20. package/src/cjs/CircleSector.js.map +1 -1
  21. package/src/cjs/CubicBezierCurve.d.ts +17 -1
  22. package/src/cjs/CubicBezierCurve.js +78 -79
  23. package/src/cjs/CubicBezierCurve.js.map +1 -1
  24. package/src/cjs/KeyHandler.d.ts +10 -3
  25. package/src/cjs/KeyHandler.js +28 -23
  26. package/src/cjs/KeyHandler.js.map +1 -1
  27. package/src/cjs/Line.d.ts +2 -1
  28. package/src/cjs/Line.js +19 -18
  29. package/src/cjs/Line.js.map +1 -1
  30. package/src/cjs/MouseHandler.js +43 -53
  31. package/src/cjs/MouseHandler.js.map +1 -1
  32. package/src/cjs/PBImage.d.ts +16 -1
  33. package/src/cjs/PBImage.js +13 -3
  34. package/src/cjs/PBImage.js.map +1 -1
  35. package/src/cjs/PBText.d.ts +130 -0
  36. package/src/cjs/PBText.js +76 -0
  37. package/src/cjs/PBText.js.map +1 -0
  38. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  39. package/src/cjs/PlotBoilerplate.js +44 -16
  40. package/src/cjs/PlotBoilerplate.js.map +1 -1
  41. package/src/cjs/Polygon.d.ts +37 -1
  42. package/src/cjs/Polygon.js +131 -54
  43. package/src/cjs/Polygon.js.map +1 -1
  44. package/src/cjs/Triangle.d.ts +16 -1
  45. package/src/cjs/Triangle.js +50 -54
  46. package/src/cjs/Triangle.js.map +1 -1
  47. package/src/cjs/VEllipse.d.ts +16 -1
  48. package/src/cjs/VEllipse.js +25 -12
  49. package/src/cjs/VEllipse.js.map +1 -1
  50. package/src/cjs/VEllipseSector.d.ts +15 -1
  51. package/src/cjs/VEllipseSector.js +11 -1
  52. package/src/cjs/VEllipseSector.js.map +1 -1
  53. package/src/cjs/Vector.d.ts +3 -1
  54. package/src/cjs/Vector.js +32 -36
  55. package/src/cjs/Vector.js.map +1 -1
  56. package/src/cjs/VertTuple.d.ts +15 -1
  57. package/src/cjs/VertTuple.js +14 -19
  58. package/src/cjs/VertTuple.js.map +1 -1
  59. package/src/cjs/Vertex.d.ts +66 -4
  60. package/src/cjs/Vertex.js +99 -17
  61. package/src/cjs/Vertex.js.map +1 -1
  62. package/src/cjs/VertexListeners.d.ts +12 -8
  63. package/src/cjs/VertexListeners.js +18 -25
  64. package/src/cjs/VertexListeners.js.map +1 -1
  65. package/src/cjs/draw.d.ts +30 -2
  66. package/src/cjs/draw.js +51 -6
  67. package/src/cjs/draw.js.map +1 -1
  68. package/src/cjs/drawgl.d.ts +29 -2
  69. package/src/cjs/drawgl.js +21 -4
  70. package/src/cjs/drawgl.js.map +1 -1
  71. package/src/cjs/drawutilssvg.d.ts +25 -7
  72. package/src/cjs/drawutilssvg.js +62 -14
  73. package/src/cjs/drawutilssvg.js.map +1 -1
  74. package/src/cjs/entry.js +1 -0
  75. package/src/cjs/index.d.ts +1 -0
  76. package/src/cjs/index.js +1 -0
  77. package/src/cjs/index.js.map +1 -1
  78. package/src/cjs/interfaces/DrawLib.d.ts +27 -6
  79. package/src/cjs/interfaces/DrawLib.js +3 -0
  80. package/src/cjs/interfaces/DrawLib.js.map +1 -1
  81. package/src/cjs/interfaces/core.d.ts +9 -1
  82. package/src/cjs/interfaces/core.js +1 -0
  83. package/src/cjs/interfaces/core.js.map +1 -1
  84. package/src/cjs/utils/creategui.js +1 -1
  85. package/src/cjs/utils/findInVertexArray.d.ts +19 -0
  86. package/src/cjs/utils/findInVertexArray.js +29 -0
  87. package/src/cjs/utils/findInVertexArray.js.map +1 -0
  88. package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +15 -0
  89. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +55 -28
  90. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  91. package/src/cjs/utils/io/DndFileHandler.d.ts +63 -0
  92. package/src/cjs/utils/io/DndFileHandler.js +112 -0
  93. package/src/cjs/utils/io/DndFileHandler.js.map +1 -0
  94. package/src/cjs/utils/io/FileDrop.d.ts +98 -0
  95. package/src/cjs/utils/io/FileDrop.js +142 -0
  96. package/src/cjs/utils/io/FileDrop.js.map +1 -0
  97. package/src/esm/BezierPath.d.ts +45 -2
  98. package/src/esm/BezierPath.js +114 -18
  99. package/src/esm/BezierPath.js.map +1 -1
  100. package/src/esm/Circle.d.ts +25 -10
  101. package/src/esm/Circle.js +38 -33
  102. package/src/esm/Circle.js.map +1 -1
  103. package/src/esm/CircleSector.d.ts +27 -13
  104. package/src/esm/CircleSector.js +38 -23
  105. package/src/esm/CircleSector.js.map +1 -1
  106. package/src/esm/CubicBezierCurve.d.ts +17 -1
  107. package/src/esm/CubicBezierCurve.js +78 -79
  108. package/src/esm/CubicBezierCurve.js.map +1 -1
  109. package/src/esm/KeyHandler.d.ts +10 -3
  110. package/src/esm/KeyHandler.js +28 -23
  111. package/src/esm/KeyHandler.js.map +1 -1
  112. package/src/esm/Line.d.ts +2 -1
  113. package/src/esm/Line.js +19 -18
  114. package/src/esm/Line.js.map +1 -1
  115. package/src/esm/MouseHandler.js +43 -53
  116. package/src/esm/MouseHandler.js.map +1 -1
  117. package/src/esm/PBImage.d.ts +16 -1
  118. package/src/esm/PBImage.js +13 -3
  119. package/src/esm/PBImage.js.map +1 -1
  120. package/src/esm/PBText.d.ts +130 -0
  121. package/src/esm/PBText.js +71 -0
  122. package/src/esm/PBText.js.map +1 -0
  123. package/src/esm/PlotBoilerplate.d.ts +2 -1
  124. package/src/esm/PlotBoilerplate.js +44 -16
  125. package/src/esm/PlotBoilerplate.js.map +1 -1
  126. package/src/esm/Polygon.d.ts +37 -1
  127. package/src/esm/Polygon.js +131 -54
  128. package/src/esm/Polygon.js.map +1 -1
  129. package/src/esm/Triangle.d.ts +16 -1
  130. package/src/esm/Triangle.js +50 -54
  131. package/src/esm/Triangle.js.map +1 -1
  132. package/src/esm/VEllipse.d.ts +16 -1
  133. package/src/esm/VEllipse.js +25 -12
  134. package/src/esm/VEllipse.js.map +1 -1
  135. package/src/esm/VEllipseSector.d.ts +15 -1
  136. package/src/esm/VEllipseSector.js +11 -1
  137. package/src/esm/VEllipseSector.js.map +1 -1
  138. package/src/esm/Vector.d.ts +3 -1
  139. package/src/esm/Vector.js +32 -36
  140. package/src/esm/Vector.js.map +1 -1
  141. package/src/esm/VertTuple.d.ts +15 -1
  142. package/src/esm/VertTuple.js +14 -19
  143. package/src/esm/VertTuple.js.map +1 -1
  144. package/src/esm/Vertex.d.ts +66 -4
  145. package/src/esm/Vertex.js +99 -17
  146. package/src/esm/Vertex.js.map +1 -1
  147. package/src/esm/VertexListeners.d.ts +12 -8
  148. package/src/esm/VertexListeners.js +18 -25
  149. package/src/esm/VertexListeners.js.map +1 -1
  150. package/src/esm/draw.d.ts +30 -2
  151. package/src/esm/draw.js +51 -6
  152. package/src/esm/draw.js.map +1 -1
  153. package/src/esm/drawgl.d.ts +29 -2
  154. package/src/esm/drawgl.js +21 -4
  155. package/src/esm/drawgl.js.map +1 -1
  156. package/src/esm/drawutilssvg.d.ts +25 -7
  157. package/src/esm/drawutilssvg.js +62 -14
  158. package/src/esm/drawutilssvg.js.map +1 -1
  159. package/src/esm/entry.js +1 -0
  160. package/src/esm/index.d.ts +1 -0
  161. package/src/esm/index.js +1 -0
  162. package/src/esm/index.js.map +1 -1
  163. package/src/esm/interfaces/DrawLib.d.ts +27 -6
  164. package/src/esm/interfaces/DrawLib.js +3 -0
  165. package/src/esm/interfaces/DrawLib.js.map +1 -1
  166. package/src/esm/interfaces/core.d.ts +9 -1
  167. package/src/esm/interfaces/core.js +1 -0
  168. package/src/esm/interfaces/core.js.map +1 -1
  169. package/src/esm/utils/findInVertexArray.d.ts +19 -0
  170. package/src/esm/utils/findInVertexArray.js +25 -0
  171. package/src/esm/utils/findInVertexArray.js.map +1 -0
  172. package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +15 -0
  173. package/src/esm/utils/helpers/BezierPathInteractionHelper.js +54 -28
  174. package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  175. package/src/esm/utils/io/DndFileHandler.d.ts +63 -0
  176. package/src/esm/utils/io/DndFileHandler.js +106 -0
  177. package/src/esm/utils/io/DndFileHandler.js.map +1 -0
  178. package/src/esm/utils/io/FileDrop.d.ts +98 -0
  179. package/src/esm/utils/io/FileDrop.js +136 -0
  180. package/src/esm/utils/io/FileDrop.js.map +1 -0
  181. package/src/ts/BezierPath.ts +133 -16
  182. package/src/ts/Circle.ts +213 -200
  183. package/src/ts/CircleSector.ts +171 -141
  184. package/src/ts/CubicBezierCurve.ts +777 -810
  185. package/src/ts/KeyHandler.ts +221 -212
  186. package/src/ts/Line.ts +77 -80
  187. package/src/ts/MouseHandler.ts +356 -368
  188. package/src/ts/PBImage.ts +81 -68
  189. package/src/ts/PBText.ts +166 -0
  190. package/src/ts/PlotBoilerplate.ts +48 -20
  191. package/src/ts/Polygon.ts +519 -451
  192. package/src/ts/Triangle.ts +484 -489
  193. package/src/ts/VEllipse.ts +36 -11
  194. package/src/ts/VEllipseSector.ts +20 -1
  195. package/src/ts/Vector.ts +164 -160
  196. package/src/ts/VertTuple.ts +333 -338
  197. package/src/ts/Vertex.ts +117 -16
  198. package/src/ts/VertexListeners.ts +248 -266
  199. package/src/ts/draw.ts +71 -8
  200. package/src/ts/drawgl.ts +48 -8
  201. package/src/ts/drawutilssvg.ts +94 -17
  202. package/src/ts/index.ts +1 -0
  203. package/src/ts/interfaces/DrawLib.ts +30 -4
  204. package/src/ts/interfaces/core.ts +9 -0
  205. package/src/ts/utils/findInVertexArray.ts +28 -0
  206. package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +61 -30
  207. package/src/ts/utils/io/FileDrop.ts +178 -0
  208. package/dist/plotboilerplate.browser.min.js.LICENSE.txt +0 -8
  209. package/src/ts/utils/algorithms/clearPolygonDuplicateVertices.ts~ +0 -5
  210. package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts~ +0 -45
  211. package/src/ts/utils/algorithms/sh-tmp.ts~ +0 -59
  212. package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts~ +0 -113
  213. package/src/ts/utils/algorithms/sutherlandHodgman.ts~ +0 -88
package/src/ts/Polygon.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  * @modified 2020-03-24 Ported this class from vanilla-JS to Typescript.
12
12
  * @modified 2020-10-30 Added the `addVertex` function.
13
13
  * @modified 2020-10-31 Added the `getVertexAt` function.
14
- * @modified 2020-11-06 Added the `move` function.
14
+ * @modified 2020-11-06 Added the `move` function.
15
15
  * @modified 2020-11-10 Added the `getBounds` function.
16
16
  * @modified 2020-11-11 Generalized `move(Vertex)` to `move(XYCoords)`.
17
17
  * @modified 2021-01-20 Added UID.
@@ -19,7 +19,11 @@
19
19
  * @modified 2021-01-29 Added the `isClockwise` function.
20
20
  * @modified 2021-01-29 Added the `area` function.
21
21
  * @modified 2021-01-29 Changed the param type for `containsVert` from Vertex to XYCoords.
22
- * @version 1.7.0
22
+ * @modified 2021-12-14 Added the `perimeter()` function.
23
+ * @modified 2021-12-16 Added the `getEvenDistributionPolygon()` function.
24
+ * @modified 2022-02-02 Added the `destroy` method.
25
+ * @modified 2022-02-02 Cleared the `Polygon.toSVGString` function (deprecated). Use `drawutilssvg` instead.
26
+ * @version 1.9.0
23
27
  *
24
28
  * @file Polygon
25
29
  * @public
@@ -31,7 +35,6 @@ import { UIDGenerator } from "./UIDGenerator";
31
35
  import { Vertex } from "./Vertex";
32
36
  import { XYCoords, SVGSerializable, UID } from "./interfaces";
33
37
 
34
-
35
38
  /**
36
39
  * @classdesc A polygon class. Any polygon consists of an array of vertices; polygons can be open or closed.
37
40
  *
@@ -44,465 +47,530 @@ import { XYCoords, SVGSerializable, UID } from "./interfaces";
44
47
  * @requires XYCoords
45
48
  */
46
49
  export class Polygon implements SVGSerializable {
47
-
48
-
49
- /**
50
- * Required to generate proper CSS classes and other class related IDs.
51
- **/
52
- readonly className : string = "Polygon";
53
-
54
-
55
- /**
56
- * The UID of this drawable object.
57
- *
58
- * @member {UID}
59
- * @memberof Polygon
60
- * @instance
61
- * @readonly
62
- */
63
- readonly uid : UID;
64
-
65
-
66
- /**
67
- * @member {Array<Vertex>}
68
- * @memberof Polygon
69
- * @type {Array<Vertex>}
70
- * @instance
71
- */
72
- vertices:Array<Vertex>;
73
-
74
-
75
- /**
76
- * @member {boolean}
77
- * @memberof Polygon
78
- * @type {boolean}
79
- * @instance
80
- */
81
- isOpen:boolean;
82
-
83
-
84
- /**
85
- * The constructor.
86
- *
87
- * @constructor
88
- * @name Polygon
89
- * @param {Vertex[]} vertices - An array of 2d vertices that shape the polygon.
90
- * @param {boolean} isOpen - Indicates if the polygon should be rendered as an open or closed shape.
91
- **/
92
- constructor( vertices?:Array<Vertex>, isOpen?:boolean ) {
93
- this.uid = UIDGenerator.next();
94
- if( typeof vertices == 'undefined' )
95
- vertices = [];
96
- this.vertices = vertices;
97
- this.isOpen = isOpen;
98
- };
99
-
100
-
101
- /**
102
- * Add a vertex to the end of the `vertices` array.
103
- *
104
- * @method addVert
105
- * @param {Vertex} vert - The vertex to add.
106
- * @instance
107
- * @memberof Polygon
108
- **/
109
- addVertex( vert:Vertex ) : void {
110
- this.vertices.push( vert );
111
- };
112
-
113
-
114
- /**
115
- * Get the polygon vertex at the given position (index).
116
- *
117
- * The index may exceed the total vertex count, and will be wrapped around then (modulo).
118
- *
119
- * For k >= 0:
120
- * - getVertexAt( vertices.length ) == getVertexAt( 0 )
121
- * - getVertexAt( vertices.length + k ) == getVertexAt( k )
122
- * - getVertexAt( -k ) == getVertexAt( vertices.length -k )
123
- *
124
- * @metho getVertexAt
125
- * @param {number} index - The index of the desired vertex.
126
- * @instance
127
- * @memberof Polygon
128
- * @return {Vertex} At the given index.
129
- **/
130
- getVertexAt( index:number ) : Vertex {
131
- if( index < 0 )
132
- return this.vertices[ this.vertices.length - (Math.abs(index)%this.vertices.length) ];
133
- else
134
- return this.vertices[ index % this.vertices.length ];
135
- };
136
-
137
-
138
- /**
139
- * Move the polygon's vertices by the given amount.
140
- *
141
- * @method move
142
- * @param {XYCoords} amount - The amount to move.
143
- * @instance
144
- * @memberof Polygon
145
- * @return {Polygon} this for chaining
146
- **/
147
- move( vert:XYCoords ) : Polygon {
148
- for( var i in this.vertices ) {
149
- this.vertices[i].add( vert );
150
- }
151
- return this;
152
- };
153
-
154
-
155
- /**
156
- * Check if the given vertex is inside this polygon.<br>
157
- * <br>
158
- * Ray-casting algorithm found at<br>
159
- * https://stackoverflow.com/questions/22521982/check-if-point-inside-a-polygon
160
- *
161
- * @method containsVert
162
- * @param {XYCoords} vert - The vertex to check. The new x-component.
163
- * @return {boolean} True if the passed vertex is inside this polygon. The polygon is considered closed.
164
- * @instance
165
- * @memberof Polygon
166
- **/
167
- containsVert( vert:XYCoords ) : boolean {
168
- // ray-casting algorithm based on
169
- // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
170
- var inside : boolean = false;
171
- for (var i = 0, j = this.vertices.length - 1; i < this.vertices.length; j = i++) {
172
- let xi : number = this.vertices[i].x, yi : number = this.vertices[i].y;
173
- let xj : number = this.vertices[j].x, yj : number = this.vertices[j].y;
174
-
175
- var intersect : boolean = ((yi > vert.y) != (yj > vert.y))
176
- && (vert.x < (xj - xi) * (vert.y - yi) / (yj - yi) + xi);
177
- if (intersect) inside = !inside;
178
- }
179
- return inside;
180
- };
181
-
182
-
50
+ /**
51
+ * Required to generate proper CSS classes and other class related IDs.
52
+ **/
53
+ readonly className: string = "Polygon";
54
+
55
+ /**
56
+ * The UID of this drawable object.
57
+ *
58
+ * @member {UID}
59
+ * @memberof Polygon
60
+ * @instance
61
+ * @readonly
62
+ */
63
+ readonly uid: UID;
64
+
65
+ /**
66
+ * @member {Array<Vertex>}
67
+ * @memberof Polygon
68
+ * @type {Array<Vertex>}
69
+ * @instance
70
+ */
71
+ vertices: Array<Vertex>;
72
+
73
+ /**
74
+ * @member {boolean}
75
+ * @memberof Polygon
76
+ * @type {boolean}
77
+ * @instance
78
+ */
79
+ isOpen: boolean;
80
+
81
+ /**
82
+ * @member isDestroyed
83
+ * @memberof Polygon
84
+ * @type {boolean}
85
+ * @instance
86
+ */
87
+ isDestroyed: boolean;
88
+
89
+ /**
90
+ * The constructor.
91
+ *
92
+ * @constructor
93
+ * @name Polygon
94
+ * @param {Vertex[]} vertices - An array of 2d vertices that shape the polygon.
95
+ * @param {boolean} isOpen - Indicates if the polygon should be rendered as an open or closed shape.
96
+ **/
97
+ constructor(vertices?: Array<Vertex>, isOpen?: boolean) {
98
+ this.uid = UIDGenerator.next();
99
+ if (typeof vertices == "undefined") vertices = [];
100
+ this.vertices = vertices;
101
+ this.isOpen = isOpen;
102
+ }
103
+
104
+ /**
105
+ * Add a vertex to the end of the `vertices` array.
106
+ *
107
+ * @method addVert
108
+ * @param {Vertex} vert - The vertex to add.
109
+ * @instance
110
+ * @memberof Polygon
111
+ **/
112
+ addVertex(vert: Vertex): void {
113
+ this.vertices.push(vert);
114
+ }
115
+
116
+ /**
117
+ * Get the polygon vertex at the given position (index).
118
+ *
119
+ * The index may exceed the total vertex count, and will be wrapped around then (modulo).
120
+ *
121
+ * For k >= 0:
122
+ * - getVertexAt( vertices.length ) == getVertexAt( 0 )
123
+ * - getVertexAt( vertices.length + k ) == getVertexAt( k )
124
+ * - getVertexAt( -k ) == getVertexAt( vertices.length -k )
125
+ *
126
+ * @metho getVertexAt
127
+ * @param {number} index - The index of the desired vertex.
128
+ * @instance
129
+ * @memberof Polygon
130
+ * @return {Vertex} At the given index.
131
+ **/
132
+ getVertexAt(index: number): Vertex {
133
+ if (index < 0) return this.vertices[this.vertices.length - (Math.abs(index) % this.vertices.length)];
134
+ else return this.vertices[index % this.vertices.length];
135
+ }
136
+
137
+ /**
138
+ * Move the polygon's vertices by the given amount.
139
+ *
140
+ * @method move
141
+ * @param {XYCoords} amount - The amount to move.
142
+ * @instance
143
+ * @memberof Polygon
144
+ * @return {Polygon} this for chaining
145
+ **/
146
+ move(vert: XYCoords): Polygon {
147
+ for (var i in this.vertices) {
148
+ this.vertices[i].add(vert);
149
+ }
150
+ return this;
151
+ }
152
+
153
+ /**
154
+ * Check if the given vertex is inside this polygon.<br>
155
+ * <br>
156
+ * Ray-casting algorithm found at<br>
157
+ * https://stackoverflow.com/questions/22521982/check-if-point-inside-a-polygon
158
+ *
159
+ * @method containsVert
160
+ * @param {XYCoords} vert - The vertex to check. The new x-component.
161
+ * @return {boolean} True if the passed vertex is inside this polygon. The polygon is considered closed.
162
+ * @instance
163
+ * @memberof Polygon
164
+ **/
165
+ containsVert(vert: XYCoords): boolean {
166
+ // ray-casting algorithm based on
167
+ // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
168
+ var inside: boolean = false;
169
+ for (var i = 0, j = this.vertices.length - 1; i < this.vertices.length; j = i++) {
170
+ let xi: number = this.vertices[i].x,
171
+ yi: number = this.vertices[i].y;
172
+ let xj: number = this.vertices[j].x,
173
+ yj: number = this.vertices[j].y;
174
+
175
+ var intersect: boolean = yi > vert.y != yj > vert.y && vert.x < ((xj - xi) * (vert.y - yi)) / (yj - yi) + xi;
176
+ if (intersect) inside = !inside;
177
+ }
178
+ return inside;
179
+ }
180
+
181
+ /**
182
+ * Calculate the area of the given polygon (unsigned).
183
+ *
184
+ * Note that this does not work for self-intersecting polygons.
185
+ *
186
+ * @method area
187
+ * @instance
188
+ * @memberof Polygon
189
+ * @return {number}
190
+ */
191
+ area(): number {
192
+ return Polygon.utils.area(this.vertices);
193
+ }
194
+
195
+ /**
196
+ * Calulate the signed polyon area by interpreting the polygon as a matrix
197
+ * and calculating its determinant.
198
+ *
199
+ * @method signedArea
200
+ * @instance
201
+ * @memberof Polygon
202
+ * @return {number}
203
+ */
204
+ signedArea(): number {
205
+ return Polygon.utils.signedArea(this.vertices);
206
+ }
207
+
208
+ /**
209
+ * Get the winding order of this polgon: clockwise or counterclockwise.
210
+ *
211
+ * @method isClockwise
212
+ * @instance
213
+ * @memberof Polygon
214
+ * @return {boolean}
215
+ */
216
+ isClockwise(): boolean {
217
+ return Polygon.utils.signedArea(this.vertices) < 0;
218
+ }
219
+
220
+ /**
221
+ * Get the perimeter of this polygon.
222
+ * The perimeter is the absolute length of the outline.
223
+ *
224
+ * If this polygon is open then the last segment (connecting the first and the
225
+ * last vertex) will be skipped.
226
+ *
227
+ * @method perimeter
228
+ * @instance
229
+ * @memberof Polygon
230
+ * @return {number}
231
+ */
232
+ perimeter(): number {
233
+ let length: number = 0;
234
+ for (var i = 1; i < this.vertices.length; i++) {
235
+ length += this.vertices[i - 1].distance(this.vertices[i]);
236
+ }
237
+ if (!this.isOpen && this.vertices.length > 1) {
238
+ length += this.vertices[0].distance(this.vertices[this.vertices.length - 1]);
239
+ }
240
+ return length;
241
+ }
242
+
243
+ /**
244
+ * Scale the polygon relative to the given center.
245
+ *
246
+ * @method scale
247
+ * @param {number} factor - The scale factor.
248
+ * @param {Vertex} center - The center of scaling.
249
+ * @return {Polygon} this, for chaining.
250
+ * @instance
251
+ * @memberof Polygon
252
+ **/
253
+ scale(factor: number, center: Vertex): Polygon {
254
+ for (var i in this.vertices) {
255
+ if (typeof this.vertices[i].scale == "function") this.vertices[i].scale(factor, center);
256
+ else console.log("There seems to be a null vertex!", this.vertices[i]);
257
+ }
258
+ return this;
259
+ }
260
+
261
+ /**
262
+ * Rotate the polygon around the given center.
263
+ *
264
+ * @method rotate
265
+ * @param {number} angle - The rotation angle.
266
+ * @param {Vertex} center - The center of rotation.
267
+ * @instance
268
+ * @memberof Polygon
269
+ * @return {Polygon} this, for chaining.
270
+ **/
271
+ rotate(angle: number, center: Vertex): Polygon {
272
+ for (var i in this.vertices) {
273
+ this.vertices[i].rotate(angle, center);
274
+ }
275
+ return this;
276
+ }
277
+
278
+ /**
279
+ * Convert this polygon into a new polygon with n evenly distributed vertices.
280
+ *
281
+ * @param {number} pointCount - Must not be negative.
282
+ */
283
+ getEvenDistributionPolygon(pointCount: number): Polygon {
284
+ if (pointCount <= 0) {
285
+ throw new Error("pointCount must be larger than zero; is " + pointCount + ".");
286
+ }
287
+ const result: Polygon = new Polygon([], this.isOpen);
288
+ if (this.vertices.length === 0) {
289
+ return result;
290
+ }
291
+
292
+ // Fetch and add the start point from the source polygon
293
+ let polygonPoint: Vertex = new Vertex(this.vertices[0]);
294
+ result.vertices.push(polygonPoint);
295
+ if (this.vertices.length === 1) {
296
+ return result;
297
+ }
298
+
299
+ const perimeter: number = this.perimeter();
300
+ const stepSize: number = perimeter / pointCount;
301
+ const n: number = this.vertices.length;
302
+
303
+ let polygonIndex: number = 1;
304
+ let nextPolygonPoint: Vertex = new Vertex(this.vertices[1]);
305
+ let segmentLength: number = polygonPoint.distance(nextPolygonPoint);
306
+ let loopMax: number = this.isOpen ? n : n + 1;
307
+ let curSegmentU: number = stepSize;
308
+ var i = 1;
309
+ while (i < pointCount && polygonIndex < loopMax) {
310
+ // Check if next eq point is inside this segment
311
+ if (curSegmentU < segmentLength) {
312
+ let newPoint: Vertex = polygonPoint.clone().lerpAbs(nextPolygonPoint, curSegmentU);
313
+ result.vertices.push(newPoint);
314
+ curSegmentU += stepSize;
315
+ i++;
316
+ } else {
317
+ polygonIndex++;
318
+ polygonPoint = nextPolygonPoint;
319
+ nextPolygonPoint = new Vertex(this.vertices[polygonIndex % n]);
320
+ curSegmentU = curSegmentU - segmentLength;
321
+ segmentLength = polygonPoint.distance(nextPolygonPoint);
322
+ }
323
+ }
324
+ return result;
325
+ }
326
+
327
+ /**
328
+ * Get the bounding box (bounds) of this polygon.
329
+ *
330
+ * @method getBounds
331
+ * @instance
332
+ * @memberof Polygon
333
+ * @return {Bounds} The rectangular bounds of this polygon.
334
+ **/
335
+ getBounds(): Bounds {
336
+ return Bounds.computeFromVertices(this.vertices);
337
+ }
338
+
339
+ /**
340
+ * Convert this polygon to a sequence of quadratic Bézier curves.<br>
341
+ * <br>
342
+ * The first vertex in the returned array is the start point.<br>
343
+ * The following sequence are pairs of control-point-and-end-point:
344
+ * <pre>startPoint, controlPoint0, pathPoint1, controlPoint1, pathPoint2, controlPoint2, ..., endPoint</pre>
345
+ *
346
+ * @method toQuadraticBezierData
347
+ * @return {Vertex[]} An array of 2d vertices that shape the quadratic Bézier curve.
348
+ * @instance
349
+ * @memberof Polygon
350
+ **/
351
+ toQuadraticBezierData(): Array<Vertex> {
352
+ if (this.vertices.length < 3) return [];
353
+ var qbezier: Array<Vertex> = [];
354
+ var cc0: Vertex = this.vertices[0];
355
+ var cc1: Vertex = this.vertices[1];
356
+ var edgeCenter: Vertex = new Vertex(cc0.x + (cc1.x - cc0.x) / 2, cc0.y + (cc1.y - cc0.y) / 2);
357
+ qbezier.push(edgeCenter);
358
+ var limit = this.isOpen ? this.vertices.length : this.vertices.length + 1;
359
+ for (var t = 1; t < limit; t++) {
360
+ cc0 = this.vertices[t % this.vertices.length];
361
+ cc1 = this.vertices[(t + 1) % this.vertices.length];
362
+ var edgeCenter: Vertex = new Vertex(cc0.x + (cc1.x - cc0.x) / 2, cc0.y + (cc1.y - cc0.y) / 2);
363
+ qbezier.push(cc0);
364
+ qbezier.push(edgeCenter);
365
+ cc0 = cc1;
366
+ }
367
+ return qbezier;
368
+ }
369
+
370
+ /**
371
+ * Convert this polygon to a quadratic bezier curve, represented as an SVG data string.
372
+ *
373
+ * @method toQuadraticBezierSVGString
374
+ * @return {string} The 'd' part for an SVG 'path' element.
375
+ * @instance
376
+ * @memberof Polygon
377
+ **/
378
+ toQuadraticBezierSVGString(): string {
379
+ var qdata: Array<Vertex> = this.toQuadraticBezierData();
380
+ if (qdata.length == 0) return "";
381
+ var buffer = ["M " + qdata[0].x + " " + qdata[0].y];
382
+ for (var i = 1; i < qdata.length; i += 2) {
383
+ buffer.push("Q " + qdata[i].x + " " + qdata[i].y + ", " + qdata[i + 1].x + " " + qdata[i + 1].y);
384
+ }
385
+ return buffer.join(" ");
386
+ }
387
+
388
+ /**
389
+ * Convert this polygon to a sequence of cubic Bézier curves.<br>
390
+ * <br>
391
+ * The first vertex in the returned array is the start point.<br>
392
+ * The following sequence are triplets of (first-control-point, secnond-control-point, end-point):<br>
393
+ * <pre>startPoint, controlPoint0_0, controlPoint1_1, pathPoint1, controlPoint1_0, controlPoint1_1, ..., endPoint</pre>
394
+ *
395
+ * @method toCubicBezierData
396
+ * @param {number=} threshold - An optional threshold (default=1.0) how strong the curve segments
397
+ * should over-/under-drive. Should be between 0.0 and 1.0 for best
398
+ * results but other values are allowed.
399
+ * @return {Vertex[]} An array of 2d vertices that shape the cubic Bézier curve.
400
+ * @instance
401
+ * @memberof Polygon
402
+ **/
403
+ toCubicBezierData(threshold: number | undefined): Array<Vertex> {
404
+ if (typeof threshold == "undefined") threshold = 1.0;
405
+
406
+ if (this.vertices.length < 3) return [];
407
+ var cbezier: Array<Vertex> = [];
408
+ var a: Vertex = this.vertices[0];
409
+ var b: Vertex = this.vertices[1];
410
+ var edgeCenter = new Vertex(a.x + (b.x - a.x) / 2, a.y + (b.y - a.y) / 2);
411
+ cbezier.push(edgeCenter);
412
+
413
+ var limit: number = this.isOpen ? this.vertices.length - 1 : this.vertices.length;
414
+ for (var t = 0; t < limit; t++) {
415
+ var a = this.vertices[t % this.vertices.length];
416
+ var b = this.vertices[(t + 1) % this.vertices.length];
417
+ var c = this.vertices[(t + 2) % this.vertices.length];
418
+
419
+ var aCenter: Vertex = new Vertex(a.x + (b.x - a.x) / 2, a.y + (b.y - a.y) / 2);
420
+ var bCenter: Vertex = new Vertex(b.x + (c.x - b.x) / 2, b.y + (c.y - b.y) / 2);
421
+
422
+ var a2: Vertex = new Vertex(aCenter.x + (b.x - aCenter.x) * threshold, aCenter.y + (b.y - aCenter.y) * threshold);
423
+ var b0: Vertex = new Vertex(bCenter.x + (b.x - bCenter.x) * threshold, bCenter.y + (b.y - bCenter.y) * threshold);
424
+
425
+ cbezier.push(a2);
426
+ cbezier.push(b0);
427
+ cbezier.push(bCenter);
428
+ }
429
+ return cbezier;
430
+ }
431
+
432
+ /**
433
+ * Convert this polygon to a cubic bezier curve, represented as an SVG data string.
434
+ *
435
+ * @method toCubicBezierSVGString
436
+ * @return {string} The 'd' part for an SVG 'path' element.
437
+ * @instance
438
+ * @memberof Polygon
439
+ **/
440
+ toCubicBezierSVGString(threshold: number): string {
441
+ var qdata: Array<Vertex> = this.toCubicBezierData(threshold);
442
+ if (qdata.length == 0) return "";
443
+ var buffer = ["M " + qdata[0].x + " " + qdata[0].y];
444
+ for (var i = 1; i < qdata.length; i += 3) {
445
+ buffer.push(
446
+ "C " +
447
+ qdata[i].x +
448
+ " " +
449
+ qdata[i].y +
450
+ ", " +
451
+ qdata[i + 1].x +
452
+ " " +
453
+ qdata[i + 1].y +
454
+ ", " +
455
+ qdata[i + 2].x +
456
+ " " +
457
+ qdata[i + 2].y
458
+ );
459
+ }
460
+ return buffer.join(" ");
461
+ }
462
+
463
+ /**
464
+ * Convert this polygon to a cubic bezier path instance.
465
+ *
466
+ * @method toCubicBezierPath
467
+ * @param {number} threshold - The threshold, usually from 0.0 to 1.0.
468
+ * @return {BezierPath} - A bezier path instance.
469
+ * @instance
470
+ * @memberof Polygon
471
+ **/
472
+ toCubicBezierPath(threshold: number): BezierPath {
473
+ var qdata: Array<Vertex> = this.toCubicBezierData(threshold);
474
+ // Conver the linear path vertices to a two-dimensional path array
475
+ var pathdata: Array<Array<Vertex>> = [];
476
+ for (var i = 0; i + 3 < qdata.length; i += 3) {
477
+ pathdata.push([qdata[i], qdata[i + 3], qdata[i + 1], qdata[i + 2]]);
478
+ }
479
+ return BezierPath.fromArray(pathdata);
480
+ }
481
+
482
+ /**
483
+ * Create an SVG representation of this polygon.
484
+ *
485
+ * @deprecated DEPRECATION Please use the drawutilssvg library and an XMLSerializer instead.
486
+ * @method toSVGString
487
+ * @param {object=} options - An optional set of options, like 'className'.
488
+ * @return {string} The SVG string.
489
+ * @instance
490
+ * @memberof Polygon
491
+ **/
492
+ toSVGString(options: { className?: string } | undefined): string {
493
+ // options = options || {};
494
+ // var buffer: Array<string> = [];
495
+ // buffer.push("<path");
496
+ // if (options.className) buffer.push(' class="' + options.className + '"');
497
+ // buffer.push(' d="');
498
+ // if (this.vertices.length > 0) {
499
+ // buffer.push("M ");
500
+ // buffer.push(this.vertices[0].x.toString());
501
+ // buffer.push(" ");
502
+ // buffer.push(this.vertices[0].y.toString());
503
+ // for (var i = 1; i < this.vertices.length; i++) {
504
+ // buffer.push(" L ");
505
+ // buffer.push(this.vertices[i].x.toString());
506
+ // buffer.push(" ");
507
+ // buffer.push(this.vertices[i].y.toString());
508
+ // }
509
+ // if (!this.isOpen) {
510
+ // buffer.push(" Z");
511
+ // }
512
+ // }
513
+ // buffer.push('" />');
514
+ // return buffer.join("");
515
+ console.warn(
516
+ "[Deprecation] Warning: the Polygon.toSVGString method is deprecated and does not return and valid SVG data any more. Please use `drawutilssvg` instead."
517
+ );
518
+ return "";
519
+ }
520
+
521
+ /**
522
+ * This function should invalidate any installed listeners and invalidate this object.
523
+ * After calling this function the object might not hold valid data any more and
524
+ * should not be used.
525
+ */
526
+ destroy() {
527
+ for (var i = 0; i < this.vertices.length; i++) {
528
+ this.vertices[i].destroy();
529
+ }
530
+ this.isDestroyed = true;
531
+ }
532
+
533
+ static utils = {
183
534
  /**
184
- * Calculate the area of the given polygon (unsigned).
535
+ * Calculate the area of the given polygon (unsigned).
185
536
  *
186
537
  * Note that this does not work for self-intersecting polygons.
187
538
  *
188
- * @method area
189
- * @instance
190
- * @memberof Polygon
539
+ * @name area
191
540
  * @return {number}
192
541
  */
193
- area() : number {
194
- return Polygon.utils.area(this.vertices);
195
- };
196
-
542
+ area(vertices: Array<XYCoords>): number {
543
+ // Found at:
544
+ // https://stackoverflow.com/questions/16285134/calculating-polygon-area
545
+ let total: number = 0.0;
546
+
547
+ for (var i = 0, l = vertices.length; i < l; i++) {
548
+ const addX = vertices[i].x;
549
+ const addY = vertices[(i + 1) % l].y;
550
+ const subX = vertices[(i + 1) % l].x;
551
+ const subY = vertices[i].y;
552
+
553
+ total += addX * addY * 0.5;
554
+ total -= subX * subY * 0.5;
555
+ }
556
+ return Math.abs(total);
557
+ },
197
558
 
198
559
  /**
199
560
  * Calulate the signed polyon area by interpreting the polygon as a matrix
200
561
  * and calculating its determinant.
201
562
  *
202
- * @method signedArea
203
- * @instance
204
- * @memberof Polygon
563
+ * @name signedArea
205
564
  * @return {number}
206
565
  */
207
- signedArea() : number {
208
- return Polygon.utils.signedArea(this.vertices)
209
- };
210
-
211
-
212
- /**
213
- * Get the winding order of this polgon: clockwise or counterclockwise.
214
- *
215
- * @method isClockwise
216
- * @instance
217
- * @memberof Polygon
218
- * @return {boolean}
219
- */
220
- isClockwise() : boolean {
221
- return Polygon.utils.signedArea(this.vertices) < 0;
222
- };
223
-
224
-
225
-
226
- /**
227
- * Scale the polygon relative to the given center.
228
- *
229
- * @method scale
230
- * @param {number} factor - The scale factor.
231
- * @param {Vertex} center - The center of scaling.
232
- * @return {Polygon} this, for chaining.
233
- * @instance
234
- * @memberof Polygon
235
- **/
236
- scale( factor:number, center:Vertex ) : Polygon {
237
- for( var i in this.vertices ) {
238
- if( typeof this.vertices[i].scale == 'function' )
239
- this.vertices[i].scale( factor, center );
240
- else
241
- console.log( 'There seems to be a null vertex!', this.vertices[i] );
242
- }
243
- return this;
244
- };
245
-
246
-
247
- /**
248
- * Rotate the polygon around the given center.
249
- *
250
- * @method rotate
251
- * @param {number} angle - The rotation angle.
252
- * @param {Vertex} center - The center of rotation.
253
- * @instance
254
- * @memberof Polygon
255
- * @return {Polygon} this, for chaining.
256
- **/
257
- rotate( angle:number, center:Vertex ) : Polygon {
258
- for( var i in this.vertices ) {
259
- this.vertices[i].rotate( angle, center );
260
- }
261
- return this;
262
- };
263
-
264
-
265
- /**
266
- * Get the bounding box (bounds) of this polygon.
267
- *
268
- * @method getBounds
269
- * @instance
270
- * @memberof Polygon
271
- * @return {Bounds} The rectangular bounds of this polygon.
272
- **/
273
- getBounds() : Bounds {
274
- return Bounds.computeFromVertices( this.vertices );
275
- };
276
-
277
-
278
- /**
279
- * Convert this polygon to a sequence of quadratic Bézier curves.<br>
280
- * <br>
281
- * The first vertex in the returned array is the start point.<br>
282
- * The following sequence are pairs of control-point-and-end-point:
283
- * <pre>startPoint, controlPoint0, pathPoint1, controlPoint1, pathPoint2, controlPoint2, ..., endPoint</pre>
284
- *
285
- * @method toQuadraticBezierData
286
- * @return {Vertex[]} An array of 2d vertices that shape the quadratic Bézier curve.
287
- * @instance
288
- * @memberof Polygon
289
- **/
290
- toQuadraticBezierData() : Array<Vertex> {
291
- if( this.vertices.length < 3 )
292
- return [];
293
- var qbezier : Array<Vertex> = [];
294
- var cc0 : Vertex = this.vertices[0];
295
- var cc1 : Vertex = this.vertices[1];
296
- var edgeCenter : Vertex = new Vertex( cc0.x + (cc1.x-cc0.x)/2,
297
- cc0.y + (cc1.y-cc0.y)/2 );
298
- qbezier.push( edgeCenter );
299
- var limit = this.isOpen ? this.vertices.length : this.vertices.length+1;
300
- for( var t = 1; t < limit; t++ ) {
301
- cc0 = this.vertices[ t%this.vertices.length ];
302
- cc1 = this.vertices[ (t+1)%this.vertices.length ];
303
- var edgeCenter : Vertex = new Vertex( cc0.x + (cc1.x-cc0.x)/2,
304
- cc0.y + (cc1.y-cc0.y)/2 );
305
- qbezier.push( cc0 );
306
- qbezier.push( edgeCenter );
307
- cc0 = cc1;
308
- }
309
- return qbezier;
310
- };
311
-
312
-
313
- /**
314
- * Convert this polygon to a quadratic bezier curve, represented as an SVG data string.
315
- *
316
- * @method toQuadraticBezierSVGString
317
- * @return {string} The 'd' part for an SVG 'path' element.
318
- * @instance
319
- * @memberof Polygon
320
- **/
321
- toQuadraticBezierSVGString() : string {
322
- var qdata : Array<Vertex> = this.toQuadraticBezierData();
323
- if( qdata.length == 0 )
324
- return "";
325
- var buffer = [ 'M ' + qdata[0].x+' '+qdata[0].y ];
326
- for( var i = 1; i < qdata.length; i+=2 ) {
327
- buffer.push( 'Q ' + qdata[i].x+' '+qdata[i].y + ', ' + qdata[i+1].x+' '+qdata[i+1].y );
328
- }
329
- return buffer.join(' ');
330
- };
331
-
332
-
333
-
334
- /**
335
- * Convert this polygon to a sequence of cubic Bézier curves.<br>
336
- * <br>
337
- * The first vertex in the returned array is the start point.<br>
338
- * The following sequence are triplets of (first-control-point, secnond-control-point, end-point):<br>
339
- * <pre>startPoint, controlPoint0_0, controlPoint1_1, pathPoint1, controlPoint1_0, controlPoint1_1, ..., endPoint</pre>
340
- *
341
- * @method toCubicBezierData
342
- * @param {number=} threshold - An optional threshold (default=1.0) how strong the curve segments
343
- * should over-/under-drive. Should be between 0.0 and 1.0 for best
344
- * results but other values are allowed.
345
- * @return {Vertex[]} An array of 2d vertices that shape the cubic Bézier curve.
346
- * @instance
347
- * @memberof Polygon
348
- **/
349
- toCubicBezierData( threshold:number|undefined ) : Array<Vertex> {
350
- if( typeof threshold == 'undefined' )
351
- threshold = 1.0;
352
-
353
- if( this.vertices.length < 3 )
354
- return [];
355
- var cbezier : Array<Vertex> = [];
356
- var a : Vertex = this.vertices[0];
357
- var b : Vertex = this.vertices[1];
358
- var edgeCenter = new Vertex( a.x + (b.x-a.x)/2, a.y + (b.y-a.y)/2 );
359
- cbezier.push( edgeCenter );
360
-
361
- var limit : number = this.isOpen ? this.vertices.length-1 : this.vertices.length;
362
- for( var t = 0; t < limit; t++ ) {
363
- var a = this.vertices[ t%this.vertices.length ];
364
- var b = this.vertices[ (t+1)%this.vertices.length ];
365
- var c = this.vertices[ (t+2)%this.vertices.length ];
366
-
367
- var aCenter : Vertex = new Vertex( a.x + (b.x-a.x)/2, a.y + (b.y-a.y)/2 );
368
- var bCenter : Vertex = new Vertex( b.x + (c.x-b.x)/2, b.y + (c.y-b.y)/2 );
369
-
370
- var a2 : Vertex = new Vertex( aCenter.x + (b.x-aCenter.x)*threshold, aCenter.y + (b.y-aCenter.y)*threshold );
371
- var b0 : Vertex = new Vertex( bCenter.x + (b.x-bCenter.x)*threshold, bCenter.y + (b.y-bCenter.y)*threshold );
372
-
373
- cbezier.push( a2 );
374
- cbezier.push( b0 );
375
- cbezier.push( bCenter );
376
-
377
- }
378
- return cbezier;
379
-
380
- };
381
-
382
-
383
-
384
- /**
385
- * Convert this polygon to a cubic bezier curve, represented as an SVG data string.
386
- *
387
- * @method toCubicBezierSVGString
388
- * @return {string} The 'd' part for an SVG 'path' element.
389
- * @instance
390
- * @memberof Polygon
391
- **/
392
- toCubicBezierSVGString( threshold:number ) : string {
393
- var qdata : Array<Vertex> = this.toCubicBezierData( threshold );
394
- if( qdata.length == 0 )
395
- return "";
396
- var buffer = [ 'M ' + qdata[0].x+' '+qdata[0].y ];
397
- for( var i = 1; i < qdata.length; i+=3 ) {
398
- buffer.push( 'C ' + qdata[i].x+' '+qdata[i].y + ', ' + qdata[i+1].x+' '+qdata[i+1].y + ', ' + qdata[i+2].x + ' ' + qdata[i+2].y );
399
- }
400
- return buffer.join(' ');
401
- };
402
-
403
-
404
-
405
- /**
406
- * Convert this polygon to a cubic bezier path instance.
407
- *
408
- * @method toCubicBezierPath
409
- * @param {number} threshold - The threshold, usually from 0.0 to 1.0.
410
- * @return {BezierPath} - A bezier path instance.
411
- * @instance
412
- * @memberof Polygon
413
- **/
414
- toCubicBezierPath( threshold:number ) : BezierPath {
415
- var qdata : Array<Vertex> = this.toCubicBezierData( threshold );
416
- // Conver the linear path vertices to a two-dimensional path array
417
- var pathdata : Array<Array<Vertex>> = [];
418
- for( var i = 0; i+3 < qdata.length; i+=3 ) {
419
- pathdata.push( [ qdata[i], qdata[i+3], qdata[i+1], qdata[i+2] ] );
420
- }
421
- return BezierPath.fromArray( pathdata );
422
- };
423
-
424
-
425
- /**
426
- * Create an SVG representation of this polygon.
427
- *
428
- * @deprecated DEPRECATION Please use the drawutilssvg library and an XMLSerializer instead.
429
- * @method toSVGString
430
- * @param {object=} options - An optional set of options, like 'className'.
431
- * @return {string} The SVG string.
432
- * @instance
433
- * @memberof Polygon
434
- **/
435
- toSVGString( options:{ className? : string }|undefined ) : string {
436
- options = options || {};
437
- var buffer : Array<string> = [];
438
- buffer.push( '<path' );
439
- if( options.className )
440
- buffer.push( ' class="' + options.className + '"' );
441
- buffer.push( ' d="' );
442
- if( this.vertices.length > 0 ) {
443
- buffer.push( 'M ' );
444
- buffer.push( this.vertices[0].x.toString() )
445
- buffer.push( ' ' );
446
- buffer.push( this.vertices[0].y.toString() );
447
- for( var i = 1; i < this.vertices.length; i++ ) {
448
- buffer.push( ' L ' );
449
- buffer.push( this.vertices[i].x.toString() )
450
- buffer.push( ' ' );
451
- buffer.push( this.vertices[i].y.toString() );
452
- }
453
- if( !this.isOpen ) {
454
- buffer.push( ' Z' );
455
- }
456
- }
457
- buffer.push( '" />' );
458
- return buffer.join('');
459
- };
460
-
461
- static utils = {
462
-
463
- /**
464
- * Calculate the area of the given polygon (unsigned).
465
- *
466
- * Note that this does not work for self-intersecting polygons.
467
- *
468
- * @name area
469
- * @return {number}
470
- */
471
- area( vertices : Array<XYCoords> ) : number {
472
- // Found at:
473
- // https://stackoverflow.com/questions/16285134/calculating-polygon-area
474
- let total : number = 0.0;
475
-
476
- for (var i = 0, l = vertices.length; i < l; i++) {
477
- const addX = vertices[i].x;
478
- const addY = vertices[(i + 1)%l].y;
479
- const subX = vertices[(i + 1)%l].x;
480
- const subY = vertices[i].y;
481
-
482
- total += (addX * addY * 0.5);
483
- total -= (subX * subY * 0.5);
484
- }
485
- return Math.abs(total);
486
- },
487
-
488
-
489
- /**
490
- * Calulate the signed polyon area by interpreting the polygon as a matrix
491
- * and calculating its determinant.
492
- *
493
- * @name signedArea
494
- * @return {number}
495
- */
496
- signedArea( vertices : Array<XYCoords> ) : number {
497
- let sum : number = 0;
498
- const n = vertices.length;
499
- for (var i = 0; i < n; i++ ) {
500
- const j = (i+1) % n;
501
- sum += (vertices[j].x - vertices[i].x) * (vertices[i].y + vertices[j].y);
502
- }
503
- return sum;
504
- }
505
-
506
- };
507
-
566
+ signedArea(vertices: Array<XYCoords>): number {
567
+ let sum: number = 0;
568
+ const n = vertices.length;
569
+ for (var i = 0; i < n; i++) {
570
+ const j = (i + 1) % n;
571
+ sum += (vertices[j].x - vertices[i].x) * (vertices[i].y + vertices[j].y);
572
+ }
573
+ return sum;
574
+ }
575
+ };
508
576
  }