演算法改寫於網上的一篇c++的,在此只不過是用c#實現了。這篇是關於點的一些演算法,後面還有線、多邊形的。
///
/// 點物件
///
public class spatialpoint
public double x;
public double y;
public double h;
public spatialpoint()
public spatialpoint(double _x,double _y,double _h)
x = _x;
y = _y;
h = _h;
///
/// 線物件
///
public class spatialpublic double startx;
public double starty;
public double starth;
public double endx;
public double endy;
public double endh;
public spatialpoint startpoint;
public spatialpoint endpoint;
public double a;
public double b;
public double c;
public spatialline()
public spatialline(double _startx, double _starty, double _starth, double _endx, double _endy, double _endh)
startx = _startx;
starty = _starty;
starth = _starth;
endx = _endx;
endy = _endy;
endh = _endh;
startpoint = new spatialpoint(startx, starty, starth);
endpoint = new spatialpoint(endx,endy,endh);
public spatialline(spatialpoint _startpoint,spatialpoint _endpoint)
startpoint = _startpoint;
endpoint = _endpoint;
startx = startpoint.x;
starty = startpoint.y;
starth = startpoint.h;
endx = endpoint.x;
endy = endpoint.y;
endh = endpoint.h;
public spatialline(double _a, double _b, double _c)
a = _a;
b = _b;
c = _c;
///
/// 平面幾何演算法---點相關
///
public class geometricclass
public const double ep = 0.000000001;
public const double inf = 0.00000000000001;
public const double pi = 3.14159265;
///
/// 計算距離
///
///
///
///
public static double calculateflatdistance(spatialpoint p1, spatialpoint p2)
return math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
///
/// 判斷兩個點在平面上是否重合
///
///
///
///
public static bool equal_point(spatialpoint p1, spatialpoint p2)
return ((math.abs(p1.x - p2.x) < ep) && (math.abs(p1.y - p2.y) < ep));
r=multiply(sp,ep,op),得到(sp-op)和(ep-op)的叉積-----二維
r>0:ep在向量opsp的逆時針方向;
r=0:opspep三點共線;
r<0:ep在向量opsp的順時針方向
public static double multiply(spatialpoint sp, spatialpoint ep, spatialpoint op)
return ((sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y));
r=dotmultiply(p1,p2,op),得到向量(p1-op)和(p2-op)的點積,如果兩個向量都非零向量 ----二維
r<0:兩向量夾角為銳角;
r=0:兩向量夾角為直角;
r>0:兩向量夾角為鈍角
public static double dotmultiply(spatialpoint p1, spatialpoint p2, spatialpoint p0)
return ((p1.x - p0.x) * (p2.x - p0.x) + (p1.y - p0.y) * (p2.y - p0.y));
public static bool pointisonline(spatialline l, spatialpoint p)
//注意:理論上是由multiply(l.endpoint, p, l.startpoint) == 0判斷在直線上,但我在用時發現會存在誤差,算出來的點乘是 //個非常小的數,所以最好是改為multiply(l.endpoint, p, l.startpoint) <0.0001
return ((multiply(l.endpoint, p, l.startpoint) == 0) && (((p.x - l.startpoint.x) * (p.x - l.endpoint.x) <= 0) && ((p.y - l.startpoint.y) * (p.y - l.endpoint.y) <= 0)));
/* 返回頂角在o點,起始邊為os,終止邊為oe的夾角(單位:弧度)
角度小於pi,返回正值
角度大於pi,返回負值
可以用於求線段之間的夾角
public static double angleofthreepoint(spatialpoint o, spatialpoint s, spatialpoint e)
double cosfi, fi, norm;
double dsx = s.x - o.x;
double dsy = s.y - o.y;
double dex = e.x - o.x;
double dey = e.y - o.y;
cosfi = dsx * dex + dsy * dey;
norm = (dsx * dsx + dsy * dsy) * (dex * dex + dey * dey);
cosfi /= math.sqrt(norm);
if (cosfi >= 1.0) return 0;
if (cosfi <= -1.0) return -3.1415926;
fi = math.acos(cosfi);
if (dsx * dey - dsy * dex > 0) return fi; // 說明向量os 在向量 oe的順時針方向
return -fi;
一些經典排序演算法的實現 C 實現
include include include using namespace std void swap int a,int b 氣泡排序 void bubblesort int a,int n 快速排序 void quicksort int a,int low,int high if i hig...
一些經典排序演算法的實現 C 實現
include include include using namespace std void swap int a,int b 氣泡排序 void bubblesort int a,int n 快速排序 void quicksort int a,int low,int high if i hig...
常用的一些排序演算法(C 實現)
常用的排序演算法有氣泡排序,選擇排序,插入排序,歸併排序 希爾排序,堆排序等 1 冒泡 氣泡排序就是像旗袍一樣,最大的值逐漸上浮,我的實現方法是採用遞迴的,當然也可以不用遞迴 void bubblesort2 int array,int length if index length 1 bubble...