Pathfinding.Polygon.SegmentIntersectionPoint C# (CSharp) Method

SegmentIntersectionPoint() public static method

public static SegmentIntersectionPoint ( Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, bool &intersects ) : Vector3
start1 Vector3
end1 Vector3
start2 Vector3
end2 Vector3
intersects bool
return Vector3
		public static Vector3 SegmentIntersectionPoint (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, out bool intersects) {
			
			Vector3 dir1 = end1-start1;
			Vector3 dir2 = end2-start2;
			
			//Color rnd = new Color (Random.value,Random.value,Random.value);
			//Debug.DrawRay (start1,dir1,rnd);
			//Debug.DrawRay (start2,dir2,rnd);
			
			float den = dir2.z*dir1.x - dir2.x * dir1.z;
			
			if (den == 0) {
				intersects = false;
				return start1;
			}
			
			float nom = dir2.x*(start1.z-start2.z)- dir2.z*(start1.x-start2.x);
			float nom2 = dir1.x*(start1.z-start2.z) - dir1.z * (start1.x - start2.x);
			float u = nom/den;
			float u2 = nom2/den;
		
			if (u < 0F || u > 1F || u2 < 0F || u2 > 1F) {
				intersects = false;
				return start1;
			}
		
			//Debug.Log ("U1 "+u.ToString ("0.00")+" U2 "+u2.ToString ("0.00")+"\nP1: "+(start1 + dir1*u)+"\nP2: "+(start2 + dir2*u2)+"\nStart1: "+start1+"  End1: "+end1);
			//Debug.DrawLine (start2,end2,Color.magenta);
			//Debug.DrawRay (start1,dir1*5,Color.green);
			intersects = true;
			return start1 + dir1*u;
		}
		

Usage Example

Ejemplo n.º 1
0
        public bool CheckSegmentIntersects(Int3 start, Int3 end, int gridX, int gridY, out Int3 outPoint, out TriangleMeshNode nearestNode)
        {
            List <object> objs = this.rasterizer.GetObjs(gridX, gridY);

            outPoint    = end;
            nearestNode = null;
            if (objs == null || objs.Count == 0)
            {
                return(false);
            }
            Int3[] array  = new Int3[3];
            bool   result = false;
            long   num    = 9223372036854775807L;

            for (int i = 0; i < objs.Count; i++)
            {
                TriangleMeshNode triangleMeshNode = objs[i] as TriangleMeshNode;
                triangleMeshNode.GetPoints(out array[0], out array[1], out array[2]);
                for (int j = 0; j < 3; j++)
                {
                    int  num2 = j;
                    int  num3 = (j + 1) % 3;
                    bool flag = false;
                    Int3 vInt = Polygon.SegmentIntersectionPoint(array[num2], array[num3], start, end, out flag);
                    if (flag)
                    {
                        long num4 = start.XZSqrMagnitude(ref vInt);
                        if (num4 < num)
                        {
                            nearestNode = triangleMeshNode;
                            num         = num4;
                            outPoint    = vInt;
                            result      = true;
                        }
                    }
                }
            }
            return(result);
        }