-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrounded_rectangle.cr
40 lines (33 loc) · 946 Bytes
/
rounded_rectangle.cr
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
require "crsfml"
macro event_property(name, code)
getter {{name}}
def {{name.id}}=(value)
@{{name.id}} = value
{{code}}
end
end
class RoundedRectangleShape < SF::ConvexShape
def initialize(@size : SF::Vector2(Float64), @radius : Float64, @corner_points : Int32 = 5)
super()
update
end
event_property size, update
event_property radius, update
event_property corner_points, update
def update
self.point_count = corner_points*4
centers = [
{size.x - radius, radius}, {radius, radius},
{radius, size.y - radius}, {size.x - radius, size.y - radius}
]
(0...point_count).each do |index|
center_index = (index / corner_points).to_i
angle = (index - center_index) * Math::PI / 2 / (corner_points - 1)
center = centers[center_index]
self[index] = {
center[0] + radius*Math.cos(angle),
center[1] - radius*Math.sin(angle)
}
end
end
end