I was asked to make a program: Generate by random a set of 2d coordinates. Find two different circles enveloping all of the points and connected to the set ( there are at least three points on the circle). Check which circle has a greater area. Use function for reading, printing appropriate data. Use local variables.
Code:
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
int nofpoints, i;
double x_max, x_min, y_max, y_min, *tabx, *taby, *tabdistance, *r, *rsquared, sumofy, sumofx;
printf("input the n\n");
scanf("%d", &nofpoints);
srand((unsigned)time(NULL));
tabx = (double*)malloc(nofpoints*sizeof(double));
taby = (double*)malloc(nofpoints*sizeof(double));
tabdistance = (double*)malloc(nofpoints*sizeof(double));
x_max = 5;
x_min = 0;
y_max = 5;
y_min = 0;
for (i = 0; i <= (nofpoints - 1); i++)
{
tabx[i] = rand() * (x_max - x_min) / (double)RAND_MAX + x_min;
taby[i] = rand() * (y_max - y_min) / (double)RAND_MAX + y_min;
printf("x%d=%lf\ny%d=%lf\n\n", i, tabx[i], i, taby[i]);
}
sumofx = 0;
sumofy = 0;
for (i = 0;i <= (nofpoints - 1);i++)
{
sumofx = sumofx + tabx[i];
sumofy = sumofy + taby[i];
}
printf("sum of x=%lf\nsum of y=%lf", sumofx, sumofy);
double averagex = (sumofx / nofpoints);
double averagey = (sumofy / nofpoints);
printf("coordinates of the center of mass\nx=%lf y=%lf\n", averagex, averagey);
for (i = 0;i <= nofpoints;i++)
{
tabdistance[i] = sqrt((((tabx[i] - averagex)*(tabx[i] - averagex)) + ((taby[i] - averagey)*(taby[i] - averagey))));
printf("distance=%lf\n", tabdistance[i]);
}
r = (double*)malloc(nofpoints*sizeof(double));
rsquared = (double*)malloc(nofpoints*sizeof(double));
for (int i = 0;i < nofpoints;i++)
{
r[i] = ((tabdistance[i]) / 2);
rsquared[i] = (r[i] * r[i]);
for (int s = 0;s < nofpoints;s++)
{
if (rsquared[i] > (((tabx[s]-averagex)*(tabx[s] - averagex)) + ((taby[s]-averagey)*(taby[s]-averagey))))
{
printf("x=%lf, y=%lf r=%lf\n", tabx[s], taby[s],r[i]);
}
if (rsquared[i] = (((tabx[s] - averagex)*(tabx[s] - averagex)) + ((taby[s] - averagey)*(taby[s] - averagey))))
printf("xboundary=%lf, yboundary=%lf r=%lf\n", tabx[s], taby[s], r[i]);
}
}
}
i have a problem with the last few lines. I have all possible radii stored in an array and i have all the points ( y and x coordinates) stored in an array. I dont know how to write the code which will print out only the radii for which a circle envelopes all the points and at least 3 points are on the circle itself.