geomerative 0.2.0-java → 0.3.2-java

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +8 -0
  3. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  4. data/.travis.yml +14 -0
  5. data/CHANGELOG.md +4 -0
  6. data/COPYING.md +17 -0
  7. data/Gemfile +8 -1
  8. data/README.md +5 -1
  9. data/examples/README.md +1 -7
  10. data/examples/Rakefile +30 -0
  11. data/examples/data/ruby.svg +16 -0
  12. data/examples/f_agent.rb +30 -0
  13. data/examples/font_agent.rb +24 -0
  14. data/examples/hello_polygonize.rb +1 -1
  15. data/examples/hello_svg_to_pdf.rb +2 -2
  16. data/examples/hello_world.rb +1 -1
  17. data/examples/jruby_merge.rb +94 -0
  18. data/examples/physics_type.rb +1 -1
  19. data/examples/rotate_first_letter.rb +3 -4
  20. data/examples/typo_deform.rb +68 -0
  21. data/examples/typo_extra_bright.rb +62 -0
  22. data/examples/typo_merge.rb +94 -0
  23. data/geomerative.gemspec +2 -3
  24. data/lib/geomerative/version.rb +1 -1
  25. data/pom.rb +47 -0
  26. data/pom.xml +35 -65
  27. data/src/geomerative/FastRClip.java +1 -1
  28. data/src/geomerative/RCommand.java +1877 -1741
  29. data/src/geomerative/RContour.java +8 -1
  30. data/src/geomerative/RFont.java +8 -5
  31. data/src/geomerative/RG.java +8 -3
  32. data/src/geomerative/RGeomElem.java +8 -9
  33. data/src/geomerative/RGroup.java +50 -28
  34. data/src/geomerative/RMatrix.java +53 -57
  35. data/src/geomerative/RMesh.java +8 -3
  36. data/src/geomerative/RPath.java +34 -29
  37. data/src/geomerative/RPoint.java +408 -408
  38. data/src/geomerative/RPolygon.java +7 -3
  39. data/src/geomerative/RSVG.java +13 -10
  40. data/src/geomerative/RShape.java +18 -11
  41. data/src/geomerative/RStrip.java +5 -1
  42. data/src/geomerative/RStyle.java +15 -11
  43. metadata +17 -18
@@ -18,7 +18,12 @@
18
18
  */
19
19
 
20
20
  package geomerative ;
21
- import processing.core.*;
21
+
22
+
23
+ import processing.core.PApplet;
24
+ import processing.core.PConstants;
25
+ import processing.core.PGraphics;
26
+
22
27
 
23
28
  /**
24
29
  * RMesh is a reduced interface for creating, holding and drawing meshes. A mesh is a group of triangular strips (RStrip).
@@ -239,7 +244,7 @@ public class RMesh extends RGeomElem
239
244
  }
240
245
 
241
246
  RPoint[] result=null;
242
- RPoint[] newresult=null;
247
+ RPoint[] newresult;
243
248
  for(int i=0;i<numStrips;i++){
244
249
  RPoint[] newPoints = strips[i].getHandles();
245
250
  if(newPoints!=null){
@@ -270,7 +275,7 @@ public class RMesh extends RGeomElem
270
275
  }
271
276
 
272
277
  RPoint[] result=null;
273
- RPoint[] newresult=null;
278
+ RPoint[] newresult;
274
279
  for(int i=0;i<numStrips;i++){
275
280
  RPoint[] newPoints = strips[i].getPoints();
276
281
  if(newPoints!=null){
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Copyright 2004-2008 Ricard Marxer <email@ricardmarxer.com>
3
3
  *
4
- This file is part of Geomerative.
4
+ * This file is part of Geomerative.
5
5
  *
6
6
  * Geomerative is free software: you can redistribute it and/or modify it under
7
7
  * the terms of the GNU General Public License as published by the Free Software
@@ -18,7 +18,10 @@
18
18
  */
19
19
  package geomerative;
20
20
 
21
- import processing.core.*;
21
+ //import processing.core.*;
22
+ import processing.core.PApplet;
23
+ import processing.core.PConstants;
24
+ import processing.core.PGraphics;
22
25
 
23
26
  /**
24
27
  * RPath is a reduced interface for creating, holding and drawing contours.
@@ -152,14 +155,14 @@ public class RPath extends RGeomElem {
152
155
  *
153
156
  * @eexample getHandles
154
157
  * @return RPoint[], the start, control and end points returned in an array.
155
- *
158
+ *
156
159
  */
157
160
  @Override
