Page 1 of 1

Why is my circle uneven?

Posted: Sat Feb 09, 2019 7:46 pm
by WIZ4
I am trying to create a circle of entities. I wrote a function for this:

Code: Select all

function circle(x,y,r)
	local angle = 0

	x = x + 0.5 - (x + 0.5) % 1
	y = y + 0.5 - (y + 0.5) % 1
	r =  r + 0.5
		while angle < 2*math.pi do
		angle = angle + r/r^2
	
		local xs = (x  +   r * math.cos (angle))
		local ys = (y  +   r * math.sin (angle))
	game.surfaces[1].create_entity{name = "wooden-chest", position= {xs,ys},force='player'}
	end
end	
But my circle is uneven. besides, chests are sometimes placed again in the same position(I identified this as a duplicate shadow).

Code: Select all

/c circle(0,0,15)
Screenshot_5.jpg
Screenshot_5.jpg (228.89 KiB) Viewed 828 times
Screenshot_4.jpg
Screenshot_4.jpg (251.1 KiB) Viewed 828 times
Orange highlighting - discrepancy
Blue Arrow - Chests x2

Code: Select all

/c circle(0,0,10)
Discrepancy none, But duplicate chests are present.
Screenshot_6.jpg
Screenshot_6.jpg (152.58 KiB) Viewed 828 times
How do i fix this?

Re: Why is my circle uneven?

Posted: Sat Feb 09, 2019 8:17 pm
by pleegwat
You're probably running into rounding errors in the angle calculation. It may be more reliable to iterate over x and y instead of over the angle.

EDIT: A quick google turns up https://en.wikipedia.org/wiki/Midpoint_circle_algorithm