🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

position along a circle

Started by
5 comments, last by Alberth 6 years, 4 months ago

Hello all,

So i want to place objects along a (half) circular line with the formula (x-a)^2 + (y-b)^2  = r^2

object pixel size are 36px.

so if i have 10 objects then r would become (10.36):2 = 180.

finding Y for X =0 (0 degrees)becomes sqrt(r^2 - 0^2)  Y becomes 180

finding Y for X =5 (90 degrees) becomes sqrt(r^2 - (5*36)^2)  Y becomes 0

this makes perfect sense...

but if i do this in a for loop then the sequence for x will be (0,36,72,108,144,180) this means that the 6th object gets the 90 degrees Y value....and not the 5th.

(for i= 0 etc)

x = i*36...

 

I know its probably very 'dumb'question but i just don't see it anymore ;)

Thx in advance...

Advertisement

Use sine and cosine instead
 


import math
r = ...
for i in range(10):
    x = r * cos(math.pi * i / 10)
    y = r * sin(math.pi * i / 10)

# 0 radian <-> 0 degrees
# pi/2 radian <-> 90 degrees
# pi radian <-> 180 degrees

# For proper centering, you may want to add 0.5 to i

 

Thx alot Alberth! ;-)

 

Still wondering though.....

 

 

I am not quite sure what you mean, actually. What is "6th"?

You are only speaking about X index values (which seems to start at 0), and you use such index values for i in the "for" statement as well. Then suddenly you have a 6th object??

The only thing I can think of is that you somewhere switch to a different indexing system, namely 1st, 2nd, 3rd, etc. Alternatively, you have switched to a languages that starts counting at index 1 instead of 0, like Matlab or Lua.

 

If so, then yes, when you start counting at 0 versus when you start counting at 1 makes exactly 1 unit difference everywhere :)

 

Sometimes i'm almost human ;-)

 

what i did was:


for(int i=0;i<aantal;i++) {
    x = i * 36;
    y = (int) Math.sqrt(Math.pow(radius, 2) - Math.pow(x, 2));
}


 


 

so the 5th object at index 4 got (4*36) obv. instead of (5*36) grrr

 

thx again Alberth.

Just always use the index value to refer to an item :)

This topic is closed to new replies.

Advertisement