-
Notifications
You must be signed in to change notification settings - Fork 3
/
verts.rb
executable file
·52 lines (42 loc) · 848 Bytes
/
verts.rb
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
#!/usr/bin/env ruby
class Polygon
attr_accessor :vertices
def initialize(n, d)
cx = d*2.0
cy = d*2.0
radius = d / 2.0
verts = []
# loop backward to produce a clockwise winding
max = n.to_i - 1
max.downto(0) {
|i|
a = 2.0 * Math::PI * i / n
x = cx + radius * Math.cos(a)
y = cy + radius * Math.sin(a)
verts.push(x)
verts.push(y)
}
@vertices = verts
translate(-cx, -cy)
end
def translate(x, y)
verts = []
i = 0
for v in @vertices
if ((i%2)==0)
verts.push(v+x)
else
verts.push(v+y)
end
i = i + 1
end
@vertices = verts
end
def to_s
"[" + @vertices.join(",") + "]"
end
end
numsides = ARGV[0].to_f
diameter = ARGV[1].to_f
o = Polygon.new(numsides, diameter)
puts o