/// <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 void DelRepeatPoint() { for (int i = 0; i < this.m_lstPoints.Count; i++) { for (int j = i + 1; j < this.m_lstPoints.Count; j++) { if (NMath.IsEqualZero(this.m_lstPoints[i] - this.m_lstPoints[j])) { this.m_lstPoints.Remove(this.m_lstPoints[j]); j = i; } } } }
/// <summary> /// 生成最终的路径点 /// </summary> /// <param name="startPos">起始点</param> /// <param name="endPos">终点</param> /// <param name="triPathList">三角形路径列表</param> /// <param name="wayPoints">路径点</param> /// <param name="offSet">移动物体宽度</param> /// <returns></returns> private PathResCode CreateWayPoints(Vector2 startPos, Vector2 endPos , List <NavTriangle> triPathList, out List <Vector2> wayPoints, int offSet) { wayPoints = new List <Vector2>(); if (triPathList.Count == 0 || startPos == null || endPos == null) { return(PathResCode.Failed); } // 保证从起点到终点的顺序 triPathList.Reverse(); // 保存出边编号 for (int i = 0; i < triPathList.Count; i++) { NavTriangle tri = triPathList[i]; if (i != triPathList.Count - 1) { NavTriangle nextTri = triPathList[i + 1]; tri.SetOutWallIndex(tri.GetWallIndex(nextTri.GetID())); } } wayPoints.Add(startPos); //起点与终点在同一三角形中 if (triPathList.Count == 1) { wayPoints.Add(endPos); return(PathResCode.Success); } WayPoint way = new WayPoint(startPos, triPathList[0]); while (!NMath.IsEqualZero(way.GetPoint() - endPos)) { way = GetFurthestWayPoint(way, triPathList, endPos, offSet); if (way == null) { return(PathResCode.CanNotGetNextWayPoint); } wayPoints.Add(way.GetPoint()); } return(PathResCode.Success); }
/// <summary> /// 两条线段是否相等 /// </summary> /// <param name="lineTemp">判断对象</param> /// <returns>是否相等</returns> public bool Equals(Line2D line) { //只是一个点 if (NMath.IsEqualZero(line.m_cStartPoint - line.m_cEndPoint) || NMath.IsEqualZero(m_cStartPoint - m_cEndPoint)) { return(false); } //whatever the direction bool bEquals = NMath.IsEqualZero(m_cStartPoint - line.m_cStartPoint) ? true : NMath.IsEqualZero(m_cStartPoint - line.m_cEndPoint); if (bEquals) { bEquals = NMath.IsEqualZero(m_cEndPoint - line.m_cStartPoint) ? true : NMath.IsEqualZero(m_cEndPoint - line.m_cEndPoint); } return(bEquals); }
/// <summary> /// 判断这条线段是否没有和其他的边相交 /// </summary> /// <param name="sPnt"></param> /// <param name="point"></param> /// <returns></returns> private bool IsVisibleIn2Point(Vector2 sPnt, Vector2 ePnt) { Line2D line = new Line2D(sPnt, ePnt); Vector2 interPos; foreach (Line2D edge in allEdges) { if (edge.Intersection(line, out interPos) == LineCrossState.CROSS) { if (!NMath.IsEqualZero(sPnt - interPos) && !NMath.IsEqualZero(ePnt - interPos)) { return(false); } } } return(true); }
/// <summary> /// 返回三角形的外接圆 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <returns></returns> public static Circle CreateCircle(Vector2 p1, Vector2 p2, Vector2 p3) { if (!NMath.IsEqualZero(p1.y - p2.y) || !NMath.IsEqualZero(p2.y - p3.y)) { double yc, xc; float m1 = -(p2.x - p1.x) / (p2.y - p1.y); float m2 = -(p3.x - p2.x) / (p3.y - p2.y); double mx1 = (p1.x + p2.x) / 2.0; double mx2 = (p2.x + p3.x) / 2.0; double my1 = (p1.y + p2.y) / 2.0; double my2 = (p2.y + p3.y) / 2.0; if (NMath.IsEqualZero(p1.y - p2.y)) { xc = (p2.x + p1.x) / 2.0; yc = m2 * (xc - mx2) + my2; } else if (NMath.IsEqualZero(p3.y - p2.y)) { xc = (p3.x + p2.x) / 2.0; yc = m1 * (xc - mx1) + my1; } else { xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2); yc = m1 * (xc - mx1) + my1; } double dx = p2.x - xc; double dy = p2.y - yc; double rsqr = dx * dx + dy * dy; double r = Math.Sqrt(rsqr); return(new Circle(new Vector2((float)xc, (float)yc), (float)r)); } return(new Circle(new Vector2(0, 0), 0)); }
/// <summary> /// 计算两条二维线段的交点 /// </summary> /// <param name="other">Other line</param> /// <param name="intersectPoint">输出的线段交点</param> /// <returns>返回值说明了两条线段的位置关系(COLINE,PARALLEL,CROSS,NOT_CROSS) </returns> public LineCrossState Intersection(Line2D other, out Vector2 intersectPoint) { intersectPoint.x = intersectPoint.y = float.NaN; if (!NMath.CheckCross(this.m_cStartPoint, this.m_cEndPoint, other.m_cStartPoint, other.m_cEndPoint)) { return(LineCrossState.NOT_CROSS); } double A1, B1, C1, A2, B2, C2; A1 = this.m_cEndPoint.y - this.m_cStartPoint.y; B1 = this.m_cStartPoint.x - this.m_cEndPoint.x; C1 = this.m_cEndPoint.x * this.m_cStartPoint.y - this.m_cStartPoint.x * this.m_cEndPoint.y; A2 = other.m_cEndPoint.y - other.m_cStartPoint.y; B2 = other.m_cStartPoint.x - other.m_cEndPoint.x; C2 = other.m_cEndPoint.x * other.m_cStartPoint.y - other.m_cStartPoint.x * other.m_cEndPoint.y; if (NMath.IsEqualZero(A1 * B2 - B1 * A2)) { if (NMath.IsEqualZero((A1 + B1) * C2 - (A2 + B2) * C1)) { return(LineCrossState.COLINE); } else { return(LineCrossState.PARALLEL); } } else { intersectPoint.x = (float)((B2 * C1 - B1 * C2) / (A2 * B1 - A1 * B2)); intersectPoint.y = (float)((A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2)); return(LineCrossState.CROSS); } }