Jump to content




Generating Polygons/finding Points On Circumfrence Of A Circle


  • You cannot reply to this topic
2 replies to this topic

#1 billysback

  • Members
  • 569 posts

Posted 30 July 2013 - 06:53 AM

Basically, it's fucking up and I have no idea why.
here's the important bit that's messing up:
--r = radius of circle, ang = angle, x,y = centre of circle
local function getPointOnCirc(r, ang, x, y)
	local x2 = x + r * math.cos(ang)
	local y2 = y + r * math.sin(ang)
	return {math.ceil(x2), math.ceil(y2)}
end

--ps = no. points, r =  radius of circle, x,y = centre of circle
local function genPoly(ps, r, x, y)
	local poly = {}
	local tot = 0
	for i=1,ps do
		local rot = 360-tot
		if rot <= 0 then rot = 1 end
		if i == 1 then rot = rot/2 end
		tot = tot + math.random(rot)
		if tot > 360 then tot = 360 end
		local np = getPointOnCirc(r, tot, x, y)
		print(tot..","..np[1]..","..np[2])
		--sleep(0.5)
		poly[#poly + 1] = np
	end
	return poly
end

here's the rest of the program in case you want to try it out:
Spoiler

for some reason, despite the angle being constantly under 360, it is drawing the points at seemingly random positions around the circle, meaning weird looking shapes are being made.
I have no idea why.

EDIT:
also, here is an example output of that function:
154,20,9
288,29,4
302,31,12
349,20,8
353,28,15
356,22,4
where the first number is the angle and the next two are the x,y coords.

NOTE:
there aren't any errors, the program *works* fine, it just doesn't work as expected

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 30 July 2013 - 08:41 AM

In Lua, sin/cos take radians, not degrees. I haven't looked at your math yet but I'm guessing that's the issue.

math.sin(90) --0.893
math.sin((90*math.pi)/180) --1


#3 billysback

  • Members
  • 569 posts

Posted 30 July 2013 - 08:51 AM

That fixed it, thanks
This exact equations has worked in previous programs though .-.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users