Beispiel #1
0
 public static bool IsPointInPolygon(Point P, ConvexPolygon polygon)
 {
     for (int i = 0; i < polygon.Count; i++)
     {
         Point A = polygon[i];
         Point B = polygon[(i + 1) % polygon.Count];
         if ((B - A).CrossProductWith(P - A).IsLess(0))
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #2
0
        public Point GetCenterOfMass()
        {
            var    centerOfMass = new Point(0, 0);
            double mass         = 0;

            for (int i = 1; i < (int)points.Count - 1; i++)
            {
                var A           = points[0];
                var B           = points[i];
                var C           = points[i + 1];
                var M           = (A + B + C) / 3;
                var currentMass = new ConvexPolygon(A, B, C).GetArea();
                centerOfMass += M * currentMass;
                mass         += currentMass;
            }
            return(centerOfMass / mass);
        }
Beispiel #3
0
        public static Segment NearestEdgeFromPointToPolygon(Point P, ConvexPolygon polygon)
        {
            double  distance = double.PositiveInfinity;
            Segment nearest  = null;

            for (int i = 0; i < polygon.Count; i++)
            {
                Point  A           = polygon[i];
                Point  B           = polygon[(i + 1) % polygon.Count];
                double curDistance = P.DistanceToSegment(new Segment(A, B));
                if (curDistance < distance)
                {
                    distance = curDistance;
                    nearest  = new Segment(A, B);
                }
                distance = Math.Min(distance, P.DistanceToSegment(new Segment(A, B)));
            }
            return(nearest);
        }
Beispiel #4
0
        public static ConvexPolygon IntersectPolygons(ConvexPolygon a, ConvexPolygon b)
        {
            var interestingPoints = new List <Point>().Concat(a).Concat(b).ToList();

            for (int i = 0; i < a.Count; i++)
            {
                Point A = a[i], B = a[(i + 1) % a.Count];
                for (int s = 0; s < b.Count; s++)
                {
                    Point   C = b[s], D = b[(s + 1) % b.Count];
                    Segment intersection = IntersectSegments(new Segment(A, B), new Segment(C, D));
                    if (intersection != null)
                    {
                        interestingPoints.Add(intersection.A);
                        interestingPoints.Add(intersection.B);
                    }
                }
            }
            interestingPoints = interestingPoints.Where(p => a.ContainsPoint(p) && b.ContainsPoint(p)).ToList();
            return(new ConvexPolygon(interestingPoints));
        }
Beispiel #5
0
 public static double DistanceFromPointToPolygon(Point P, ConvexPolygon polygon)
 {
     return(NearestEdgeFromPointToPolygon(P, polygon).DistanceToPoint(P));
 }
Beispiel #6
0
 public static bool IsPointOnPolygonBorder(Point P, ConvexPolygon polygon)
 {
     return(polygon.Where((t, i) => new Segment(t, polygon[(i + 1) % polygon.Count]).ContainsPoint(P)).Any());
 }
Beispiel #7
0
 public double DistanceToPolygon(ConvexPolygon polygon)
 {
     return(GeometryOperations.DistanceFromPointToPolygon(this, polygon));
 }
Beispiel #8
0
 public ConvexPolygon IntersectWithPolygon(ConvexPolygon other)
 {
     return(GeometryOperations.IntersectPolygons(this, other));
 }