typedef struct POINT
{
double x;
double y;
}Point;
输入点数和各个点的坐标:
把坐标数组和数组的长度作为参数传入。
{
double x;
double y;
}Point;
输入点数和各个点的坐标:
void hjd_init_points()
{
printf("please input the number of points:\n");
int nM=0, nPos=0;
scanf("%d", &nM);
Point myPoints[nM];
for(nPos=0;nPos<nM;nPos++)
{
printf("\nplease input the No.%d point:(like x,y)\n", nPos);
scanf("%lf,%lf", &myPoints[nPos].x, &myPoints[nPos].y);
}
printf("\nComplete input the Points, and now to calculate the max number of points that a line can cross...\n");
int nMax=hjd_Get_Line_Cross_Points(myPoints, nM);
printf("\nThe max number of points that a line can cross = %d\n", nMax);
}
把坐标数组和数组的长度作为参数传入。
P0,P1,...,P(m-1)个点,从第一个点开始,依次求与后面某个点之间直线的方程,然后从 P0 开始计算满足该直线方程的点的个数,与历史记录比较,记录大的,最后就得到最多的点数,返回。
int hjd_Get_Line_Cross_Points(Point myPoints[], int m)
{
int i=0, j=0;
int nMaxCrossPoint=0;
for(i=0;i<(m-1);i++)
{
for(j=i+1;j<m;j++)
{
double k = ((myPoints[i].y)-(myPoints[j].y))/((myPoints[i].x)-(myPoints[j].x));
double b = myPoints[i].y-k*myPoints[i].x;
printf("\nThe Line form:y=%f*x+%f\n", k, b);
int nCount=0;
for(int nPos=0;nPos<m;nPos++)
{
if(myPoints[nPos].y==(k*myPoints[nPos].x+b))
{
nCount++;
}
if (nCount>nMaxCrossPoint)
nMaxCrossPoint=nCount;
}
}
}
return(nMaxCrossPoint);
}