【原理】
GIS算法P121
【代码】
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#define maxSize 100
typedef struct
{
double x, y;
}Point,Vector;//给同一类型的数据多个名称,从而使得代码表达的更清楚
//矢量叉积
double CrossProduct(Vector v1,Vector v2)
{
return v1.x*v2.y - v2.x*v1.y;
}
//点到直线距离算法:计算点p到以矢量<p0,p1>表示的直线L的距离
double Distance(Point p, Point p0, Point p1)
{
//设矢量<p0,p1>为vl
Vector vl;
vl.x = p1.x - p0.x;
vl.y = p1.y - p0.y;
//设矢量<p0,p>为w
Vector w;
w.x = p.x - p0.x;
w.y = p.y - p0.y;
//计算vl的模长
double len = sqrt(vl.x*vl.x + vl.y*vl.y);//小心—>曾经写错过:没有乘以平方
//计算点到直线的距离(利用平行四边形的面积公式)
double d = fabs(CrossProduct(vl, w) / len);
return d;
}
//求数组中各点距离直线L(p0,p1)的距离最大值以及数组下标j
double MaxDistPoint(Point p[], int low, int high, Point p0, Point p1, int* j)
{
int i, k;
double dist,maxd = 0;
for (i = low; i <= high; ++i)
{
dist = Distance(p[i], p0, p1);
if (dist > maxd)
{
maxd = dist;
k = i;
}
}
*j = k;
return maxd;
}
int main()
{
return 0;
}