在MATLAB中顺时针排列多边形点 [英] Sorting clockwise polygon points in MATLAB
本文介绍了在MATLAB中顺时针排列多边形点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个向量,它们是多边形的8个顶点的x和y坐标
x = [5 5 7 7 9 9 5 7]
y = [8 6 6 8 6 8 10 10]
我想对它们进行排序(顺时针)以获得正确的矢量(以正确绘制多边形)
x = [5 7 9 9 7 7 5 5]
y = [6 6 6 8 8 10 10 8]
第1步:找到质心:
cx => (x)的均值;
cy = mean(y);
第二步:找到角度:
a = atan2(y - cy,x - cx);
第3步:找到正确的排序顺序:
[〜,order] = sort(a);
第4步:重新排序坐标:
x = x(order);
y = y(order);
I have 2 vectors that are x and y coordinates of the 8 vertexes of a polygon
x=[5 5 7 7 9 9 5 7]
y=[8 6 6 8 6 8 10 10]
I wanna sort them (clockwise) to obtain the right vectors (to draw the polygon correctly)
x=[5 7 9 9 7 7 5 5]
y=[6 6 6 8 8 10 10 8]
解决方案
Step 1: Find the centroid:
cx = mean(x);
cy = mean(y);
Step 2: Find the angles:
a = atan2(y - cy, x - cx);
Step 3: Find the correct sorted order:
[~, order] = sort(a);
Step 4: Reorder the coordinates:
x = x(order);
y = y(order);
这篇关于在MATLAB中顺时针排列多边形点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文