def make_verts(s, r)
a = Math::PI/s
b = Math::PI/(s/2)
d = r/Math.cos(a)
verts = []
verts.push(d*Math.cos(a),d*Math.sin(a))
for i in (1..s-1)
c = (a+i)*b
verts.push(d*Math.cos(c)) #x
verts.push(d*Math.sin(c)) #y
end
verts
end
num_verts = ARGV[0].to_i
radius = ARGV[1].to_f / 2
vert_array = make_verts(num_verts, radius).join(",")
puts "[" + vert_array + "]"
Refactorings
No refactoring yet !
slf
March 9, 2010, March 09, 2010 13:53, permalink
I think I've corrected the algorithm to spit stuff out in the correct order, but it doesn't seem to be giving me accurate results. I've obviously missed something somewhere.
def make_verts(n, r)
# assume centerpoint of 0,0 for now
xc = 0.0
yc = 0.0
verts = []
for i in (0..n-1)
a = 2.0 * Math::PI * i / n
x = xc + r * Math::cos(a)
y = yc + r * Math::sin(a)
verts.push(x)
verts.push(y)
end
verts
end
Can somebody check my math? I quickly sketched a couple of octagons, and it seemed ok but I just wanted to make sure. Also, any way to make this better?