158
161
  public RPoint[] getHandles() {
159
162
  int numCommands = countCommands();
160
163
 
161
164
  RPoint[] result = null;
162
- RPoint[] newresult = null;
165
+ RPoint[] newresult;
163
166
  for (int i = 0; i < numCommands; i++) {
164
167
  RPoint[] newPoints = commands[i].getHandles();
165
168
  if (newPoints != null) {
@@ -187,7 +190,7 @@ public class RPath extends RGeomElem {
187
190
  *
188
191
  * @eexample getPoints
189
192
  * @return RPoint[], the vertices returned in an array.
190
- *
193
+ *
191
194
  */
192
195
  @Override
193
196
  public RPoint[] getPoints() {
@@ -196,11 +199,11 @@ public class RPath extends RGeomElem {
196
199
  return null;
197
200
  }
198
201
 
199
- // Add the curve points of each command
202
+ // Add the curve points of each command
200
203
  // First set the accumulated offset to the value of the inital offset
201
204
  RCommand.segmentAccOffset = RCommand.segmentOffset;
202
205
  RPoint[] result = null;
203
- RPoint[] newresult = null;
206
+ RPoint[] newresult;
204
207
  for (int i = 0; i < numCommands; i++) {
205
208
  RPoint[] newPoints = commands[i].getPoints(false);
206
209
  if (newPoints != null) {
@@ -208,7 +211,7 @@ public class RPath extends RGeomElem {
208
211
  result = new RPoint[newPoints.length];
209
212
  System.arraycopy(newPoints, 0, result, 0, newPoints.length);
210
213
  } else {
211
- // Check for overlapping
214
+ // Check for overlapping
212
215
  // Overlapping happens when the last point of the last command
213
216
  // is the same as the first point of the current command
214
217
  RPoint lastp = result[result.length - 1];
@@ -224,12 +227,13 @@ public class RPath extends RGeomElem {
224
227
  }
225
228
  }
226
229
  }
227
-
228
230
  // Always add last point
231
+ if (result == null) {
232
+ return result;
233
+ }
229
234
  newresult = new RPoint[result.length + 1];
230
235
  System.arraycopy(result, 0, newresult, 0, result.length);
231
236
  newresult[newresult.length - 1] = new RPoint(commands[numCommands - 1].endPoint);
232
-
233
237
  return newresult;
234
238
  }
235
239
 
@@ -239,7 +243,7 @@ public class RPath extends RGeomElem {
239
243
  *
240
244
  * @eexample RGroup_getPoints
241
245
  * @return RPoint[], the points returned in an array.
242
- *
246
+ *
243
247
  */
244
248
  @Override
245
249
  public RPoint[][] getPointsInPaths() {
@@ -253,7 +257,7 @@ public class RPath extends RGeomElem {
253
257
  *
254
258
  * @eexample RGroup_getHandles
255
259
  * @return RPoint[], the handles returned in an array.
256
- *
260
+ *
257
261
  */
258
262
  @Override
259
263
  public RPoint[][] getHandlesInPaths() {
@@ -267,7 +271,7 @@ public class RPath extends RGeomElem {
267
271
  *
268
272
  * @eexample RGroup_getTangents
269
273
  * @return RPoint[], the tangents returned in an array.
270
- *
274
+ *
271
275
  */
272
276
  @Override
273
277
  public RPoint[][] getTangentsInPaths() {
@@ -291,7 +295,7 @@ public class RPath extends RGeomElem {
291
295
  *
292
296
  * @eexample getTangents
293
297
  * @return RPoint[], the tangent vectors returned in an array.
294
- *
298
+ *
295
299
  */
296
300
  @Override
297
301
  public RPoint[] getTangents() {
@@ -301,7 +305,7 @@ public class RPath extends RGeomElem {
301
305
  }
302
306
 
303
307
  RPoint[] result = null;
304
- RPoint[] newresult = null;
308
+ RPoint[] newresult;
305
309
  for (int i = 0; i < numCommands; i++) {
306
310
  RPoint[] newTangents = commands[i].getTangents();
307
311
  if (newTangents != null) {
@@ -328,7 +332,7 @@ public class RPath extends RGeomElem {
328
332
  *
329
333
  * @param other
330
334
  * @return RPoint[], the intersection points returned in an array.
331
- *
335
+ *
332
336
  */
333
337
  public RPoint[] intersectionPoints(RCommand other) {
334
338
  int numCommands = countCommands();
@@ -337,7 +341,7 @@ public class RPath extends RGeomElem {
337
341
  }
338
342
 
339
343
  RPoint[] result = null;
340
- RPoint[] newresult = null;
344
+ RPoint[] newresult;
341
345
  for (int i = 0; i < numCommands; i++) {
342
346
  RPoint[] newPoints = commands[i].intersectionPoints(other);
343
347
  if (newPoints != null) {
@@ -361,7 +365,7 @@ public class RPath extends RGeomElem {
361
365
  *
362
366
  * @param other
363
367
  * @return RPoint[], the intersection points returned in an array.
364
- *
368
+ *
365
369
  */
366
370
  public RPoint[] intersectionPoints(RPath other) {
367
371
  int numCommands = countCommands();
@@ -372,7 +376,7 @@ public class RPath extends RGeomElem {
372
376
  }
373
377
 
374
378
  RPoint[] result = null;
375
- RPoint[] newresult = null;
379
+ RPoint[] newresult;
376
380
 
377
381
  for (int j = 0; j < numOtherCommands; j++) {
378
382
  for (int i = 0; i < numCommands; i++) {
@@ -400,7 +404,7 @@ public class RPath extends RGeomElem {
400
404
  *
401
405
  * @param other
402
406
  * @return RPoint[], the intersection points returned in an array.
403
- *
407
+ *
404
408
  */
405
409
  public RClosest closestPoints(RCommand other) {
406
410
  int numCommands = countCommands();
@@ -428,7 +432,7 @@ public class RPath extends RGeomElem {
428
432
  *
429
433
  * @param other
430
434
  * @return RPoint[], the intersection points returned in an array.
431
- *
435
+ *
432
436
  */
433
437
  public RClosest closestPoints(RPath other) {
434
438
  int numCommands = countCommands();
@@ -461,7 +465,7 @@ public class RPath extends RGeomElem {
461
465
  * @param t the parameter of advancement on the curve. t must have values
462
466
  * between 0 and 1.
463
467
  * @return RPoint, the vertice returned.
464
- *
468
+ *
465
469
  */
466
470
  @Override
467
471
  public RPoint getPoint(float t) {
@@ -492,7 +496,7 @@ public class RPath extends RGeomElem {
492
496
  * @param t float, the parameter of advancement on the curve. t must have
493
497
  * values between 0 and 1.
494
498
  * @return RPoint, the vertice returned.
495
- *
499
+ *
496
500
  */
497
501
  @Override
498
502
  public RPoint getTangent(float t) {
@@ -540,7 +544,7 @@ public class RPath extends RGeomElem {
540
544
  *
541
545
  * @param p the point for which to test containement..
542
546
  * @return boolean, true if the point is in the path.
543
- *
547
+ *
544
548
  */
545
549
  @Override
546
550
  public boolean contains(RPoint p) {
@@ -567,7 +571,8 @@ public class RPath extends RGeomElem {
567
571
  }
568
572
 
569
573
  int nvert = verts.length;
570
- int i, j = 0;
574
+ int i;
575
+ int j;
571
576
  boolean c = false;
572
577
  for (i = 0, j = nvert - 1; i < nvert; j = i++) {
573
578
  if (((verts[i].y > testy) != (verts[j].y > testy))
@@ -584,7 +589,7 @@ public class RPath extends RGeomElem {
584
589
  * @eexample insertHandle
585
590
  * @param t float, the parameter of advancement on the curve. t must have
586
591
  * values between 0 and 1.
587
- *
592
+ *
588
593
  */
589
594
  public void insertHandle(float t) {
590
595
  if ((t == 0F) || (t == 1F)) {
@@ -620,7 +625,7 @@ public class RPath extends RGeomElem {
620
625
  * @eexample insertHandleInPaths
621
626
  * @param t float, the parameter of advancement on the curve. t must have
622
627
  * values between 0 and 1.
623
- *
628
+ *
624
629
  */
625
630
  public void insertHandleInPaths(float t) {
626
631
  if ((t == 0F) || (t == 1F)) {
@@ -657,7 +662,7 @@ public class RPath extends RGeomElem {
657
662
  * @param t float, the parameter of advancement on the curve. t must have
658
663
  * values between 0 and 1.
659
664
  * @return RPath[], an array of two RPath.
660
- *
665
+ *
661
666
  */
662
667
  public RPath[] split(float t) {
663
668
  RPath[] result = new RPath[2];
@@ -973,7 +978,7 @@ public class RPath extends RGeomElem {
973
978
  * @param m RMatrix, the matrix defining the affine transformation
974
979
  * @related draw ( )
975
980
  */
976
- // OPT: not transform the EndPoint since it's equal to the next StartPoint
981
+ // OPT: not transform the EndPoint since it's equal to the next StartPoint
977
982
  /*
978
983
  public void transform(RMatrix m){
979
984
  RPoint[] ps = getHandles();
@@ -1,419 +1,419 @@
1
1
  /**
2
- Copyright 2004-2008 Ricard Marxer <email@ricardmarxer.com>
3
-
2
+ * Copyright 2004-2008 Ricard Marxer <email@ricardmarxer.com>
3
+ *
4
4
  This file is part of Geomerative.
5
-
6
- Geomerative is free software: you can redistribute it and/or modify
7
- it under the terms of the GNU General Public License as published by
8
- the Free Software Foundation, either version 3 of the License, or
9
- (at your option) any later version.
10
-
11
- Geomerative is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- GNU General Public License for more details.
15
-
16
- You should have received a copy of the GNU General Public License
17
- along with Geomerative. If not, see <http://www.gnu.org/licenses/>.
18
- */
19
-
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
+ */
20
19
  package geomerative;
21
20
 
22
21
  /**
23
- * RPoint is a very simple interface for creating, holding and drawing 2D points.
22
+ * RPoint is a very simple interface for creating, holding and drawing 2D
23
+ * points.
24
+ *
24
25
  * @eexample RPoint
25
26
  * @usage Geometry
26
27
  * @related x
27
28
  * @related y
28
29
  */
29
- public class RPoint
30
- {
31
- /**
32
- * The x coordinate of the point.
33
- * @eexample RPoint_x
34
- * @usage Geometry
35
- * @related y
36
- */
37
- public float x;
38
-
39
- /**
40
- * The y coordinate of the point.
41
- * @eexample RPoint_y
42
- * @usage Geometry
43
- * @related x
44
- */
45
- public float y;
46
-
47
- /**
48
- * Create a new point, given the coordinates.
49
- * @eexample RPoint_constructor
50
- * @usage Geometry
51
- * @param x the x coordinate of the new point
52
- * @param y the y coordinate of the new point
53
- * @related x
54
- * @related y
55
- */
56
- public RPoint(float x,float y)
57
- {
58
- this.x = x;
59
- this.y = y;
60
- }
61
-
62
- public RPoint(double x, double y)
63
- {
64
- this.x = (float)x;
65
- this.y = (float)y;
66
- }
67
-
68
- /**
69
- * Create a new point at (0, 0).
70
- * @eexample RPoint_constructor
71
- * @usage Geometry
72
- * @related x
73
- * @related y
74
- */
75
- public RPoint()
76
- {
77
- x = 0;
78
- y = 0;
79
- }
80
-
81
- /**
82
- * Copy a point.
83
- * @eexample RPoint_constructor
84
- * @usage Geometry
85
- * @param p the point we wish to make a copy of
86
- * @related x
87
- * @related y
88
- */
89
- public RPoint(RPoint p)
90
- {
91
- this.x = p.x;
92
- this.y = p.y;
93
- }
94
-
95
- /**
96
- * @invisible
97
- */
98
- float getX()
99
- {
100
- return this.x;
101
- }
102
-
103
- /**
104
- * @invisible
105
- */
106
- float getY()
107
- {
108
- return this.y;
109
- }
110
-
111
-
112
- /**
113
- * @invisible
114
- */
115
- void setLocation(float nx, float ny)
116
- {
117
- this.x = nx;
118
- this.y = ny;
119
- }
120
-
121
- /**
122
- * Use this to apply a transformation to the point.
123
- * @eexample RPoint_transform
124
- * @usage Geometry
125
- * @param m the transformation matrix to be applied
126
- * @related translate ( )
127
- * @related rotate ( )
128
- * @related scale ( )
129
- */
130
- public void transform(RMatrix m)
131
- {
132
- float tempx = m.m00*x + m.m01*y + m.m02;
133
- float tempy = m.m10*x + m.m11*y + m.m12;
134
-
135
- x = tempx;
136
- y = tempy;
137
- }
138
-
139
- /**
140
- * Apply a translation to the point.
141
- * @eexample RPoint_translate
142
- * @usage Geometry
143
- * @param tx the coefficient of x translation
144
- * @param ty the coefficient of y translation
145
- * @related transform ( )
146
- * @related rotate ( )
147
- * @related scale ( )
148
- */
149
- public void translate(float tx, float ty)
150
- {
151
- x += tx;
152
- y += ty;
153
- }
154
-
155
- /**
156
- * Apply a translation to the point.
157
- * @eexample RPoint_translate
158
- * @usage Geometry
159
- * @param t the translation vector to be applied
160
- * @related transform ( )
161
- * @related rotate ( )
162
- * @related scale ( )
163
- */
164
- public void translate(RPoint t)
165
- {
166
- x += t.x;
167
- y += t.y;
168
- }
169
-
170
- /**
171
- * Apply a rotation to the point, given the angle and optionally the coordinates of the center of rotation.
172
- * @eexample RPoint_rotate
173
- * @usage Geometry
174
- * @param angle the angle of rotation to be applied
175
- * @param vx the x coordinate of the center of rotation
176
- * @param vy the y coordinate of the center of rotation
177
- * @related transform ( )
178
- * @related translate ( )
179
- * @related scale ( )
180
- */
181
- public void rotate(float angle, float vx, float vy)
182
- {
183
- float c = (float)Math.cos(angle);
184
- float s = (float)Math.sin(angle);
185
-
186
- x -= vx;
187
- y -= vy;
188
-
189
- float tempx = x;
190
- float tempy = y;
191
-
192
- x = tempx*c - tempy*s;
193
- y = tempx*s + tempy*c;
194
-
195
- x += vx;
196
- y += vy;
197
- }
198
-
199
- public void rotate(float angle)
200
- {
201
- float c = (float)Math.cos(angle);
202
- float s = (float)Math.sin(angle);
203
-
204
- float tempx = x;
205
- float tempy = y;
206
-
207
- x = tempx*c - tempy*s;
208
- y = tempx*s + tempy*c;
209
- }
210
-
211
- /**
212
- * Apply a rotation to the point, given the angle and optionally the point of the center of rotation.
213
- * @eexample RPoint_rotate
214
- * @usage Geometry
215
- * @param angle the angle of rotation to be applied
216
- * @param v the position vector of the center of rotation
217
- * @related transform ( )
218
- * @related translate ( )
219
- * @related scale ( )
220
- */
221
- public void rotate(float angle, RPoint v)
222
- {
223
- float c = (float)Math.cos(angle);
224
- float s = (float)Math.sin(angle);
225
-
226
- x -= v.x;
227
- y -= v.y;
228
-
229
- float tempx = x;
230
- float tempy = y;
231
-
232
- x = tempx*c - tempy*s;
233
- y = tempx*s + tempy*c;
234
-
235
- x += v.x;
236
- y += v.y;
237
- }
238
-
239
- /**
240
- * Apply a scaling to the point, given the scaling factors.
241
- * @eexample RPoint_scale
242
- * @usage Geometry
243
- * @param sx the scaling coefficient over the x axis
244
- * @param sy the scaling coefficient over the y axis
245
- * @related transform ( )
246
- * @related translate ( )
247
- * @related rotate ( )
248
- */
249
- public void scale (float sx, float sy)
250
- {
251
- x *= sx;
252
- y *= sy;
253
- }
254
-
255
- /**
256
- * Apply a scaling to the point, given a scaling factor.
257
- * @eexample RPoint_scale
258
- * @usage Geometry
259
- * @param s the scaling coefficient for a uniform scaling
260
- * @related transform ( )
261
- * @related translate ( )
262
- * @related rotate ( )
263
- */
264
- public void scale (float s)
265
- {
266
- x *= s;
267
- y *= s;
268
- }
269
-
270
- /**
271
- * Apply a scaling to the point, given a scaling vector.
272
- * @eexample RPoint_scale
273
- * @usage Geometry
274
- * @param s the scaling vector
275
- * @related transform ( )
276
- * @related translate ( )
277
- * @related rotate ( )
278
- */
279
- public void scale (RPoint s)
280
- {
281
- x *= s.x;
282
- y *= s.y;
283
- }
284
-
285
-
286
- /**
287
- * Use this to normalize the point. This means that after applying, it's norm will be equal to 1.
288
- * @eexample RPoint_normalize
289
- * @usage Geometry
290
- * @related transform ( )
291
- * @related translate ( )
292
- * @related rotate ( )
293
- * @related scale ( )
294
- */
295
- public void normalize ()
296
- {
297
- float norma = norm();
298
- if(norma!=0) scale(1/norma);
299
- }
300
-
301
- /**
302
- * Use this to subtract a vector from this point.
303
- * @eexample RPoint_sub
304
- * @usage Geometry
305
- * @param p the vector to substract
306
- * @related add ( )
307
- * @related mult ( )
308
- * @related cross ( )
309
- */
310
- public void sub (RPoint p)
311
- {
312
- x -= p.x;
313
- y -= p.y;
314
- }
315
-
316
- /**
317
- * Use this to add a vector to this point.
318
- * @eexample RPoint_add
319
- * @usage Geometry
320
- * @param p the vector to add
321
- * @related sub ( )
322
- * @related mult ( )
323
- * @related cross ( )
324
- */
325
- public void add (RPoint p)
326
- {
327
- x += p.x;
328
- y += p.y;
329
- }
330
-
331
- /**
332
- * Use this to multiply a vector to this point. This returns a float corresponding to the scalar product of both vectors.
333
- * @eexample RPoint_mult
334
- * @usage Geometry
335
- * @param p the vector to multiply
336
- * @return float, the result of the scalar product
337
- * @related add ( )
338
- * @related sub ( )
339
- * @related cross ( )
340
- */
341
- public float mult (RPoint p)
342
- {
343
- return (x * p.x + y * p.y);
344
- }
345
-
346
- /**
347
- * Use this to perform a cross product of the point with another point. This returns a RPoint corresponding to the cross product of both vectors.
348
- * @eexample RPoint_cross
349
- * @usage Geometry
350
- * @param p the vector to perform the cross product with
351
- * @return RPoint, the resulting vector of the cross product
352
- * @related add ( )
353
- * @related sub ( )
354
- * @related mult ( )
355
- */
356
- public RPoint cross (RPoint p)
357
- {
358
- return new RPoint(x * p.y - p.x * y, y * p.x - p.y * x);
359
- }
360
-
361
- /**
362
- * Use this to obtain the norm of the point.
363
- * @eexample RPoint_norm
364
- * @usage Geometry
365
- * @return float, the norm of the point
366
- * @related angle ( )
367
- */
368
- public float norm ()
369
- {
370
- return (float)Math.sqrt(mult(this));
371
- }
372
-
373
- /**
374
- * Use this to obtain the square norm of the point.
375
- * @eexample RPoint_norm
376
- * @usage Geometry
377
- * @return float, the norm of the point
378
- * @related angle ( )
379
- */
380
- public float sqrnorm ()
381
- {
382
- return (float)mult(this);
383
- }
384
-
385
- /**
386
- * Use this to obtain the angle between the vector and another vector
387
- * @eexample RPoint_angle
388
- * @usage Geometry
389
- * @param p the vector relative to which we want to evaluate the angle
390
- * @return float, the angle between the two vectors
391
- * @related norm ( )
392
- */
393
- public float angle (RPoint p)
394
- {
395
- float normp = p.norm();
396
- float normthis = norm();
397
- return (float)Math.acos(mult(p)/(normp*normthis));
398
- }
399
-
400
- /**
401
- * Use this to obtain the distance between the vector and another vector
402
- * @eexample RPoint_dist
403
- * @usage Geometry
404
- * @param p the vector relative to which we want to evaluate the distance
405
- * @return float, the distance between the two vectors
406
- * @related norm ( )
407
- */
408
- public float dist (RPoint p)
409
- {
410
- float dx = (p.x-this.x);
411
- float dy = (p.y-this.y);
412
- return (float)Math.sqrt(dx*dx + dy*dy);
413
- }
414
-
415
-
416
- public void print(){
417
- System.out.print("("+x+","+y+")\n");
418
- }
30
+ public class RPoint {
31
+
32
+ /**
33
+ * The x coordinate of the point.
34
+ *
35
+ * @eexample RPoint_x
36
+ * @usage Geometry
37
+ * @related y
38
+ */
39
+ public float x;
40
+
41
+ /**
42
+ * The y coordinate of the point.
43
+ *
44
+ * @eexample RPoint_y
45
+ * @usage Geometry
46
+ * @related x
47
+ */
48
+ public float y;
49
+
50
+ /**
51
+ * Create a new point, given the coordinates.
52
+ *
53
+ * @eexample RPoint_constructor
54
+ * @usage Geometry
55
+ * @param x the x coordinate of the new point
56
+ * @param y the y coordinate of the new point
57
+ * @related x
58
+ * @related y
59
+ */
60
+ public RPoint(float x, float y) {
61
+ this.x = x;
62
+ this.y = y;
63
+ }
64
+
65
+ public RPoint(double x, double y) {
66
+ this.x = (float) x;
67
+ this.y = (float) y;
68
+ }
69
+
70
+ /**
71
+ * Create a new point at (0, 0).
72
+ *
73
+ * @eexample RPoint_constructor
74
+ * @usage Geometry
75
+ * @related x
76
+ * @related y
77
+ */
78
+ public RPoint() {
79
+ x = 0;
80
+ y = 0;
81
+ }
82
+
83
+ /**
84
+ * Copy a point.
85
+ *
86
+ * @eexample RPoint_constructor
87
+ * @usage Geometry
88
+ * @param p the point we wish to make a copy of
89
+ * @related x
90
+ * @related y
91
+ */
92
+ public RPoint(RPoint p) {
93
+ this.x = p.x;
94
+ this.y = p.y;
95
+ }
96
+
97
+ /**
98
+ * @invisible
99
+ */
100
+ float getX() {
101
+ return this.x;
102
+ }
103
+
104
+ /**
105
+ * @invisible
106
+ */
107
+ float getY() {
108
+ return this.y;
109
+ }
110
+
111
+ /**
112
+ * @invisible
113
+ */
114
+ void setLocation(float nx, float ny) {
115
+ this.x = nx;
116
+ this.y = ny;
117
+ }
118
+
119
+ /**
120
+ * Use this to apply a transformation to the point.
121
+ *
122
+ * @eexample RPoint_transform
123
+ * @usage Geometry
124
+ * @param m the transformation matrix to be applied
125
+ * @related translate ( )
126
+ * @related rotate ( )
127
+ * @related scale ( )
128
+ */
129
+ public void transform(RMatrix m) {
130
+ float tempx = m.m00 * x + m.m01 * y + m.m02;
131
+ float tempy = m.m10 * x + m.m11 * y + m.m12;
132
+
133
+ x = tempx;
134
+ y = tempy;
135
+ }
136
+
137
+ /**
138
+ * Apply a translation to the point.
139
+ *
140
+ * @eexample RPoint_translate
141
+ * @usage Geometry
142
+ * @param tx the coefficient of x translation
143
+ * @param ty the coefficient of y translation
144
+ * @related transform ( )
145
+ * @related rotate ( )
146
+ * @related scale ( )
147
+ */
148
+ public void translate(float tx, float ty) {
149
+ x += tx;
150
+ y += ty;
151
+ }
152
+
153
+ /**
154
+ * Apply a translation to the point.
155
+ *
156
+ * @eexample RPoint_translate
157
+ * @usage Geometry
158
+ * @param t the translation vector to be applied
159
+ * @related transform ( )
160
+ * @related rotate ( )
161
+ * @related scale ( )
162
+ */
163
+ public void translate(RPoint t) {
164
+ x += t.x;
165
+ y += t.y;
166
+ }
167
+
168
+ /**
169
+ * Apply a rotation to the point, given the angle and optionally the
170
+ * coordinates of the center of rotation.
171
+ *
172
+ * @eexample RPoint_rotate
173
+ * @usage Geometry
174
+ * @param angle the angle of rotation to be applied
175
+ * @param vx the x coordinate of the center of rotation
176
+ * @param vy the y coordinate of the center of rotation
177
+ * @related transform ( )
178
+ * @related translate ( )
179
+ * @related scale ( )
180
+ */
181
+ public void rotate(float angle, float vx, float vy) {
182
+ float c = (float) Math.cos(angle);
183
+ float s = (float) Math.sin(angle);
184
+
185
+ x -= vx;
186
+ y -= vy;
187
+
188
+ float tempx = x;
189
+ float tempy = y;
190
+
191
+ x = tempx * c - tempy * s;
192
+ y = tempx * s + tempy * c;
193
+
194
+ x += vx;
195
+ y += vy;
196
+ }
197
+
198
+ public void rotate(float angle) {
199
+ float c = (float) Math.cos(angle);
200
+ float s = (float) Math.sin(angle);
201
+
202
+ float tempx = x;
203
+ float tempy = y;
204
+
205
+ x = tempx * c - tempy * s;
206
+ y = tempx * s + tempy * c;
207
+ }
208
+
209
+ /**
210
+ * Apply a rotation to the point, given the angle and optionally the point
211
+ * of the center of rotation.
212
+ *
213
+ * @eexample RPoint_rotate
214
+ * @usage Geometry
215
+ * @param angle the angle of rotation to be applied
216
+ * @param v the position vector of the center of rotation
217
+ * @related transform ( )
218
+ * @related translate ( )
219
+ * @related scale ( )
220
+ */
221
+ public void rotate(float angle, RPoint v) {
222
+ float c = (float) Math.cos(angle);
223
+ float s = (float) Math.sin(angle);
224
+
225
+ x -= v.x;
226
+ y -= v.y;
227
+
228
+ float tempx = x;
229
+ float tempy = y;
230
+
231
+ x = tempx * c - tempy * s;
232
+ y = tempx * s + tempy * c;
233
+
234
+ x += v.x;
235
+ y += v.y;
236
+ }
237
+
238
+ /**
239
+ * Apply a scaling to the point, given the scaling factors.
240
+ *
241
+ * @eexample RPoint_scale
242
+ * @usage Geometry
243
+ * @param sx the scaling coefficient over the x axis
244
+ * @param sy the scaling coefficient over the y axis
245
+ * @related transform ( )
246
+ * @related translate ( )
247
+ * @related rotate ( )
248
+ */
249
+ public void scale(float sx, float sy) {
250
+ x *= sx;
251
+ y *= sy;
252
+ }
253
+
254
+ /**
255
+ * Apply a scaling to the point, given a scaling factor.
256
+ *
257
+ * @eexample RPoint_scale
258
+ * @usage Geometry
259
+ * @param s the scaling coefficient for a uniform scaling
260
+ * @related transform ( )
261
+ * @related translate ( )
262
+ * @related rotate ( )
263
+ */
264
+ public void scale(float s) {
265
+ x *= s;
266
+ y *= s;
267
+ }
268
+
269
+ /**
270
+ * Apply a scaling to the point, given a scaling vector.
271
+ *
272
+ * @eexample RPoint_scale
273
+ * @usage Geometry
274
+ * @param s the scaling vector
275
+ * @related transform ( )
276
+ * @related translate ( )
277
+ * @related rotate ( )
278
+ */
279
+ public void scale(RPoint s) {
280
+ x *= s.x;
281
+ y *= s.y;
282
+ }
283
+
284
+ /**
285
+ * Use this to normalize the point. This means that after applying, it's
286
+ * norm will be equal to 1.
287
+ *
288
+ * @eexample RPoint_normalize
289
+ * @usage Geometry
290
+ * @related transform ( )
291
+ * @related translate ( )
292
+ * @related rotate ( )
293
+ * @related scale ( )
294
+ */
295
+ public void normalize() {
296
+ float norma = norm();
297
+ if (norma != 0) {
298
+ scale(1 / norma);
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Use this to subtract a vector from this point.
304
+ *
305
+ * @eexample RPoint_sub
306
+ * @usage Geometry
307
+ * @param p the vector to substract
308
+ * @related add ( )
309
+ * @related mult ( )
310
+ * @related cross ( )
311
+ */
312
+ public void sub(RPoint p) {
313
+ x -= p.x;
314
+ y -= p.y;
315
+ }
316
+
317
+ /**
318
+ * Use this to add a vector to this point.
319
+ *
320
+ * @eexample RPoint_add
321
+ * @usage Geometry
322
+ * @param p the vector to add
323
+ * @related sub ( )
324
+ * @related mult ( )
325
+ * @related cross ( )
326
+ */
327
+ public void add(RPoint p) {
328
+ x += p.x;
329
+ y += p.y;
330
+ }
331
+
332
+ /**
333
+ * Use this to multiply a vector to this point. This returns a float
334
+ * corresponding to the scalar product of both vectors.
335
+ *
336
+ * @eexample RPoint_mult
337
+ * @usage Geometry
338
+ * @param p the vector to multiply
339
+ * @return float, the result of the scalar product
340
+ * @related add ( )
341
+ * @related sub ( )
342
+ * @related cross ( )
343
+ */
344
+ public float mult(RPoint p) {
345
+ return (x * p.x + y * p.y);
346
+ }
347
+
348
+ /**
349
+ * Use this to perform a cross product of the point with another point. This
350
+ * returns a RPoint corresponding to the cross product of both vectors.
351
+ *
352
+ * @eexample RPoint_cross
353
+ * @usage Geometry
354
+ * @param p the vector to perform the cross product with
355
+ * @return RPoint, the resulting vector of the cross product
356
+ * @related add ( )
357
+ * @related sub ( )
358
+ * @related mult ( )
359
+ */
360
+ public RPoint cross(RPoint p) {
361
+ return new RPoint(x * p.y - p.x * y, y * p.x - p.y * x);
362
+ }
363
+
364
+ /**
365
+ * Use this to obtain the norm of the point.
366
+ *
367
+ * @eexample RPoint_norm
368
+ * @usage Geometry
369
+ * @return float, the norm of the point
370
+ * @related angle ( )
371
+ */
372
+ public float norm() {
373
+ return (float) Math.sqrt(mult(this));
374
+ }
375
+
376
+ /**
377
+ * Use this to obtain the square norm of the point.
378
+ *
379
+ * @eexample RPoint_norm
380
+ * @usage Geometry
381
+ * @return float, the norm of the point
382
+ * @related angle ( )
383
+ */
384
+ public float sqrnorm() {
385
+ return (float) mult(this);
386
+ }
387
+
388
+ /**
389
+ * Use this to obtain the angle between the vector and another vector
390
+ *
391
+ * @eexample RPoint_angle
392
+ * @usage Geometry
393
+ * @param p the vector relative to which we want to evaluate the angle
394
+ * @return float, the angle between the two vectors
395
+ * @related norm ( )
396
+ */
397
+ public float angle(RPoint p) {
398
+ float normp = p.norm();
399
+ float normthis = norm();
400
+ return (float) Math.acos(mult(p) / (normp * normthis));
401
+ }
402
+
403
+ /**
404
+ * Use this to obtain the distance between the vector and another vector
405
+ *
406
+ * @eexample RPoint_dist
407
+ * @usage Geometry
408
+ * @param p the vector relative to which we want to evaluate the distance
409
+ * @return float, the distance between the two vectors
410
+ * @related norm ( )
411
+ */
412
+ public float dist(RPoint p) {
413
+ return (float) Math.hypot(p.x - this.x, p.y - this.y);
414
+ }
415
+
416
+ public void print() {
417
+ System.out.print("(" + x + "," + y + ")\n");
418
+ }
419
419
  }