Why is my circle uneven?

Place to get help with not working mods / modding interface.
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Why is my circle uneven?

Post 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 825 times
Screenshot_4.jpg
Screenshot_4.jpg (251.1 KiB) Viewed 825 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 825 times
How do i fix this?
Last edited by WIZ4 on Sun Feb 10, 2019 5:44 pm, edited 1 time in total.
My native language is russian. Sorry if my messages are difficult to read.
pleegwat
Filter Inserter
Filter Inserter
Posts: 278
Joined: Fri May 19, 2017 7:31 pm
Contact:

Re: Why is my circle uneven?

Post 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
Post Reply

Return to “Modding help”