formula for points on a circle.

Hi All
I am looking for some help please. I am building a sort of cnc machine (not using GRBL)
I am looking for a formula that I can use to calculate the X and Y coordinates of points around a circle.
The information I will have is the radius of the circle and the number of holes centre of the circle will be at 0,0 X,Y.
I have tried using the rcos =X and rsin=Y but this does not seem to work unless I am doing it wrong (probably) .
Is there anyone out there that can help me please.
Bob

float r=0;
float radius=0;
float h=0;
float holes=0;
float j=0;
float i=0;
float x=0;
float y=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
Serial.println("what is your Radius?" );
while (Serial.available()==0){}                                  //Wait for user input
   radius = Serial.parseInt();
   Serial.println(radius);        
Serial.println("How many holes?" );
while (Serial.available()==0){}                                  //Wait for user input
   holes = Serial.parseInt();
   Serial.println(holes);
j=360/holes;//convert holes to degrees per positon   
i=j*3.14/180;//convert to degrees per positon radians 

Serial.println();
for (int k = 0; k <=holes-1; k++) {
x=radius*cos(i);
y=radius*sin(i);
Serial.print(x);
Serial.print(",");
Serial.println(y);
i=i+i;
x=radius*cos(i);
y=radius*sin(i);
//Serial.print(x);
//Serial.print(",");
//Serial.println(y);
Serial.println();
}





}

There’s a variant of Bresenham’s algorithm for circles.

Alternatively, think Pythagoras, not polar.

but this does not seem to work

Is a justifiably-hated phrase around these parts.

Hi thanks TheMemberFormerlyKnownAsAWOL
I am not very good at maths I was hoping for an easy formula I looked up Bresenham's algorithm for circles
but got lost after the first paragraph.
Thanks
Bob

i tested it, the formula works great, the way you are reading from Serial isn't right

Hi KASSIMSAMJI
can you explain what I am doing wrong.
Thanks
Bob

Redpiperbob:
Hi KASSIMSAMJI
can you explain what I am doing wrong.
Thanks
Bob

see this tutorial here

you will be able to properly read data from Serial

I have tried using the rcos =X and rsin=Y but this does not seem to work unless I am doing it wrong (probably) .

j=360/holes;//convert holes to degrees per positon

Use “360.0” to make sure that i comes out as a fraction.

i=j*3.14/180;//convert to degrees per positon radians 
for (int k = 0; k <=holes-1; k++) {
x=radius*cos(i);
y=radius*sin(i);
i=i+i;

You are doubling i each time instead of incrementing by j. (and i should probably start at 0)

Redpiperbob:
I am looking for a formula that I can use to calculate the X and Y coordinates of points around a circle.
The information I will have is the radius of the circle and the number of holes centre of the circle will be at 0,0 X,Y.
Is there anyone out there that can help me please.
Bob

while the code for using polar coordinates is OK, wuold it not be simpler and less 'hard' on the arduino to just use the cartesian equation of a circle:

x^2 + y^2 = r^2 for a circle starting at 0,0

the code I reckon would simpler as well; basically its would be for each 'step' in 'x' in the range -r to r calculate 'y', then move to that point.

step being the 'x' distance between each hole.

Hi Sherzaad
could you maybe show me some code to do want I want please
Thanks
Bob

Hi Westfw
thanks for that
I alered the code but i am getting strange resualts.for instance if I have a circle of 30 mm radius and want 4 points around the circumfeence the x,y coordinates should be 0,30/-30,0/0,-30/30,0
But I get 0.01,30.00/-26.82,-13.43/24.03,-17.96/5.29,29.53/
the code in red is completely wrong it should read 0,-30

float r=0;
float radius=0;
float h=0;
float holes=0;
float j=0;
float i=0;
float x=0;
float y=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("what is your Radius?" );
while (Serial.available()==0){}                                  //Wait for user input
   radius = Serial.parseInt();
   Serial.println(radius);        
Serial.println("How many holes?" );
while (Serial.available()==0){}                                  //Wait for user input
   holes = Serial.parseInt();
   Serial.println(holes);
j=360.0/holes;//convert holes to degrees per positon   
i=j*3.141/180;//convert to degrees per positon radians 

Serial.println();
for (int k = 0; k <=holes-1; k++) {
  x=radius*cos(i);
  y=radius*sin(i);
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  i=i+j;
  Serial.print("/");
}





}
void loop() {
  // put your main code here, to run repeatedly:


}

sherzaad:
while the code for using polar coordinates is OK, wuold it not be simpler and less 'hard' on the arduino to just use the cartesian equation of a circle:

x^2 + y^2 = r^2 for a circle starting at 0,0

Perhaps the equation is "simpler", but it makes it really hard to locate the points equally-spaced around the circle's circumference.

You’re close. Try:

void setup() {
  float theta, increment, x, y;
  float radius = 30.0;
  uint16_t numPoints = 4;

  Serial.begin(115200);
  delay(1000);

  theta = 0.0;
  increment = 2 * PI / numPoints;
  for (uint16_t i = 0; i < numPoints; i++) {
    x = radius * cos(theta);
    y = radius * sin(theta);

    Serial.print("X = ");
    Serial.print(x, 4);
    Serial.print(", Y = ");
    Serial.println(y, 4);
    theta += increment;
  }
}

void loop() {
}
X = 30.0000, Y = 0.0000
X = -0.0000, Y = 30.0000
X = -30.0000, Y = -0.0000
X = 0.0000, Y = -30.0000

Hi Gfvalvo
Thats it works perfect sir you are a genuis
Thank you
Bob

gfvalvo:
Perhaps the equation is “simpler”, but it makes it really hard to locate the points equally-spaced around the circle’s circumference.

Your absolutely right! found that out the hard way… well it was good experimentation for me! :slight_smile:

anyway, here’s my code to read in from serial monitor the circle radius and number of equally spaced holes to be made and output the xy positions on the holes.

const byte numChars = 16;
const float pi = 3.141592;

char receivedChars[numChars];
float radius = 0;
uint16_t holes = 0;

void setup() {
  Serial.begin(115200);
  Serial.println(F("Enter Circle Radius and Number of Holes <radius,holes>:"));
}

void loop() {
  if (ReceiveUserInput()) {
    float i = (2 * pi) / holes; //convert holes to radians per positon

    //first point
    float x = radius;
    float y = 0;
    Serial.print(x);
    Serial.print(F(","));
    Serial.println(y);

    //remaining points
    for (uint16_t j = 1; j < holes; j++) {
      x = radius * cos(i * j);
      y = radius * sin(i * j);
      Serial.print(x);
      Serial.print(F(","));
      Serial.println(y);
    }
    Serial.println();
  }
}

boolean ReceiveUserInput() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char delimiter = ',';
  char endMarker = '>';
  char rc;
  boolean DataReady = false;

  while (Serial.available() > 0) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        if (rc != delimiter) {
          receivedChars[ndx] = rc;
          ndx++;
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          radius = atof(receivedChars);
          ndx = 0;
        }
        if (ndx >= numChars) {
          return false;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        holes = atoi(receivedChars);
        recvInProgress = false;
        ndx = 0;
        DataReady = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
      DataReady = false;
    }
  }

  return DataReady;
}
  for (int k = 0; k <= holes - 1; k++) 
  {
    float angle = i * k;
    x = radius * cos(angle);
    y = radius * sin(angle);
    Serial.print(x);
    Serial.print(",");
    Serial.println(y);
  }

Pretty unusual and somewhat convoluted.:

johnwasser:

  for (int k = 0; k <= holes - 1; k++)

Conventional practice is:

  for (int k = 0; k < holes; k++)