我有这个家庭作业,但我有点受不了了。
Create n random points, using these points create 2 quadrilaterals, which envelop the rest of the points, and compare the area of the 2 shapes.(not a graphical solution, purely mathematical)
我不知道如何连接我的点或如何告诉计算机连接哪些点。我在想也许找到最大和最小 X 和 Y 可以工作,但我没有比这更进一步。
提前致谢!
void genp(int n,int** x,int** y);
void rectangle(int n,int** x,int** y);
void file(int n, int** x, int** y);
int main()
{
int n;
printf("Please specify number of points!\n");
scanf("%d", &n);
int *x;
int *y;
genp(n,&x,&y);
for (int i = 0; i < n; i++)
{
printf("x[%d]=%d\ty[%d]=%d\n", i, x[i], i, y[i]);
}
rectangle(n,&x,&y);
file(n,&x,&y);
free(x);
free(y);
return 0;
}
void genp(int n,int** x, int** y)
{
int r;
srand(time(NULL));
*x = (int*)malloc(n * sizeof(int));
*y = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
r = rand();
(*x)[i] = r;
r = rand();
(*y)[i] = r;
}
}
void rectangle(int n,int** x,int **y)
{
int maxlocx,maxlocy,maxx,maxy,minx,miny,minlocx,minlocy;
int zerox=*x[0];
int zeroy=*y[0];
for(int i=0;i<n;i++)
{
if ((*x)[i]>zerox)
{
maxx=(*x)[i];
maxlocx=i;
}
if ((*y)[i]>zeroy)
{
maxy=(*y)[i];
maxlocy=i;
}
}
for (int i=0;i<n;i++)
{
if ((*x)[i]<zerox)
{
minx=(*x)[i];
minlocx=i;
}
if ((*y)[i]<zeroy)
{
miny=(*y)[i];
minlocy=i;
}
}
printf("\nThe max x:%d, corresponding y:%d\n",maxx,(*y)[maxlocx]);
printf("\nThe max y:%d, corresponding x:%d\n",maxy,(*x)[maxlocy]);
printf("\nThe min x:%d, corresponding y:%d\n",minx,(*y)[minlocx]);
printf("\nThe min y:%d, corresponding x:%d\n",miny,(*x)[minlocy]);
int area=
}
void file(int n,int** x,int** y)
{
FILE *f;
f=fopen("data.txt","w");
fprintf(f,"n=%d\n",n);
for (int i=0;i<n;i++)
{
fprintf(f,"%d,%d\n",(*x)[i],(*y)[i]);
}
fclose(f);
}
最佳答案
I was thinking maybe finding the max and min X and Y could work but I didn't get further than that.
这听起来很适合我,也是我在阅读该问题陈述时想到的第一件事。
至于第二个四边形,也许您可以尝试找到一个具有倾斜轴而不是与 X 轴和 Y 轴平行的轴的边界矩形。例如,倾斜 45 度时,边界框将如下所示:
I can not figure out how to connect my points or how to tell the computer which points to connect.
我的印象是您正在尝试找到 Convex Hull你的一组点。这将是一个比仅仅找到边界框更难的问题。 (而且它可能无论如何都不是四边形)
关于创建n个随机点并创建四边形(卡住)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684035/