日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++笔记-学习算法与实现-计算几何-二维向量和线段运算

發布時間:2024/3/13 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++笔记-学习算法与实现-计算几何-二维向量和线段运算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

判斷正負號

??給定一個double類型的數,判斷它的符號;

const double eps = 1e-8; int cmp(double x) {if (fabs(x) < eps) return 0;if (x > 0) return 1;return -1; }

??通過acos計算PI的值;計算sqr平方值;

const double pi = acos(-1.0); inline double sqr(double x) {return x * x; }

計算幾何點類

??設計了一個二維點類,可以進行一些向量運算。
????det:計算兩個向量的叉積;
????dot:計算兩個向量的點積;
????dist:計算兩個點的距離;
????rotate_point: o p ? \vec{op} op ?繞原點逆時針旋轉A(弧度);

struct point {double x, y;point() :x(0.0), y(0.0) {}point(double a, double b) :x(a), y(b) {}void input() { scanf("%lf%lf", &x, &y); }friend point operator+(const point& a, const point& b) {return point(a.x + b.x, a.y + b.y);}friend point operator-(const point& a, const point& b) {return point(a.x - b.x, a.y - b.y);}friend point operator*(const point& a, const point& b) {return point(a.x * b.x, a.y * b.y);}friend point operator*(const double& a, const point& b) {return point(a * b.x, a * b.y);}friend point operator*(const point& a, const double& b) {return point(b * a.x, b * a.y);}friend point operator/(const point& a, const double& b) {return point(a.x / b, a.y / b);}double norm() {return sqrt(sqr(x) + sqr(y));} }; double det(const point& a, const point& b) {return a.x * b.y - a.y * b.x; } double dot(const point& a, const point& b) {return a.x * b.x + a.y * b.y; } double dist(const point& a, const point& b) {return (a - b).norm(); } point rotate_point(const point& p, double A) {double tx = p.x;double ty = p.y;return point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A)); }

??點積(也叫內積)結果 為 x1 * x2 + y1 * y2 = | a ? \vec{a} a || b ? \vec{b} b | cos< a ? \vec{a} a , b ? \vec{b} b >,可以理解為向量a在向量b上投影的長度乘以向量b的長度。
??叉積(也叫外積)的模為 x1 * y2 - x2 * y1 = | a ? \vec{a} a || b ? \vec{b} b | sin< a ? \vec{a} a , b ? \vec{b} b >,可以理解為平行四邊形的有向面積(三維以上為體積)。外積的方向垂直于這兩個方向。

計算幾何線段類

??線段類使用一個有向線段表示,線段類的運算使用向量運算;內部使用線段的兩個點記錄,用a->b表示有向線段;

struct line {point a;// beginpoint b;// endline() {}line(point x, point y) :a(x), b(y) {} }; // 用兩個點 line point_make_line(const point a, const point b) {return line(a, b); } // 計算p點到線段st的距離 double dis_point_segment(const point p, const point s, const point t) {if (cmp(dot(p - s, t - s)) == 0) return (p - s).norm();if (cmp(dot(p - t, s - t)) == 0) return (p - t).norm();return fabs(det(s - p, t - p) / dist(s, t)); } // 計算p點在線段st上的投影點cp void PointProjLine(const point p, const point s, const point t, point& cp) {double r = dot((t - s), (p - s)) / dot(t - s, t - s);cp = s + r * (t - s); } // 判斷p點是否在線段st上(包括端點) bool PointOnSegment(point p, point s, point t) {return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0; } // 判斷線段a和線段b是否平行; bool parallel(line a, line b) {return !cmp(det(a.a - a.b, b.a - b.b)); } // 判斷線段a和線段b是否相交,如果相交則返回true且交點保存在res中; bool line_make_point(line a, line b, point& res) {if (parallel(a, b)) return false;double s1 = det(a.a - b.a, b.b - b.a);double s2 = det(a.b - b.a, b.b - b.a);res = (s1 * a.b - s2 * a.a) / (s1 - s2);return true; } // 將線段a沿著法線方向平移距離len得到的線段 line move_d(line a, const double& len) {point d = a.b - a.a;d = d / d.norm();d = rotate_point(d, pi / 2);return line(a.a + d * len, a.b + d * len); }

計算p點到線段st的距離

??計算距離的特殊情況是p點與s點的連線垂直于st向量或者p點與t點的連線垂直于st向量時,直接使用ps或者pt向量的模;

??一般情況下,使用面積法計算p點到st段上的投影,即使用ps向量和st向量的叉積表示兩個向量形成的平行四邊形的面積,除以st的線段長度,就是p點到st向量的距離;

??具體代碼,參照之前代碼的dis_point_segment函數;

總結

以上是生活随笔為你收集整理的C++笔记-学习算法与实现-计算几何-二维向量和线段运算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。