I’m trying to figure out a program in c to calculate the x,y coordinates in a bolt circle pattern. I’ve got the math part right but can’t figure out how to get it to list the starting angle and coordinates first instead of last and the degrees not to go past 360. Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
float x; //x location
float y; //y location
float h; //# of holes
float d; //diameter of bc
float a; //starting angle
int n; //hole count
printf("enter dia of circle\n");
scanf("%f", &d);
printf("enter number of holes\n");
scanf("%f", &h);
printf("enter starting angle, counter clockwise from 3:00\n");
scanf("%f", &a);
float r = d / 2; //radius
float k = 360 / h; //degrees between holes
for (n = 0; n < h; n++) {
a = a + k;
x = r * (cos(a * M_PI / 180));
y = r * (sin(a * M_PI / 180));
printf("%.3f, %.3f, %.2f\n", x, y, a);
}
return 0;
}
I’m doing this in c to get the code right then I’ll put it in objective c for the iPhone, what would be the best way to list the output for each x & y coordinate and the angle? For example would I use a table view? Thanks for any advice.
I’d suggest replacing
a = a + kwith something more like this:Repeated addition of floating point numbers leads to increased errors. Maybe it won’t matter for this (after all, you’re only using
floats rather thandoubles — but errors accumulate “faster” withfloats thandoubles, so it is a more pressing problem) but the fix is easy enough: multiply the difference with the count and add that to a constant base.I’m also a little worried about the comparison
n < h—nis anintwhilehis afloat. While I’m normally worried about floating-point loop variables, I’m more worried about a float being used for the number of holes. Is that what the problem specifies?