轮廓点顺时针或逆时针方向 [英] Contour Points direction clockwise or counterclockwise

查看:666
本文介绍了轮廓点顺时针或逆时针方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按顺序说1-10,这是在2-D平面上创建轮廓(多边形)。现在我想检查从startPoint到endPoint的这些点是顺时针还是逆时针方向移动以创建多边形。任何有效的方法来检查这一点。此多边形可以位于XY平面中的任何位置,并且可以旋转一定角度。

I am having some points in order say 1-10 which are creating a contour(polygon) in 2-D plane. Now I want to check that these points from startPoint to endPoint are moving in clockwise or anticlockwise direction to create the polygon. Any efficient way to check this out. This polygon can be on any where in X-Y plane and may be rotated by some angle.

推荐答案

至少需要3个点来检测轮廓方向。使用前2个点创建光线(通过连接它们,光线的原点是第一个点)。如果第三个点是光线的左,则轮廓的方向是逆时针方向。如果第三个点是光线的右,则轮廓的方向是顺时针方向。



您可以通过检查光线的斜率然后查看该点是高于还是低于该点的点来检查第三个点是否在光线的左侧或右侧。与第三个点具有相同x坐标的光线。需要注意的是光线的斜率是否是无穷大(即垂直线)。在这种特殊情况下,您需要做的就是检查点是否在光线的x坐标的左侧或右侧。如果斜率为零(即水平线),则只需检查第三个点是否高于或低于光线的y坐标。



只是一个快速的提示,因为我知道,一旦你有更高的数学,这个数学基础可以很容易地滑动,斜率是通过上升超过运行来计算的(即,y - 两点之间的坐标差异,由相同两点之间的x坐标差异计算得出)。你可能会很好地利用好的旧y = mx + b斜率截距方程。



哦,还有一件事。如果您有可以使用的数学库,则可以使用叉积来计算3个点是顺时针还是逆时针方向。这是3D渲染中的常用技术。只需从3个点中制作一个三角形。您可以阅读有关此技术的更多信息:此处。如果这很重要,使用这种技术可以加快计算速度。
You will need at least 3 points to detect a direction of contour. Create a ray using the first 2 points (by connecting them, with the origin of the ray being the first point). If the third point is to the "left" of the ray, the direction of the contour is counterclockwise. If the third point is the the "right" of the ray, the direction of the contour is clockwise.

You can check if the third point is to the left or right of the ray by checking the slope of the ray then seeing if the point is above or below the point on the ray that has the same x-coordinate as the third point. One gotcha to look out for is if the slope of the ray is infinity (i.e., a vertical line). In this special case, all you need to do is check if the point is to the left or right of the ray's x-coordinate. If the slope is zero (i.e., a horizontal line), you just check if the third point is above or below the ray's y-coordinate.

Just a quick tip, because I know this basic of math can slip easily once you've had higher maths, slope is calculated by "rise over run" (i.e., y-coordinate differences between two points, calculated by x-coordinate differences between those same two points). And you'll probably be making good use of the good old "y = mx + b" slope-intercept equation.

Oh, and one more thing. If you have a math library you can use, the cross product can be used to calculate whether the 3 points are in a clockwise or counterclockwise orientation. This is a common technique in 3D rendering. Just make a triangle out of 3 of the points. You can read more about this technique: here. Using this technique should speed up the calculation, if that is important.


这篇关于轮廓点顺时针或逆时针方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