Skip to content

Commit dcdebf5

Browse files
committed
Axis stuff
1 parent d46f9e1 commit dcdebf5

File tree

4 files changed

+276
-3
lines changed

4 files changed

+276
-3
lines changed

contributed/library/branch3D/rotator.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Rotator module is required because rotate is not implemented for
22
# JRubyArt 3D. Here we use Vec2D.rotate! to do Euclid rotation about an
3-
# axis (we can then apply the rotation to each axis in turn)
3+
# axis (we can then apply the rotation to each axis in turn) we mimic Toxiclibs
4+
# Vec3D implementation
45
# NB: we use quaternions in ArcBall (to avoid gimbal lock)
6+
# See:
7+
# https://medium.com/@behreajj/3d-rotations-in-processing-vectors-matrices-quaternions-10e2fed5f0a3
58
module Rotate
69
def self.axis!(axis, vec, theta)
710
array = vec.to_a
811
array.slice! axis
912
other = Vec2D.new(*array).rotate! theta
1013
case axis
1114
when 0 # xaxis
12-
vec.y = other.x
13-
vec.z = other.y
15+
vec.y = other.y
16+
vec.z = other.x
1417
when 1 # yaxis
1518
vec.x = other.x
1619
vec.z = other.y
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# frozen_string_literal: true
2+
3+
INSTRUCTIONS = <<~TEXT
4+
#######################################################
5+
1. Mouse Drag to rotate axes
6+
2. Hold down x, y or z key to clamp rotation in one axis
7+
3. Mouse wheel to zoom
8+
Red = xaxis
9+
Green = yaxis
10+
Blue = zaxis
11+
#######################################################
12+
TEXT
13+
14+
SIDES = 30
15+
ANGLE = 360 / SIDES
16+
LENGTH = 200
17+
RADIUS = 20
18+
19+
def setup
20+
sketch_title 'Rotate Axes'
21+
ArcBall.init self
22+
puts INSTRUCTIONS
23+
end
24+
25+
def draw
26+
background(128, 128, 128)
27+
lights
28+
no_stroke
29+
draw_axes
30+
end
31+
32+
def draw_axes
33+
fill(255, 0, 0)
34+
draw_x_axis
35+
fill(0, 255, 0)
36+
draw_y_axis
37+
fill(0, 0, 255)
38+
draw_z_axis
39+
end
40+
41+
def draw_y_axis
42+
# draw top of the tube
43+
begin_shape
44+
(0..SIDES).each do |i|
45+
y = DegLut.cos(i * ANGLE) * RADIUS
46+
x = DegLut.sin(i * ANGLE) * RADIUS
47+
vertex(x, 0, y)
48+
end
49+
end_shape(CLOSE)
50+
# draw bottom of the tube
51+
begin_shape
52+
(0..SIDES).each do |i|
53+
y = DegLut.cos(i * ANGLE) * RADIUS
54+
x = DegLut.sin(i * ANGLE) * RADIUS
55+
vertex(x, LENGTH, y)
56+
end
57+
end_shape(CLOSE)
58+
# draw SIDES
59+
begin_shape(TRIANGLE_STRIP)
60+
(0..SIDES).each do |i|
61+
y = DegLut.cos(i * ANGLE) * RADIUS
62+
x = DegLut.sin(i * ANGLE) * RADIUS
63+
vertex(x, 0, y)
64+
vertex(x, LENGTH, y)
65+
end
66+
end_shape(CLOSE)
67+
end
68+
69+
def draw_z_axis
70+
# draw top of the tube
71+
begin_shape
72+
(0..SIDES).each do |i|
73+
x = DegLut.cos(i * ANGLE) * RADIUS
74+
y = DegLut.sin(i * ANGLE) * RADIUS
75+
vertex(0, x, y)
76+
end
77+
end_shape(CLOSE)
78+
# draw bottom of the tube
79+
begin_shape
80+
(0..SIDES).each do |i|
81+
x = DegLut.cos(i * ANGLE) * RADIUS
82+
y = DegLut.sin(i * ANGLE) * RADIUS
83+
vertex(LENGTH, x, y)
84+
end
85+
end_shape(CLOSE)
86+
# draw SIDES
87+
begin_shape(TRIANGLE_STRIP)
88+
(0..SIDES).each do |i|
89+
x = DegLut.cos(i * ANGLE) * RADIUS
90+
y = DegLut.sin(i * ANGLE) * RADIUS
91+
vertex(0, x, y)
92+
vertex(LENGTH, x, y)
93+
end
94+
end_shape(CLOSE)
95+
end
96+
97+
def draw_x_axis
98+
# draw top of the tube
99+
begin_shape
100+
(0..SIDES).each do |i|
101+
x = DegLut.cos(i * ANGLE) * RADIUS
102+
y = DegLut.sin(i * ANGLE) * RADIUS
103+
vertex(x, y, 0)
104+
end
105+
end_shape(CLOSE)
106+
# draw bottom of the tube
107+
begin_shape
108+
(0..SIDES).each do |i|
109+
x = DegLut.cos(i * ANGLE) * RADIUS
110+
y = DegLut.sin(i * ANGLE) * RADIUS
111+
vertex(x, y, LENGTH)
112+
end
113+
end_shape(CLOSE)
114+
# draw SIDES
115+
begin_shape(TRIANGLE_STRIP)
116+
(0..SIDES).each do |i|
117+
x = DegLut.cos(i * ANGLE) * RADIUS
118+
y = DegLut.sin(i * ANGLE) * RADIUS
119+
vertex(x, y, 0)
120+
vertex(x, y, LENGTH)
121+
end
122+
end_shape(CLOSE)
123+
end
124+
125+
def settings
126+
size(500, 500, P3D)
127+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
INSTRUCTIONS = <<~TEXT
4+
#######################################################
5+
1. Mouse Drag to rotate axes
6+
2. Hold down x, y or z key to clamp rotation in one axis
7+
3. Mouse wheel to zoom
8+
Red = xaxis
9+
Green = yaxis
10+
Blue = zaxis
11+
#######################################################
12+
TEXT
13+
14+
load_library :axes
15+
16+
attr_reader :axes
17+
18+
def setup
19+
sketch_title 'Rotate Axes'
20+
ArcBall.init self
21+
@axes = Axes.new self
22+
puts INSTRUCTIONS
23+
end
24+
25+
def draw
26+
background(128, 128, 128)
27+
lights
28+
no_stroke
29+
axes.draw
30+
end
31+
32+
def settings
33+
size(500, 500, P3D)
34+
end
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# frozen_string_literal: true
2+
3+
java_import 'processing.core.PConstants'
4+
# Three axes as a class
5+
class Axes
6+
SIDES = 30
7+
ANGLE = 360 / SIDES
8+
LENGTH = 200
9+
RADIUS = 20
10+
11+
attr_reader :app
12+
13+
def initialize(app)
14+
@app = app
15+
end
16+
17+
def draw
18+
app.fill(255, 0, 0)
19+
draw_x_axis
20+
app.fill(0, 255, 0)
21+
draw_y_axis
22+
app.fill(0, 0, 255)
23+
draw_z_axis
24+
end
25+
26+
def draw_y_axis
27+
# draw top of the tube
28+
app.begin_shape
29+
(0..SIDES).each do |i|
30+
y = DegLut.cos(i * ANGLE) * RADIUS
31+
x = DegLut.sin(i * ANGLE) * RADIUS
32+
app.vertex(x, 0, y)
33+
end
34+
app.end_shape(PConstants::CLOSE)
35+
# draw bottom of the tube
36+
app.begin_shape
37+
(0..SIDES).each do |i|
38+
y = DegLut.cos(i * ANGLE) * RADIUS
39+
x = DegLut.sin(i * ANGLE) * RADIUS
40+
app.vertex(x, LENGTH, y)
41+
end
42+
app.end_shape(PConstants::CLOSE)
43+
# draw SIDES
44+
app.begin_shape(PConstants::TRIANGLE_STRIP)
45+
(0..SIDES).each do |i|
46+
y = DegLut.cos(i * ANGLE) * RADIUS
47+
x = DegLut.sin(i * ANGLE) * RADIUS
48+
app.vertex(x, 0, y)
49+
app.vertex(x, LENGTH, y)
50+
end
51+
app.end_shape(PConstants::CLOSE)
52+
end
53+
54+
def draw_z_axis
55+
# draw top of the tube
56+
app.begin_shape
57+
(0..SIDES).each do |i|
58+
x = DegLut.cos(i * ANGLE) * RADIUS
59+
y = DegLut.sin(i * ANGLE) * RADIUS
60+
app.vertex(0, x, y)
61+
end
62+
app.end_shape(PConstants::CLOSE)
63+
# draw bottom of the tube
64+
app.begin_shape
65+
(0..SIDES).each do |i|
66+
x = DegLut.cos(i * ANGLE) * RADIUS
67+
y = DegLut.sin(i * ANGLE) * RADIUS
68+
app.vertex(LENGTH, x, y)
69+
end
70+
app.end_shape(PConstants::CLOSE)
71+
# draw SIDES
72+
app.begin_shape(PConstants::TRIANGLE_STRIP)
73+
(0..SIDES).each do |i|
74+
x = DegLut.cos(i * ANGLE) * RADIUS
75+
y = DegLut.sin(i * ANGLE) * RADIUS
76+
app.vertex(0, x, y)
77+
app.vertex(LENGTH, x, y)
78+
end
79+
app.end_shape(PConstants::CLOSE)
80+
end
81+
82+
def draw_x_axis
83+
# draw top of the tube
84+
app.begin_shape
85+
(0..SIDES).each do |i|
86+
x = DegLut.cos(i * ANGLE) * RADIUS
87+
y = DegLut.sin(i * ANGLE) * RADIUS
88+
app.vertex(x, y, 0)
89+
end
90+
app.end_shape(PConstants::CLOSE)
91+
# draw bottom of the tube
92+
app.begin_shape
93+
(0..SIDES).each do |i|
94+
x = DegLut.cos(i * ANGLE) * RADIUS
95+
y = DegLut.sin(i * ANGLE) * RADIUS
96+
app.vertex(x, y, LENGTH)
97+
end
98+
app.end_shape(PConstants::CLOSE)
99+
# draw SIDES
100+
app.begin_shape(PConstants::TRIANGLE_STRIP)
101+
(0..SIDES).each do |i|
102+
x = DegLut.cos(i * ANGLE) * RADIUS
103+
y = DegLut.sin(i * ANGLE) * RADIUS
104+
app.vertex(x, y, 0)
105+
app.vertex(x, y, LENGTH)
106+
end
107+
app.end_shape(PConstants::CLOSE)
108+
end
109+
end

0 commit comments

Comments
 (0)