-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaabb_test.rb
More file actions
104 lines (86 loc) · 1.81 KB
/
aabb_test.rb
File metadata and controls
104 lines (86 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Click on the box and drag it across the screen.
attr_reader :block, :block_locked, :over_block, :bounds
BLOCK_WIDTH = 150
def setup
sketch_title 'AaBb Example'
on_top
@block = Block.new(
center: Vec2D.new(width / 2, height / 2),
size: Vec2D.new(BLOCK_WIDTH, BLOCK_WIDTH))
@locked = false
@over_block = false
@bounds = AaBb.new(
center: Vec2D.new(width / 2, height / 2),
extent: Vec2D.new(width - BLOCK_WIDTH, height - BLOCK_WIDTH)
)
end
def draw
background 0
fill 153
if block.over?(Vec2D.new(mouse_x, mouse_y))
@over_block = true
stroke 255
fill 255 if block_locked?
else
@over_block = false
stroke 153
end
# Draw the box as a shape
begin_shape
block.points_array.each { |vec| vec.to_vertex(renderer) }
end_shape(CLOSE)
end
def renderer
@renderer ||= GfxRender.new(self.g)
end
def block_locked?
block_locked
end
def over_block?
over_block
end
def mouse_pressed
if over_block?
@block_locked = true
fill 255
else
@block_locked = false
end
end
def mouse_dragged
return unless block_locked?
position = Vec2D.new(mouse_x, mouse_y)
block.new_position(position) { bounds.contains? position }
end
def mouse_released
@block_locked = false
end
def settings
size 640, 360
smooth 4
end
# Use class to contain block behaviour
class Block
attr_reader :aabb
def initialize(center:, size:)
@aabb = AaBb.new(center: center, extent: size)
end
def new_position(center, &block)
@aabb.position(center.dup, &block)
end
def over?(vec)
aabb.contains? vec
end
# use for shape
def points_array
a = aabb.center - aabb.extent * 0.5
c = aabb.center + aabb.extent * 0.5
b = Vec2D.new(c.x, a.y)
d = Vec2D.new(a.x, c.y)
[a, b, c, d]
end
# use for rect
def points
[aabb.center, aabb.extent]
end
end