Search code examples
cmathpebble-watchpebble-sdk

Coordinates of points dividing circle into n equal halves in Pebble


I am trying to divide the circle into n equal parts using straight lines. Idea is to get the coordinates of the end points on the circumference of the circle and then to draw the line.

to find coodrinates I am using following code:

static void getPoints(int x0,int y0,int r)
{

   double angle = 0;
   double angleToRadian = 0;

   for(int i = 0 ; i < noOfDividingPoints  ;i++)
   {   
      angle = i * (360/noOfDividingPoints);

      angleToRadian = ( angle * 3.141 ) / 180;

      APP_LOG(APP_LOG_LEVEL_DEBUG, "LOG: Angle: %lf, AngleToRadian: %lf", angle, angleToRadian );

      x[i] = (int) (x0 + r * cos_lookup(angleToRadian));
      y[i] = (int) (y0 + r * sin_lookup(angleToRadian));
      APP_LOG(APP_LOG_LEVEL_DEBUG, "LOG: i: %d,  x[i]: %d, y[i]: %d", i, (int)x[i], (int)y[i] );
   }
}

Using this getPoints method I am filling the array x[] and y[] and then iterate over these arrays to draw lines between x[i] and y[i]

However, the values calculated from the above code comes out to be weird, below are the logs printing the contents of x[] and y[]

[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f

[14:18:57] WatchFace.c:45> LOG: i: 0,  x[i]: 4587522, y[i]: 84
[14:18:57] WatchFace.c:36> LOG: Angle: 90
[14:18:57] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 1,  x[i]: 4587522, y[i]: 84
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 2,  x[i]: 4587452, y[i]: 504
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 3,  x[i]: 4587452, y[i]: 924
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 4,  x[i]: 4587452, y[i]: 1344
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 5,  x[i]: 4587452, y[i]: 1344
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 6,  x[i]: 4587452, y[i]: 1834
[14:18:58] WatchFace.c:36> LOG: Angle: 90
[14:18:58] WatchFace.c:41> LOG: Angle: f, AngleToRadian: f
[14:18:58] WatchFace.c:45> LOG: i: 7,  x[i]: 4587452, y[i]: 2254

Please point out what I am missing here.

Thanks.


Solution

  • The lookup-functions for sine and cosine in Pebble don't take the angle in radians or degrees; they use an integer angle normalized to TRIG_MAX_ANGLE, which corresponds to a full circle or 360°.

    The same applies to the returned value, which is a signed int that must be normalized with TRIG_MAX_RATIO to get a valid cosine or sine.

    So you should be able to do something like this.

    for (int i = 0; i < n ; i++) {
        int32_t angle = i * TRIG_MAX_ANGLE / n;
    
        x[i] = x0 + (r * cos_lookup(angle)) / TRIG_MAX_RATIO;
        y[i] = y0 + (r * sin_lookup(angle)) / TRIG_MAX_RATIO;
    }
    

    Note that all calculations are done with integers. Take care to multiply first and then divide, so that you don't truncate results. For example,

    cos_lookup(angle) / TRIG_MAX_RATIO
    

    will always yield zero, except when the angle corresponds to 0° or 180°.

    You also might want to have a look at the clock hand example on the page linked above.