解决wpf - How to draw a polygon from a set of unordered points
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
Currently, I am using a convex hull algorithm to get the outer most points from a set of points randomly placed. What I aim to do is draw a polygon from the set of points returned by the convex hull however, when I try to draw the polygon it looks quite strange.
My question, how do I order the points so the polygon draws correctly?
Thanks.
EDIT:
Also, I have tried sorting using orderby(...).ThenBy(...) and I cant seem to get it working.
wpf sorting draw polygon|
this question asked Jan 18 '13 at 3:47 Rhexis 853 3 12 30
|
2 Answers
2
解决方法
Have you tried the gift wrapping algorithm ( http://en.wikipedia.org/wiki/Gift_wrapping_algorithm)? This should return points in the correct order.
|
this answer edited Jan 18 '13 at 4:05 Mark Hall 43.4k 8 64 82 answered Jan 18 '13 at 4:04 user1149913 3,454 1 11 20 Thansk for your response, I shall have a look at this, thanks. – Rhexis Jan 18 '13 at 4:27
|
I had an issue where a random set of points were generated from which a wrapped elevation vector needed a base contour. Having read the link supplied by @user1149913 and found a sample of gift-wrapping a hull, the following is a sample of my implementation:
private static PointCollection CalculateContour (List<Point> points) {
// locate lower-leftmost point
int hull = 0;
int i;
for (i = 1 ; i < points.Count ; i++) {
if (ComparePoint(points[i], points[hull])) {
hull = i;
}
}
// wrap contour
var outIndices = new int[points.Count];
int endPt;
i = 0;
do {
outIndices[i++] = hull;
endPt = 0;
for (int j = 1 ; j < points.Count ; j++)
if (hull == endPt || IsLeft(points[hull], points[endPt], points[j]))
endPt = j;
hull = endPt;
} while (endPt != outIndices[0]);
// build countour points
var contourPoints = new PointCollection(points.Capacity);
int results = i;
for (i = 0 ; i < results ; i++)
contourPoints.Add(points[outIndices[i]]);
return contourPoints;
}
|
this answer answered Nov 12 '14 at 19:30 IAbstract 13.7k 11 58 111
|
相关阅读排行
- 1WPF TreeView 绑定到层次结构数据库
- 2C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):目录
- 3深入浅出WPF(1)——什么是WPF
- 4WPF窗口跳转及window和page区别
- 5深入浅出WPF(7)——数据的绿色通道,Binding(上)