1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
/// <summary>
/// 获取直线方程 ax + by + c = 0
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static double[] GetLineEquation(SGPoint p1, SGPoint p2)
{
double a = p2.y() - p1.y();
double b = p1.x() - p2.x();
double c = p2.x() * p1.y() - p1.x() * p2.y();
return new double[] { a, b, c };
}
// 获取点到线的距离
public static double GetDistanceToLine(SGPoint p, SGPoint ps, SGPoint pe)
{
if (IsSamePoint(ps, pe))
{
return p.distanceTo(ps);
}
else
{
var par = GetLineEquation(ps, pe);
double distance = (par[0] * p.x() + par[1] * p.y() + par[2]) / Math.Sqrt(par[0] * par[0] + par[1] * par[1]);
return distance;
}
}
// 获取点到线段的距离
public static double GetDistanceToLineSegment(SGPoint p, SGPoint ps, SGPoint pe)
{
double pqx = pe.x() - ps.x();
double pqy = pe.y() - ps.y();
double dx = p.x() - ps.x();
double dy = p.y() - ps.y();
double d = pqx * pqx + pqy * pqy;
double t = pqx * dx + pqy * dy;
if (d > 0)
t /= d;
if (t < 0)
t = 0;
else if (t > 1)
t = 1;
dx = ps.x() + t * pqx - p.x();
dy = ps.y() + t * pqy - p.y();
return Math.Sqrt(dx * dx + dy * dy);
}
public static double GetDistance(SGPoint p1,SGPoint p2)
{
return Math.Sqrt(Math.Pow((p1.x()-p2.x()),2) + Math.Pow((p1.y() - p2.y()), 2) );
}
|