Example #1
0
        public static bool SegmentsIntersecting(gvec2 p0, gvec2 p1, gvec2 pa, gvec2 pb)
        {
            gvec2 dir0 = p1 - p0;
            gvec2 dir1 = pb - pa;

            {
                gvec2 dir0n = dir0.Normalized;
                gvec2 dir1n = dir1.Normalized;

                if ((dir0n.x.cmp(dir1n.x) && dir0n.y.cmp(dir1n.y)) || (dir0n.x.cmp(-dir1n.x) && dir0n.y.cmp(-dir1n.y)))
                {
                    return(false);
                }
            }

            double t0 =
                ((dir1.x) * (p0.y - pa.y) - (dir1.y) * (p0.x - pa.x)) /
                ((dir1.y) * (dir0.x) - (dir1.x) * (dir0.y));

            if (t0 < 0 || t0 >= 1)
            {
                return(false);
            }

            double t1 =
                ((dir0.x) * (p0.y - pa.y) - (dir0.y) * (p0.x - pa.x)) /
                ((dir1.y) * (dir0.x) - (dir1.x) * (dir0.y));

            if (t1 < 0 || t1 >= 1)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        public static bool SegmentsIntersecting(gvec2 p0, gvec2 p1, gvec2 pa, gvec2 pb, ref gvec2 intersection, ref double parameter0, ref double parameter1)
        {
            gvec2 dir0 = p1 - p0;
            gvec2 dir1 = pb - pa;

            {
                gvec2 dir0n = dir0.Normalized;
                gvec2 dir1n = dir1.Normalized;

                if ((dir0n.x.cmp(dir1n.x) && dir0n.y.cmp(dir1n.y)) || (dir0n.x.cmp(-dir1n.x) && dir0n.y.cmp(-dir1n.y)))
                {
                    return(false);
                }
            }


            parameter0 =
                ((dir1.x) * (p0.y - pa.y) - (dir1.y) * (p0.x - pa.x)) /
                ((dir1.y) * (dir0.x) - (dir1.x) * (dir0.y));

            if (parameter0 < 0 || parameter0 >= 1)
            {
                return(false);
            }

            parameter1 =
                ((dir0.x) * (p0.y - pa.y) - (dir0.y) * (p0.x - pa.x)) /
                ((dir1.y) * (dir0.x) - (dir1.x) * (dir0.y));

            if (parameter1 < 0 || parameter1 >= 1)
            {
                return(false);
            }

            intersection = dir0 * parameter0 + p0;

            return(true);
        }
Example #3
0
 public bool IsPointInside(gvec2 point)
 {
     return(point.x >= ll.x && point.x < ur.x && point.y >= ll.y && point.y < ur.y);
 }
Example #4
0
 public static bool PointInCCWTriangle(gvec2 pt, gvec2 v0, gvec2 v1, gvec2 v2) => IsCCW(pt, v0, v1) && IsCCW(pt, v1, v2) && IsCCW(pt, v2, v0);
Example #5
0
 public static bool IsCCW(gvec2 v0, gvec2 v1, gvec2 v2) => (v1.x - v0.x) * (v1.y + v0.y) + (v2.x - v1.x) * (v2.y + v1.y) + (v0.x - v2.x) * (v0.y + v2.y) <= 0;