propane 0.6.0-java → 0.7.0-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.
@@ -12,7 +12,7 @@ public class ShapeRender implements JRender {
12
12
 
13
13
  /**
14
14
  *
15
- * @param shape
15
+ * @param shape PShape
16
16
  */
17
17
  public ShapeRender(final PShape shape) {
18
18
  this.shape = shape;
@@ -21,8 +21,8 @@ public class ShapeRender implements JRender {
21
21
 
22
22
  /**
23
23
  *
24
- * @param x
25
- * @param y
24
+ * @param x double
25
+ * @param y double
26
26
  */
27
27
  @Override
28
28
  public void vertex(double x, double y) {
@@ -31,8 +31,8 @@ public class ShapeRender implements JRender {
31
31
 
32
32
  /**
33
33
  *
34
- * @param x
35
- * @param y
34
+ * @param x double
35
+ * @param y double
36
36
  */
37
37
  @Override
38
38
  public void curveVertex(double x, double y) {
@@ -41,9 +41,9 @@ public class ShapeRender implements JRender {
41
41
 
42
42
  /**
43
43
  *
44
- * @param x
45
- * @param y
46
- * @param z
44
+ * @param x double
45
+ * @param y double
46
+ * @param z double
47
47
  */
48
48
  @Override
49
49
  public void vertex(double x, double y, double z) {
@@ -52,9 +52,9 @@ public class ShapeRender implements JRender {
52
52
 
53
53
  /**
54
54
  *
55
- * @param x
56
- * @param y
57
- * @param z
55
+ * @param x double
56
+ * @param y double
57
+ * @param z double
58
58
  */
59
59
  @Override
60
60
  public void normal(double x, double y, double z) {
@@ -63,11 +63,11 @@ public class ShapeRender implements JRender {
63
63
 
64
64
  /**
65
65
  *
66
- * @param x
67
- * @param y
68
- * @param z
69
- * @param u
70
- * @param v
66
+ * @param x double
67
+ * @param y double
68
+ * @param z double
69
+ * @param u double
70
+ * @param v double
71
71
  */
72
72
  @Override
73
73
  public void vertex(double x, double y, double z, double u, double v) {
@@ -76,9 +76,9 @@ public class ShapeRender implements JRender {
76
76
 
77
77
  /**
78
78
  *
79
- * @param x
80
- * @param y
81
- * @param z
79
+ * @param x double
80
+ * @param y double
81
+ * @param z double
82
82
  */
83
83
  @Override
84
84
  public void curveVertex(double x, double y, double z) {
@@ -0,0 +1,20 @@
1
+ /*
2
+ * Copyright (c) 2015-16 Martin Prout
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * http://creativecommons.org/licenses/LGPL/2.1/
10
+ *
11
+ * This library 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 GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with this library; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+ package monkstone.vecmath;
@@ -1,6 +1,7 @@
1
1
  package monkstone.vecmath.vec2;
2
+
2
3
  /*
3
- * Copyright (C) 2015-16 Martin Prout
4
+ * Copyright (c) 2015-16 Martin Prout
4
5
  *
5
6
  * This library is free software; you can redistribute it and/or
6
7
  * modify it under the terms of the GNU Lesser General Public
@@ -17,649 +18,718 @@ package monkstone.vecmath.vec2;
17
18
  * You should have received a copy of the GNU Lesser General Public
18
19
  * License along with this library; if not, write to the Free Software
19
20
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- */
21
-
21
+ */
22
22
  import org.jruby.Ruby;
23
23
  import org.jruby.RubyArray;
24
- import org.jruby.RubyBoolean;
25
24
  import org.jruby.RubyClass;
25
+ import org.jruby.RubyFixnum;
26
+ import org.jruby.RubyFloat;
26
27
  import org.jruby.RubyObject;
28
+ import org.jruby.RubySymbol;
27
29
  import org.jruby.anno.JRubyClass;
28
30
  import org.jruby.anno.JRubyMethod;
29
31
  import org.jruby.runtime.Arity;
30
32
  import org.jruby.runtime.Block;
31
- import org.jruby.runtime.ObjectAllocator;
32
33
  import org.jruby.runtime.ThreadContext;
33
34
  import org.jruby.runtime.builtin.IRubyObject;
34
35
  import monkstone.vecmath.JRender;
35
36
 
36
37
  /**
37
- *
38
- * @author Martin Prout
39
- */
38
+ *
39
+ * @author Martin Prout
40
+ */
40
41
  @JRubyClass(name = "Vec2D")
41
42
  public class Vec2 extends RubyObject {
42
-
43
- static final double EPSILON = 9.999999747378752e-05; // matches processing.org EPSILON
44
- private static final long serialVersionUID = -7013225882277559392L;
45
- private double jx = 0;
46
- private double jy = 0;
47
-
48
- public double javax(){
43
+
44
+ static final double EPSILON = 9.999999747378752e-05; // matches processing.org EPSILON
45
+ private static final long serialVersionUID = -2950154560223211646L;
46
+
47
+ private double jx = 0;
48
+ private double jy = 0;
49
+
50
+ /**
51
+ *
52
+ * @param runtime ThreadContext
53
+ */
54
+ public static void createVec2(final Ruby runtime) {
55
+ RubyClass vec2Cls = runtime.defineClass("Vec2D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec2(runtime1, rubyClass));
56
+ vec2Cls.defineAnnotatedMethods(Vec2.class);
57
+ }
58
+
59
+ public double javax() {
49
60
  return jx;
50
61
  }
51
-
52
- public double javay(){
62
+
63
+ public double javay() {
53
64
  return jy;
54
65
  }
55
-
56
- /**
57
- *
58
- * @param runtime
59
- */
60
- public static void createVec2(final Ruby runtime) {
61
- RubyClass vec2Cls = runtime.defineClass("Vec2D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec2(runtime1, rubyClass));
62
- vec2Cls.defineAnnotatedMethods(Vec2.class);
63
- }
64
-
65
-
66
- /**
67
- *
68
- * @param context
69
- * @param klazz
70
- * @param args optional (no args jx = 0, jy = 0)
71
- * @return new Vec2 object (ruby)
72
- */
73
- @JRubyMethod(name = "new", meta = true, rest = true)
74
- public static final IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
75
- Vec2 vec2 = (Vec2) ((RubyClass) klazz).allocate();
76
- vec2.init(context, args);
77
- return vec2;
78
- }
79
-
80
- /**
81
- *
82
- * @param runtime
83
- * @param klass
84
- */
85
- public Vec2(Ruby runtime, RubyClass klass) {
86
- super(runtime, klass);
87
- }
88
-
89
- void init(ThreadContext context, IRubyObject[] args) {
90
- if (Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 2) == 2) {
91
- jx = (Double) args[0].toJava(Double.class);
92
- jy = (Double) args[1].toJava(Double.class);
93
- }
94
- }
95
-
96
- /**
97
- *
98
- * @param context
99
- * @return jx float
100
- */
101
- @JRubyMethod(name = "x")
102
-
103
- public IRubyObject getX(ThreadContext context) {
104
- return context.getRuntime().newFloat(jx);
105
- }
106
-
107
- /**
108
- *
109
- * @param context
110
- * @return jy float
111
- */
112
- @JRubyMethod(name = "y")
113
-
114
- public IRubyObject getY(ThreadContext context) {
115
- return context.getRuntime().newFloat(jy);
116
- }
117
-
118
- /**
119
- *
120
- * @param context
121
- * @param other
122
- * @return jx float
123
- */
124
- @JRubyMethod(name = "x=")
125
-
126
- public IRubyObject setX(ThreadContext context, IRubyObject other) {
127
- jx = (Double) other.toJava(Double.class);
128
- return other;
129
- }
130
-
131
- /**
132
- *
133
- * @param context
134
- * @param other
135
- * @return jy float
136
- */
137
- @JRubyMethod(name = "y=")
138
-
139
- public IRubyObject setY(ThreadContext context, IRubyObject other) {
140
- jy = (Double) other.toJava(Double.class);
141
- return other;
142
- }
143
-
144
- /**
145
- *
146
- * @param context
147
- * @param other
148
- * @return hypotenuse float
149
- */
150
- @JRubyMethod(name = "dist", required = 1)
151
-
152
- public IRubyObject dist(ThreadContext context, IRubyObject other) {
153
- Vec2 b = null;
154
- Ruby runtime = context.getRuntime();
155
- if (other instanceof Vec2) {
156
- b = (Vec2) other.toJava(Vec2.class);
157
- } else {
158
- throw runtime.newTypeError("argument should be Vec2D");
159
- }
160
- double result = Math.hypot((jx - b.jx), (jy - b.jy));
161
- return runtime.newFloat(result);
162
- }
163
-
164
- /**
165
- *
166
- * @param context
167
- * @param other
168
- * @return cross product as a new Vec3D
169
- */
170
- @JRubyMethod(name = "cross", required = 1)
171
-
172
- public IRubyObject cross(ThreadContext context, IRubyObject other) {
173
- Vec2 b = null;
174
- Ruby runtime = context.getRuntime();
175
- if (other instanceof Vec2) {
176
- b = (Vec2) other.toJava(Vec2.class);
177
- } else {
178
- throw runtime.newTypeError("argument should be Vec2D");
179
- }
180
- return runtime.newFloat(jx * b.jy - jy * b.jx);
181
- }
182
-
183
- /**
184
- *
185
- * @param context
186
- * @param other
187
- * @return do product as a float
188
- */
189
- @JRubyMethod(name = "dot", required = 1)
190
-
191
- public IRubyObject dot(ThreadContext context, IRubyObject other) {
192
- Vec2 b = null;
193
- Ruby runtime = context.getRuntime();
194
- if (other instanceof Vec2) {
195
- b = (Vec2) other.toJava(Vec2.class);
196
- } else {
197
- throw runtime.newTypeError("argument should be Vec2D");
198
- }
199
- return runtime.newFloat(jx * b.jx + jy * b.jy);
200
- }
201
-
202
- /**
203
- *
204
- * @param context
205
- * @param other
206
- * @return new Vec2 object (ruby)
207
- */
208
- @JRubyMethod(name = "+", required = 1)
209
-
210
- public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
211
- Vec2 b = null;
212
- Ruby runtime = context.getRuntime();
213
- if (other instanceof Vec2) {
214
- b = (Vec2) other.toJava(Vec2.class);
215
- } else {
216
- throw runtime.newTypeError("argument should be Vec2D");
217
- }
218
- return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
219
- runtime.newFloat(jx + b.jx),
220
- runtime.newFloat(jy + b.jy)});
221
- }
222
-
223
- /**
224
- *
225
- * @param context
226
- * @param other
227
- * @return new Vec2 object (ruby)
228
- */
229
- @JRubyMethod(name = "-", required = 1)
230
-
231
- public IRubyObject op_minus(ThreadContext context, IRubyObject other) {
232
- Vec2 b = null;
233
- Ruby runtime = context.getRuntime();
234
- if (other instanceof Vec2) {
235
- b = (Vec2) other.toJava(Vec2.class);
236
- } else {
237
- throw runtime.newTypeError("argument should be Vec2D");
238
- }
239
- return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
240
- runtime.newFloat(jx - b.jx),
241
- runtime.newFloat(jy - b.jy)});
242
- }
243
-
244
- /**
245
- *
246
- * @param context
247
- * @param other
248
- * @return new Vec2 object (ruby)
249
- */
250
- @JRubyMethod(name = "*")
251
-
252
- public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
253
- Ruby runtime = context.getRuntime();
254
- double scalar = (Double) other.toJava(Double.class);
255
- return Vec2.rbNew(context, this.getMetaClass(),
256
- new IRubyObject[]{runtime.newFloat(jx * scalar),
257
- runtime.newFloat(jy * scalar)});
258
- }
259
-
260
- /**
261
- *
262
- * @param context
263
- * @param other
264
- * @return new Vec2 object (ruby)
265
- */
266
- @JRubyMethod(name = "/", required = 1)
267
-
268
- public IRubyObject op_div(ThreadContext context, IRubyObject other) {
269
- Ruby runtime = context.getRuntime();
270
- double scalar = (Double) other.toJava(Double.class);
271
- if (Math.abs(scalar) < Vec2.EPSILON) {
272
- return this;
273
- }
274
- return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
275
- runtime.newFloat(jx / scalar),
276
- runtime.newFloat(jy / scalar)});
277
- }
278
-
279
- /**
280
- *
281
- * @param context
282
- * @return angle radians as a float
283
- */
284
- @JRubyMethod(name = "heading")
285
- public IRubyObject heading(ThreadContext context) {
286
- return context.getRuntime().newFloat(Math.atan2(jy, jx));
287
- }
288
-
289
- /**
290
- *
291
- * @param context
292
- * @return magnitude float
293
- */
294
- @JRubyMethod(name = "mag")
295
-
296
- public IRubyObject mag(ThreadContext context) {
297
- double result = 0;
298
- if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
299
- result = Math.hypot(jx, jy);
300
- }
301
- else{
302
- if (Math.abs(jy) > EPSILON) {
303
- result = Math.abs(jy);
304
- }
305
- if (Math.abs(jx) > EPSILON) {
306
- result = Math.abs(jx);
307
- }
308
- }
309
- return context.getRuntime().newFloat(result);
310
- }
311
-
312
- /**
313
- * Call yield if block given, do nothing if yield == false else set_mag to
314
- * given scalar
315
- *
316
- * @param context
317
- * @param scalar double value to set
318
- * @param block should return a boolean (optional)
319
- * @return this Vec2D with the new magnitude
320
- */
321
- @JRubyMethod(name = "set_mag")
322
-
323
- public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
324
- double new_mag = (Double) scalar.toJava(Double.class);
325
- if (block.isGiven()) {
326
- if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
66
+
67
+ /**
68
+ *
69
+ * @param context ThreadContext
70
+ * @param klazz IRubyObject
71
+ * @param args optional (no args jx = 0, jy = 0)
72
+ * @return new Vec2 object (ruby)
73
+ */
74
+ @JRubyMethod(name = "new", meta = true, rest = true)
75
+ public static final IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
76
+ Vec2 vec2 = (Vec2) ((RubyClass) klazz).allocate();
77
+ vec2.init(context, args);
78
+ return vec2;
79
+ }
80
+
81
+ /**
82
+ *
83
+ * @param runtime Ruby
84
+ * @param klass RubyClass
85
+ */
86
+ public Vec2(Ruby runtime, RubyClass klass) {
87
+ super(runtime, klass);
88
+ }
89
+
90
+ void init(ThreadContext context, IRubyObject[] args) {
91
+ if (Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 2) == 2) {
92
+ jx = (args[0] instanceof RubyFloat)
93
+ ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
94
+ jy = (args[1] instanceof RubyFloat)
95
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
96
+ }
97
+ }
98
+
99
+ /**
100
+ *
101
+ * @param context ThreadContext
102
+ * @return x IRubyObject
103
+ */
104
+ @JRubyMethod(name = "x")
105
+
106
+ public IRubyObject getX(ThreadContext context) {
107
+ return context.runtime.newFloat(jx);
108
+ }
109
+
110
+ /**
111
+ *
112
+ * @param context ThreadContext
113
+ * @return y IRubyObject
114
+ */
115
+ @JRubyMethod(name = "y")
116
+
117
+ public IRubyObject getY(ThreadContext context) {
118
+ return context.runtime.newFloat(jy);
119
+ }
120
+
121
+ /**
122
+ *
123
+ * @param context ThreadContext
124
+ * @param key as symbol
125
+ * @return value float
126
+ */
127
+ @JRubyMethod(name = "[]", required = 1)
128
+
129
+ public IRubyObject aref(ThreadContext context, IRubyObject key) {
130
+ Ruby runtime = context.runtime;
131
+ if (key instanceof RubySymbol) {
132
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
133
+ return runtime.newFloat(jx);
134
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
135
+ return runtime.newFloat(jy);
136
+ } else {
137
+ throw runtime.newIndexError("invalid key");
138
+ }
139
+ } else {
140
+ throw runtime.newIndexError("invalid key");
141
+ }
142
+ }
143
+
144
+ /**
145
+ * @param context ThreadContext
146
+ * @param key as symbol
147
+ * @param value as float
148
+ * @return value float
149
+ */
150
+ @JRubyMethod(name = "[]=")
151
+
152
+ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) {
153
+ Ruby runtime = context.runtime;
154
+ if (key instanceof RubySymbol) {
155
+ if (key == RubySymbol.newSymbol(runtime, "x")) {
156
+ return runtime.newFloat(jx);
157
+ } else if (key == RubySymbol.newSymbol(runtime, "y")) {
158
+ return runtime.newFloat(jy);
159
+ }
160
+ } else {
161
+ throw runtime.newIndexError("invalid key");
162
+ }
163
+ return value;
164
+ }
165
+
166
+ /**
167
+ *
168
+ * @param context ThreadContext
169
+ * @param other IRubyObject
170
+ * @return x IRubyObject
171
+ */
172
+ @JRubyMethod(name = "x=")
173
+
174
+ public IRubyObject setX(ThreadContext context, IRubyObject other) {
175
+ if (other instanceof RubyFloat) {
176
+ jx = ((RubyFloat) other).getValue();
177
+ } else {
178
+ jx = ((RubyFixnum) other).getDoubleValue();
179
+ }
180
+ return other;
181
+ }
182
+
183
+ /**
184
+ *
185
+ * @param context ThreadContext
186
+ * @param other IRubyObject
187
+ * @return y IRubyObject
188
+ */
189
+ @JRubyMethod(name = "y=")
190
+
191
+ public IRubyObject setY(ThreadContext context, IRubyObject other) {
192
+ if (other instanceof RubyFloat) {
193
+ jy = ((RubyFloat) other).getValue();
194
+ } else {
195
+ jy = ((RubyFixnum) other).getDoubleValue();
196
+ }
197
+ return other;
198
+ }
199
+
200
+ /**
201
+ *
202
+ * @param context ThreadContext
203
+ * @param other IRubyObject
204
+ * @return distance float
205
+ */
206
+ @JRubyMethod(name = "dist", required = 1)
207
+
208
+ public IRubyObject dist(ThreadContext context, IRubyObject other) {
209
+ Vec2 b = null;
210
+ Ruby runtime = context.runtime;
211
+ if (other instanceof Vec2) {
212
+ b = (Vec2) other.toJava(Vec2.class);
213
+ } else {
214
+ throw runtime.newTypeError("argument should be Vec2D");
215
+ }
216
+ double result = Math.hypot((jx - b.jx), (jy - b.jy));
217
+ return runtime.newFloat(result);
218
+ }
219
+
220
+ /**
221
+ *
222
+ * @param context ThreadContext
223
+ * @param other IRubyObject
224
+ * @return cross product IRubyObject
225
+ */
226
+ @JRubyMethod(name = "cross", required = 1)
227
+
228
+ public IRubyObject cross(ThreadContext context, IRubyObject other) {
229
+ Vec2 b = null;
230
+ Ruby runtime = context.runtime;
231
+ if (other instanceof Vec2) {
232
+ b = (Vec2) other.toJava(Vec2.class);
233
+ } else {
234
+ throw runtime.newTypeError("argument should be Vec2D");
235
+ }
236
+ return runtime.newFloat(jx * b.jy - jy * b.jx);
237
+ }
238
+
239
+ /**
240
+ *
241
+ * @param context ThreadContext
242
+ * @param other IRubyObject
243
+ * @return dot product IRubyObject
244
+ */
245
+ @JRubyMethod(name = "dot", required = 1)
246
+
247
+ public IRubyObject dot(ThreadContext context, IRubyObject other) {
248
+ Vec2 b = null;
249
+ Ruby runtime = context.runtime;
250
+ if (other instanceof Vec2) {
251
+ b = (Vec2) other.toJava(Vec2.class);
252
+ } else {
253
+ throw runtime.newTypeError("argument should be Vec2D");
254
+ }
255
+ return runtime.newFloat(jx * b.jx + jy * b.jy);
256
+ }
257
+
258
+ /**
259
+ *
260
+ * @param context ThreadContext
261
+ * @param other IRubyObject
262
+ * @return new Vec2D object (ruby)
263
+ */
264
+ @JRubyMethod(name = "+", required = 1)
265
+
266
+ public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
267
+ Vec2 b = null;
268
+ Ruby runtime = context.runtime;
269
+ if (other instanceof Vec2) {
270
+ b = (Vec2) other.toJava(Vec2.class);
271
+ } else {
272
+ throw runtime.newTypeError("argument should be Vec2D");
273
+ }
274
+ return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
275
+ runtime.newFloat(jx + b.jx),
276
+ runtime.newFloat(jy + b.jy)});
277
+ }
278
+
279
+ /**
280
+ *
281
+ * @param context ThreadContext
282
+ * @param other IRubyObject
283
+ * @return new Vec2D object (ruby)
284
+ */
285
+ @JRubyMethod(name = "-", required = 1)
286
+
287
+ public IRubyObject op_minus(ThreadContext context, IRubyObject other) {
288
+ Vec2 b = null;
289
+ Ruby runtime = context.runtime;
290
+ if (other instanceof Vec2) {
291
+ b = (Vec2) other.toJava(Vec2.class);
292
+ } else {
293
+ throw runtime.newTypeError("argument should be Vec2D");
294
+ }
295
+ return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
296
+ runtime.newFloat(jx - b.jx),
297
+ runtime.newFloat(jy - b.jy)});
298
+ }
299
+
300
+ /**
301
+ *
302
+ * @param context ThreadContext
303
+ * @param other IRubyObject scalar
304
+ * @return new Vec2D object (ruby)
305
+ */
306
+ @JRubyMethod(name = "*")
307
+
308
+ public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
309
+ Ruby runtime = context.runtime;
310
+ double scalar = (other instanceof RubyFloat)
311
+ ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue();
312
+ return Vec2.rbNew(context, this.getMetaClass(),
313
+ new IRubyObject[]{runtime.newFloat(jx * scalar),
314
+ runtime.newFloat(jy * scalar)});
315
+ }
316
+
317
+ /**
318
+ *
319
+ * @param context ThreadContext
320
+ * @param other IRubyObject scalar
321
+ * @return new Vec2D object (ruby)
322
+ */
323
+ @JRubyMethod(name = "/", required = 1)
324
+
325
+ public IRubyObject op_div(ThreadContext context, IRubyObject other) {
326
+ Ruby runtime = context.runtime;
327
+ double scalar = (other instanceof RubyFloat)
328
+ ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue();
329
+ if (Math.abs(scalar) < Vec2.EPSILON) {
330
+ return this;
331
+ }
332
+ return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
333
+ runtime.newFloat(jx / scalar),
334
+ runtime.newFloat(jy / scalar)});
335
+ }
336
+
337
+ /**
338
+ *
339
+ * @param context ThreadContext
340
+ * @return heading IRubyObject radians
341
+ */
342
+ @JRubyMethod(name = "heading")
343
+ public IRubyObject heading(ThreadContext context) {
344
+ return context.runtime.newFloat(Math.atan2(jy, jx));
345
+ }
346
+
347
+ /**
348
+ *
349
+ * @param context ThreadContext
350
+ * @return magnitude IRubyObject
351
+ */
352
+ @JRubyMethod(name = "mag")
353
+
354
+ public IRubyObject mag(ThreadContext context) {
355
+ double result = 0;
356
+ if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
357
+ result = Math.hypot(jx, jy);
358
+ } else {
359
+ if (Math.abs(jy) > EPSILON) {
360
+ result = Math.abs(jy);
361
+ }
362
+ if (Math.abs(jx) > EPSILON) {
363
+ result = Math.abs(jx);
364
+ }
365
+ }
366
+ return context.runtime.newFloat(result);
367
+ }
368
+
369
+ /**
370
+ * Call yield if block given, do nothing if yield == false else set_mag to
371
+ * given scalar
372
+ *
373
+ * @param context ThreadContext
374
+ * @param scalar double value to set
375
+ * @param block should return a boolean (optional)
376
+ * @return magnitude IRubyObject
377
+ */
378
+ @JRubyMethod(name = "set_mag")
379
+
380
+ public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
381
+ double new_mag = (Double) scalar.toJava(Double.class);
382
+ if (block.isGiven()) {
383
+ if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
384
+ return this;
385
+ }
386
+ }
387
+ double current = 0;
388
+ if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
389
+ current = Math.hypot(jx, jy);
390
+ } else {
391
+ if (Math.abs(jy) > EPSILON) {
392
+ current = Math.abs(jy);
393
+ }
394
+ if (Math.abs(jx) > EPSILON) {
395
+ current = Math.abs(jx);
396
+ }
397
+ }
398
+ if (current > 0) {
399
+ jx *= new_mag / current;
400
+ jy *= new_mag / current;
401
+ }
327
402
  return this;
