geomerative 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/LICENSE +662 -0
  4. data/LICENSE.md +9 -0
  5. data/README.md +2 -0
  6. data/Rakefile +37 -0
  7. data/examples/README.md +7 -0
  8. data/examples/data/FreeSans.ttf +0 -0
  9. data/examples/data/ReplicaBold.ttf +0 -0
  10. data/examples/data/bot1.svg +160 -0
  11. data/examples/hello_svg_to_pdf.rb +26 -0
  12. data/examples/hello_world.rb +23 -0
  13. data/examples/physics_type.rb +77 -0
  14. data/examples/rotate_first_letter.rb +28 -0
  15. data/geomerative.gemspec +32 -0
  16. data/lib/geomerative.rb +12 -0
  17. data/lib/geomerative/version.rb +3 -0
  18. data/pom.xml +110 -0
  19. data/src/geomerative/FastRClip.java +2715 -0
  20. data/src/geomerative/RClip.java +2892 -0
  21. data/src/geomerative/RClosest.java +64 -0
  22. data/src/geomerative/RCommand.java +1941 -0
  23. data/src/geomerative/RContour.java +348 -0
  24. data/src/geomerative/RFont.java +583 -0
  25. data/src/geomerative/RG.java +753 -0
  26. data/src/geomerative/RGeomElem.java +1075 -0
  27. data/src/geomerative/RGroup.java +888 -0
  28. data/src/geomerative/RMatrix.java +401 -0
  29. data/src/geomerative/RMesh.java +420 -0
  30. data/src/geomerative/RPath.java +1095 -0
  31. data/src/geomerative/RPoint.java +419 -0
  32. data/src/geomerative/RPolygon.java +1110 -0
  33. data/src/geomerative/RRectangle.java +91 -0
  34. data/src/geomerative/RSVG.java +976 -0
  35. data/src/geomerative/RShape.java +2045 -0
  36. data/src/geomerative/RStrip.java +221 -0
  37. data/src/geomerative/RStyle.java +469 -0
  38. data/src/org/apache/batik/svggen/font/.SVGFont.java.swp +0 -0
  39. data/src/org/apache/batik/svggen/font/Font.java +188 -0
  40. data/src/org/apache/batik/svggen/font/Glyph.java +113 -0
  41. data/src/org/apache/batik/svggen/font/Messages.java.bak +72 -0
  42. data/src/org/apache/batik/svggen/font/Point.java +38 -0
  43. data/src/org/apache/batik/svggen/font/RandomAccessFileEmulator.java +15 -0
  44. data/src/org/apache/batik/svggen/font/table/ClassDef.java +42 -0
  45. data/src/org/apache/batik/svggen/font/table/ClassDefFormat1.java +55 -0
  46. data/src/org/apache/batik/svggen/font/table/ClassDefFormat2.java +49 -0
  47. data/src/org/apache/batik/svggen/font/table/CmapFormat.java +81 -0
  48. data/src/org/apache/batik/svggen/font/table/CmapFormat0.java +60 -0
  49. data/src/org/apache/batik/svggen/font/table/CmapFormat2.java +48 -0
  50. data/src/org/apache/batik/svggen/font/table/CmapFormat4.java +147 -0
  51. data/src/org/apache/batik/svggen/font/table/CmapFormat6.java +60 -0
  52. data/src/org/apache/batik/svggen/font/table/CmapIndexEntry.java +84 -0
  53. data/src/org/apache/batik/svggen/font/table/CmapTable.java +87 -0
  54. data/src/org/apache/batik/svggen/font/table/Coverage.java +50 -0
  55. data/src/org/apache/batik/svggen/font/table/CoverageFormat1.java +59 -0
  56. data/src/org/apache/batik/svggen/font/table/CoverageFormat2.java +56 -0
  57. data/src/org/apache/batik/svggen/font/table/CvtTable.java +48 -0
  58. data/src/org/apache/batik/svggen/font/table/Device.java +63 -0
  59. data/src/org/apache/batik/svggen/font/table/DirectoryEntry.java +73 -0
  60. data/src/org/apache/batik/svggen/font/table/Feature.java +56 -0
  61. data/src/org/apache/batik/svggen/font/table/FeatureList.java +70 -0
  62. data/src/org/apache/batik/svggen/font/table/FeatureRecord.java +52 -0
  63. data/src/org/apache/batik/svggen/font/table/FeatureTags.java +30 -0
  64. data/src/org/apache/batik/svggen/font/table/FpgmTable.java +38 -0
  65. data/src/org/apache/batik/svggen/font/table/GlyfCompositeComp.java +165 -0
  66. data/src/org/apache/batik/svggen/font/table/GlyfCompositeDescript.java +160 -0
  67. data/src/org/apache/batik/svggen/font/table/GlyfDescript.java +79 -0
  68. data/src/org/apache/batik/svggen/font/table/GlyfSimpleDescript.java +155 -0
  69. data/src/org/apache/batik/svggen/font/table/GlyfTable.java +111 -0
  70. data/src/org/apache/batik/svggen/font/table/GlyphDescription.java +39 -0
  71. data/src/org/apache/batik/svggen/font/table/GposTable.java +80 -0
  72. data/src/org/apache/batik/svggen/font/table/GsubTable.java +118 -0
  73. data/src/org/apache/batik/svggen/font/table/HeadTable.java +159 -0
  74. data/src/org/apache/batik/svggen/font/table/HheaTable.java +109 -0
  75. data/src/org/apache/batik/svggen/font/table/HmtxTable.java +99 -0
  76. data/src/org/apache/batik/svggen/font/table/KernSubtable.java +58 -0
  77. data/src/org/apache/batik/svggen/font/table/KernSubtableFormat0.java +65 -0
  78. data/src/org/apache/batik/svggen/font/table/KernSubtableFormat2.java +56 -0
  79. data/src/org/apache/batik/svggen/font/table/KernTable.java +64 -0
  80. data/src/org/apache/batik/svggen/font/table/KerningPair.java +53 -0
  81. data/src/org/apache/batik/svggen/font/table/LangSys.java +58 -0
  82. data/src/org/apache/batik/svggen/font/table/LangSysRecord.java +52 -0
  83. data/src/org/apache/batik/svggen/font/table/Ligature.java +57 -0
  84. data/src/org/apache/batik/svggen/font/table/LigatureSet.java +55 -0
  85. data/src/org/apache/batik/svggen/font/table/LigatureSubst.java +40 -0
  86. data/src/org/apache/batik/svggen/font/table/LigatureSubstFormat1.java +63 -0
  87. data/src/org/apache/batik/svggen/font/table/LocaTable.java +72 -0
  88. data/src/org/apache/batik/svggen/font/table/Lookup.java +77 -0
  89. data/src/org/apache/batik/svggen/font/table/LookupList.java +68 -0
  90. data/src/org/apache/batik/svggen/font/table/LookupSubtable.java +27 -0
  91. data/src/org/apache/batik/svggen/font/table/LookupSubtableFactory.java +31 -0
  92. data/src/org/apache/batik/svggen/font/table/MaxpTable.java +124 -0
  93. data/src/org/apache/batik/svggen/font/table/NameRecord.java +98 -0
  94. data/src/org/apache/batik/svggen/font/table/NameTable.java +67 -0
  95. data/src/org/apache/batik/svggen/font/table/Os2Table.java +232 -0
  96. data/src/org/apache/batik/svggen/font/table/Panose.java +108 -0
  97. data/src/org/apache/batik/svggen/font/table/PostTable.java +379 -0
  98. data/src/org/apache/batik/svggen/font/table/PrepTable.java +38 -0
  99. data/src/org/apache/batik/svggen/font/table/Program.java +49 -0
  100. data/src/org/apache/batik/svggen/font/table/RangeRecord.java +57 -0
  101. data/src/org/apache/batik/svggen/font/table/Script.java +72 -0
  102. data/src/org/apache/batik/svggen/font/table/ScriptList.java +78 -0
  103. data/src/org/apache/batik/svggen/font/table/ScriptRecord.java +52 -0
  104. data/src/org/apache/batik/svggen/font/table/ScriptTags.java +28 -0
  105. data/src/org/apache/batik/svggen/font/table/SingleSubst.java +47 -0
  106. data/src/org/apache/batik/svggen/font/table/SingleSubstFormat1.java +67 -0
  107. data/src/org/apache/batik/svggen/font/table/SingleSubstFormat2.java +67 -0
  108. data/src/org/apache/batik/svggen/font/table/Table.java +204 -0
  109. data/src/org/apache/batik/svggen/font/table/TableDirectory.java +94 -0
  110. data/src/org/apache/batik/svggen/font/table/TableFactory.java +121 -0
  111. metadata +206 -0
