home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
source
/
piemnsrc.sit
/
plotcircle.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-09-14
|
6KB
|
280 lines
#include <stdio.h>
#define I_RADIUS 40 /* Radius of inner circle */
#define O_RADIUS 60 /* Radius of outer circle */
#define C_RADIUS 5 /* Radius of center radius */
#define CENTER_X 0
#define CENTER_Y 0
#define MAX_PTS 1000
#define X_COORD 1
#define Y_COORD 2
#define I_POINTS 1
#define O_POINTS 2
#define C_POINTS 3
/* This is the representation of a point. */
struct xy_pt {
int x;
int y;
} pt[MAX_PTS];
/* Array index into which to generate the next point. */
int num_pts;
/* File into which the data is written. */
FILE *fp;
/* Local Function prototypes. */
void circle(int xc, int y_center, int radius);
int sort(struct xy_pt *, int, int);
int compare(struct xy_pt, struct xy_pt, int);
int swap(struct xy_pt *, struct xy_pt *);
main()
{
if ((fp = fopen("circledata.h", "w")) != NULL) {
/* Generate the set of interior points */
circle(CENTER_X, CENTER_Y, I_RADIUS); /* Generate the points. */
sortpoints(); /* Sort the points by x,y */
eliminate_duplicates(); /* Duplicate points aren't needed */
generate_quadrants(); /* Flip quadrant 1 three times. */
dumppoints(I_POINTS); /* Print the list of points. */
/* Generate the set of outer points */
circle(CENTER_X, CENTER_Y, O_RADIUS); /* Generate the points. */
sortpoints(); /* Sort the points by x,y */
eliminate_duplicates(); /* Duplicate points aren't needed */
generate_quadrants(); /* Flip quadrant 1 three times. */
dumppoints(O_POINTS); /* Print the list of points. */
/* Generate the set of center points */
circle(CENTER_X, CENTER_Y, C_RADIUS); /* Generate the points. */
sortpoints(); /* Sort the points by x,y */
eliminate_duplicates(); /* Duplicate points aren't needed */
generate_quadrants(); /* Flip quadrant 1 three times. */
dumppoints(C_POINTS); /* Print the list of points. */
fclose(fp);
}
}
void
circle(x_center, y_center, radius)
int x_center, y_center ,radius;
{
int x,y,d;
num_pts = 0;
y = radius;
d = 3 - 2 * radius;
/* Use x,y to control the plotting of the first octant. */
for (x = 0; x < y;) {
if (num_pts + 1 >= MAX_PTS)
return;
pt[num_pts].x = x + x_center;
pt[num_pts++].y = -y + y_center;
pt[num_pts].x = y + x_center;
pt[num_pts++].y = -x + y_center;
if (d < 0)
d += 4 * x + 6;
else {
d += 4 * (x - y) + 10;
--y;
}
++x;
}
/* If we happened to land on the last point, add it to the list */
if (x == y) {
pt[num_pts].x = x + x_center;
pt[num_pts++].y = -y + y_center;
}
fprintf(fp,"\n");
}
sortpoints()
{
sort(pt,num_pts,X_COORD);
sort(pt,num_pts,Y_COORD);
}
eliminate_duplicates()
{
}
int
generate_quadrants()
{
int i;
/*
* Second quadrant same as first with a positive y coordinates.
* Note that the end points are not duplicated.
*/
for (i = num_pts - 1; i >= 0; --i) {
pt[num_pts].x = pt[i].x;
pt[num_pts].y = -pt[i].y;
++num_pts;
}
/* 3rd & 4th quadrants same as 1st & 2nd with negative x coordinates */
for (i = num_pts - 1; i >= 0; --i) {
pt[num_pts].x = -pt[i].x;
pt[num_pts].y = pt[i].y;
++num_pts;
}
}
int
dumppoints(type)
int type;
{
int i;
int cols = 0;
/* Print out the title. */
fprintf(fp, "/*\n");
fprintf(fp, " * Points of a circle with origin at %d,%d ", CENTER_X, CENTER_Y);
if (type == I_POINTS) {
fprintf(fp, " * with a radius of: %d\n", I_RADIUS);
fprintf(fp, " */\n\n");
fprintf(fp,"#define\t\tCENTER_X\t\t\t%d\n", CENTER_X);
fprintf(fp,"#define\t\tCENTER_Y\t\t\t%d\n\n", CENTER_Y);
fprintf(fp,"#define\t\tI_RADIUS\t\t\t%d\n", I_RADIUS);
fprintf(fp,"#define\t\tNUM_I_POINTS\t\t%d\n\n",num_pts);
}
else if (type == O_POINTS) {
fprintf(fp, "with a radius of: %d\n", O_RADIUS);
fprintf(fp, " */\n");
fprintf(fp,"#define\t\tO_RADIUS\t\t\t%d\n", O_RADIUS);
fprintf(fp,"#define\t\tNUM_O_POINTS\t\t%d\n\n",num_pts);
}
else {
fprintf(fp, "with a radius of: %d\n", C_RADIUS);
fprintf(fp, " */\n");
fprintf(fp,"#define\t\tC_RADIUS\t\t\t%d\n", C_RADIUS);
fprintf(fp,"#define\t\tNUM_C_POINTS\t\t%d\n\n",num_pts);
}
/* Print the structure description we are about to define */
if (type == I_POINTS) {
fprintf(fp,"/* This is the representation of a point. */\n");
fprintf(fp,"struct xy_pt {\n");
fprintf(fp,"\tint x;\n");
fprintf(fp,"\tint y;\n");
fprintf(fp,"};\n\n");
fprintf(fp,"struct xy_pt i_circle_points[NUM_I_POINTS] = {\n\t");
}
else if (type == O_POINTS) {
fprintf(fp,"struct xy_pt o_circle_points[NUM_O_POINTS] = {\n\t");
}
else {
fprintf(fp,"struct xy_pt c_circle_points[NUM_C_POINTS] = {\n\t");
}
for (i = 0; i < num_pts; i++) {
fprintf(fp,"{%5d,%5d},\t", pt[i].x, pt[i].y);
++cols;
if ( (cols % 5) == 0 ) {
fprintf(fp,"\n\t");
cols = 0;
}
}
if (cols != 0)
fprintf(fp,"\n");
fprintf(fp,"};\n");
}
sort(v, n, sort_field)
struct xy_pt v[];
int n;
int sort_field;
{
int gap, i, j;
for (gap = n/2; gap > 0; gap /= 2)
for (i = gap; i < n; i++)
for (j = i-gap; j >= 0; j -= gap) {
if (compare(v[j], v[j+gap], sort_field) <= 0)
break;
swap(&v[j], &v[j+gap]);
}
}
int
compare(v1, v2, sort_field)
struct xy_pt v1,v2;
int sort_field;
{
int temp1,temp2;
/* When sorting Y coordinates, don't swap unless X's are equal */
if ( (sort_field == Y_COORD) && (v1.x != v2.x) ) {
return(0);
}
/* Extract the appropriate values to compare */
if (sort_field == X_COORD) {
temp1 = v1.x;
temp2 = v2.x;
}
else {
temp1 = v1.y;
temp2 = v2.y;
}
/* Return the comparison of the two values */
if (temp1 < temp2)
return(-1);
else if (temp1 > temp2)
return(1);
else
return(0);
}
int
swap(p1, p2)
struct xy_pt *p1, *p2;
{
struct xy_pt temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}