picrate 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +47 -0
- data/.mvn/extensions.xml +9 -0
- data/.mvn/wrapper/maven-wrapper.properties +1 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE.md +165 -0
- data/README.md +51 -0
- data/Rakefile +59 -0
- data/bin/picrate +8 -0
- data/docs/.gitignore +6 -0
- data/docs/_config.yml +30 -0
- data/docs/_includes/footer.html +38 -0
- data/docs/_includes/head.html +16 -0
- data/docs/_includes/header.html +27 -0
- data/docs/_includes/icon-github.html +1 -0
- data/docs/_includes/icon-github.svg +1 -0
- data/docs/_includes/icon-twitter.html +1 -0
- data/docs/_includes/icon-twitter.svg +1 -0
- data/docs/_includes/navigation.html +24 -0
- data/docs/_layouts/default.html +20 -0
- data/docs/_layouts/page.html +14 -0
- data/docs/_layouts/post.html +15 -0
- data/docs/_posts/2018-05-06-getting_started.md +8 -0
- data/docs/_posts/2018-05-06-install_jruby.md +35 -0
- data/docs/_sass/_base.scss +206 -0
- data/docs/_sass/_layout.scss +242 -0
- data/docs/_sass/_syntax-highlighting.scss +71 -0
- data/docs/about.md +10 -0
- data/docs/css/main.scss +38 -0
- data/docs/favicon.ico +0 -0
- data/docs/feed.xml +30 -0
- data/docs/index.html +38 -0
- data/lib/picrate.rb +10 -0
- data/lib/picrate/app.rb +187 -0
- data/lib/picrate/creators/sketch_class.rb +57 -0
- data/lib/picrate/creators/sketch_factory.rb +12 -0
- data/lib/picrate/creators/sketch_writer.rb +21 -0
- data/lib/picrate/helper_methods.rb +214 -0
- data/lib/picrate/helpers/numeric.rb +9 -0
- data/lib/picrate/library.rb +69 -0
- data/lib/picrate/library_loader.rb +53 -0
- data/lib/picrate/native_folder.rb +35 -0
- data/lib/picrate/native_loader.rb +27 -0
- data/lib/picrate/runner.rb +81 -0
- data/lib/picrate/version.rb +4 -0
- data/library/boids/boids.rb +209 -0
- data/library/chooser/chooser.rb +19 -0
- data/library/control_panel/control_panel.rb +182 -0
- data/library/library_proxy/README.md +99 -0
- data/library/library_proxy/library_proxy.rb +14 -0
- data/library/slider/slider.rb +42 -0
- data/library/vector_utils/vector_utils.rb +69 -0
- data/library/video_event/video_event.rb +3 -0
- data/license.txt +508 -0
- data/picrate.gemspec +35 -0
- data/pom.rb +122 -0
- data/pom.xml +214 -0
- data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
- data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
- data/src/main/java/monkstone/ColorUtil.java +115 -0
- data/src/main/java/monkstone/MathToolModule.java +236 -0
- data/src/main/java/monkstone/PicrateLibrary.java +47 -0
- data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
- data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
- data/src/main/java/monkstone/fastmath/package-info.java +6 -0
- data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
- data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
- data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
- data/src/main/java/monkstone/slider/Slider.java +67 -0
- data/src/main/java/monkstone/slider/SliderBar.java +277 -0
- data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
- data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
- data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
- data/src/main/java/monkstone/vecmath/JRender.java +56 -0
- data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
- data/src/main/java/monkstone/vecmath/package-info.java +20 -0
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
- data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
- data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
- data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
- data/src/main/java/monkstone/videoevent/package-info.java +20 -0
- data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
- data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
- data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
- data/src/main/java/processing/core/PApplet.java +17647 -0
- data/src/main/java/processing/core/PConstants.java +1033 -0
- data/src/main/java/processing/core/PFont.java +1250 -0
- data/src/main/java/processing/core/PGraphics.java +9614 -0
- data/src/main/java/processing/core/PImage.java +3608 -0
- data/src/main/java/processing/core/PMatrix.java +347 -0
- data/src/main/java/processing/core/PMatrix2D.java +694 -0
- data/src/main/java/processing/core/PMatrix3D.java +1153 -0
- data/src/main/java/processing/core/PShape.java +4332 -0
- data/src/main/java/processing/core/PShapeOBJ.java +544 -0
- data/src/main/java/processing/core/PShapeSVG.java +1987 -0
- data/src/main/java/processing/core/PStyle.java +208 -0
- data/src/main/java/processing/core/PSurface.java +242 -0
- data/src/main/java/processing/core/PSurfaceNone.java +479 -0
- data/src/main/java/processing/core/PVector.java +1140 -0
- data/src/main/java/processing/data/FloatDict.java +829 -0
- data/src/main/java/processing/data/FloatList.java +912 -0
- data/src/main/java/processing/data/IntDict.java +796 -0
- data/src/main/java/processing/data/IntList.java +913 -0
- data/src/main/java/processing/data/JSONArray.java +1260 -0
- data/src/main/java/processing/data/JSONObject.java +2282 -0
- data/src/main/java/processing/data/JSONTokener.java +435 -0
- data/src/main/java/processing/data/Sort.java +46 -0
- data/src/main/java/processing/data/StringDict.java +601 -0
- data/src/main/java/processing/data/StringList.java +775 -0
- data/src/main/java/processing/data/Table.java +4923 -0
- data/src/main/java/processing/data/TableRow.java +198 -0
- data/src/main/java/processing/data/XML.java +1149 -0
- data/src/main/java/processing/event/Event.java +108 -0
- data/src/main/java/processing/event/KeyEvent.java +70 -0
- data/src/main/java/processing/event/MouseEvent.java +149 -0
- data/src/main/java/processing/event/TouchEvent.java +57 -0
- data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
- data/src/main/java/processing/opengl/FontTexture.java +379 -0
- data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
- data/src/main/java/processing/opengl/LinePath.java +623 -0
- data/src/main/java/processing/opengl/LineStroker.java +685 -0
- data/src/main/java/processing/opengl/PGL.java +3366 -0
- data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
- data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
- data/src/main/java/processing/opengl/PJOGL.java +1966 -0
- data/src/main/java/processing/opengl/PShader.java +1478 -0
- data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
- data/src/main/java/processing/opengl/Texture.java +1670 -0
- data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
- data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
- data/src/main/java/processing/opengl/cursors/cross.png +0 -0
- data/src/main/java/processing/opengl/cursors/hand.png +0 -0
- data/src/main/java/processing/opengl/cursors/license.txt +27 -0
- data/src/main/java/processing/opengl/cursors/move.png +0 -0
- data/src/main/java/processing/opengl/cursors/text.png +0 -0
- data/src/main/java/processing/opengl/cursors/wait.png +0 -0
- data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
- data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
- data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
- data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
- data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
- data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
- data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
- data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
- data/src/main/resources/icon/icon-1024.png +0 -0
- data/src/main/resources/icon/icon-128.png +0 -0
- data/src/main/resources/icon/icon-16.png +0 -0
- data/src/main/resources/icon/icon-256.png +0 -0
- data/src/main/resources/icon/icon-32.png +0 -0
- data/src/main/resources/icon/icon-48.png +0 -0
- data/src/main/resources/icon/icon-512.png +0 -0
- data/src/main/resources/icon/icon-64.png +0 -0
- data/src/main/resources/license.txt +508 -0
- data/test/create_test.rb +68 -0
- data/test/deglut_spec_test.rb +24 -0
- data/test/helper_methods_test.rb +58 -0
- data/test/math_tool_test.rb +75 -0
- data/test/respond_to_test.rb +215 -0
- data/test/sketches/key_event.rb +37 -0
- data/test/sketches/library/my_library/my_library.rb +32 -0
- data/test/test_helper.rb +3 -0
- data/test/vecmath_spec_test.rb +522 -0
- data/vendors/Rakefile +127 -0
- metadata +289 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2016-18 Martin Prout
|
3
|
+
*
|
4
|
+
* This library is free software; you can redistribute it and/or
|
5
|
+
* modify it under the terms of the GNU General Public
|
6
|
+
* License as published by the Free Software Foundation; either
|
7
|
+
* version 3.0 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 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.slider;
|
21
|
+
|
22
|
+
import java.util.ArrayList;
|
23
|
+
import java.util.List;
|
24
|
+
import processing.core.PApplet;
|
25
|
+
|
26
|
+
/**
|
27
|
+
*
|
28
|
+
* @author tux
|
29
|
+
*/
|
30
|
+
public class SliderGroup {
|
31
|
+
|
32
|
+
int count = 0;
|
33
|
+
List<Slider> sliders;
|
34
|
+
PApplet applet;
|
35
|
+
boolean vertical;
|
36
|
+
|
37
|
+
/**
|
38
|
+
*
|
39
|
+
* @param outer
|
40
|
+
*/
|
41
|
+
public SliderGroup(final PApplet outer) {
|
42
|
+
applet = outer;
|
43
|
+
sliders = new ArrayList<>();
|
44
|
+
vertical = false;
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
*
|
49
|
+
*/
|
50
|
+
public void vertical() {
|
51
|
+
vertical = true;
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
*
|
56
|
+
* @param beginRange
|
57
|
+
* @param endRange
|
58
|
+
* @param initial
|
59
|
+
*/
|
60
|
+
public void addSlider(float beginRange, float endRange, float initial) {
|
61
|
+
if (vertical) {
|
62
|
+
sliders.add(new SimpleVerticalSlider(applet, beginRange, endRange, initial, count));
|
63
|
+
} else {
|
64
|
+
sliders.add(new SimpleHorizontalSlider(applet, beginRange, endRange, initial, count));
|
65
|
+
}
|
66
|
+
count = sliders.size();
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
*
|
71
|
+
* @param count
|
72
|
+
* @return
|
73
|
+
*/
|
74
|
+
public float readValue(int count) {
|
75
|
+
return sliders.get(count).readValue();
|
76
|
+
}
|
77
|
+
|
78
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2016-18 Martin Prout
|
3
|
+
*
|
4
|
+
* This library is free software; you can redistribute it and/or
|
5
|
+
* modify it under the terms of the GNU General Public
|
6
|
+
* License as published by the Free Software Foundation; either
|
7
|
+
* version 3.0 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 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.slider;
|
21
|
+
|
22
|
+
/**
|
23
|
+
*
|
24
|
+
* from a borrowed pattern seen in Jonathan Feinbergs Peasycam
|
25
|
+
* when I was struggling with non functioning browser applet,
|
26
|
+
* probably superfluous here.
|
27
|
+
*/
|
28
|
+
public interface WheelHandler {
|
29
|
+
/**
|
30
|
+
*
|
31
|
+
* @param amount int
|
32
|
+
*/
|
33
|
+
|
34
|
+
public void handleWheel(final short amount);
|
35
|
+
}
|
@@ -0,0 +1,87 @@
|
|
1
|
+
package monkstone.vecmath;
|
2
|
+
|
3
|
+
import processing.core.PApplet;
|
4
|
+
import processing.core.PGraphics;
|
5
|
+
|
6
|
+
/**
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
public class AppRender implements JRender {
|
11
|
+
|
12
|
+
final PGraphics g;
|
13
|
+
|
14
|
+
/**
|
15
|
+
*
|
16
|
+
* @param app PApplet
|
17
|
+
*/
|
18
|
+
public AppRender(final PApplet app) {
|
19
|
+
this.g = app.g;
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
*
|
24
|
+
* @param x double
|
25
|
+
* @param y double
|
26
|
+
*/
|
27
|
+
@Override
|
28
|
+
public void vertex(double x, double y) {
|
29
|
+
g.vertex((float) x, (float) y);
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
*
|
34
|
+
* @param x double
|
35
|
+
* @param y double
|
36
|
+
*/
|
37
|
+
@Override
|
38
|
+
public void curveVertex(double x, double y) {
|
39
|
+
g.curveVertex((float) x, (float) y);
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
*
|
44
|
+
* @param x double
|
45
|
+
* @param y double
|
46
|
+
* @param z double
|
47
|
+
*/
|
48
|
+
@Override
|
49
|
+
public void vertex(double x, double y, double z) {
|
50
|
+
g.vertex((float) x, (float) y, (float) z);
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
*
|
55
|
+
* @param x double
|
56
|
+
* @param y double
|
57
|
+
* @param z double
|
58
|
+
*/
|
59
|
+
@Override
|
60
|
+
public void normal(double x, double y, double z) {
|
61
|
+
g.normal((float) x, (float) y, (float) z);
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
*
|
66
|
+
* @param x double
|
67
|
+
* @param y double
|
68
|
+
* @param z double
|
69
|
+
* @param u double
|
70
|
+
* @param v double
|
71
|
+
*/
|
72
|
+
@Override
|
73
|
+
public void vertex(double x, double y, double z, double u, double v) {
|
74
|
+
g.vertex((float) x, (float) y, (float) z, (float) u, (float) v);
|
75
|
+
}
|
76
|
+
|
77
|
+
/**
|
78
|
+
*
|
79
|
+
* @param x double
|
80
|
+
* @param y double
|
81
|
+
* @param z double
|
82
|
+
*/
|
83
|
+
@Override
|
84
|
+
public void curveVertex(double x, double y, double z) {
|
85
|
+
g.curveVertex((float) x, (float) y, (float) z);
|
86
|
+
}
|
87
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
package monkstone.vecmath;
|
2
|
+
|
3
|
+
/**
|
4
|
+
*
|
5
|
+
*
|
6
|
+
*/
|
7
|
+
public interface JRender {
|
8
|
+
|
9
|
+
/**
|
10
|
+
*
|
11
|
+
* @param x double
|
12
|
+
* @param y double
|
13
|
+
*/
|
14
|
+
public void vertex(double x, double y);
|
15
|
+
|
16
|
+
/**
|
17
|
+
*
|
18
|
+
* @param x double
|
19
|
+
* @param y double
|
20
|
+
*/
|
21
|
+
public void curveVertex(double x, double y);
|
22
|
+
|
23
|
+
/**
|
24
|
+
*
|
25
|
+
* @param x double
|
26
|
+
* @param y double
|
27
|
+
* @param z double
|
28
|
+
*/
|
29
|
+
public void vertex(double x, double y, double z);
|
30
|
+
|
31
|
+
/**
|
32
|
+
*
|
33
|
+
* @param x double
|
34
|
+
* @param y double
|
35
|
+
* @param z double
|
36
|
+
* @param u double
|
37
|
+
* @param v double
|
38
|
+
*/
|
39
|
+
public void vertex(double x, double y, double z, double u, double v);
|
40
|
+
|
41
|
+
/**
|
42
|
+
*
|
43
|
+
* @param x double
|
44
|
+
* @param y double
|
45
|
+
* @param z double
|
46
|
+
*/
|
47
|
+
public void curveVertex(double x, double y, double z);
|
48
|
+
|
49
|
+
/**
|
50
|
+
*
|
51
|
+
* @param x double
|
52
|
+
* @param y double
|
53
|
+
* @param z double
|
54
|
+
*/
|
55
|
+
public void normal(double x, double y, double z);
|
56
|
+
}
|
@@ -0,0 +1,87 @@
|
|
1
|
+
package monkstone.vecmath;
|
2
|
+
|
3
|
+
import processing.core.PShape;
|
4
|
+
|
5
|
+
/**
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
public class ShapeRender implements JRender {
|
10
|
+
|
11
|
+
final PShape shape;
|
12
|
+
|
13
|
+
/**
|
14
|
+
*
|
15
|
+
* @param shape PShape
|
16
|
+
*/
|
17
|
+
public ShapeRender(final PShape shape) {
|
18
|
+
this.shape = shape;
|
19
|
+
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
*
|
24
|
+
* @param x double
|
25
|
+
* @param y double
|
26
|
+
*/
|
27
|
+
@Override
|
28
|
+
public void vertex(double x, double y) {
|
29
|
+
shape.vertex((float) x, (float) y);
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
*
|
34
|
+
* @param x double
|
35
|
+
* @param y double
|
36
|
+
*/
|
37
|
+
@Override
|
38
|
+
public void curveVertex(double x, double y) {
|
39
|
+
throw new UnsupportedOperationException("Not implemented for this renderer");
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
*
|
44
|
+
* @param x double
|
45
|
+
* @param y double
|
46
|
+
* @param z double
|
47
|
+
*/
|
48
|
+
@Override
|
49
|
+
public void vertex(double x, double y, double z) {
|
50
|
+
shape.vertex((float) x, (float) y, (float) z);
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
*
|
55
|
+
* @param x double
|
56
|
+
* @param y double
|
57
|
+
* @param z double
|
58
|
+
*/
|
59
|
+
@Override
|
60
|
+
public void normal(double x, double y, double z) {
|
61
|
+
shape.normal((float) x, (float) y, (float) z);
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
*
|
66
|
+
* @param x double
|
67
|
+
* @param y double
|
68
|
+
* @param z double
|
69
|
+
* @param u double
|
70
|
+
* @param v double
|
71
|
+
*/
|
72
|
+
@Override
|
73
|
+
public void vertex(double x, double y, double z, double u, double v) {
|
74
|
+
shape.vertex((float) x, (float) y, (float) z, (float) u, (float) v);
|
75
|
+
}
|
76
|
+
|
77
|
+
/**
|
78
|
+
*
|
79
|
+
* @param x double
|
80
|
+
* @param y double
|
81
|
+
* @param z double
|
82
|
+
*/
|
83
|
+
@Override
|
84
|
+
public void curveVertex(double x, double y, double z) {
|
85
|
+
throw new UnsupportedOperationException("Not implemented for this renderer");
|
86
|
+
}
|
87
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2018 Martin Prout
|
3
|
+
*
|
4
|
+
* This library is free software; you can redistribute it and/or
|
5
|
+
* modify it under the terms of the GNU General Public
|
6
|
+
* License as published by the Free Software Foundation; either
|
7
|
+
* version 3.0 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 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;
|
@@ -0,0 +1,757 @@
|
|
1
|
+
package monkstone.vecmath.vec2;
|
2
|
+
|
3
|
+
/*
|
4
|
+
* Copyright (c) 2018 Martin Prout
|
5
|
+
*
|
6
|
+
* This library is free software; you can redistribute it and/or
|
7
|
+
* modify it under the terms of the GNU General Public
|
8
|
+
* License as published by the Free Software Foundation; either
|
9
|
+
* version 3.0 of the License, or (at your option) any later version.
|
10
|
+
*
|
11
|
+
* http://creativecommons.org/licenses/LGPL/2.1/
|
12
|
+
*
|
13
|
+
* This library is distributed in the hope that it will be useful,
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16
|
+
* Lesser General Public License for more details.
|
17
|
+
*
|
18
|
+
* You should have received a copy of the GNU General Public
|
19
|
+
* License along with this library; if not, write to the Free Software
|
20
|
+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
21
|
+
*/
|
22
|
+
import org.jruby.Ruby;
|
23
|
+
import org.jruby.RubyArray;
|
24
|
+
import org.jruby.RubyClass;
|
25
|
+
import org.jruby.RubyFixnum;
|
26
|
+
import org.jruby.RubyFloat;
|
27
|
+
import org.jruby.RubyObject;
|
28
|
+
import org.jruby.RubySymbol;
|
29
|
+
import org.jruby.anno.JRubyClass;
|
30
|
+
import org.jruby.anno.JRubyMethod;
|
31
|
+
import org.jruby.runtime.Block;
|
32
|
+
import org.jruby.runtime.ThreadContext;
|
33
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
34
|
+
import monkstone.vecmath.JRender;
|
35
|
+
|
36
|
+
/**
|
37
|
+
*
|
38
|
+
*
|
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 = -2950154560223211646L;
|
45
|
+
|
46
|
+
private double jx = 0;
|
47
|
+
private double jy = 0;
|
48
|
+
|
49
|
+
/**
|
50
|
+
*
|
51
|
+
* @param runtime ThreadContext
|
52
|
+
*/
|
53
|
+
public static void createVec2(final Ruby runtime) {
|
54
|
+
RubyClass vec2Cls = runtime.defineClass("Vec2D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec2(runtime1, rubyClass));
|
55
|
+
vec2Cls.defineAnnotatedMethods(Vec2.class);
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
*
|
60
|
+
* @return
|
61
|
+
*/
|
62
|
+
public double javax() {
|
63
|
+
return jx;
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
*
|
68
|
+
* @return
|
69
|
+
*/
|
70
|
+
public double javay() {
|
71
|
+
return jy;
|
72
|
+
}
|
73
|
+
|
74
|
+
/**
|
75
|
+
*
|
76
|
+
* @param context ThreadContext
|
77
|
+
* @param klazz IRubyObject
|
78
|
+
* @param args optional (no args jx = 0, jy = 0)
|
79
|
+
* @return new Vec2 object (ruby)
|
80
|
+
*/
|
81
|
+
@JRubyMethod(name = "new", meta = true, rest = true)
|
82
|
+
public static final IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject... args) {
|
83
|
+
Vec2 vec2 = (Vec2) ((RubyClass) klazz).allocate();
|
84
|
+
vec2.init(context, args);
|
85
|
+
return vec2;
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
*
|
90
|
+
* @param runtime Ruby
|
91
|
+
* @param klass RubyClass
|
92
|
+
*/
|
93
|
+
public Vec2(Ruby runtime, RubyClass klass) {
|
94
|
+
super(runtime, klass);
|
95
|
+
}
|
96
|
+
|
97
|
+
void init(ThreadContext context, IRubyObject... args) {
|
98
|
+
int count = args.length;
|
99
|
+
if (count == 2) {
|
100
|
+
jx = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
|
101
|
+
jy = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
|
102
|
+
} // allow ruby ducktyping in constructor
|
103
|
+
if (count == 1) {
|
104
|
+
if (!(args[0].respondsTo("x"))) {
|
105
|
+
throw context.runtime.newTypeError(args[0].getType() + " doesn't respond_to :x & :y");
|
106
|
+
}
|
107
|
+
jx = ((args[0].callMethod(context, "x")) instanceof RubyFloat)
|
108
|
+
? ((RubyFloat) args[0].callMethod(context, "x")).getValue() : ((RubyFixnum) args[0].callMethod(context, "x")).getDoubleValue();
|
109
|
+
jy = ((args[0].callMethod(context, "y")) instanceof RubyFloat)
|
110
|
+
? ((RubyFloat) args[0].callMethod(context, "y")).getValue() : ((RubyFixnum) args[0].callMethod(context, "y")).getDoubleValue();
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
*
|
116
|
+
* @param context ThreadContext
|
117
|
+
* @return x IRubyObject
|
118
|
+
*/
|
119
|
+
@JRubyMethod(name = "x")
|
120
|
+
|
121
|
+
public IRubyObject getX(ThreadContext context) {
|
122
|
+
return context.runtime.newFloat(jx);
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
*
|
127
|
+
* @param context ThreadContext
|
128
|
+
* @return y IRubyObject
|
129
|
+
*/
|
130
|
+
@JRubyMethod(name = "y")
|
131
|
+
|
132
|
+
public IRubyObject getY(ThreadContext context) {
|
133
|
+
return context.runtime.newFloat(jy);
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
*
|
138
|
+
* @param context ThreadContext
|
139
|
+
* @param key as symbol
|
140
|
+
* @return value float
|
141
|
+
*/
|
142
|
+
@JRubyMethod(name = "[]", required = 1)
|
143
|
+
|
144
|
+
public IRubyObject aref(ThreadContext context, IRubyObject key) {
|
145
|
+
Ruby runtime = context.runtime;
|
146
|
+
if (key instanceof RubySymbol) {
|
147
|
+
if (key == RubySymbol.newSymbol(runtime, "x")) {
|
148
|
+
return runtime.newFloat(jx);
|
149
|
+
} else if (key == RubySymbol.newSymbol(runtime, "y")) {
|
150
|
+
return runtime.newFloat(jy);
|
151
|
+
} else {
|
152
|
+
throw runtime.newIndexError("invalid key");
|
153
|
+
}
|
154
|
+
} else {
|
155
|
+
throw runtime.newIndexError("invalid key");
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
/**
|
160
|
+
* @param context ThreadContext
|
161
|
+
* @param key as symbol
|
162
|
+
* @param value as float
|
163
|
+
* @return value float
|
164
|
+
*/
|
165
|
+
@JRubyMethod(name = "[]=")
|
166
|
+
|
167
|
+
public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) {
|
168
|
+
Ruby runtime = context.runtime;
|
169
|
+
if (key instanceof RubySymbol) {
|
170
|
+
if (key == RubySymbol.newSymbol(runtime, "x")) {
|
171
|
+
jx = (value instanceof RubyFloat)
|
172
|
+
? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
|
173
|
+
} else if (key == RubySymbol.newSymbol(runtime, "y")) {
|
174
|
+
jy = (value instanceof RubyFloat)
|
175
|
+
? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue();
|
176
|
+
}
|
177
|
+
} else {
|
178
|
+
throw runtime.newIndexError("invalid key");
|
179
|
+
}
|
180
|
+
return value;
|
181
|
+
}
|
182
|
+
|
183
|
+
/**
|
184
|
+
*
|
185
|
+
* @param context ThreadContext
|
186
|
+
* @param other IRubyObject
|
187
|
+
* @return x IRubyObject
|
188
|
+
*/
|
189
|
+
@JRubyMethod(name = "x=")
|
190
|
+
|
191
|
+
public IRubyObject setX(ThreadContext context, IRubyObject other) {
|
192
|
+
if (other instanceof RubyFloat) {
|
193
|
+
jx = ((RubyFloat) other).getValue();
|
194
|
+
} else {
|
195
|
+
jx = ((RubyFixnum) other).getDoubleValue();
|
196
|
+
}
|
197
|
+
return other;
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
*
|
202
|
+
* @param context ThreadContext
|
203
|
+
* @param other IRubyObject
|
204
|
+
* @return y IRubyObject
|
205
|
+
*/
|
206
|
+
@JRubyMethod(name = "y=")
|
207
|
+
|
208
|
+
public IRubyObject setY(ThreadContext context, IRubyObject other) {
|
209
|
+
if (other instanceof RubyFloat) {
|
210
|
+
jy = ((RubyFloat) other).getValue();
|
211
|
+
} else {
|
212
|
+
jy = ((RubyFixnum) other).getDoubleValue();
|
213
|
+
}
|
214
|
+
return other;
|
215
|
+
}
|
216
|
+
|
217
|
+
/**
|
218
|
+
*
|
219
|
+
* @param context ThreadContext
|
220
|
+
* @param other IRubyObject
|
221
|
+
* @return distance float
|
222
|
+
*/
|
223
|
+
@JRubyMethod(name = "dist", required = 1)
|
224
|
+
|
225
|
+
public IRubyObject dist(ThreadContext context, IRubyObject other) {
|
226
|
+
Vec2 b = null;
|
227
|
+
Ruby runtime = context.runtime;
|
228
|
+
if (other instanceof Vec2) {
|
229
|
+
b = (Vec2) other.toJava(Vec2.class);
|
230
|
+
} else {
|
231
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
232
|
+
}
|
233
|
+
double result = Math.hypot((jx - b.jx), (jy - b.jy));
|
234
|
+
return runtime.newFloat(result);
|
235
|
+
}
|
236
|
+
|
237
|
+
/**
|
238
|
+
*
|
239
|
+
* @param context ThreadContext
|
240
|
+
* @param other IRubyObject
|
241
|
+
* @return cross product IRubyObject
|
242
|
+
*/
|
243
|
+
@JRubyMethod(name = "cross", required = 1)
|
244
|
+
|
245
|
+
public IRubyObject cross(ThreadContext context, IRubyObject other) {
|
246
|
+
Vec2 b = null;
|
247
|
+
Ruby runtime = context.runtime;
|
248
|
+
if (other instanceof Vec2) {
|
249
|
+
b = (Vec2) other.toJava(Vec2.class);
|
250
|
+
} else {
|
251
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
252
|
+
}
|
253
|
+
return runtime.newFloat(jx * b.jy - jy * b.jx);
|
254
|
+
}
|
255
|
+
|
256
|
+
/**
|
257
|
+
*
|
258
|
+
* @param context ThreadContext
|
259
|
+
* @param other IRubyObject
|
260
|
+
* @return dot product IRubyObject
|
261
|
+
*/
|
262
|
+
@JRubyMethod(name = "dot", required = 1)
|
263
|
+
|
264
|
+
public IRubyObject dot(ThreadContext context, IRubyObject other) {
|
265
|
+
Vec2 b = null;
|
266
|
+
Ruby runtime = context.runtime;
|
267
|
+
if (other instanceof Vec2) {
|
268
|
+
b = (Vec2) other.toJava(Vec2.class);
|
269
|
+
} else {
|
270
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
271
|
+
}
|
272
|
+
return runtime.newFloat(jx * b.jx + jy * b.jy);
|
273
|
+
}
|
274
|
+
|
275
|
+
/**
|
276
|
+
*
|
277
|
+
* @param context ThreadContext
|
278
|
+
* @param other IRubyObject
|
279
|
+
* @return new Vec2D object (ruby)
|
280
|
+
*/
|
281
|
+
@JRubyMethod(name = "+", required = 1)
|
282
|
+
|
283
|
+
public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
|
284
|
+
Vec2 b = null;
|
285
|
+
Ruby runtime = context.runtime;
|
286
|
+
if (other instanceof Vec2) {
|
287
|
+
b = (Vec2) other.toJava(Vec2.class);
|
288
|
+
} else {
|
289
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
290
|
+
}
|
291
|
+
return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
|
292
|
+
runtime.newFloat(jx + b.jx),
|
293
|
+
runtime.newFloat(jy + b.jy)});
|
294
|
+
}
|
295
|
+
|
296
|
+
/**
|
297
|
+
*
|
298
|
+
* @param context ThreadContext
|
299
|
+
* @param other IRubyObject
|
300
|
+
* @return new Vec2D object (ruby)
|
301
|
+
*/
|
302
|
+
@JRubyMethod(name = "-", required = 1)
|
303
|
+
|
304
|
+
public IRubyObject op_minus(ThreadContext context, IRubyObject other) {
|
305
|
+
Vec2 b = null;
|
306
|
+
Ruby runtime = context.runtime;
|
307
|
+
if (other instanceof Vec2) {
|
308
|
+
b = (Vec2) other.toJava(Vec2.class);
|
309
|
+
} else {
|
310
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
311
|
+
}
|
312
|
+
return Vec2.rbNew(context, other.getMetaClass(), new IRubyObject[]{
|
313
|
+
runtime.newFloat(jx - b.jx),
|
314
|
+
runtime.newFloat(jy - b.jy)});
|
315
|
+
}
|
316
|
+
|
317
|
+
/**
|
318
|
+
*
|
319
|
+
* @param context ThreadContext
|
320
|
+
* @param other IRubyObject scalar
|
321
|
+
* @return new Vec2D object (ruby)
|
322
|
+
*/
|
323
|
+
@JRubyMethod(name = "*")
|
324
|
+
|
325
|
+
public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
|
326
|
+
Ruby runtime = context.runtime;
|
327
|
+
double scalar = (other instanceof RubyFloat)
|
328
|
+
? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue();
|
329
|
+
return Vec2.rbNew(context, this.getMetaClass(),
|
330
|
+
new IRubyObject[]{runtime.newFloat(jx * scalar),
|
331
|
+
runtime.newFloat(jy * scalar)});
|
332
|
+
}
|
333
|
+
|
334
|
+
/**
|
335
|
+
*
|
336
|
+
* @param context ThreadContext
|
337
|
+
* @param other IRubyObject scalar
|
338
|
+
* @return new Vec2D object (ruby)
|
339
|
+
*/
|
340
|
+
@JRubyMethod(name = "/", required = 1)
|
341
|
+
|
342
|
+
public IRubyObject op_div(ThreadContext context, IRubyObject other) {
|
343
|
+
Ruby runtime = context.runtime;
|
344
|
+
double scalar = (other instanceof RubyFloat)
|
345
|
+
? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue();
|
346
|
+
if (Math.abs(scalar) < Vec2.EPSILON) {
|
347
|
+
return this;
|
348
|
+
}
|
349
|
+
return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
|
350
|
+
runtime.newFloat(jx / scalar),
|
351
|
+
runtime.newFloat(jy / scalar)});
|
352
|
+
}
|
353
|
+
|
354
|
+
/**
|
355
|
+
*
|
356
|
+
* @param context ThreadContext
|
357
|
+
* @return heading IRubyObject radians
|
358
|
+
*/
|
359
|
+
@JRubyMethod(name = "heading")
|
360
|
+
public IRubyObject heading(ThreadContext context) {
|
361
|
+
return context.runtime.newFloat(Math.atan2(jy, jx));
|
362
|
+
}
|
363
|
+
|
364
|
+
/**
|
365
|
+
*
|
366
|
+
* @param context ThreadContext
|
367
|
+
* @return magnitude IRubyObject
|
368
|
+
*/
|
369
|
+
@JRubyMethod(name = "mag")
|
370
|
+
|
371
|
+
public IRubyObject mag(ThreadContext context) {
|
372
|
+
double result = 0;
|
373
|
+
if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
|
374
|
+
result = Math.hypot(jx, jy);
|
375
|
+
} else {
|
376
|
+
if (Math.abs(jy) > EPSILON) {
|
377
|
+
result = Math.abs(jy);
|
378
|
+
}
|
379
|
+
if (Math.abs(jx) > EPSILON) {
|
380
|
+
result = Math.abs(jx);
|
381
|
+
}
|
382
|
+
}
|
383
|
+
return context.runtime.newFloat(result);
|
384
|
+
}
|
385
|
+
|
386
|
+
/**
|
387
|
+
* Call yield if block given, do nothing if yield == false else set_mag to
|
388
|
+
* given scalar
|
389
|
+
*
|
390
|
+
* @param context ThreadContext
|
391
|
+
* @param scalar double value to set
|
392
|
+
* @param block should return a boolean (optional)
|
393
|
+
* @return magnitude IRubyObject
|
394
|
+
*/
|
395
|
+
@JRubyMethod(name = "set_mag")
|
396
|
+
|
397
|
+
public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
|
398
|
+
double new_mag = (Double) scalar.toJava(Double.class);
|
399
|
+
if (block.isGiven()) {
|
400
|
+
if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) {
|
401
|
+
return this;
|
402
|
+
}
|
403
|
+
}
|
404
|
+
double current = 0;
|
405
|
+
if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
|
406
|
+
current = Math.hypot(jx, jy);
|
407
|
+
} else {
|
408
|
+
if (Math.abs(jy) > EPSILON) {
|
409
|
+
current = Math.abs(jy);
|
410
|
+
}
|
411
|
+
if (Math.abs(jx) > EPSILON) {
|
412
|
+
current = Math.abs(jx);
|
413
|
+
}
|
414
|
+
}
|
415
|
+
if (current > 0) {
|
416
|
+
jx *= new_mag / current;
|
417
|
+
jy *= new_mag / current;
|
418
|
+
}
|
419
|
+
return this;
|
420
|
+
}
|
421
|
+
|
422
|
+
/**
|
423
|
+
*
|
424
|
+
* @param context ThreadContext
|
425
|
+
* @return this as a ruby object
|
426
|
+
*/
|
427
|
+
@JRubyMethod(name = "normalize!")
|
428
|
+
|
429
|
+
public IRubyObject normalize_bang(ThreadContext context) {
|
430
|
+
double mag = 0;
|
431
|
+
if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
|
432
|
+
mag = Math.hypot(jx, jy);
|
433
|
+
} else {
|
434
|
+
if (Math.abs(jx) > EPSILON) {
|
435
|
+
mag = Math.abs(jx);
|
436
|
+
}
|
437
|
+
if (Math.abs(jy) > EPSILON) {
|
438
|
+
mag = Math.abs(jy);
|
439
|
+
}
|
440
|
+
}
|
441
|
+
if (mag > 0) {
|
442
|
+
jx /= mag;
|
443
|
+
jy /= mag;
|
444
|
+
}
|
445
|
+
return this;
|
446
|
+
}
|
447
|
+
|
448
|
+
/**
|
449
|
+
*
|
450
|
+
* @param context ThreadContext
|
451
|
+
* @return new normalized Vec3D object (ruby)
|
452
|
+
*/
|
453
|
+
@JRubyMethod(name = "normalize")
|
454
|
+
|
455
|
+
public IRubyObject normalize(ThreadContext context) {
|
456
|
+
double mag = 0;
|
457
|
+
Ruby runtime = context.runtime;
|
458
|
+
if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
|
459
|
+
mag = Math.hypot(jx, jy);
|
460
|
+
} else {
|
461
|
+
if (Math.abs(jx) > EPSILON) {
|
462
|
+
mag = jx;
|
463
|
+
}
|
464
|
+
if (Math.abs(jy) > EPSILON) {
|
465
|
+
mag = jy;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
if (mag < EPSILON) {
|
469
|
+
mag = 1.0;
|
470
|
+
}
|
471
|
+
return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
|
472
|
+
runtime.newFloat(jx / mag),
|
473
|
+
runtime.newFloat(jy / mag)});
|
474
|
+
}
|
475
|
+
|
476
|
+
/**
|
477
|
+
* Example of a regular ruby class method Use Math rather than RadLut
|
478
|
+
* here!!!
|
479
|
+
*
|
480
|
+
* @param context ThreadContext
|
481
|
+
* @param klazz IRubyObject
|
482
|
+
* @param scalar input angle in radians
|
483
|
+
* @return new Vec2 object (ruby)
|
484
|
+
*/
|
485
|
+
@JRubyMethod(name = "from_angle", meta = true)
|
486
|
+
public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) {
|
487
|
+
Ruby runtime = context.runtime;
|
488
|
+
double angle = (scalar instanceof RubyFloat)
|
489
|
+
? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
|
490
|
+
return Vec2.rbNew(context, klazz, new IRubyObject[]{
|
491
|
+
runtime.newFloat(Math.cos(angle)),
|
492
|
+
runtime.newFloat(Math.sin(angle))});
|
493
|
+
}
|
494
|
+
|
495
|
+
/**
|
496
|
+
* Example of a regular ruby class method
|
497
|
+
*
|
498
|
+
* @param context ThreadContext
|
499
|
+
* @param klazz IRubyObject
|
500
|
+
* @return new Vec2 object (ruby)
|
501
|
+
*/
|
502
|
+
@JRubyMethod(name = "random", meta = true)
|
503
|
+
public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) {
|
504
|
+
Ruby runtime = context.runtime;
|
505
|
+
double angle = Math.random() * Math.PI * 2;
|
506
|
+
return Vec2.rbNew(context, klazz, new IRubyObject[]{
|
507
|
+
runtime.newFloat(Math.cos(angle)),
|
508
|
+
runtime.newFloat(Math.sin(angle))});
|
509
|
+
}
|
510
|
+
|
511
|
+
/**
|
512
|
+
*
|
513
|
+
* @param context ThreadContext
|
514
|
+
* @param scalar IRubyObject
|
515
|
+
* @return this Vec2 object rotated
|
516
|
+
*/
|
517
|
+
@JRubyMethod(name = "rotate!")
|
518
|
+
public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) {
|
519
|
+
double theta = (scalar instanceof RubyFloat)
|
520
|
+
? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
|
521
|
+
double x = (jx * Math.cos(theta) - jy * Math.sin(theta));
|
522
|
+
double y = (jx * Math.sin(theta) + jy * Math.cos(theta));
|
523
|
+
jx = x;
|
524
|
+
jy = y;
|
525
|
+
return this;
|
526
|
+
}
|
527
|
+
|
528
|
+
/**
|
529
|
+
*
|
530
|
+
* @param context ThreadContext
|
531
|
+
* @param scalar IRubyObject
|
532
|
+
* @return a new Vec2 object rotated
|
533
|
+
*/
|
534
|
+
@JRubyMethod(name = "rotate")
|
535
|
+
public IRubyObject rotate(ThreadContext context, IRubyObject scalar) {
|
536
|
+
Ruby runtime = context.runtime;
|
537
|
+
double theta = (scalar instanceof RubyFloat)
|
538
|
+
? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
|
539
|
+
IRubyObject[] ary = new IRubyObject[]{
|
540
|
+
runtime.newFloat(jx * Math.cos(theta) - jy * Math.sin(theta)),
|
541
|
+
runtime.newFloat(jx * Math.sin(theta) + jy * Math.cos(theta))};
|
542
|
+
return Vec2.rbNew(context, this.getMetaClass(), ary);
|
543
|
+
}
|
544
|
+
|
545
|
+
/**
|
546
|
+
*
|
547
|
+
* @param context ThreadContext
|
548
|
+
* @param args IRubyObject[]
|
549
|
+
* @return as a new Vec2 object (ruby)
|
550
|
+
*/
|
551
|
+
@JRubyMethod(name = "lerp", rest = true)
|
552
|
+
public IRubyObject lerp(ThreadContext context, IRubyObject[] args) {
|
553
|
+
Ruby runtime = context.runtime;
|
554
|
+
if (args.length != 2) {
|
555
|
+
throw runtime.newSyntaxError("Check syntax");
|
556
|
+
}
|
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
|
+
return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
|
563
|
+
runtime.newFloat(jx + (vec.jx - jx) * scalar),
|
564
|
+
runtime.newFloat(jy + (vec.jy - jy) * scalar)});
|
565
|
+
}
|
566
|
+
|
567
|
+
/**
|
568
|
+
*
|
569
|
+
* @param context ThreadContext
|
570
|
+
* @param args IRubyObject[]
|
571
|
+
* @return this IRubyObject
|
572
|
+
*/
|
573
|
+
@JRubyMethod(name = "lerp!", rest = true)
|
574
|
+
public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) {
|
575
|
+
Ruby runtime = context.runtime;
|
576
|
+
if (args.length != 2) {
|
577
|
+
throw runtime.newSyntaxError("Check syntax");
|
578
|
+
}
|
579
|
+
Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
|
580
|
+
double scalar = (args[1] instanceof RubyFloat)
|
581
|
+
? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
|
582
|
+
assert (scalar >= 0 && scalar < 1.0) :
|
583
|
+
"Lerp value " + scalar + " out of range 0..1.0";
|
584
|
+
jx += (vec.jx - jx) * scalar;
|
585
|
+
jy += (vec.jy - jy) * scalar;
|
586
|
+
return this;
|
587
|
+
}
|
588
|
+
|
589
|
+
/**
|
590
|
+
*
|
591
|
+
* @param context ThreadContext
|
592
|
+
* @param other IRubyObject another Vec3D
|
593
|
+
* @return angle IRubyObject in radians
|
594
|
+
*/
|
595
|
+
@JRubyMethod(name = "angle_between")
|
596
|
+
|
597
|
+
public IRubyObject angleBetween(ThreadContext context, IRubyObject other) {
|
598
|
+
Vec2 vec = null;
|
599
|
+
Ruby runtime = context.runtime;
|
600
|
+
if (other instanceof Vec2) {
|
601
|
+
vec = (Vec2) other.toJava(Vec2.class);
|
602
|
+
} else {
|
603
|
+
throw runtime.newTypeError("argument should be Vec2D");
|
604
|
+
}
|
605
|
+
return runtime.newFloat(Math.atan2(jx - vec.jx, jy - vec.jy));
|
606
|
+
}
|
607
|
+
|
608
|
+
/**
|
609
|
+
*
|
610
|
+
* @param context ThreadContext
|
611
|
+
* @return IRubyObject copy
|
612
|
+
*/
|
613
|
+
@JRubyMethod(name = {"copy", "dup"})
|
614
|
+
|
615
|
+
public IRubyObject copy(ThreadContext context) {
|
616
|
+
Ruby runtime = context.runtime;
|
617
|
+
return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{
|
618
|
+
runtime.newFloat(jx),
|
619
|
+
runtime.newFloat(jy)});
|
620
|
+
}
|
621
|
+
|
622
|
+
/**
|
623
|
+
*
|
624
|
+
* @param context ThreadContext
|
625
|
+
* @return IRubyObject array of float
|
626
|
+
*/
|
627
|
+
@JRubyMethod(name = "to_a")
|
628
|
+
|
629
|
+
public IRubyObject toArray(ThreadContext context) {
|
630
|
+
Ruby runtime = context.runtime;
|
631
|
+
return RubyArray.newArray(runtime, new IRubyObject[]{
|
632
|
+
runtime.newFloat(jx),
|
633
|
+
runtime.newFloat(jy)});
|
634
|
+
}
|
635
|
+
|
636
|
+
/**
|
637
|
+
*
|
638
|
+
* To vertex
|
639
|
+
*
|
640
|
+
* @param context ThreadContext
|
641
|
+
* @param object IRubyObject vertex renderer
|
642
|
+
*/
|
643
|
+
@JRubyMethod(name = "to_vertex")
|
644
|
+
|
645
|
+
public void toVertex(ThreadContext context, IRubyObject object) {
|
646
|
+
JRender renderer = (JRender) object.toJava(JRender.class);
|
647
|
+
renderer.vertex(jx, jy);
|
648
|
+
}
|
649
|
+
|
650
|
+
/**
|
651
|
+
*
|
652
|
+
* To curve vertex
|
653
|
+
*
|
654
|
+
* @param context ThreadContext
|
655
|
+
* @param object IRubyObject vertex renderer
|
656
|
+
*/
|
657
|
+
@JRubyMethod(name = "to_curve_vertex")
|
658
|
+
|
659
|
+
public void toCurveVertex(ThreadContext context, IRubyObject object) {
|
660
|
+
JRender renderer = (JRender) object.toJava(JRender.class);
|
661
|
+
renderer.curveVertex(jx, jy);
|
662
|
+
}
|
663
|
+
|
664
|
+
/**
|
665
|
+
* For jruby-9000 we alias to inspect
|
666
|
+
*
|
667
|
+
* @param context ThreadContext
|
668
|
+
* @return IRubyObject to_s
|
669
|
+
*/
|
670
|
+
@JRubyMethod(name = {"to_s", "inspect"})
|
671
|
+
|
672
|
+
public IRubyObject to_s(ThreadContext context) {
|
673
|
+
return context.runtime.newString(String.format("Vec2D(x = %4.4f, y = %4.4f)", jx, jy));
|
674
|
+
}
|
675
|
+
|
676
|
+
/**
|
677
|
+
*
|
678
|
+
* @return hash int
|
679
|
+
*/
|
680
|
+
@Override
|
681
|
+
public int hashCode() {
|
682
|
+
int hash = 5;
|
683
|
+
hash = 53 * hash + (int) (Double.doubleToLongBits(this.jx) ^ (Double.doubleToLongBits(this.jx) >>> 32));
|
684
|
+
hash = 53 * hash + (int) (Double.doubleToLongBits(this.jy) ^ (Double.doubleToLongBits(this.jy) >>> 32));
|
685
|
+
return hash;
|
686
|
+
}
|
687
|
+
|
688
|
+
/**
|
689
|
+
*
|
690
|
+
* Java Equals
|
691
|
+
*
|
692
|
+
* @param obj Object
|
693
|
+
* @return result boolean
|
694
|
+
*/
|
695
|
+
@Override
|
696
|
+
public boolean equals(Object obj) {
|
697
|
+
if (obj == this) {
|
698
|
+
return true;
|
699
|
+
}
|
700
|
+
if (obj instanceof Vec2) {
|
701
|
+
final Vec2 other = (Vec2) obj;
|
702
|
+
if ((Double.compare(jx, (Double) other.jx) == 0)
|
703
|
+
&& (Double.compare(jy, (Double) other.jy) == 0)) {
|
704
|
+
return true;
|
705
|
+
}
|
706
|
+
}
|
707
|
+
return false;
|
708
|
+
}
|
709
|
+
|
710
|
+
/**
|
711
|
+
*
|
712
|
+
* @param context ThreadContext
|
713
|
+
* @param other IRubyObject
|
714
|
+
* @return result IRubyObject as boolean
|
715
|
+
*/
|
716
|
+
@JRubyMethod(name = "eql?", required = 1)
|
717
|
+
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
|
718
|
+
Ruby runtime = context.runtime;
|
719
|
+
if (other == this) {
|
720
|
+
return runtime.newBoolean(true);
|
721
|
+
}
|
722
|
+
if (other instanceof Vec2) {
|
723
|
+
Vec2 v = (Vec2) other.toJava(Vec2.class);
|
724
|
+
if ((Double.compare(jx, (Double) v.jx) == 0)
|
725
|
+
&& (Double.compare(jy, (Double) v.jy) == 0)) {
|
726
|
+
return runtime.newBoolean(true);
|
727
|
+
}
|
728
|
+
}
|
729
|
+
return runtime.newBoolean(false);
|
730
|
+
}
|
731
|
+
|
732
|
+
/**
|
733
|
+
*
|
734
|
+
* @param context ThreadContext
|
735
|
+
* @param other IRubyObject
|
736
|
+
* @return result IRubyObject as boolean
|
737
|
+
*/
|
738
|
+
@JRubyMethod(name = "==", required = 1)
|
739
|
+
|
740
|
+
@Override
|
741
|
+
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
|
742
|
+
Ruby runtime = context.runtime;
|
743
|
+
if (other == this) {
|
744
|
+
return runtime.newBoolean(true);
|
745
|
+
}
|
746
|
+
if (other instanceof Vec2) {
|
747
|
+
Vec2 v = (Vec2) other.toJava(Vec2.class);
|
748
|
+
double diff = jx - v.jx;
|
749
|
+
if ((diff < 0 ? -diff : diff) > Vec2.EPSILON) {
|
750
|
+
return runtime.newBoolean(false);
|
751
|
+
}
|
752
|
+
diff = jy - v.jy;
|
753
|
+
return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec2.EPSILON);
|
754
|
+
}
|
755
|
+
return runtime.newBoolean(false);
|
756
|
+
}
|
757
|
+
}
|