@@ -0,0 +1,1075 @@
1
+ /**
2
+ * Copyright 2004-2008 Ricard Marxer <email@ricardmarxer.com>
3
+ *
4
+ This file is part of Geomerative.
5
+ *
6
+ * Geomerative is free software: you can redistribute it and/or modify it under
7
+ * the terms of the GNU General Public License as published by the Free Software
8
+ * Foundation, either version 3 of the License, or (at your option) any later
9
+ * version.
10
+ *
11
+ * Geomerative is distributed in the hope that it will be useful, but WITHOUT
12
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ * details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along with
17
+ * Geomerative. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ package geomerative;
20
+
21
+ import processing.core.*;
22
+
23
+ /**
24
+ * RGeomElem is an interface to any geometric element that can be drawn and
25
+ * transformed, such as shapes, polygons or meshes.
26
+ *
27
+ * @extended
28
+ */
29
+ public abstract class RGeomElem {
30
+
31
+ /**
32
+ * @invisible
33
+ */
34
+ public static final int SHAPE = 0;
35
+ /**
36
+ * @invisible
37
+ */
38
+ public static final int SUBSHAPE = 1;
39
+ /**
40
+ * @invisible
41
+ */
42
+ public static final int COMMAND = 2;
43
+
44
+ /**
45
+ * @invisible
46
+ */
47
+ public static final int POLYGON = 3;
48
+ /**
49
+ * @invisible
50
+ */
51
+ public static final int CONTOUR = 4;
52
+
53
+ /**
54
+ * @invisible
55
+ */
56
+ public static final int MESH = 5;
57
+ /**
58
+ * @invisible
59
+ */
60
+ public static final int TRISTRIP = 6;
61
+
62
+ /**
63
+ * @invisible
64
+ */
65
+ public static final int GROUP = 7;
66
+
67
+ /**
68
+ * @invisible
69
+ */
70
+ public static final int UNKNOWN = 8;
71
+
72
+ /**
73
+ * Shape document width.
74
+ */
75
+ public float width;
76
+
77
+ /**
78
+ * Shape document height.
79
+ */
80
+ public float height;
81
+
82
+ float origWidth;
83
+ float origHeight;
84
+
85
+ // Functions dependent of the type of element
86
+ // They must be overrided
87
+ public abstract void draw(PGraphics g);
88
+
89
+ public abstract void draw(PApplet g);
90
+
91
+ public void draw() {
92
+ this.draw(RG.parent());
93
+ }
94
+
95
+ public abstract RPoint getPoint(float t);
96
+
97
+ public abstract RPoint getTangent(float t);
98
+
99
+ public abstract RPoint[] getHandles();
100
+
101
+ public abstract RPoint[] getPoints();
102
+
103
+ public abstract RPoint[] getTangents();
104
+
105
+ public abstract RPoint[][] getHandlesInPaths();
106
+
107
+ public abstract RPoint[][] getPointsInPaths();
108
+
109
+ public abstract RPoint[][] getTangentsInPaths();
110
+
111
+ public abstract boolean contains(RPoint p);
112
+
113
+ /**
114
+ * Use this method to test if the shape contains all the points of another
115
+ * shape.
116
+ *
117
+ * @param shp
118
+ * @eexample contains
119
+ * @return boolean, true if the shape contains all the points of the other
120
+ * shape
121
+ * @related containsBounds ( )
122
+ * @related containsHandles ( )
123
+ */
124
+ public boolean contains(RGeomElem shp) {
125
+ return contains(shp.getPoints());
126
+ }
127
+
128
+ /**
129
+ * Use this method to test if the shape contains the bounding box of another
130
+ * shape.
131
+ *
132
+ * @param shp
133
+ * @eexample contains
134
+ * @return boolean, true if the shape contains the bounding box of the other
135
+ * shape
136
+ * @related contains ( )
137
+ * @related containsHandles ( )
138
+ */
139
+ public boolean containsBounds(RGeomElem shp) {
140
+ RPoint tl = shp.getTopLeft();
141
+ RPoint tr = shp.getTopRight();
142
+ RPoint bl = shp.getBottomRight();
143
+ RPoint br = shp.getBottomLeft();
144
+
145
+ if (this.contains(tl)
146
+ && this.contains(tr)
147
+ && this.contains(bl)
148
+ && this.contains(br)) {
149
+
150
+ return true;
151
+ }
152
+
153
+ return false;
154
+ }
155
+
156
+ /**
157
+ * Use this method to test if the shape contains the handles of another
158
+ * shape. This method is faster than contains(), but the results might not
159
+ * be perfect.
160
+ *
161
+ * @param shp
162
+ * @eexample contains
163
+ * @return boolean, true if the shape contains all the handles of the other
164
+ * shape
165
+ * @related containsBounds ( )
166
+ * @related contains ( )
167
+ */
168
+ public boolean containsHandles(RGeomElem shp) {
169
+ return contains(shp.getHandles());
170
+ }
171
+
172
+ /**
173
+ * Use this method to test if the shape contains an array of points.
174
+ *
175
+ * @param ps
176
+ * @eexample contains
177
+ * @return boolean, true if the shape contains all the points
178
+ * @related contains ( )
179
+ * @related containsBounds ( )
180
+ * @related containsHandles ( )
181
+ */
182
+ public boolean contains(RPoint[] ps) {
183
+ boolean contains = false;
184
+ if (ps != null) {
185
+ for (RPoint p : ps) {
186
+ contains &= this.contains(p);
187
+ }
188
+ }
189
+ return contains;
190
+ }
191
+
192
+ /**
193
+ * Use this method to test if the shape intersects another shape.
194
+ *
195
+ * @param shp
196
+ * @eexample intersects
197
+ * @return boolean, true if the shape intersects all the points of the other
198
+ * shape
199
+ * @related intersectsBounds ( )
200
+ * @related intersectsHandles ( )
201
+ */
202
+ public boolean intersects(RGeomElem shp) {
203
+ return intersects(shp.getPoints());
204
+ }
205
+
206
+ /**
207
+ * Use this method to test if the shape intersects the bounding box of
208
+ * another shape.
209
+ *
210
+ * @param shp
211
+ * @eexample intersects
212
+ * @return boolean, true if the shape intersects the bounding box of the
213
+ * other shape
214
+ * @related intersects ( )
215
+ * @related intersectsHandles ( )
216
+ */
217
+ public boolean intersectsBounds(RGeomElem shp) {
218
+ RPoint tl = shp.getTopLeft();
219
+ RPoint tr = shp.getTopRight();
220
+ RPoint bl = shp.getBottomRight();
221
+ RPoint br = shp.getBottomLeft();
222
+ return (this.contains(tl)
223
+ || this.contains(tr)
224
+ || this.contains(bl)
225
+ || this.contains(br));
226
+ }
227
+
228
+ /**
229
+ * Use this method to test if the shape intersects the handles of another
230
+ * shape. This method is faster than intersects(), but the results might not
231
+ * be perfect.
232
+ *
233
+ * @param shp
234
+ * @eexample intersects
235
+ * @return boolean, true if the shape intersects all the handles of the
236
+ * other shape
237
+ * @related intersectsBounds ( )
238
+ * @related intersects ( )
239
+ */
240
+ public boolean intersectsHandles(RGeomElem shp) {
241
+ return intersects(shp.getHandles());
242
+ }
243
+
244
+ /**
245
+ * Use this method to test if the shape intersects an array of points.
246
+ *
247
+ * @param ps
248
+ * @eexample intersects
249
+ * @return boolean, true if the shape intersects all the points
250
+ * @related intersects ( )
251
+ * @related intersectsBounds ( )
252
+ * @related intersectsHandles ( )
253
+ */
254
+ public boolean intersects(RPoint[] ps) {
255
+ boolean intersects = false;
256
+ if (ps != null) {
257
+ for (RPoint p : ps) {
258
+ intersects |= this.contains(p);
259
+ }
260
+ }
261
+ return intersects;
262
+ }
263
+
264
+ public abstract int getType();
265
+
266
+ //public abstract RMesh toMesh();
267
+ //public abstract RPolygon toPolygon();
268
+ public abstract RShape toShape();
269
+
270
+ public void print() {
271
+ }
272
+ ;
273
+
274
+ protected float[] lenCurves;
275
+ protected float lenCurve = -1F;
276
+
277
+ public String name = "";
278
+
279
+ protected RStyle style = new RStyle();
280
+
281
+ public void setFill(boolean _fill) {
282
+ style.setFill(_fill);
283
+ }
284
+
285
+ public void setFill(int _fillColor) {
286
+ style.setFill(_fillColor);
287
+ }
288
+
289
+ public void setFill(String str) {
290
+ style.setFill(str);
291
+ }
292
+
293
+ public void setStroke(boolean _stroke) {
294
+ style.setStroke(_stroke);
295
+ }
296
+
297
+ public void setStroke(int _strokeColor) {
298
+ style.setStroke(_strokeColor);
299
+ }
300
+
301
+ public void setStroke(String str) {
302
+ style.setStroke(str);
303
+ }
304
+
305
+ public void setStrokeWeight(float value) {
306
+ style.setStrokeWeight(value);
307
+ }
308
+
309
+ public void setStrokeWeight(String str) {
310
+ style.setStrokeWeight(str);
311
+ }
312
+
313
+ public void setStrokeCap(String str) {
314
+ style.setStrokeCap(str);
315
+ }
316
+
317
+ public void setStrokeJoin(String str) {
318
+ style.setStrokeJoin(str);
319
+ }
320
+
321
+ public void setStrokeAlpha(int opacity) {
322
+ style.setStrokeAlpha(opacity);
323
+ }
324
+
325
+ public void setStrokeAlpha(String str) {
326
+ style.setStrokeAlpha(str);
327
+ }
328
+
329
+ public void setFillAlpha(int opacity) {
330
+ style.setFillAlpha(opacity);
331
+ }
332
+
333
+ public void setFillAlpha(String str) {
334
+ style.setFillAlpha(str);
335
+ }
336
+
337
+ public void setAlpha(float opacity) {
338
+ style.setAlpha(opacity);
339
+ }
340
+
341
+ public void setAlpha(int opacity) {
342
+ style.setAlpha(opacity);
343
+ }
344
+
345
+ public void setAlpha(String str) {
346
+ style.setAlpha(str);
347
+ }
348
+
349
+ public RStyle getStyle() {
350
+ return this.style;
351
+ }
352
+
353
+ protected void saveContext(PGraphics g) {
354
+ style.saveContext(g);
355
+ }
356
+
357
+ protected void saveContext(PApplet p) {
358
+ style.saveContext(p);
359
+ }
360
+
361
+ protected void saveContext() {
362
+ style.saveContext();
363
+ }
364
+
365
+ protected void restoreContext(PGraphics g) {
366
+ style.restoreContext(g);
367
+ }
368
+
369
+ protected void restoreContext(PApplet p) {
370
+ style.restoreContext(p);
371
+ }
372
+
373
+ protected void restoreContext() {
374
+ style.restoreContext();
375
+ }
376
+
377
+ protected void setContext(PGraphics g) {
378
+ style.setContext(g);
379
+ }
380
+
381
+ protected void setContext(PApplet p) {
382
+ style.setContext(p);
383
+ }
384
+
385
+ protected void setContext() {
386
+ style.setContext();
387
+ }
388
+
389
+ public void setStyle(RStyle s) {
390
+ style = s;
391
+ }
392
+
393
+ protected void setStyle(RGeomElem p) {
394
+ name = p.name;
395
+ width = p.width;
396
+ height = p.height;
397
+ origWidth = p.origWidth;
398
+ origHeight = p.origHeight;
399
+
400
+ style = new RStyle(p.style);
401
+ }
402
+
403
+ protected void setStyle(String styleString) {
404
+ style.setStyle(styleString);
405
+ }
406
+
407
+ public void setName(String str) {
408
+ this.name = str;
409
+ }
410
+
411
+ protected void calculateCurveLengths() {
412
+ PApplet.println("Feature not yet implemented for this class.");
413
+ }
414
+
415
+ /**
416
+ * Use this to return arclengths of each command on the curve.
417
+ *
418
+ * @eexample getCurveLength
419
+ * @return float[], the arclengths of each command on the curve.
420
+ *
421
+ */
422
+ public float[] getCurveLengths() {
423
+ /* If the cache with the commands lengths is empty, we fill it up */
424
+ if (lenCurves == null) {
425
+ calculateCurveLengths();
426
+ }
427
+
428
+ return lenCurves;
429
+ }
430
+
431
+ /**
432
+ * Use this to return the length of the curve.
433
+ *
434
+ * @eexample getCurveLength
435
+ * @return float, the arclength of the path.
436
+ *
437
+ */
438
+ public float getCurveLength() {
439
+ /* If the cache with the commands lengths is empty, we fill it up */
440
+ if (lenCurve == -1F) {
441
+ calculateCurveLengths();
442
+ }
443
+
444
+ return lenCurve;
445
+ }
446
+
447
+ public RPolygon toPolygon() {
448
+ return toShape().toPolygon();
449
+ }
450
+
451
+ public RMesh toMesh() {
452
+ return toShape().toPolygon().toMesh();
453
+ }
454
+
455
+ // Functions independent of the type of element
456
+ // No need of being overrided
457
+ public void transform(RMatrix m) {
458
+ RPoint[] ps = getHandles();
459
+
460
+ if (ps == null) {
461
+ return;
462
+ }
463
+
464
+ for (RPoint p : ps) {
465
+ p.transform(m);
466
+ }
467
+ }
468
+
469
+ /**
470
+ * Transform the geometric object to fit in a rectangle defined by the
471
+ * parameters passed.
472
+ *
473
+ * @param x
474
+ * @param y
475
+ * @param w
476
+ * @param h
477
+ * @param keepAspectRatio
478
+ * @eexample getBounds
479
+ * @related getCenter ( )
480
+ */
481
+ public void transform(float x, float y, float w, float h, boolean keepAspectRatio) {
482
+ RMatrix mtx = new RMatrix();
483
+ RRectangle orig = this.getBounds();
484
+ float orig_w = orig.getMaxX() - orig.getMinX();
485
+ float orig_h = orig.getMaxY() - orig.getMinY();
486
+
487
+ mtx.translate(-orig.getMinX(), -orig.getMinY());
488
+ if (keepAspectRatio) {
489
+ mtx.scale(Math.min(w / orig_w, h / orig_h));
490
+ } else {
491
+ mtx.scale(w / orig_w, h / orig_h);
492
+ }
493
+ mtx.translate(x, y);
494
+
495
+ this.transform(mtx);
496
+ }
497
+
498
+ public void transform(float x, float y, float w, float h) {
499
+ this.transform(x, y, w, h, true);
500
+ }
501
+
502
+ /**
503
+ * Use this method to get the bounding box of the element.
504
+ *
505
+ * @eexample getBounds
506
+ * @return RRectangle, the bounding box of the element in the form of a
507
+ * four-point contour
508
+ * @related getCenter ( )
509
+ */
510
+ public RRectangle getBounds() {
511
+ float xmax = Float.NEGATIVE_INFINITY;
512
+ float ymax = Float.NEGATIVE_INFINITY;
513
+ float xmin = Float.POSITIVE_INFINITY;
514
+ float ymin = Float.POSITIVE_INFINITY;
515
+
516
+ RPoint[] points = getHandles();
517
+
518
+ if (points != null) {
519
+ for (RPoint point : points) {
520
+ float tempx = point.x;
521
+ float tempy = point.y;
522
+ if (tempx < xmin) {
523
+ xmin = tempx;
524
+ }
525
+ if (tempx > xmax) {
526
+ xmax = tempx;
527
+ }
528
+ if (tempy < ymin) {
529
+ ymin = tempy;
530
+ }
531
+ if (tempy > ymax) {
532
+ ymax = tempy;
533
+ }
534
+ }
535
+ }
536
+
537
+ RRectangle c = new RRectangle(new RPoint(xmin, ymin), new RPoint(xmax, ymax));
538
+ return c;
539
+ }
540
+
541
+ /**
542
+ * Use this method to get the points of the bounding box of the element.
543
+ *
544
+ * @eexample getBounds
545
+ * @return RRectangle, the bounding box of the element in the form of a
546
+ * four-point contour
547
+ * @related getCenter ( )
548
+ */
549
+ public RPoint[] getBoundsPoints() {
550
+ return getBounds().getPoints();
551
+ }
552
+
553
+ /**
554
+ * Use this method to get the top left position of the element.
555
+ *
556
+ * @e
557
+ * @return example getX
558
+ * @related getTopRight ( )
559
+ * @related getBottomLeft ( )
560
+ * @related getBottomRight ( )
561
+ * @related getWidth ( )
562
+ * @related getHeight ( )
563
+ * @related getCenter ( )
564
+ */
565
+ public RPoint getTopLeft() {
566
+ RRectangle orig = this.getBounds();
567
+ return new RPoint(orig.getMinX(), orig.getMinY());
568
+ }
569
+
570
+ /**
571
+ * Use this method to get the top right position of the element.
572
+ *
573
+ * @return
574
+ * @eexample getX
575
+ * @related getTopRight ( )
576
+ * @related getBottomLeft ( )
577
+ * @related getBottomRight ( )
578
+ * @related getWidth ( )
579
+ * @related getHeight ( )
580
+ * @related getCenter ( )
581
+ */
582
+ public RPoint getTopRight() {
583
+ RRectangle orig = this.getBounds();
584
+ return new RPoint(orig.getMaxX(), orig.getMinY());
585
+ }
586
+
587
+ /**
588
+ * Use this method to get the bottom left position of the element.
589
+ *
590
+ * @return
591
+ * @eexample getX
592
+ * @related getTopRight ( )
593
+ * @related getBottomLeft ( )
594
+ * @related getBottomRight ( )
595
+ * @related getWidth ( )
596
+ * @related getHeight ( )
597
+ * @related getCenter ( )
598
+ */
599
+ public RPoint getBottomLeft() {
600
+ RRectangle orig = this.getBounds();
601
+ return new RPoint(orig.getMinX(), orig.getMaxY());
602
+ }
603
+
604
+ /**
605
+ * Use this method to get the bottom right position of the element.
606
+ *
607
+ * @return
608
+ * @eexample getX
609
+ * @related getTopRight ( )
610
+ * @related getBottomLeft ( )
611
+ * @related getBottomRight ( )
612
+ * @related getWidth ( )
613
+ * @related getHeight ( )
614
+ * @related getCenter ( )
615
+ */
616
+ public RPoint getBottomRight() {
617
+ RRectangle orig = this.getBounds();
618
+ return new RPoint(orig.getMaxX(), orig.getMaxY());
619
+ }
620
+
621
+ /**
622
+ * Use this method to get the x (left side) position of the element.
623
+ *
624
+ * @eexample getX
625
+ * @return float, the x coordinate of the element
626
+ * @related getY ( )
627
+ * @related getWidth ( )
628
+ * @related getHeight ( )
629
+ * @related getCenter ( )
630
+ */
631
+ public float getX() {
632
+ RRectangle orig = this.getBounds();
633
+ return orig.getMinX();
634
+ }
635
+
636
+ /**
637
+ * Use this method to get the y position of the element.
638
+ *
639
+ * @eexample getY
640
+ * @return float, the y coordinate of the element
641
+ * @related getY ( )
642
+ * @related getWidth ( )
643
+ * @related getHeight ( )
644
+ * @related getCenter ( )
645
+ */
646
+ public float getY() {
647
+ RRectangle orig = this.getBounds();
648
+ return orig.getMinY();
649
+ }
650
+
651
+ /**
652
+ * Use this method to get the original height of the element.
653
+ *
654
+ * @eexample getOrigHeight
655
+ * @return float, the original height of the element before applying any
656
+ * transformations
657
+ * @related getCenter ( )
658
+ */
659
+ public float getOrigHeight() {
660
+ return origHeight != 0.0 ? origHeight : getHeight();
661
+ }
662
+
663
+ /**
664
+ * Use this method to get the original width of the element.
665
+ *
666
+ * @eexample getOrigWidth
667
+ * @return float, the original width of the element before applying any
668
+ * transformations
669
+ * @related getCenter ( )
670
+ */
671
+ public float getOrigWidth() {
672
+ return origWidth != 0.0 ? origWidth : getWidth();
673
+ }
674
+
675
+ protected void updateOrigParams() {
676
+ this.origWidth = this.getWidth();
677
+ this.origHeight = this.getHeight();
678
+ }
679
+
680
+ /**
681
+ * Use this method to get the width of the element.
682
+ *
683
+ * @eexample getWidth
684
+ * @return float, the width of the element
685
+ * @related getCenter ( )
686
+ */
687
+ public float getWidth() {
688
+ RRectangle orig = this.getBounds();
689
+ return orig.getMaxX() - orig.getMinX();
690
+ }
691
+
692
+ /**
693
+ * Use this method to get the height of the element.
694
+ *
695
+ * @eexample getHeight
696
+ * @return float, the height of the element
697
+ * @related getCenter ( )
698
+ */
699
+ public float getHeight() {
700
+ RRectangle orig = this.getBounds();
701
+ return orig.getMaxY() - orig.getMinY();
702
+ }
703
+
704
+ /**
705
+ * Use this method to get the center point of the element.
706
+ *
707
+ * @eexample RGroup_getCenter
708
+ * @return RPoint, the center point of the element
709
+ * @related getBounds ( )
710
+ */
711
+ public RPoint getCenter() {
712
+ RRectangle c = getBounds();
713
+ return new RPoint((c.getMaxX() + c.getMinX()) / 2, (c.getMaxY() + c.getMinY()) / 2);
714
+ }
715
+
716
+ /**
717
+ * Use this method to get the centroid of the element.
718
+ *
719
+ * @eexample RGroup_getCentroid
720
+ * @return RPoint, the centroid point of the element
721
+ * @related getBounds ( )
722
+ * @related getCenter ( )
723
+ */
724
+ public RPoint getCentroid() {
725
+ RPoint[] ps = getPoints();
726
+
727
+ float areaAcc = 0.0f;
728
+ float xAcc = 0.0f;
729
+ float yAcc = 0.0f;
730
+
731
+ for (int i = 0; i < ps.length - 1; i++) {
732
+ areaAcc += ps[i].x * ps[i + 1].y - ps[i + 1].x * ps[i].y;
733
+ xAcc += (ps[i].x + ps[i + 1].x) * (ps[i].x * ps[i + 1].y - ps[i + 1].x * ps[i].y);
734
+ yAcc += (ps[i].y + ps[i + 1].y) * (ps[i].x * ps[i + 1].y - ps[i + 1].x * ps[i].y);
735
+ }
736
+ areaAcc /= 2.0f;
737
+ RPoint p = new RPoint(xAcc / (6.0f * areaAcc), yAcc / (6.0f * areaAcc));
738
+ return p;
739
+ }
740
+
741
+ /**
742
+ * Use this method to get the area of an element.
743
+ *
744
+ * @eexample RGroup_getArea
745
+ * @return float, the area point of the element
746
+ * @related getBounds ( )
747
+ * @related getCenter ( )
748
+ * @related getCentroid ( )
749
+ */
750
+ public float getArea() {
751
+ RPoint[] ps = getPoints();
752
+
753
+ float areaAcc = 0.0f;
754
+ for (int i = 0; i < ps.length - 1; i++) {
755
+ areaAcc += ps[i].x * ps[i + 1].y - ps[i + 1].x * ps[i].y;
756
+ }
757
+ areaAcc /= 2.0f;
758
+ return Math.abs(areaAcc);
759
+ }
760
+
761
+ /**
762
+ * Use this method to know if the shape is inside a graphics object. This
763
+ * might be useful if we want to delete objects that go offscreen.
764
+ *
765
+ * @eexample RShape_isIn
766
+ * @usage Geometry
767
+ * @param g the graphics object
768
+ * @return boolean, whether the shape is in or not the graphics object
769
+ */
770
+ public boolean isIn(PGraphics g) {
771
+ RRectangle c = getBounds();
772
+ float x0 = g.screenX(c.topLeft.x, c.topLeft.y);
773
+ float y0 = g.screenY(c.topLeft.x, c.topLeft.y);
774
+ float x1 = g.screenX(c.bottomRight.x, c.topLeft.y);
775
+ float y1 = g.screenY(c.bottomRight.x, c.topLeft.y);
776
+ float x2 = g.screenX(c.bottomRight.x, c.bottomRight.y);
777
+ float y2 = g.screenY(c.bottomRight.x, c.bottomRight.y);
778
+ float x3 = g.screenX(c.topLeft.x, c.bottomRight.y);
779
+ float y3 = g.screenY(c.topLeft.x, c.bottomRight.y);
780
+
781
+ float xmax = Math.max(Math.max(x0, x1), Math.max(x2, x3));
782
+ float ymax = Math.max(Math.max(y0, y1), Math.max(y2, y3));
783
+ float xmin = Math.min(Math.min(x0, x1), Math.min(x2, x3));
784
+ float ymin = Math.min(Math.min(y0, y1), Math.min(y2, y3));
785
+
786
+ return !((xmax < 0 || xmin > g.width) && (ymax < 0 || ymin > g.height));
787
+ }
788
+
789
+ public boolean isIn(PApplet g) {
790
+ RRectangle c = getBounds();
791
+ float x0 = g.screenX(c.topLeft.x, c.topLeft.y);
792
+ float y0 = g.screenY(c.topLeft.x, c.topLeft.y);
793
+ float x1 = g.screenX(c.bottomRight.x, c.topLeft.y);
794
+ float y1 = g.screenY(c.bottomRight.x, c.topLeft.y);
795
+ float x2 = g.screenX(c.bottomRight.x, c.bottomRight.y);
796
+ float y2 = g.screenY(c.bottomRight.x, c.bottomRight.y);
797
+ float x3 = g.screenX(c.topLeft.x, c.bottomRight.y);
798
+ float y3 = g.screenY(c.topLeft.x, c.bottomRight.y);
799
+
800
+ float xmax = Math.max(Math.max(x0, x1), Math.max(x2, x3));
801
+ float ymax = Math.max(Math.max(y0, y1), Math.max(y2, y3));
802
+ float xmin = Math.min(Math.min(x0, x1), Math.min(x2, x3));
803
+ float ymin = Math.min(Math.min(y0, y1), Math.min(y2, y3));
804
+
805
+ return !((xmax < 0 || xmin > g.width) && (ymax < 0 || ymin > g.height));
806
+ }
807
+
808
+ /**
809
+ * Use this method to get the transformation matrix in order to fit and
810
+ * center the element on the canvas. Scaling and translation damping
811
+ * parameters are available, in order to create animations.
812
+ *
813
+ * @eexample RGeomElem_getCenteringTransf
814
+ * @return RMatrix, the transformation matrix
815
+ * @param g the canvas to which to fit and center the path
816
+ * @param margin the margin to take into account when fitting
817
+ * @param sclDamping a value from 0 to 1. The damping coefficient for the
818
+ * scale, if the value is 0, then no scaling is applied.
819
+ * @param trnsDamping a value from 0 to 1. The damping coefficient for the
820
+ * translation, if the value is 0, then no translation is applied.
821
+ * @related getBounds ( )
822
+ */
823
+ public RMatrix getCenteringTransf(PGraphics g, float margin, float sclDamping, float trnsDamping) throws RuntimeException {
824
+ RMatrix transf;
825
+
826
+ float mrgn = margin * 2;
827
+ RRectangle c = getBounds();
828
+ float scl = (float) Math.min((g.width - mrgn) / (float) Math.abs(c.getMinX() - c.getMaxX()), (g.height - mrgn) / (float) Math.abs(c.getMinY() - c.getMaxY()));
829
+ RPoint trns = getCenter();
830
+ transf = new RMatrix();
831
+
832
+ if (sclDamping != 0) {
833
+ transf.scale(1 + (scl - 1) * sclDamping);
834
+ }
835
+
836
+ if (trnsDamping != 0) {
837
+ transf.translate(-trns.x * trnsDamping, -trns.y * trnsDamping);
838
+ }
839
+
840
+ return transf;
841
+ }
842
+
843
+ public RMatrix getCenteringTransf(PGraphics g) throws RuntimeException {
844
+ return getCenteringTransf(g, 0, 1, 1);
845
+ }
846
+
847
+ public RMatrix getCenteringTransf(PGraphics g, float margin) throws RuntimeException {
848
+ return getCenteringTransf(g, margin, 1, 1);
849
+ }
850
+
851
+ public void centerIn(PGraphics g) {
852
+ transform(getCenteringTransf(g));
853
+ }
854
+
855
+ public void centerIn(PGraphics g, float margin) {
856
+ transform(getCenteringTransf(g, margin, 1, 1));
857
+ }
858
+
859
+ public void centerIn(PGraphics g, float margin, float sclDamping, float trnsDamping) throws RuntimeException {
860
+ transform(getCenteringTransf(g, margin, sclDamping, trnsDamping));
861
+ }
862
+
863
+ /**
864
+ * Apply a translation to the element, given translation coordinates.
865
+ *
866
+ * @eexample RGeomElem_translate
867
+ * @usage Geometry
868
+ * @param tx the coefficient of x translation
869
+ * @param ty the coefficient of y translation
870
+ * @related transform ( )
871
+ * @related rotate ( )
872
+ * @related scale ( )
873
+ */
874
+ public void translate(float tx, float ty) {
875
+ RMatrix transf = new RMatrix();
876
+ transf.translate(tx, ty);
877
+ transform(transf);
878
+ }
879
+
880
+ /**
881
+ * Apply a translation to the element, given a point.
882
+ *
883
+ * @eexample RGeomElem_translate
884
+ * @usage Geometry
885
+ * @param t the translation vector to be applied
886
+ * @related transform ( )
887
+ * @related rotate ( )
888
+ * @related scale ( )
889
+ */
890
+ public void translate(RPoint t) {
891
+ RMatrix transf = new RMatrix();
892
+ transf.translate(t);
893
+ transform(transf);
894
+ }
895
+
896
+ /**
897
+ * Apply a rotation to the element, given an angle and optionally a rotation
898
+ * center.
899
+ *
900
+ * @eexample RPoint_rotate
901
+ * @usage Geometry
902
+ * @param angle the angle of rotation to be applied
903
+ * @param vx the x coordinate of the center of rotation
904
+ * @param vy the y coordinate of the center of rotation
905
+ * @related transform ( )
906
+ * @related translate ( )
907
+ * @related scale ( )
908
+ */
909
+ public void rotate(float angle, float vx, float vy) {
910
+ RMatrix transf = new RMatrix();
911
+ transf.rotate(angle, vx, vy);
912
+ transform(transf);
913
+ }
914
+
915
+ public void rotate(float angle) {
916
+ RMatrix transf = new RMatrix();
917
+ transf.rotate(angle);
918
+ transform(transf);
919
+ }
920
+
921
+ /**
922
+ * Apply a rotation to the element, given an angle and optionally a rotation
923
+ * center.
924
+ *
925
+ * @eexample RPoint_rotate
926
+ * @usage Geometry
927
+ * @param angle the angle of rotation to be applied
928
+ * @param v the position vector of the center of rotation
929
+ * @related transform ( )
930
+ * @related translate ( )
931
+ * @related scale ( )
932
+ */
933
+ public void rotate(float angle, RPoint v) {
934
+ RMatrix transf = new RMatrix();
935
+ transf.rotate(angle, v);
936
+ transform(transf);
937
+ }
938
+
939
+ /**
940
+ * Apply a scale to the element, given scaling factors and optionally a
941
+ * scaling center.
942
+ *
943
+ * @eexample RPoint_scale
944
+ * @usage Geometry
945
+ * @param sx the scaling coefficient over the x axis
946
+ * @param sy the scaling coefficient over the y axis
947
+ * @param p the position vector of the center of the scaling
948
+ * @related transform ( )
949
+ * @related translate ( )
950
+ * @related rotate ( )
951
+ */
952
+ public void scale(float sx, float sy, RPoint p) {
953
+ RMatrix transf = new RMatrix();
954
+ transf.scale(sx, sy, p);
955
+ transform(transf);
956
+ }
957
+
958
+ public void scale(float sx, float sy) {
959
+ RMatrix transf = new RMatrix();
960
+ transf.scale(sx, sy);
961
+ transform(transf);
962
+ }
963
+
964
+ /**
965
+ * Apply a scale to the element, given scaling factors and optionally a
966
+ * scaling center.
967
+ *
968
+ * @eexample RPoint_scale
969
+ * @usage Geometry
970
+ * @param sx the scaling coefficient over the x axis
971
+ * @param sy the scaling coefficient over the y axis
972
+ * @param x x coordinate of the position vector of the center of the scaling
973
+ * @param y y coordinate of the position vector of the center of the scaling
974
+ * @related transform ( )
975
+ * @related translate ( )
976
+ * @related rotate ( )
977
+ */
978
+ public void scale(float sx, float sy, float x, float y) {
979
+ RMatrix transf = new RMatrix();
980
+ transf.scale(sx, sy, x, y);
981
+ transform(transf);
982
+ }
983
+
984
+ /**
985
+ * Apply a scale to the element, given scaling factors and optionally a
986
+ * scaling center.
987
+ *
988
+ * @eexample RPoint_scale
989
+ * @usage Geometry
990
+ * @param s the scaling coefficient for a uniform scaling
991
+ * @param p the position vector of the center of the scaling
992
+ * @related transform ( )
993
+ * @related translate ( )
994
+ * @related rotate ( )
995
+ */
996
+ public void scale(float s, RPoint p) {
997
+ RMatrix transf = new RMatrix();
998
+ transf.scale(s, p);
999
+ transform(transf);
1000
+ }
1001
+
1002
+ public void scale(float s) {
1003
+ RMatrix transf = new RMatrix();
1004
+ transf.scale(s);
1005
+ transform(transf);
1006
+ }
1007
+
1008
+ /**
1009
+ * Apply a scale to the element, given scaling factors and optionally a
1010
+ * scaling center.
1011
+ *
1012
+ * @eexample RPoint_scale
1013
+ * @usage Geometry
1014
+ * @param s the scaling coefficient for a uniform scaling
1015
+ * @param x x coordinate of the position vector of the center of the scaling
1016
+ * @param y y coordinate of the position vector of the center of the scaling
1017
+ * @related transform ( )
1018
+ * @related translate ( )
1019
+ * @related rotate ( )
1020
+ */
1021
+ public void scale(float s, float x, float y) {
1022
+ RMatrix transf = new RMatrix();
1023
+ transf.scale(s, x, y);
1024
+ transform(transf);
1025
+ }
1026
+
1027
+ /**
1028
+ * Apply a horizontal skew to the element, given skewing angle
1029
+ *
1030
+ * @eexample RMatrix_skewing
1031
+ * @param angle skewing angle
1032
+ * @usage Geometry
1033
+ * @related rotate ( )
1034
+ * @related scale ( )
1035
+ * @related translate ( )
1036
+ */
1037
+ public void skewX(float angle) {
1038
+ RMatrix transf = new RMatrix();
1039
+ transf.skewY(angle);
1040
+ transform(transf);
1041
+ }
1042
+
1043
+ /**
1044
+ * Apply a vertical skew to the element, given skewing angle
1045
+ *
1046
+ * @eexample RMatrix_skewing
1047
+ * @param angle skewing angle
1048
+ * @usage Geometry
1049
+ * @related rotate ( )
1050
+ * @related scale ( )
1051
+ * @related translate ( )
1052
+ */
1053
+ public void skewY(float angle) {
1054
+ RMatrix transf = new RMatrix();
1055
+ transf.skewY(angle);
1056
+ transform(transf);
1057
+ }
1058
+
1059
+ /**
1060
+ * Apply a shear to the element, given shearing factors
1061
+ *
1062
+ * @eexample RMatrix_translate
1063
+ * @param shx x coordinate shearing
1064
+ * @param shy y coordinate shearing
1065
+ * @usage Geometry
1066
+ * @related rotate ( )
1067
+ * @related scale ( )
1068
+ * @related translate ( )
1069
+ */
1070
+ public void shear(float shx, float shy) {
1071
+ RMatrix transf = new RMatrix();
1072
+ transf.shear(shx, shy);
1073
+ transform(transf);
1074
+ }
1075
+ }