示例#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);
 }
示例#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);
        }
示例#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);
        }
示例#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));
        }
示例#5
0
 public static double DistanceFromPointToPolygon(Point P, ConvexPolygon polygon)
 {
     return(NearestEdgeFromPointToPolygon(P, polygon).DistanceToPoint(P));
 }
示例#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());
 }
示例#7
0
文件: Point.cs 项目: Umqra/GameTask
 public double DistanceToPolygon(ConvexPolygon polygon)
 {
     return(GeometryOperations.DistanceFromPointToPolygon(this, polygon));
 }
示例#8
0
 public ConvexPolygon IntersectWithPolygon(ConvexPolygon other)
 {
     return(GeometryOperations.IntersectPolygons(this, other));
 }