328
- }
329
- }
330
- double current = 0;
331
- if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
332
- current = Math.hypot(jx, jy);
333
- }
334
- else{
335
- if (Math.abs(jy) > EPSILON) {
336
- current = Math.abs(jy);
337
- }
338
- if (Math.abs(jx) > EPSILON) {
339
- current = Math.abs(jx);
340
- }
341
- }
342
- if (current > 0) {
343
- jx *= new_mag / current;
344
- jy *= new_mag / current;
345
- }
346
- return this;
347
- }
348
-
349
- /**
350
- *
351
- * @param context
352
- * @return this as a ruby object
353
- */
354
- @JRubyMethod(name = "normalize!")
355
-
356
- public IRubyObject normalize_bang(ThreadContext context) {
357
- double mag = 0;
358
- if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
359
- mag = Math.hypot(jx, jy);
360
- }
361
- else{
362
- if (Math.abs(jx) > EPSILON) {
363
- mag = Math.abs(jx);
364
- }
365
- if (Math.abs(jy) > EPSILON) {
366
- mag = Math.abs(jy);
367
- }
368
- }
369
- if (mag > 0) {
370
- jx /= mag;
371
- jy /= mag;
372
- }
373
- return this;
374
- }
375
-
376
- /**
377
- *
378
- * @param context
379
- * @return new Vec2 object (ruby)
380
- */
381
- @JRubyMethod(name = "normalize")
382
-
383
- public IRubyObject normalize(ThreadContext context) {
384
- double mag = 0;
385
- Ruby runtime = context.getRuntime();
386
- if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
387
- mag = Math.hypot(jx, jy);
388
- }
389
- else{
390
- if (Math.abs(jx) > EPSILON) {
391
- mag = jx;
392
- }
393
- if (Math.abs(jy) > EPSILON) {
394
- mag = jy;
395
- }
396
- }
397
- if (mag < EPSILON) {
398
- mag = 1.0;
399
- }
400
- return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
401
- runtime.newFloat(jx / mag),
402
- runtime.newFloat(jy / mag)});
403
- }
404
-
405
- /**
406
- * Example of a regular ruby class method Use Math rather than RadLut
407
- * here!!!
408
- *
409
- * @param context
410
- * @param klazz
411
- * @param other input angle in radians
412
- * @return new Vec2 object (ruby)
413
- */
414
- @JRubyMethod(name = "from_angle", meta = true)
415
- public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject other) {
416
- Ruby runtime = context.getRuntime();
417
- double scalar = (Double) other.toJava(Double.class);
418
- return Vec2.rbNew(context, klazz, new IRubyObject[]{
419
- runtime.newFloat(Math.cos(scalar)),
420
- runtime.newFloat(Math.sin(scalar))});
421
- }
422
-
423
- /**
424
- *
425
- * @param context
426
- * @param other
427
- * @return this Vec2 object rotated
428
- */
429
- @JRubyMethod(name = "rotate!")
430
- public IRubyObject rotate_bang(ThreadContext context, IRubyObject other) {
431
- double theta = (Double) other.toJava(Double.class);
432
- double x = (jx * Math.cos(theta) - jy * Math.sin(theta));
433
- double y = (jx * Math.sin(theta) + jy * Math.cos(theta));
434
- jx = x;
435
- jy = y;
436
- return this;
437
- }
438
-
439
-
440
-
441
- /**
442
- *
443
- * @param context
444
- * @param other
445
- * @return a new Vec2 object rotated
446
- */
447
- @JRubyMethod(name = "rotate")
448
- public IRubyObject rotate(ThreadContext context, IRubyObject other) {
449
- Ruby runtime = context.getRuntime();
450
- double theta = (Double) other.toJava(Double.class);
451
- IRubyObject[] ary = new IRubyObject[]{
452
- runtime.newFloat(jx * Math.cos(theta) - jy * Math.sin(theta)),
453
- runtime.newFloat(jx * Math.sin(theta) + jy * Math.cos(theta))};
454
- return Vec2.rbNew(context, this.getMetaClass(), ary);
455
- }
456
-
457
- /**
458
- *
459
- * @param context
460
- * @param args
461
- * @return as a new Vec2 object (ruby)
462
- */
463
- @JRubyMethod(name = "lerp", rest = true)
464
- public IRubyObject lerp(ThreadContext context, IRubyObject[] args) {
465
- Ruby runtime = context.getRuntime();
466
- Arity.checkArgumentCount(runtime, args, 2, 2);
467
- Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
468
- double scalar = (Double) args[1].toJava(Double.class);
469
- assert (scalar >= 0 && scalar < 1.0) :
470
- "Lerp value " + scalar + " out of range 0 .. 1.0";
471
- return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
472
- runtime.newFloat(jx + (vec.jx - jx) * scalar),
473
- runtime.newFloat(jy + (vec.jy - jy) * scalar)});
474
- }
475
-
476
- /**
477
- *
478
- * @param context
479
- * @param args
480
- * @return this
481
- */
482
- @JRubyMethod(name = "lerp!", rest = true)
483
- public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) {
484
- Arity.checkArgumentCount(context.getRuntime(), args, 2, 2);
485
- Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
486
- double scalar = (Double) args[1].toJava(Double.class);
487
- assert (scalar >= 0 && scalar < 1.0) :
488
- "Lerp value " + scalar + " out of range 0 .. 1.0";
489
- jx += (vec.jx - jx) * scalar;
490
- jy += (vec.jy - jy) * scalar;
491
- return this;
492
- }
493
-
494
- /**
495
- *
496
- * @param context
497
- * @param other
498
- * @return theta radians float
499
- */
500
- @JRubyMethod(name = "angle_between")
501
-
502
- public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
503
- Vec2 vec = null;
504
- Ruby runtime = context.getRuntime();
505
- if (other instanceof Vec2) {
506
- vec = (Vec2) other.toJava(Vec2.class);
507
- } else {
508
- throw runtime.newTypeError("argument should be Vec2D");
509
- }
510
- return runtime.newFloat(Math.atan2(jx - vec.jx, jy - vec.jy));
511
- }
403
+ }
512
404
 
