473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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 '07 #1
7 3215
In article <11*********************@p77g2000hsh.googlegroups. com>,
<he********@gmail.comwrote:
>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 dj******@csclub.uwaterloo.ca
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
Jun 2 '07 #2
On Jun 2, 11:23 am, heterod...@gmail.com 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

Jun 2 '07 #3

<he********@gmail.comha scritto nel messaggio
news:11*********************@p77g2000hsh.googlegro ups.com...
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.
Jun 2 '07 #4
he********@gmail.com 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
Jun 2 '07 #5

"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:5c*************@mid.individual.net...
he********@gmail.com 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;
}
Jun 3 '07 #6
Army1987 wrote:
"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:5c*************@mid.individual.net...
>he********@gmail.com 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;
}

Jun 3 '07 #7

"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:5c*************@mid.individual.net...
Army1987 wrote:
>"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:5c*************@mid.individual.net...
>>he********@gmail.com 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;
}

Jun 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Talon | last post by:
Hi all, I am new to Tk, so please bear with me. I need someone better at math than me to help me figure this out. I am drawing multiple arcs on the same circle. All arcs start at 90 and have...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
6
by: Craig Parsons | last post by:
Folks, I have an image of a circle, which I am trying to straighten out into a flat line. I am essentially wanting to look at the image, and read a straight line from the centre, and then plot...
0
by: Ranjit | last post by:
Hi, I would like to know how to place circles in the connecting points of the lines, and very close to the circles I would like to place the values like a,b,c...etc automatically for each...
4
by: Soumyadip Rakshit | last post by:
Hi, Could you please tell me the most efficient way of finding the curvature of a circle passing through three (non-colinear) points. Thanks a ton, Soumyadip.
14
by: Pythor | last post by:
I wrote the following code for a personal project. I need a function that will plot a filled circle in a two dimensional array. I found Bresenham's algorithm, and produced this code. Please tell...
0
by: Carl Gilbert | last post by:
Hi I am trying to plot a series of shapes in a circular fashion. All shapes are evenly spaced with lines going between each shape. At present, all shapes are the same size so I can easily plot...
9
by: saraaana | last post by:
Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the center and a point on the circle. The program...
1
by: cmwb2000 | last post by:
Hi I am trying to calculate the radius of a circle of best fit from 2 or 3 mouse positions. At present I am recording a series of points a mouse passes, I intended to pick the first and last point...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.