0
A
回答
0
我不知道這是否回答你的問題,但這裏有一段代碼,我幾年前寫的。
它計算多邊形點列表中的點索引以及垂直於最近邊緣的距離。
它使用一個Vector2
結構定義矢量運算+
,-
,*
(點積)和方法GetLength
和GetSquaredLength
。同樣的代碼也應該與Vector3
(3D)一起運行。
請參閱備註以瞭解詳情。
// Polygon points
List<Vector2> p;
/// <summary> Calculates the perpendicular vector from nearest point on polygon to given point. If no points available minIndex is -1. </summary>
public void GetPolygonDist(Vector2 point, out Vector2 minDist, out int minIndex) {
if (p.Count == 0) { minDist = Vector2.Null(); minIndex = -1; return; }
Vector2 dist;
minDist = point - p[0];
minIndex = 0;
for (int i = 0; i < p.Count - 1; i++) {
Vector2 l = p[i + 1] - p[i]; // Edge
if (l.GetLength() < 1e-9) continue; // Ignore edge of length almost zero
Vector2 d = point - p[i]; // Polygon point to point
Vector2 b = (l * d)/(l * l) * l; // Projection to edge
double f = Math.Sign(b * l) * (b.GetLength()/l.GetLength()); // Where on the edge is the perpendicular?
if (f < 0.0) dist = point - p[i]; // Point before the edge
else if (f > 1.0) dist = point - p[i + 1]; // Point after the edge
else dist = d - b; // Perpendicular
if (dist.GetSquaredLength() < minDist.GetSquaredLength()) {
minDist = dist;
minIndex = i;
}
}
}
+0
thanx,但我需要一個js –
+1
你沒有標記它js。它有點數學,所以把它轉換成js應該不是很難。 – joe
相關問題
- 1. 從一個點到多邊形的距離
- 2. 找到所有點與多邊形邊界之間的最小距離
- 3. 點特徵到最近多邊形的距離R
- 4. 多邊形邊上的點
- 5. 如何找到遠離某個多邊形的所有點?
- 6. Mysql從測試點到多邊形的距離
- 7. 如何在多邊形內找到點?
- 8. 如何確定一個多邊形的頂點,該多邊形的外邊緣距離Google地圖上javascript或線條的某個給定距離?
- 9. Python:找到點是否位於多邊形的邊界上
- 10. 給定一個多邊形和一個二維點,如何找到最接近該點的多邊形的特徵(頂點或邊)?
- 11. 多邊形點離質心最遠的距離
- 12. 在每個多邊形中查找一組多邊形的最大點R
- 13. 如何在不規則多邊形內找到一個點
- 14. 如何從一組線中找到包圍點的多邊形?
- 15. API距離多邊形
- 16. 確定一個點的多邊形是
- 17. 給定非凸多邊形中的一大組頂點,我如何找到邊?
- 18. 確定哪些多邊形的點是從一個大組多邊形
- 19. 用於多個多邊形的點多邊形算法
- 20. 兩個多邊形的最近點
- 21. Graphviz:減少傾斜的多邊形節點上的邊距
- 22. 多邊形頂點從一組點
- 23. 檢查點是一個多邊形
- 24. 我如何找到多點之間指定點的最短距離?
- 25. 點在多邊形
- 26. 從多個多邊形搜索點
- 27. Python中找到最近的多邊形給定的點座標
- 28. MySQL查找離點最近的多邊形
- 29. 查找圖層中一個多邊形與其他多邊形的最小距離?
- 30. QGis:找到一個點所在的多邊形
到目前爲止您的進展如何?請顯示一些代碼。 – joe
嗨,歡迎來到Stack Overflow。請修改您的帖子以包含您迄今爲止解決問題的嘗試,以及您收到的結果和錯誤。 – Noich