enter image description here

Suppose I need the coordinates of 360 points on a circle, one for each degree of rotation

There is an assumption:

int x = (int)Math.Cos(2 * Math.PI * i / n) * R + x[0]; int y = (int)Math.Sin(2 * Math.PI * i / n) * R + y[0]; 

Where i is the point number, n is the number of points = 360, R is the radius, x [0] and y [0] are the coordinates of the center of the circle

Only in this way only these four points are calculated:

enter image description here

How to go through all 360 points?

  • 3
    you yourself cast to int, it cannot take floating point values ​​(namely, you need them). - etki
  • It works, thanks. spent the hour as usual in general - zaki hatfild
  • one
    @zakihatfild, you would attach the answer, suddenly someone does not understand) - SanŚ́́́́́́́́́́́́́

2 answers 2

 int x = (int)Math.Cos(2 * Math.PI * i / n) * R + x[0]; int y = (int)Math.Sin(2 * Math.PI * i / n) * R + y[0]; 

I guess we need exactly the integer coordinates. In this case, you should perform rounding after multiplying by the radius.

 int x = (int)(Math.Cos(2 * Math.PI * i / n) * R + 0.5) + x0; int y = (int)(Math.Sin(2 * Math.PI * i / n) * R + 0.5) + y0; 

This still does not guarantee that all 360 points will be obtained, but now there will not always be 4 of them.

http://ideone.com/fxzB8V

 4 of 360 when radius is 1 76 of 360 when radius is 10 140 of 360 when radius is 20 268 of 360 when radius is 40 356 of 360 when radius is 80 360 of 360 when radius is 90 360 of 360 when radius is 100 

If integer coordinates are not required, then double should be used instead of int:

 double x = Math.Cos(2 * Math.PI * i / n) * R + x0; double y = Math.Sin(2 * Math.PI * i / n) * R + y0; 
  • And what does this answer mean? How should the displacement of the center of the circle by 0.5 should solve the formulated problem? - Kromster
  • @KromStern, I do not shift the center. I put the brackets so that rounding occurs after multiplying by the radius, not before. + 0.5 also in parentheses and provides mathematical rounding instead of dropping the fractional part. - Qwertiy
  • even if you apply mathematical rounding, it still gives you only a few points, and not 360, as the author needs. See the Etki comment. - Kromster
  • 2
    @KromStern, in its code the result of the call sin and cos is rounded, so it has 4 points regardless of the radius. And in my final coordinate is rounded, so the number of points will be the corresponding radius. That is the fix. - Qwertiy
  • one
    If we are talking about C #, more idiomatic rounding is through Math.Round . - VladD
 int r = 5; for(int i = 0 ; i < 360; i++) { double rad = (double)i / 180 * 3.14; double x = r * cos(rad); double y = r * sin(rad); qDebug() << "X:" << x << " Y:" << y << " Rad:" << rad; } 

This is a qt c ++ solution, as visual studio is not at hand. The center offset value is added to the resulting x, y. r is the radius of the circle

The output is like this:

  X: 5 Y: 0 Rad: 0 X: 4.99924 Y: 0.0872178 Rad: 0.0174444 X: 4.99696 Y: 0.174409 Rad: 0.0348889 X: 4.99315 Y: 0.261547 Rad: 0.0523333 X: 4.98783 Y: 0.348606 Rad: 0.0697778 X: 4.98099 Y: 0.435558 Rad: 0.0872222 X: 4.97264 Y: 0.522378 Rad: 0.104667 X: 4.96277 Y: 0.609039 Rad: 0.122111 X: 4.95139 Y: 0.695515 Rad: 0.139556 X: 4.9385 Y: 0.781779 Rad: 0.157 X: 4.92412 Y: 0.867805 Rad: 0.174444 ....