hello,
can anyone give me a clue or simple code on task:
for example we have in 2D an equation fo circle:
(x - 3)^2 + (y - 4)^2 = 25.
now the program must return for example a 40 pairs of points (x,y)
which belong to this cirlce. and these points must be taken evenly
form the circle because i need them for planning a trajectory in CNC.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
struct point
{
double x;
double y;
};
struct point circle40(struct point c, double r, size_t which);
inline double sqr(double x)
{
return x * x;
}
inline double radius(struct point c, struct point p)
{
return (sqrt(sqr(p.x - c.x) + sqr(p.y - c.y)));
}
int main(void)
{
int i;
struct point this, center = { 3, 4 };
printf("Here are ten points chosen from the 40\n"
"nearly equally spaced ones on a circle centered\n"
"at (3,4) with a radius 5.\n\n"
"%6s%13s%18s\n", "x", "y", "radius");
srand(time(0));
for (i = 0; i < 10; i++) {
this = circle40(center, 5, rand() * 40. / (1. + RAND_MAX));
printf("%12f %12f %12f\n",
this.x, this.y, radius(center, this));
}
}
struct point circle40(struct point c, double r, size_t which)
{
static struct point edge[40] = {
{1, 0},
{0.987688340595138, 0.156434465040231},
{0.951056516295154, 0.309016994374947},
{0.891006524188368, 0.453990499739547},
{0.809016994374947, 0.587785252292473},
{0.707106781186548, 0.707106781186547},
{0.587785252292473, 0.809016994374947},
{0.453990499739547, 0.891006524188368},
{0.309016994374947, 0.951056516295154},
{0.156434465040231, 0.987688340595138},
{6.12303176911189e-17, 1},
{-0.156434465040231, 0.987688340595138},
{-0.309016994374947, 0.951056516295154},
{-0.453990499739547, 0.891006524188368},
{-0.587785252292473, 0.809016994374947},
{-0.707106781186547, 0.707106781186548},
{-0.809016994374947, 0.587785252292473},
{-0.891006524188368, 0.453990499739547},
{-0.951056516295154, 0.309016994374948},
{-0.987688340595138, 0.156434465040231},
{-1, 1.22460635382238e-16},
{-0.987688340595138, -0.156434465040231},
{-0.951056516295154, -0.309016994374947},
{-0.891006524188368, -0.453990499739547},
{-0.809016994374948, -0.587785252292473},
{-0.707106781186548, -0.707106781186547},
{-0.587785252292473, -0.809016994374947},
{-0.453990499739547, -0.891006524188368},
{-0.309016994374948, -0.951056516295154},
{-0.156434465040231, -0.987688340595138},
{-1.83690953073357e-16, -1},
{0.156434465040231, -0.987688340595138},
{0.309016994374947, -0.951056516295154},
{0.453990499739547, -0.891006524188368},
{0.587785252292473, -0.809016994374948},
{0.707106781186547, -0.707106781186548},
{0.809016994374947, -0.587785252292473},
{0.891006524188368, -0.453990499739547},
{0.951056516295154, -0.309016994374948},
{0.987688340595138, -0.156434465040231}
};
struct point retval;
retval.x = edge[which].x * r + c.x;
retval.y = edge[which].y * r + c.y;
return retval;
}
[output from one run]
Here are ten points chosen from the 40
nearly equally spaced ones on a circle centered
at (3,4) with a radius 5.
x y radius
-1.938442 4.782172 5.000000
7.755283 2.454915 5.000000
6.535534 0.464466 5.000000
1.454915 8.755283 5.000000
5.938926 8.045085 5.000000
0.061074 -0.045085 5.000000
6.535534 0.464466 5.000000
-1.455033 6.269952 5.000000
0.730048 8.455033 5.000000
7.455033 1.730048 5.000000