propane 0.6.0-java → 0.7.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +1 -1
- data/.travis.yml +12 -4
- data/CHANGELOG.md +1 -0
- data/Rakefile +1 -1
- data/examples/complete/landscape.rb +1 -1
- data/examples/regular/colors_two.rb +1 -3
- data/examples/regular/tree.rb +0 -1
- data/examples/regular/vector_math.rb +4 -3
- data/lib/propane/app.rb +1 -4
- data/lib/propane/helpers/numeric.rb +2 -2
- data/lib/propane/runner.rb +32 -8
- data/lib/propane/version.rb +1 -1
- data/pom.rb +5 -5
- data/pom.xml +6 -6
- data/propane.gemspec +1 -1
- data/src/monkstone/CodeListener.java +7 -0
- data/src/monkstone/{MathTool.java → MathToolModule.java} +59 -68
- data/src/monkstone/PropaneLibrary.java +1 -1
- data/src/monkstone/WatchSketchDir.java +190 -0
- data/src/monkstone/fastmath/Deglut.java +47 -38
- data/src/monkstone/fastmath/package-info.java +6 -0
- data/src/monkstone/vecmath/AppRender.java +19 -20
- data/src/monkstone/vecmath/JRender.java +19 -19
- data/src/monkstone/vecmath/ShapeRender.java +19 -19
- data/src/monkstone/vecmath/package-info.java +20 -0
- data/src/monkstone/vecmath/vec2/Vec2.java +694 -624
- data/src/monkstone/vecmath/vec2/package-info.java +6 -0
- data/src/monkstone/vecmath/vec3/Vec3.java +704 -669
- data/src/monkstone/vecmath/vec3/package-info.java +6 -0
- data/vendors/Rakefile +2 -2
- metadata +11 -5
@@ -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 (
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
Vec2
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
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
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
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
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
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
|
}
|