513
- /**
514
- * Example of a regular ruby class method Use Math rather than RadLut
515
- * here!!!
516
- *
517
- * @param context
518
- * @param klazz
519
- * @return new Vec2 object (ruby)
520
- */
521
- @JRubyMethod(name = "random", meta = true)
522
- public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
523
- Ruby runtime = context.getRuntime();
524
- double angle = Math.random() * Math.PI * 2;
525
- return Vec2.rbNew(context, klazz, new IRubyObject[]{
526
- runtime.newFloat(Math.cos(angle)),
527
- runtime.newFloat(Math.sin(angle))});
528
- }
529
-
530
- /**
531
- *
532
- * @param context
533
- * @return new copy
534
- */
535
- @JRubyMethod(name = {"copy", "dup"})
536
-
537
- public IRubyObject copy(ThreadContext context) {
538
- Ruby runtime = context.runtime;
539
- return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
540
- runtime.newFloat(jx),
541
- runtime.newFloat(jy)});
542
- }
543
-
544
- /**
545
- *
546
- * @param context
547
- * @return ruby array
548
- */
549
- @JRubyMethod(name = "to_a")
550
-
551
- public IRubyObject toArray(ThreadContext context) {
552
- Ruby runtime = context.runtime;
553
- return RubyArray.newArray(context.getRuntime(), new IRubyObject[]{
554
- runtime.newFloat(jx),
555
- runtime.newFloat(jy)});
556
- }
557
-
558
- /**
559
- *
560
- * @param context
561
- * @param object
562
- */
563
- @JRubyMethod(name = "to_vertex")
564
-
565
- public void toVertex(ThreadContext context, IRubyObject object) {
566
- JRender renderer = (JRender) object.toJava(JRender.class);
567
- renderer.vertex(jx, jy);
568
- }
569
-
570
- /**
571
- *
572
- * @param context
573
- * @param object
574
- */
575
- @JRubyMethod(name = "to_curve_vertex")
576
-
577
- public void toCurveVertex(ThreadContext context, IRubyObject object) {
578
- JRender renderer = (JRender) object.toJava(JRender.class);
579
- renderer.curveVertex(jx, jy);
580
- }
581
-
582
-
583
- /**
584
- * For jruby-9000 we alias to inspect
585
- * @param context
586
- * @return custom to string (inspect)
587
- */
588
- @JRubyMethod(name = {"to_s", "inspect"})
589
-
590
- public IRubyObject to_s(ThreadContext context) {
591
- return context.getRuntime().newString(String.format("Vec2D(x = %4.4f, y = %4.4f)", jx, jy));
592
- }
593
-
594
- /**
595
- *
596
- * @return hash int
597
- */
598
- @Override
599
- public int hashCode() {
600
- int hash = 5;
601
- hash = 53 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
602
- hash = 53 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
603
- return hash;
604
- }
605
-
606
- /**
607
- *
608
- * @param obj
609
- * @return ruby boolean
610
- */
611
- @Override
612
- public boolean equals(Object obj) {
613
- if (obj instanceof Vec2){
614
- final Vec2 other = (Vec2) obj;
615
- if (!((Double)this.jx).equals(other.jx)) {
405
+ /**
406
+ *
407
+ * @param context ThreadContext
408
+ * @return this as a ruby object
409
+ */
410
+ @JRubyMethod(name = "normalize!")
411
+
412
+ public IRubyObject normalize_bang(ThreadContext context) {
413
+ double mag = 0;
414
+ if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
415
+ mag = Math.hypot(jx, jy);
416
+ } else {
417
+ if (Math.abs(jx) > EPSILON) {
418
+ mag = Math.abs(jx);
419
+ }
420
+ if (Math.abs(jy) > EPSILON) {
421
+ mag = Math.abs(jy);
422
+ }
423
+ }
424
+ if (mag > 0) {
425
+ jx /= mag;
426
+ jy /= mag;
427
+ }
428
+ return this;
429
+ }
430
+
431
+ /**
432
+ *
433
+ * @param context ThreadContext
434
+ * @return new normalized Vec3D object (ruby)
435
+ */
436
+ @JRubyMethod(name = "normalize")
437
+
438
+ public IRubyObject normalize(ThreadContext context) {
439
+ double mag = 0;
440
+ Ruby runtime = context.runtime;
441
+ if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
442
+ mag = Math.hypot(jx, jy);
443
+ } else {
444
+ if (Math.abs(jx) > EPSILON) {
445
+ mag = jx;
446
+ }
447
+ if (Math.abs(jy) > EPSILON) {
448
+ mag = jy;
449
+ }
450
+ }
451
+ if (mag < EPSILON) {
452
+ mag = 1.0;
453
+ }
454
+ return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
455
+ runtime.newFloat(jx / mag),
456
+ runtime.newFloat(jy / mag)});
457
+ }
458
+
459
+ /**
460
+ * Example of a regular ruby class method Use Math rather than RadLut
461
+ * here!!!
462
+ *
463
+ * @param context ThreadContext
464
+ * @param klazz IRubyObject
465
+ * @param scalar input angle in radians
466
+ * @return new Vec2 object (ruby)
467
+ */
468
+ @JRubyMethod(name = "from_angle", meta = true)
469
+ public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) {
470
+ Ruby runtime = context.runtime;
471
+ double angle = (scalar instanceof RubyFloat)
472
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
473
+ return Vec2.rbNew(context, klazz, new IRubyObject[]{
474
+ runtime.newFloat(Math.cos(angle)),
475
+ runtime.newFloat(Math.sin(angle))});
476
+ }
477
+
478
+ /**
479
+ * Example of a regular ruby class method
480
+ *
481
+ * @param context ThreadContext
482
+ * @param klazz IRubyObject
483
+ * @return new Vec2 object (ruby)
484
+ */
485
+ @JRubyMethod(name = "random", meta = true)
486
+ public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
487
+ Ruby runtime = context.runtime;
488
+ double angle = Math.random() * Math.PI * 2;
489
+ return Vec2.rbNew(context, klazz, new IRubyObject[]{
490
+ runtime.newFloat(Math.cos(angle)),
491
+ runtime.newFloat(Math.sin(angle))});
492
+ }
493
+
494
+ /**
495
+ *
496
+ * @param context ThreadContext
497
+ * @param scalar IRubyObject
498
+ * @return this Vec2 object rotated
499
+ */
500
+ @JRubyMethod(name = "rotate!")
501
+ public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) {
502
+ double theta = (scalar instanceof RubyFloat)
503
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
504
+ double x = (jx * Math.cos(theta) - jy * Math.sin(theta));
505
+ double y = (jx * Math.sin(theta) + jy * Math.cos(theta));
506
+ jx = x;
507
+ jy = y;
508
+ return this;
509
+ }
510
+
511
+ /**
512
+ *
513
+ * @param context ThreadContext
514
+ * @param scalar IRubyObject
515
+ * @return a new Vec2 object rotated
516
+ */
517
+ @JRubyMethod(name = "rotate")
518
+ public IRubyObject rotate(ThreadContext context, IRubyObject scalar) {
519
+ Ruby runtime = context.runtime;
520
+ double theta = (scalar instanceof RubyFloat)
521
+ ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
522
+ IRubyObject[] ary = new IRubyObject[]{
523
+ runtime.newFloat(jx * Math.cos(theta) - jy * Math.sin(theta)),
524
+ runtime.newFloat(jx * Math.sin(theta) + jy * Math.cos(theta))};
525
+ return Vec2.rbNew(context, this.getMetaClass(), ary);
526
+ }
527
+
528
+ /**
529
+ *
530
+ * @param context ThreadContext
531
+ * @param args IRubyObject[]
532
+ * @return as a new Vec2 object (ruby)
533
+ */
534
+ @JRubyMethod(name = "lerp", rest = true)
535
+ public IRubyObject lerp(ThreadContext context, IRubyObject[] args) {
536
+ Ruby runtime = context.runtime;
537
+ Arity.checkArgumentCount(runtime, args, 2, 2);
538
+ Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
539
+ double scalar = (args[1] instanceof RubyFloat)
540
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
541
+ assert (scalar >= 0 && scalar < 1.0) :
542
+ "Lerp value " + scalar + " out of range 0..1.0";
543
+ return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
544
+ runtime.newFloat(jx + (vec.jx - jx) * scalar),
545
+ runtime.newFloat(jy + (vec.jy - jy) * scalar)});
546
+ }
547
+
548
+ /**
549
+ *
550
+ * @param context ThreadContext
551
+ * @param args IRubyObject[]
552
+ * @return this IRubyObject
553
+ */
554
+ @JRubyMethod(name = "lerp!", rest = true)
555
+ public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) {
556
+ Arity.checkArgumentCount(context.runtime, args, 2, 2);
557
+ Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
558
+ double scalar = (args[1] instanceof RubyFloat)
559
+ ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
560
+ assert (scalar >= 0 && scalar < 1.0) :
561
+ "Lerp value " + scalar + " out of range 0..1.0";
562
+ jx += (vec.jx - jx) * scalar;
563
+ jy += (vec.jy - jy) * scalar;
564
+ return this;
565
+ }
566
+
567
+ /**
568
+ *
569
+ * @param context ThreadContext
570
+ * @param other IRubyObject another Vec3D
571
+ * @return angle IRubyObject in radians
572
+ */
573
+ @JRubyMethod(name = "angle_between")
574
+
575
+ public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
576
+ Vec2 vec = null;
577
+ Ruby runtime = context.runtime;
578
+ if (other instanceof Vec2) {
579
+ vec = (Vec2) other.toJava(Vec2.class);
580
+ } else {
581
+ throw runtime.newTypeError("argument should be Vec2D");
582
+ }
583
+ return runtime.newFloat(Math.atan2(jx - vec.jx, jy - vec.jy));
584
+ }
585
+
586
+ /**
587
+ *
588
+ * @param context ThreadContext
589
+ * @return IRubyObject copy
590
+ */
591
+ @JRubyMethod(name = {"copy", "dup"})
592
+
593
+ public IRubyObject copy(ThreadContext context) {
594
+ Ruby runtime = context.runtime;
595
+ return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
596
+ runtime.newFloat(jx),
597
+ runtime.newFloat(jy)});
598
+ }
599
+
600
+ /**
601
+ *
602
+ * @param context ThreadContext
603
+ * @return IRubyObject array of float
604
+ */
605
+ @JRubyMethod(name = "to_a")
606
+
607
+ public IRubyObject toArray(ThreadContext context) {
608
+ Ruby runtime = context.runtime;
609
+ return RubyArray.newArray(runtime, new IRubyObject[]{
610
+ runtime.newFloat(jx),
611
+ runtime.newFloat(jy)});
612
+ }
613
+
614
+ /**
615
+ *
616
+ * To vertex
617
+ *
618
+ * @param context ThreadContext
619
+ * @param object IRubyObject vertex renderer
620
+ */
621
+ @JRubyMethod(name = "to_vertex")
622
+
623
+ public void toVertex(ThreadContext context, IRubyObject object) {
624
+ JRender renderer = (JRender) object.toJava(JRender.class);
625
+ renderer.vertex(jx, jy);
626
+ }
627
+
628
+ /**
629
+ *
630
+ * To curve vertex
631
+ *
632
+ * @param context ThreadContext
633
+ * @param object IRubyObject vertex renderer
634
+ */
635
+ @JRubyMethod(name = "to_curve_vertex")
636
+
637
+ public void toCurveVertex(ThreadContext context, IRubyObject object) {
638
+ JRender renderer = (JRender) object.toJava(JRender.class);
639
+ renderer.curveVertex(jx, jy);
640
+ }
641
+
642
+ /**
643
+ * For jruby-9000 we alias to inspect
644
+ *
645
+ * @param context ThreadContext
646
+ * @return IRubyObject to_s
647
+ */
648
+ @JRubyMethod(name = {"to_s", "inspect"})
649
+
650
+ public IRubyObject to_s(ThreadContext context) {
651
+ return context.runtime.newString(String.format("Vec2D(x = %4.4f, y = %4.4f)", jx, jy));
652
+ }
653
+
654
+ /**
655
+ *
656
+ * @return hash int
657
+ */
658
+ @Override
659
+ public int hashCode() {
660
+ int hash = 5;
661
+ hash = 53 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
662
+ hash = 53 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
663
+ return hash;
664
+ }
665
+
666
+ /**
667
+ *
668
+ * Java Equals
669
+ *
670
+ * @param obj Object
671
+ * @return result boolean
672
+ */
673
+ @Override
674
+ public boolean equals(Object obj) {
675
+ if (obj == this) {
676
+ return true;
677
+ }
678
+ if (obj instanceof Vec2) {
679
+ final Vec2 other = (Vec2) obj;
680
+ if ((Double.compare(jx, (Double) other.jx) == 0)
681
+ && (Double.compare(jy, (Double) other.jy) == 0)) {
682
+ return true;
683
+ }
684
+ }
616
685
  return false;
617
- }
618
- return ((Double)this.jy).equals(other.jy);
619
- }
620
- return false;
621
- }
622
-
623
- /**
624
- *
625
- * @param context
626
- * @param other
627
- * @return ruby boolean
628
- */
629
- @JRubyMethod(name = "eql?", required = 1)
630
- public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
631
- Ruby runtime = context.getRuntime();
632
- if (other instanceof Vec2){
633
- Vec2 v = (Vec2) other.toJava(Vec2.class);
634
- if (!((Double)this.jx).equals(v.jx)) {
635
- return RubyBoolean.newBoolean(runtime, false);
636
- }
637
- return RubyBoolean.newBoolean(runtime, ((Double)this.jy).equals(v.jy));
638
- }
639
- return RubyBoolean.newBoolean(runtime, false);
640
- }
641
-
642
- /**
643
- *
644
- * @param context
645
- * @param other
646
- * @return ruby boolean
647
- */
648
- @JRubyMethod(name = "==", required = 1)
649
-
650
- @Override
651
- public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
652
- Ruby runtime = context.getRuntime();
653
- if (other instanceof Vec2) {
654
- Vec2 v = (Vec2) other.toJava(Vec2.class);
655
- double diff = jx - v.jx;
656
- if ((diff < 0 ? -diff : diff) > Vec2.EPSILON) {
657
- return RubyBoolean.newBoolean(runtime, false);
658
- }
659
- diff = jy - v.jy;
660
- boolean result = ((diff < 0 ? -diff : diff) < Vec2.EPSILON);
661
- return RubyBoolean.newBoolean(runtime, result);
662
- }
663
- return RubyBoolean.newBoolean(runtime, false);
664
- }
686
+ }
687
+
688
+ /**
689
+ *
690
+ * @param context ThreadContext
691
+ * @param other IRubyObject
692
+ * @return result IRubyObject as boolean
693
+ */
694
+ @JRubyMethod(name = "eql?", required = 1)
695
+ public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
696
+ Ruby runtime = context.runtime;
697
+ if (other == this) {
698
+ return runtime.newBoolean(true);
699
+ }
700
+ if (other instanceof Vec2) {
701
+ Vec2 v = (Vec2) other.toJava(Vec2.class);
702
+ if ((Double.compare(jx, (Double) v.jx) == 0)
703
+ && (Double.compare(jy, (Double) v.jy) == 0)) {
704
+ return runtime.newBoolean(true);
705
+ }
706
+ }
707
+ return runtime.newBoolean(false);
708
+ }
709
+
710
+ /**
711
+ *
712
+ * @param context ThreadContext
713
+ * @param other IRubyObject
714
+ * @return result IRubyObject as boolean
715
+ */
716
+ @JRubyMethod(name = "==", required = 1)
717
+
718
+ @Override
719
+ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
720
+ Ruby runtime = context.runtime;
721
+ if (other == this) {
722
+ return runtime.newBoolean(true);
723
+ }
724
+ if (other instanceof Vec2) {
725
+ Vec2 v = (Vec2) other.toJava(Vec2.class);
726
+ double diff = jx - v.jx;
727
+ if ((diff < 0 ? -diff : diff) > Vec2.EPSILON) {
728
+ return runtime.newBoolean(false);
729
+ }
730
+ diff = jy - v.jy;
731
+ return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec2.EPSILON);
732
+ }
733
+ return runtime.newBoolean(false);
734
+ }
665
735
  }