098e5c1a565b47e9860539fbebc3fa98

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?

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 !

098e5c1a565b47e9860539fbebc3fa98

slf

March 9, 2010, March 09, 2010 13:53, permalink

No rating. Login to rate!

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
098e5c1a565b47e9860539fbebc3fa98

slf

March 16, 2010, March 16, 2010 16:08, permalink

No rating. Login to rate!

FYI the workaround was to always make sure the coords were in the positive X positive Y space because of the ArcTan problem... not a fix, but it's fine for now

Your refactoring





Format Copy from initial code

or Cancel