/// <summary> /// 判断点与直线的关系,假设你站在a点朝向b点, /// 则输入点与直线的关系分为:Left, Right or Centered on the line /// </summary> /// <param name="point">判断点</param> /// <returns>判断结果</returns> public PointSide ClassifyPoint(Vector2 point) { if (point == this.m_cStartPoint || point == this.m_cEndPoint) { return(PointSide.ON_LINE); } //向量a Vector2 vectorA = this.m_cEndPoint - this.m_cStartPoint; //向量b Vector2 vectorB = point - this.m_cStartPoint; float crossResult = NMath.CrossProduct(vectorA, vectorB); if (NMath.IsEqualZero(crossResult)) { return(PointSide.ON_LINE); } else if (crossResult < 0) { return(PointSide.RIGHT_SIDE); } else { return(PointSide.LEFT_SIDE); } }
/// <summary> /// 判断是否是顺时针 /// </summary> /// <returns></returns> public bool IsCW() { if (this.m_lstPoints.Count <= 2) { return(false); } //最上(y最小)最左(x最小)点, 肯定是一个凸点 //寻找最上点 Vector2 topPoint = this.m_lstPoints[0]; int topIndex = 0; for (int i = 1; i < this.m_lstPoints.Count; i++) { Vector2 currPoint = this.m_lstPoints[i]; if ((topPoint.y > currPoint.y) || ((topPoint.y == currPoint.y) && (topPoint.x > currPoint.x))) { topPoint = currPoint; topIndex = i; } } //寻找左右邻居 int preIndex = (topIndex - 1) >= 0 ? (topIndex - 1) : (this.m_lstPoints.Count - 1); int nextIndex = (topIndex + 1) < this.m_lstPoints.Count ? (topIndex + 1) : 0; Vector2 prePoint = this.m_lstPoints[preIndex]; Vector2 nextPoint = this.m_lstPoints[nextIndex]; //三点共线情况不存在,若三点共线则说明必有一点的y(斜线)或x(水平线)小于topPt float r = NMath.CrossProduct((prePoint - topPoint), (nextPoint - topPoint)); if (r > 0) { return(true); } return(false); }