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