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,32 @@
|
|
1
|
+
# This class demonstrates how by inheriting from the abstract class LibraryProxy
|
2
|
+
# we can access 'keyEvent' and 'draw' (Note we need a draw method even
|
3
|
+
# though can be empty)
|
4
|
+
class MyLibrary < LibraryProxy
|
5
|
+
java_import 'processing.event.KeyEvent'
|
6
|
+
|
7
|
+
attr_reader :app
|
8
|
+
|
9
|
+
def initialize(parent)
|
10
|
+
@app = parent
|
11
|
+
end
|
12
|
+
|
13
|
+
def draw # optional
|
14
|
+
fill app.color(200, 0, 0, 100)
|
15
|
+
app.rect 100, 100, 60, 90
|
16
|
+
end
|
17
|
+
|
18
|
+
# favor guard clause no_op unless key pressed
|
19
|
+
# and no_op unless ascii key
|
20
|
+
def keyEvent(e) # NB: need camel case for reflection to work
|
21
|
+
return unless e.get_action == KeyEvent::PRESS
|
22
|
+
return if e.get_key > 122 # else we can't use :chr
|
23
|
+
case e.get_key.chr.upcase
|
24
|
+
when 'S'
|
25
|
+
app.send :hide, false
|
26
|
+
when 'H'
|
27
|
+
app.send :hide, true
|
28
|
+
else
|
29
|
+
puts e.get_key.chr
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,522 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
require 'java'
|
3
|
+
require_relative '../lib/picrate'
|
4
|
+
|
5
|
+
Java::Monkstone::PicrateLibrary.load(JRuby.runtime)
|
6
|
+
|
7
|
+
|
8
|
+
Dir.chdir(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
class VecmathTest < Minitest::Test
|
11
|
+
|
12
|
+
# duck for Vec2D constructor
|
13
|
+
Point = Struct.new(:x, :y)
|
14
|
+
# duck for Vec3D constructor
|
15
|
+
Point3 = Struct.new(:x, :y, :z)
|
16
|
+
# non-duck to test fail
|
17
|
+
Pointless = Struct.new(:a, :b)
|
18
|
+
# non-duck to test fail
|
19
|
+
Pointless3 = Struct.new(:a, :b, :c)
|
20
|
+
def setup
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_equals
|
25
|
+
x, y = 1.0000001, 1.01
|
26
|
+
a = Vec2D.new(x, y)
|
27
|
+
assert_equal(a.to_a, [x, y], 'Failed to return Vec2D as an Array')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_not_equals
|
31
|
+
a = Vec2D.new(3, 5)
|
32
|
+
b = Vec2D.new(6, 7)
|
33
|
+
refute_equal(a, b, 'Failed equals false')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_copy_equals
|
37
|
+
x, y = 1.0000001, 1.01
|
38
|
+
a = Vec2D.new(x, y)
|
39
|
+
b = a.copy
|
40
|
+
assert_equal(a.to_a, b.to_a, 'Failed deep copy')
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_constructor_float
|
44
|
+
val = Point.new(1.0, 8.0) # duck type
|
45
|
+
expected = Vec2D.new(val)
|
46
|
+
assert_equal(expected, Vec2D.new(1.0, 8.0), 'Failed duck type constructor floats')
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_constructor_fixnum
|
50
|
+
val = Point.new(1, 8) # duck type fixnum
|
51
|
+
expected = Vec2D.new(val)
|
52
|
+
assert_equal(expected, Vec2D.new(1.0, 8.0), 'Failed duck type constructor fixnum')
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_failed_duck_type
|
56
|
+
val = Pointless.new(1.0, 8.0) # non duck type
|
57
|
+
assert_raises TypeError do
|
58
|
+
Vec2D.new(val)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_copy_not_equals
|
63
|
+
x, y = 1.0000001, 1.01
|
64
|
+
a = Vec2D.new(x, y)
|
65
|
+
b = a.copy
|
66
|
+
b *= 0
|
67
|
+
refute_equal(a.to_a, b.to_a, 'Failed deep copy')
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_equals_when_close
|
71
|
+
a = Vec2D.new(3.0000000, 5.00000)
|
72
|
+
b = Vec2D.new(3.0000000, 5.000001)
|
73
|
+
assert_equal(a, b, 'Failed to return equal when v. close')
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_sum
|
77
|
+
a = Vec2D.new(3, 5)
|
78
|
+
b = Vec2D.new(6, 7)
|
79
|
+
c = Vec2D.new(9, 12)
|
80
|
+
assert_equal(a + b, c, 'Failed to sum vectors')
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_subtract
|
84
|
+
a = Vec2D.new(3, 5)
|
85
|
+
b = Vec2D.new(6, 7)
|
86
|
+
c = Vec2D.new(-3, -2)
|
87
|
+
assert_equal(a - b, c, 'Failed to subtract vectors')
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_multiply
|
91
|
+
a = Vec2D.new(3, 5)
|
92
|
+
b = 2
|
93
|
+
c = a * b
|
94
|
+
d = Vec2D.new(6, 10)
|
95
|
+
assert_equal(c, d, 'Failed to multiply vector by scalar')
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_divide
|
99
|
+
a = Vec2D.new(3, 5)
|
100
|
+
b = 2
|
101
|
+
c = Vec2D.new(1.5, 2.5)
|
102
|
+
d = a / b
|
103
|
+
assert_equal(c, d, 'Failed to divide vector by scalar')
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_dot
|
107
|
+
a = Vec2D.new(3, 5)
|
108
|
+
b = Vec2D.new(6, 7)
|
109
|
+
assert_in_epsilon(a.dot(b), 53, 0.001, 'Failed to dot product')
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_self_dot
|
113
|
+
a = Vec2D.new(3, 5)
|
114
|
+
assert_in_epsilon(a.dot(a), 34, 0.001, 'Failed self dot product')
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_from_angle
|
118
|
+
a = Vec2D.from_angle(Math::PI * 0.75)
|
119
|
+
assert_equal(a, Vec2D.new(-1 * Math.sqrt(0.5), Math.sqrt(0.5)), 'Failed to create vector from angle')
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_random
|
123
|
+
a = Vec2D.random
|
124
|
+
assert a.kind_of? Vec2D
|
125
|
+
assert_in_epsilon(a.mag, 1.0)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_assign_value
|
129
|
+
a = Vec2D.new(3, 5)
|
130
|
+
a.x = 23
|
131
|
+
assert_equal(a.x, 23, 'Failed to assign x value')
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_mag
|
135
|
+
a = Vec2D.new(-3, -4)
|
136
|
+
assert_in_epsilon(a.mag, 5, 0.001,'Failed to return magnitude of vector')
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_mag_variant
|
140
|
+
a = Vec2D.new(3.0, 2)
|
141
|
+
b = Math.sqrt(3.0**2 + 2**2)
|
142
|
+
assert_in_epsilon(a.mag, b, 0.001, 'Failed to return magnitude of vector')
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_mag_zero_one
|
146
|
+
a = Vec2D.new(-1, 0)
|
147
|
+
assert_in_epsilon(a.mag, 1, 0.001, 'Failed to return magnitude of vector')
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_dist
|
151
|
+
a = Vec2D.new(3, 5)
|
152
|
+
b = Vec2D.new(6, 7)
|
153
|
+
assert_in_epsilon(a.dist(b), Math.sqrt(3.0**2 + 2**2), 0.001, 'Failed to return distance between two vectors')
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_lerp
|
157
|
+
a = Vec2D.new(1, 1)
|
158
|
+
b = Vec2D.new(3, 3)
|
159
|
+
assert_equal(a.lerp(b, 0.5), Vec2D.new(2, 2), 'Failed to return lerp between two vectors')
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_lerp_exception
|
163
|
+
a = Vec2D.new(1, 1)
|
164
|
+
b = Vec2D.new(3, 3)
|
165
|
+
assert_raises SyntaxError do
|
166
|
+
a.lerp(b, 0.5, 0.5)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_lerp_unclamped
|
171
|
+
a = Vec2D.new(1, 1)
|
172
|
+
b = Vec2D.new(3, 3)
|
173
|
+
assert_equal(a.lerp(b, 5), Vec2D.new(11, 11), 'Failed to return lerp between two vectors')
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_lerp!
|
177
|
+
a = Vec2D.new(1, 1)
|
178
|
+
b = Vec2D.new(3, 3)
|
179
|
+
a.lerp!(b, 0.5)
|
180
|
+
assert_equal(a, Vec2D.new(2, 2), 'Failed to return lerp! between two vectors')
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_lerp_unclamped!
|
184
|
+
a = Vec2D.new(1, 1)
|
185
|
+
b = Vec2D.new(3, 3)
|
186
|
+
a.lerp!(b, 5)
|
187
|
+
assert_equal(a, Vec2D.new(11, 11), 'Failed to return lerp! between two vectors')
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_set_mag
|
191
|
+
a = Vec2D.new(1, 1)
|
192
|
+
assert_equal(a.set_mag(Math.sqrt(32)), Vec2D.new(4, 4), 'Failed to set_mag vector')
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_set_mag_block
|
196
|
+
a = Vec2D.new(1, 1)
|
197
|
+
assert_equal(a.set_mag(Math.sqrt(32)) { true }, Vec2D.new(4, 4), 'Failed to set_mag_block true vector')
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_set_mag_block_false
|
201
|
+
a = Vec2D.new(1, 1)
|
202
|
+
assert_equal(a.set_mag(Math.sqrt(32)) { false }, Vec2D.new(1, 1), 'Failed to set_mag_block true vector')
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_plus_assign
|
206
|
+
a = Vec2D.new(3, 5)
|
207
|
+
b = Vec2D.new(6, 7)
|
208
|
+
a += b
|
209
|
+
assert_equal(a, Vec2D.new(9, 12), 'Failed to += assign')
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_normalize
|
213
|
+
a = Vec2D.new(3, 5)
|
214
|
+
b = a.normalize
|
215
|
+
assert_in_epsilon(b.mag, 1, 0.001, 'Failed to return a normalized vector')
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_normalize!
|
219
|
+
a = Vec2D.new(3, 5)
|
220
|
+
a.normalize!
|
221
|
+
assert_in_epsilon(a.mag, 1, 0.001, 'Failed to return a normalized! vector')
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_heading
|
225
|
+
a = Vec2D.new(1, 1)
|
226
|
+
assert_in_epsilon(a.heading, Math::PI / 4.0, 0.001, 'Failed to return heading in radians')
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_rotate
|
230
|
+
x, y = 20, 10
|
231
|
+
b = Vec2D.new(x, y)
|
232
|
+
a = b.rotate(Math::PI / 2)
|
233
|
+
assert_equal(a, Vec2D.new(-10, 20), 'Failed to rotate vector by scalar radians')
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_hash_index
|
237
|
+
x, y = 10, 20
|
238
|
+
b = Vec2D.new(x, y)
|
239
|
+
assert_equal(b[:x], x, 'Failed to hash index')
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_hash_set
|
243
|
+
x = 10
|
244
|
+
b = Vec2D.new
|
245
|
+
b[:x] = x
|
246
|
+
assert_equal(b, Vec2D.new(x, 0), 'Failed to hash assign')
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_inspect
|
250
|
+
a = Vec2D.new(3, 2.000000000000001)
|
251
|
+
assert_equal(a.inspect, 'Vec2D(x = 3.0000, y = 2.0000)')
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_array_reduce
|
255
|
+
array = [Vec2D.new(1, 2), Vec2D.new(10, 2), Vec2D.new(1, 2)]
|
256
|
+
sum = array.reduce(Vec2D.new) { |c, d| c + d }
|
257
|
+
assert_equal(sum, Vec2D.new(12, 6))
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_array_zip
|
261
|
+
one = [Vec2D.new(1, 2), Vec2D.new(10, 2), Vec2D.new(1, 2)]
|
262
|
+
two = [Vec2D.new(1, 2), Vec2D.new(10, 2), Vec2D.new(1, 2)]
|
263
|
+
zipped = one.zip(two).flatten
|
264
|
+
expected = [Vec2D.new(1, 2), Vec2D.new(1, 2), Vec2D.new(10, 2), Vec2D.new(10, 2), Vec2D.new(1, 2), Vec2D.new(1, 2)]
|
265
|
+
assert_equal(zipped, expected)
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_cross_area # NB: the sign might be negative
|
269
|
+
a = Vec2D.new(200, 0)
|
270
|
+
b = Vec2D.new(0, 200)
|
271
|
+
# Expected result is an area, twice that of the triangle created by the vectors
|
272
|
+
assert_equal((a).cross(b).abs, 40_000.0, 'Failed area test using 2D vector cross product')
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_cross_non_zero # Could be used to calculate area of triangle
|
276
|
+
a = Vec2D.new(40, 40)
|
277
|
+
b = Vec2D.new(40, 140)
|
278
|
+
c = Vec2D.new(140, 40)
|
279
|
+
assert_equal((a - b).cross(b - c).abs / 2, 5_000.0, 'Failed area calculation using 2D vector cross product')
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_cross_zero # where a, b, c are collinear area == 0
|
283
|
+
a = Vec2D.new(0, 0)
|
284
|
+
b = Vec2D.new(100, 100)
|
285
|
+
c = Vec2D.new(200, 200)
|
286
|
+
# see http://mathworld.wolfram.com/Collinear.html for details
|
287
|
+
assert((a - b).cross(b - c).zero?, 'Failed collinearity test using 2D vector cross product')
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_equals3
|
291
|
+
x, y, z = 1.0000001, 1.01, 0.0
|
292
|
+
a = Vec3D.new(x, y)
|
293
|
+
assert_equal(a.to_a, [x, y, z], 'Failed to return Vec3D as an Array')
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_constructor_float3
|
297
|
+
val = Point3.new(1.0, 8.0, 7.0) # duck type
|
298
|
+
expected = Vec3D.new(val)
|
299
|
+
assert_equal(expected, Vec3D.new(1.0, 8.0, 7.0), 'Failed duck type constructor floats')
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_constructor_fixnum3
|
303
|
+
val = Point3.new(1, 8, 7) # duck type fixnum
|
304
|
+
expected = Vec3D.new(val)
|
305
|
+
assert_equal(expected, Vec3D.new(1.0, 8.0, 7.0), 'Failed duck type constructor fixnum')
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_failed_duck_type3
|
309
|
+
val = Pointless3.new(1.0, 8.0, 7.0) # non duck type
|
310
|
+
assert_raises TypeError do
|
311
|
+
Vec3D.new(val)
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_not_equals3
|
316
|
+
a = Vec3D.new(3, 5, 1)
|
317
|
+
b = Vec3D.new(6, 7, 1)
|
318
|
+
refute_equal(a, b, 'Failed equals false')
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_copy_equals3
|
322
|
+
x, y, z = 1.0000001, 1.01, 1
|
323
|
+
a = Vec3D.new(x, y, z)
|
324
|
+
b = a.copy
|
325
|
+
assert_equal(a.to_a, b.to_a, 'Failed deep copy')
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_copy_not_equals3
|
329
|
+
x, y, z = 1.0000001, 1.01, 6.0
|
330
|
+
a = Vec3D.new(x, y, z)
|
331
|
+
b = a.copy
|
332
|
+
b *= 0
|
333
|
+
refute_equal(a.to_a, b.to_a, 'Failed deep copy')
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_equals_when_close3
|
337
|
+
a = Vec3D.new(3.0000000, 5.00000, 2)
|
338
|
+
b = Vec3D.new(3.0000000, 5.000001, 2)
|
339
|
+
assert_equal(a, b, 'Failed to return equal when v. close')
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_sum3
|
343
|
+
a = Vec3D.new(3, 5, 1)
|
344
|
+
b = Vec3D.new(6, 7, 1)
|
345
|
+
c = Vec3D.new(9, 12, 2)
|
346
|
+
assert_equal(a + b, c, 'Failed to sum vectors')
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_subtract3
|
350
|
+
a = Vec3D.new(3, 5, 0)
|
351
|
+
b = Vec3D.new(6, 7, 1)
|
352
|
+
c = Vec3D.new(-3, -2, -1)
|
353
|
+
assert_equal(a - b, c, 'Failed to subtract vectors')
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_multiply3
|
357
|
+
a = Vec3D.new(3, 5, 1)
|
358
|
+
b = 2
|
359
|
+
c = a * b
|
360
|
+
d = Vec3D.new(6, 10, 2)
|
361
|
+
assert_equal(c, d, 'Failed to multiply vector by scalar')
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_divide3
|
365
|
+
a = Vec3D.new(3, 5, 4)
|
366
|
+
b = 2
|
367
|
+
c = Vec3D.new(1.5, 2.5, 2)
|
368
|
+
d = a / b
|
369
|
+
assert_equal(c, d, 'Failed to divide vector by scalar')
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_random3
|
373
|
+
a = Vec3D.random
|
374
|
+
assert a.kind_of? Vec3D
|
375
|
+
assert_in_epsilon(a.mag, 1.0)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_assign_value3
|
379
|
+
a = Vec3D.new(3, 5)
|
380
|
+
a.x=23
|
381
|
+
assert_equal(a.x, 23, 'Failed to assign x value')
|
382
|
+
end
|
383
|
+
|
384
|
+
def test_mag3
|
385
|
+
a = Vec3D.new(-3, -4)
|
386
|
+
assert_equal(a.mag, 5, 'Failed to return magnitude of vector')
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_mag_variant3
|
390
|
+
a = Vec3D.new(3.0, 2)
|
391
|
+
b = Math.sqrt(3.0**2 + 2**2)
|
392
|
+
assert_in_epsilon(a.mag, b, 0.001, 'Failed to return magnitude of vector')
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_mag_zero_one3
|
396
|
+
a = Vec3D.new(-1, 0)
|
397
|
+
assert_equal(a.mag, 1, 'Failed to return magnitude of vector')
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_dist3
|
401
|
+
a = Vec3D.new(3, 5, 2)
|
402
|
+
b = Vec3D.new(6, 7, 1)
|
403
|
+
message = 'Failed to return distance between two vectors'
|
404
|
+
assert_equal(a.dist(b), Math.sqrt(3.0**2 + 2**2 + 1), message)
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_dist_squared3
|
408
|
+
a = Vec3D.new(3, 5, 2)
|
409
|
+
b = Vec3D.new(6, 7, 1)
|
410
|
+
message = 'Failed to return distance squared between two vectors'
|
411
|
+
assert_equal(a.dist_squared(b), 3.0**2 + 2**2 + 1, message)
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_dot3
|
415
|
+
a = Vec3D.new(10, 20, 0)
|
416
|
+
b = Vec3D.new(60, 80, 0)
|
417
|
+
assert_in_epsilon(a.dot(b), 2200.0, 0.001, 'Failed to dot product')
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_self_dot3
|
421
|
+
a = Vec3D.new(10, 20, 4)
|
422
|
+
assert_in_epsilon(a.dot(a), 516.0, 0.001, 'Failed to self dot product')
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_cross3
|
426
|
+
a = Vec3D.new(3, 5, 2)
|
427
|
+
b = Vec3D.new(6, 7, 1)
|
428
|
+
c = Vec3D.new(-9.0, 9.0, -9.0)
|
429
|
+
assert_equal(a.cross(b), c, 'Failed cross product')
|
430
|
+
end
|
431
|
+
|
432
|
+
def test_set_mag3
|
433
|
+
a = Vec3D.new(1, 1)
|
434
|
+
assert_equal(a.set_mag(Math.sqrt(32)), Vec3D.new(4, 4), 'Failed to set_mag vector')
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_set_mag_block3
|
438
|
+
a = Vec3D.new(1, 1)
|
439
|
+
assert_equal(a.set_mag(Math.sqrt(32)) { true }, Vec3D.new(4, 4), 'Failed to set_mag_block true vector')
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_set_mag_block_false3
|
443
|
+
a = Vec3D.new(1, 1)
|
444
|
+
assert_equal(a.set_mag(Math.sqrt(32)) { false }, Vec3D.new(1, 1), 'Failed to set_mag_block true vector')
|
445
|
+
end
|
446
|
+
|
447
|
+
def test_plus_assign3
|
448
|
+
a = Vec3D.new(3, 5)
|
449
|
+
b = Vec3D.new(6, 7)
|
450
|
+
a += b
|
451
|
+
assert_equal(a, Vec3D.new(9, 12), 'Failed to += assign')
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_normalize3
|
455
|
+
a = Vec3D.new(3, 5)
|
456
|
+
b = a.normalize
|
457
|
+
assert_in_epsilon(b.mag, 1, 0.001, 'Failed to return a normalized vector')
|
458
|
+
end
|
459
|
+
|
460
|
+
def test_normalize3!
|
461
|
+
a = Vec3D.new(3, 5)
|
462
|
+
a.normalize!
|
463
|
+
assert_in_epsilon(a.mag, 1, 0.001, 'Failed to return a normalized! vector')
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_inspect3
|
467
|
+
a = Vec3D.new(3, 2.000000000000001, 1)
|
468
|
+
assert_equal(a.inspect, 'Vec3D(x = 3.0000, y = 2.0000, z = 1.0000)')
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_array_reduce3
|
472
|
+
array = [Vec3D.new(1, 2), Vec3D.new(10, 2), Vec3D.new(1, 2)]
|
473
|
+
sum = array.reduce(Vec3D.new) { |c, d| c + d }
|
474
|
+
assert_equal(sum, Vec3D.new(12, 6))
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_array_zip3
|
478
|
+
one = [Vec3D.new(1, 2), Vec3D.new(10, 2), Vec3D.new(1, 2)]
|
479
|
+
two = [Vec3D.new(1, 2), Vec3D.new(10, 2), Vec3D.new(1, 2)]
|
480
|
+
zipped = one.zip(two).flatten
|
481
|
+
expected = [Vec3D.new(1, 2), Vec3D.new(1, 2), Vec3D.new(10, 2), Vec3D.new(10, 2), Vec3D.new(1, 2), Vec3D.new(1, 2)]
|
482
|
+
assert_equal(zipped, expected)
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_eql3?
|
486
|
+
a = Vec3D.new(3.0, 5.0, 0)
|
487
|
+
b = Vec3D.new(3.0, 5.0, 0)
|
488
|
+
assert(a.eql?(b))
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_not_eql3?
|
492
|
+
a = Vec3D.new(3.0, 5.0, 0)
|
493
|
+
b = Vec3D.new(3.0, 5.000001, 0)
|
494
|
+
refute(a.eql?(b))
|
495
|
+
end
|
496
|
+
|
497
|
+
def test_equal3?
|
498
|
+
a = Vec3D.new(3.0, 5.0, 0)
|
499
|
+
assert(a.equal?(a))
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_not_equal3?
|
503
|
+
a = Vec3D.new(3.0, 5.0, 0)
|
504
|
+
b = Vec3D.new(3.0, 5.0, 0)
|
505
|
+
refute(a.equal?(b))
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_hash_key3
|
509
|
+
x, y, z = 10, 20, 50
|
510
|
+
b = Vec3D.new(x, y, z)
|
511
|
+
assert_equal(b[:x], x, 'Failed hash key access')
|
512
|
+
assert_equal(b[:y], y, 'Failed hash key access')
|
513
|
+
assert_equal(b[:z], z, 'Failed hash key access')
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_hash_set3
|
517
|
+
x = 10
|
518
|
+
b = Vec3D.new
|
519
|
+
b[:x] = x
|
520
|
+
assert_equal(b, Vec3D.new(x, 0, 0), 'Failed to hash assign')
|
521
|
+
end
|
522
|
+
end
|