[英]How to Draw Lines Between Convex Hull Points
假設我已通過quickhull算法正確計算了凸包點,那么我想在帶有線的面板上顯示凸包。 但是,我當前的嵌套for循環顯示一條通過計算出的每個點的直線。 有沒有辦法只在外部點之間畫線? 我的for嵌套循環錯誤地顯示了如下行:
我只想在外部顯示線條(如圍欄)。 我該怎么做呢?
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (pointsClicked.Count() == 0)
{
}
else
{
base.OnPaint(e);
using (Graphics g = e.Graphics)
{
var p = new Pen(Color.Black, 3);
// draw a line for each point created
for (int i = 0; i < convexHullPoints.Count() - 1; i++)
{
for (int j = 0; j < convexHullPoints.Count(); j++)
{
g.DrawLine(p, convexHullPoints[i], convexHullPoints[j]);
}
}
}
}
}
將兩個嵌套的for
循環替換for
一個在凸包多邊形的所有邊緣上進行迭代的循環:
int n = convexHullPoints.Count();
// Draw the edges [P_{n-1},P_0], [P_0,P_1], ..., [P_{n-2},P_{n-1}]
for (int i = n - 1, j = 0; j < n; i = j++)
{
g.DrawLine(p, convexHullPoints[i], convexHullPoints[j]);
}
注意:我在這里假設, convexHullPoints
組中的點是沿着凸凹convexHullPoints
排列的,可能並非如此。 如果不是,則存在對它們進行重新排序的方法(例如,計算點的重心並按與重心成直角的方式對點進行排序)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.