Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update drawolver to use Vec3D

  • Loading branch information...
commit 8cde8a866c9448c7e89547d0853390dbbbf7f966 1 parent 829157b
@monkstone monkstone authored
Showing with 18 additions and 39 deletions.
  1. +1 −1  CHANGELOG
  2. +17 −38 samples/contributed/drawolver.rb
View
2  CHANGELOG
@@ -1,7 +1,7 @@
v2.4.3 Update to JRuby-1.7.11
* Added an autorun demo Rakefile to some sample directories
* Support utf-8 in sketches
-* Refactor and extend vecmath
+* Refactor and extend vecmath (updated drawolver to use Vec3D)
v2.4.2 Update to JRuby-1.7.10
* Revised suggestions for PROCESSING_ROOT on MacOSX
View
55 samples/contributed/drawolver.rb
@@ -1,10 +1,14 @@
# Drawolver: draw 2D & revolve 3D
-# Example to show how to extend Ruby classes in a useful way and how to
-# use PVector and the Array is extended to yield one_of_each
-# pair of pts. See the drawolver library. Also features the use each_cons,
-# possibly a rare use for this ruby Enumerable method?
-# 2010-03-22 - fjenett (last revised by monkstone 2013-09-13)
+# This example demonstrates the use of the `vecmath` library (a ruby-processing
+# replacement for PVector). Also demonstrated is how to extend an instance of a
+# here an instance of a core ruby class. In this case the ruby Array class,
+# is extended to yield one_of_each pair of pts (see ExtendedArray module). This
+# examples also includes a possibly rare use of the each_cons Enumerable method?
+# For simpler illustrations of `vecmath` usage see the library examples.
+# 2010-03-22 - fjenett (last revised by monkstone 2014-02-18)
+
+load_library :vecmath
attr_reader :drawing_mode, :points, :rot_x, :rot_y, :vertices
@@ -24,7 +28,6 @@ def one_of_each( &block )
end
end
-
def setup
size 1024, 768, P3D
frame_rate 30
@@ -71,15 +74,15 @@ def reset_scene
def mouse_pressed
reset_scene
- points << RPVector.new(mouse_x, mouse_y)
+ points << Vec3D.new(mouse_x, mouse_y)
end
def mouse_dragged
- points << RPVector.new(mouse_x, mouse_y)
+ points << Vec3D.new(mouse_x, mouse_y)
end
def mouse_released
- points << RPVector.new(mouse_x, mouse_y)
+ points << Vec3D.new(mouse_x, mouse_y)
recalculate_shape
end
@@ -87,16 +90,16 @@ def recalculate_shape
@vertices = []
points.each_cons(2) do |ps, pe|
b = points.last - points.first
- len = b.mag
- b.normalize
+ # len = b.mag
+ b.normalize!
a = ps - points.first
dot = a.dot b
- b = b * dot
+ b *= dot
normal = points.first + b
c = ps - normal
- nlen = c.mag
+ # nlen = c.mag
vertices << []
- (0..TWO_PI).step(PI/15) do |ang|
+ (0..TAU).step(PI/15) do |ang|
e = normal + c * cos(ang)
e.z = c.mag * sin(ang)
vertices.last << e
@@ -105,27 +108,3 @@ def recalculate_shape
@drawing_mode = false
end
-# a wrapper around PVector that implements operators methods for +, -, *, /
-#
-class RPVector < Java::ProcessingCore::PVector
-
- def + (vect)
- RPVector.new self.x + vect.x, self.y + vect.y, self.z + vect.z
- end
-
- def - (vect)
- RPVector.new self.x - vect.x, self.y - vect.y, self.z - vect.z
- end
-
- def * (scalar)
- RPVector.new self.x * scalar, self.y * scalar, self.z * scalar
- end
-
- def / (scalar)
- RPVector.new(self.x / scalar, self.y / scalar, self.z / scalar) unless scalar == 0
- end
-
-end
-
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.