示例#1
0
        public static bool SegmentToRay(Vertex2 segmentStart, Vertex2 segmentEnd, Vertex2 rayStart, Vertex2 rayEnd,
                                        out Vertex2 intersection)
        {
            intersection = Vertex2.zero;
            float r, s;

            if (LineToLine(rayStart, rayEnd, segmentStart, segmentEnd, out r, out s))
            {
                if (r >= 0)
                {
                    if (s >= 0 && s <= 1)
                    {
                        intersection = rayStart + (rayEnd - rayStart) * r;
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#2
0
        public static bool IntSegmentToRay(IntVertex2 segmentStart, IntVertex2 segmentEnd, Vertex2 rayStart,
                                           Vertex2 rayEnd, out IntVertex2 intersection)
        {
            intersection = IntVertex2.zero;
            float r, s;

            if (LineToLine(rayStart, rayEnd, (Vertex2)segmentStart, (Vertex2)segmentEnd, out r, out s))
            {
                if (r >= 0)
                {
                    if (s >= 0 && s <= 1)
                    {
                        var inter = rayStart + (rayEnd - rayStart) * r;
                        intersection = new IntVertex2(inter.x, inter.y);
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#3
0
        public static void DrawGradientLine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color0,
                                            Color color1)
        {
            var distance = Vertex2.Distance(new Vertex2(x0, y0), new Vertex2(x1, y1));
            Action <int, int> draw;

            if (distance > 0)
            {
                draw = (x, y) =>
                {
                    var percent = Vertex2.Distance(new Vertex2(x0, y0), new Vertex2(x, y)) / distance;
                    texture.SetPixel(x, y, Color.Lerp(color0, color1, percent));
                };
            }
            else
            {
                draw = (x, y) => texture.SetPixel(x, y, color0);
            }
            BresenhamLine(x0, y0, x1, y1, draw);
            texture.Apply();
        }
示例#4
0
        public static float PointToLineSegment(Vertex2 point, Vertex2 segmentStart, Vertex2 segmentEnd)
        {
            Vertex2 v = segmentEnd - segmentStart;
            Vertex2 w = point - segmentStart;

            float c1 = Vertex2.Dot(w, v);

            if (c1 <= 0)
            {
                return(Vertex2.Distance(point, segmentStart));
            }

            float c2 = Vertex2.Dot(v, v);

            if (c2 <= c1)
            {
                return(Vertex2.Distance(point, segmentEnd));
            }

            float   b  = c1 / c2;
            Vertex2 pb = segmentStart + b * v;

            return(Vertex2.Distance(point, pb));
        }
示例#5
0
 public static bool SegmentToRay(Segment2 segment, Vertex2 rayStart, Vertex2 rayEnd, out Vertex2 intersection)
 {
     return(SegmentToRay(segment.a, segment.b, rayStart, rayEnd, out intersection));
 }
示例#6
0
 public static bool SegmentToRay(Segment2 segment, Ray2D ray, out Vertex2 intersection)
 {
     return(SegmentToRay(segment.a, segment.b, new Vertex2(ray.origin), new Vertex2(ray.origin + ray.direction),
                         out intersection));
 }
示例#7
0
 public static IntersectionType SegmentToSegmentE(Vertex2 start0, Vertex2 end0, Vertex2 start1,
                                                  Vertex2 end1, out Segment2 intersection, bool testBoundingBox = false)
 {
     return(SegmentToSegmentE(start0.x, start0.y, end0.x, end0.y, start1.x, start1.y, end1.x, end1.y,
                              out intersection, testBoundingBox));
 }
示例#8
0
 public Segment2(Vertex2 a, Vertex2 b)
 {
     this.a = a;
     this.b = b;
 }
示例#9
0
 public static float PerpDot(Vertex2 a, Vertex2 b)
 {
     return(a.x * b.y - a.y * b.x);
 }
示例#10
0
        private static bool LineToLine(Vertex2 vertex1, Vertex2 vertex2, Vertex2 vertex3, Vertex2 vertex4, out float r,
                                       out float s)
        {
            r = 0;
            s = 0;
            //Make sure the lines aren't parallel
            Vertex2 line1 = vertex2 - vertex1;
            Vertex2 line2 = vertex4 - vertex3;

            //if (vertex1to2.x * -vertex3to4.y + vertex1to2.y * vertex3to4.x != 0)
            //{
            //if (line1.y/line1.x != line2.y/line2.x)
            //{

            //}

            float d = PGUtils.PerpDot(line1, line2);

            if (d != 0)
            {
                Vertex2 vertex3to1 = vertex1 - vertex3;
                r = (vertex3to1.y * line2.x - vertex3to1.x * line2.y) / d;
                s = (vertex3to1.y * line1.x - vertex3to1.x * line1.y) / d;
                return(true);
            }
            else
            {
                //Parallel
            }

            return(false);
        }
示例#11
0
        public static bool LineToLine(Vertex2 line1Start, Vertex2 line1End, Vertex2 line2Start, Vertex2 line2End,
                                      out Vertex2 intersection)
        {
            intersection = Vertex2.zero;
            float r, s;

            if (LineToLine(line1Start, line1End, line2Start, line2End, out r, out s))
            {
                intersection = line1Start + (line1End - line1Start) * r;
                return(true);
            }
            return(false);
        }
示例#12
0
 public static float PointToLine(Vertex2 point, Segment2 segment)
 {
     return(PointToLine(point, segment.a, segment.b));
 }
示例#13
0
        public static float LineSegmentToLineSegment(Segment2 S1, Segment2 S2)
        {
            Vertex2 u = S1.b - S1.a;
            Vertex2 v = S2.b - S2.a;
            Vertex2 w = S1.a - S2.a;
            float   a = Vertex2.Dot(u, u); // always >= 0
            float   b = Vertex2.Dot(u, v);
            float   c = Vertex2.Dot(v, v); // always >= 0
            float   d = Vertex2.Dot(u, w);
            float   e = Vertex2.Dot(v, w);
            float   D = a * c - b * b; // always >= 0
            float   sN, sD = D;        // sc = sN / sD, default sD = D >= 0
            float   tN, tD = D;        // tc = tN / tD, default tD = D >= 0

            // compute the line parameters of the two closest points
            if (D < Mathf.Epsilon)
            {
                // the lines are almost parallel
                sN = 0; // force using point P0 on segment S1
                sD = 1; // to prevent possible division by 0.0 later
                tN = e;
                tD = c;
            }
            else
            {
                // get the closest points on the infinite lines
                sN = (b * e - c * d);
                tN = (a * e - b * d);
                if (sN < 0)
                {
                    // sc < 0 => the s=0 edge is visible
                    sN = 0;
                    tN = e;
                    tD = c;
                }
                else if (sN > sD)
                {
                    // sc > 1  => the s=1 edge is visible
                    sN = sD;
                    tN = e + b;
                    tD = c;
                }
            }

            if (tN < 0)
            {
                // tc < 0 => the t=0 edge is visible
                tN = 0;
                // recompute sc for this edge
                if (-d < 0)
                {
                    sN = 0;
                }
                else if (-d > a)
                {
                    sN = sD;
                }
                else
                {
                    sN = -d;
                    sD = a;
                }
            }
            else if (tN > tD)
            {
                // tc > 1  => the t=1 edge is visible
                tN = tD;
                // recompute sc for this edge
                if ((-d + b) < 0)
                {
                    sN = 0;
                }
                else if ((-d + b) > a)
                {
                    sN = sD;
                }
                else
                {
                    sN = (-d + b);
                    sD = a;
                }
            }
            // finally do the division to get sc and tc
            float sc = (Mathf.Abs(sN) < Mathf.Epsilon ? 0 : sN / sD);
            float tc = (Mathf.Abs(tN) < Mathf.Epsilon ? 0 : tN / tD);

            // get the difference of the two closest points
            Vertex2 dP = w + (sc * u) - (tc * v); // =  S1(sc) - S2(tc)

            return(dP.magnitude);                 // return the closest distance
        }
示例#14
0
 public Segment2(float x0, float y0, float x1, float y1)
 {
     a = new Vertex2(x0, y0);
     b = new Vertex2(x1, y1);
 }
示例#15
0
 public Segment2(Vertex2 b)
 {
     a      = Vertex2.zero;
     this.b = b;
 }
示例#16
0
 public static bool SegmentToPoint(Segment2 segment, Vertex2 point)
 {
     return(SegmentToPoint(segment.a, segment.b, point));
 }
示例#17
0
 /// <summary>
 /// Returns perp of vector
 /// </summary>
 /// <remarks>
 /// Hill, F. S. Jr. "The Pleasures of 'Perp Dot' Products."
 /// Ch. II.5 in Graphics Gems IV (Ed. P. S. Heckbert). San Diego: Academic Press, pp. 138-148, 1994
 /// </remarks>
 public static Vertex2 Perp(Vertex2 vector)
 {
     return(new Vertex2(-vector.y, vector.x));
 }
示例#18
0
        public static bool SegmentToSegment(Vertex2 start0, Vertex2 end0, Vertex2 start1, Vertex2 end1,
                                            bool testBoundingBox = false)
        {
            Segment2 intersection;

            return(SegmentToSegmentE(start0.x, start0.y, end0.x, end0.y, start1.x, start1.y, end1.x, end1.y,
                                     out intersection, testBoundingBox) != IntersectionType.None);
        }
示例#19
0
 /// <summary>
 /// More than 0 - left, less than 0 - right, equals 0 - on line
 /// </summary>
 public static float LocatePointOnLine(Vertex2 line1, Vertex2 line2, Vertex2 point)
 {
     return((line2.x - line1.x) * (point.y - line1.y) - (point.x - line1.x) * (line2.y - line1.y));
 }
示例#20
0
 public Segment2(Segment2 segment)
 {
     a = segment.a;
     b = segment.b;
 }