[C] a circle in 2D/3D - return a pairs o points

Discussion in 'C Programming' started by heterodon7@gmail.com, Jun 2, 2007.

  1. Guest

    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.
     
    , Jun 2, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >hello,
    >
    >can anyone give me a clue or simple code on task:


    I hope not. DYODH.


    >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.


    If you'd found somewhere appropriate to ask the question, instead of
    asking a bunch of C programmers a question that has nothing to do with C,
    you might have heard something about parametric curves.


    dave

    --
    Dave Vandervies
    One of the flaws is that it isn't _very_ human-readable. I don't care;
    I'm a programmer, I was never human.
    --Maarten Wiltink in the scary devil monastery
     
    Dave Vandervies, Jun 2, 2007
    #2
    1. Advertising

  3. per9000 Guest

    Re: a circle in 2D/3D - return a pairs o points

    On Jun 2, 11:23 am, wrote:
    > hello,
    >
    > can anyone give me a clue or simple code on task:


    Why not solve the problem without code first? Do not let the eager to
    code destroy your project - I have done many a mistake because of too
    much eager to code.

    > for example we have in 2D an equation fo circle:
    > (x - 3)^2 + (y - 4)^2 = 25.


    Thus a circle with center-point in (3,4) and a radius of 5 (since 5^2
    =25), see more at f.x. http://en.wikipedia.org/wiki/Circle

    > now the program must return for example a 40 pairs of points (x,y)
    > which belong to this circle. and these points must be taken evenly
    > form the circle because i need them for planning a trajectory in CNC.


    Now I'd recommend you to divide 2*pi by 40 and read up on the Unit
    Circle: http://en.wikipedia.org/wiki/Unit_Circle

    Now you've practically solved the problem, good luck.

    HTH,
    Per

    --

    Per Erik Strandberg
    home: www.pererikstrandberg.se
    work: www.incf.org
    also: www.spongswedencare.se
     
    per9000, Jun 2, 2007
    #3
  4. Army1987 Guest

    <> ha scritto nel messaggio
    news:...
    > 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.


    You can return a struct with two elements, e.g. struct point { double x,
    y; }
    To compute pi, you can use 4.0 * atan(1.0), or you can #define PI 3.1415926
    with enough digits.

    Each point is point.x = 3 + 5*cos(i * 2*pi / 40); point.y = 4 + 5*sin(i *
    2*pi / 40);

    Use a for loop, and you're done.
     
    Army1987, Jun 2, 2007
    #4
  5. wrote:
    > 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
     
    Martin Ambuhl, Jun 2, 2007
    #5
  6. Army1987 Guest

    "Martin Ambuhl" <> ha scritto nel messaggio
    news:...
    > wrote:
    >> 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)));
    > }

    If you're using C99, as the use of "inline" suggests, you have the
    hypot() function. BTW, IMO it would make more sense to call it
    distance() than radius().

    >
    > 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));
    > }
    > }

    The OP asked for 40 evenly spaced points on the circle, not 10
    (pseudo)randomly choosen (and not necessarily distinct) vertices of
    the regular 40gon. So you should've used
    for (i = 0; i < 40; i++) {
    this = circle40(center, 5, i);
    /* etc... */
    >
    > 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},

    If you are indeed doing something like that rather than simplily
    use sin() and cos(), at least use 'exact' values. For example,
    cos(pi/2) is exactly 0.

    struct point circlepoint(struct point c, double r, double angle)
    {
    struct point result;
    result.x = c.x + r * cos(angle);
    result.y = c.y + r * sin(angle);
    return result;
    }
     
    Army1987, Jun 3, 2007
    #6
  7. Army1987 wrote:
    > "Martin Ambuhl" <> ha scritto nel messaggio
    > news:...
    >> wrote:
    >>> 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)));
    >> }

    > If you're using C99, as the use of "inline" suggests, you have the
    > hypot() function. BTW, IMO it would make more sense to call it
    > distance() than radius().


    Hey! If you want to do this lazy person's homework, go ahead. There is
    nothing wrong with the code as posted. And making it C89 compilable
    requires nothing more than adding
    #define inline
    while your "suggestion" requires writing the hypot() function.
    When you do this turkey's homework, you can call the function any damn
    thing you want.


    >
    >> 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));
    >> }
    >> }

    > The OP asked for 40 evenly spaced points on the circle, not 10
    > (pseudo)randomly choosen (and not necessarily distinct) vertices of
    > the regular 40gon. So you should've used
    > for (i = 0; i < 40; i++) {
    > this = circle40(center, 5, i);
    > /* etc... */


    Did you bother to look at the function circle40? circle40 provides 40
    evenly spaced points on a circle. There is no reason why my example of
    use should be subject to your asinine criticism.


    >> 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},

    > If you are indeed doing something like that rather than simplily
    > use sin() and cos(), at least use 'exact' values. For example,
    > cos(pi/2) is exactly 0.
    >
    > struct point circlepoint(struct point c, double r, double angle)
    > {
    > struct point result;
    > result.x = c.x + r * cos(angle);
    > result.y = c.y + r * sin(angle);
    > return result;
    > }
    >
    >
     
    Martin Ambuhl, Jun 3, 2007
    #7
  8. Army1987 Guest

    "Martin Ambuhl" <> ha scritto nel messaggio
    news:...
    > Army1987 wrote:
    >> "Martin Ambuhl" <> ha scritto nel messaggio
    >> news:...
    >>> wrote:
    >>>> 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)));
    >>> }

    >> If you're using C99, as the use of "inline" suggests, you have the
    >> hypot() function. BTW, IMO it would make more sense to call it
    >> distance() than radius().

    >
    > Hey! If you want to do this lazy person's homework, go ahead. There is
    > nothing wrong with the code as posted. And making it C89 compilable
    > requires nothing more than adding
    > #define inline
    > while your "suggestion" requires writing the hypot() function.

    #define hypot(x, y) (sqrt((x)*(x) + (y)*(y)))

    > When you do this turkey's homework, you can call the function any damn
    > thing you want.
    >
    >
    >>
    >>> 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));
    >>> }
    >>> }

    >> The OP asked for 40 evenly spaced points on the circle, not 10
    >> (pseudo)randomly choosen (and not necessarily distinct) vertices of
    >> the regular 40gon. So you should've used
    >> for (i = 0; i < 40; i++) {
    >> this = circle40(center, 5, i);
    >> /* etc... */

    >
    > Did you bother to look at the function circle40? circle40 provides 40
    > evenly spaced points on a circle. There is no reason why my example of
    > use should be subject to your asinine criticism.


    Yes, I did.
    The OP asked for a *program* which provided 40 points. And your
    main() function provides 10 points. Also, he said "for example 40",
    so why did you hard-code that number in the function rather than
    use a parameter?
    >
    >>> struct point circle40(struct point c, double r, size_t which)

    Why do you use size_t? Details about how the function is
    implemented shouldn't affect the interface. The function returns
    a point at a distance r from c, at an angle of n/40 of a full
    circle starting from the easternmost point counterclockwise. What
    has that purpose to do with bytes or members of arrays, except the
    fact that you implement it the way you do?

    BTW, I would replace edge[which] with edge[which % 40]. Ever heard
    about the least astonishment principle? If someone calls
    circlepoint(origin, 1.0, 43) or circlepoint(origin, 1.0, -4) they
    are far more likely to expect to get the third or 36th point than
    to expect demons to fly out of their nose.
    (Also, I'd rename edge to vertex...)
    >>> {
    >>> 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},

    >> If you are indeed doing something like that rather than simplily
    >> use sin() and cos(), at least use 'exact' values. For example,
    >> cos(pi/2) is exactly 0.
    >>
    >> struct point circlepoint(struct point c, double r, double angle)
    >> {
    >> struct point result;
    >> result.x = c.x + r * cos(angle);
    >> result.y = c.y + r * sin(angle);
    >> return result;
    >> }
     
    Army1987, Jun 8, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Talon

    Circle Hell

    Talon, Sep 4, 2003, in forum: Perl
    Replies:
    2
    Views:
    1,113
    Mark Carter
    Sep 4, 2003
  2. =?Utf-8?B?dGFnaHJlZWQ=?=

    moving label in c# around circle

    =?Utf-8?B?dGFnaHJlZWQ=?=, May 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,865
    clintonG
    May 3, 2004
  3. Daniel Moore
    Replies:
    19
    Views:
    343
  4. Giovanni Nicco
    Replies:
    2
    Views:
    98
    Giovanni Nicco
    Aug 29, 2004
  5. Ian Pattison

    Return all points with x km of y

    Ian Pattison, Dec 17, 2003, in forum: Perl Misc
    Replies:
    11
    Views:
    189
    Sarita Moodie
    Dec 26, 2004
Loading...

Share This Page