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