public Fraction DistanceSquared(PointFr that)
        {
            var dX = _x - that.X;
            var dY = _y - that.Y;

            return(dX * dX + dY * dY);
        }
        public double Distance(PointFr that)
        {
            var dX = (_x - that.X).ToDouble();
            var dY = (_y - that.Y).ToDouble();

            return(Math.Sqrt(dX * dX + dY * dY));
        }
 public static PointFr Midpoint(PointFr a, PointFr b)
 {
     return(new PointFr(
                (a.X + b.X) / 2,
                (a.Y + b.Y) / 2
                ));
 }
        public Fraction DistanceXY(PointFr that)
        {
            var dX = (_x - that.X).Abs();
            var dY = (_y - that.Y).Abs();

            return(dX + dY);
        }
 public EventPoint(PointFr value, EventPointType type, LineSegmentFr[] crossingSegments, SweepLine sweepLine)
 {
     PointValue       = value;
     Type             = type;
     CrossingSegments = crossingSegments;
     SweepLine        = sweepLine;
 }
 public EventPoint(PointFr value, EventPointType type, LineSegmentFr?segment, SweepLine sweepLine)
 {
     PointValue = value;
     Type       = type;
     Segment    = segment;
     SweepLine  = sweepLine;
 }
        } // test bada współliniowość, a nie czy punkt jest zawarty w odcinku

        public static Fraction PointFrOrientationTest(PointFr point, LineSegmentFr segment)
        {
            var p0 = point;
            var p1 = segment.StartPoint;
            var p2 = segment.EndPoint;

            return((p1.X - p0.X) * (p2.Y - p0.Y) - (p1.Y - p0.Y) * (p2.X - p0.X));
        }
Exemple #8
0
 public bool Contains(PointFr p)
 {
     if (IsVertical())
     {
         return(Constant.Equals(p.X));
     }
     return(Slope * p.X + Constant == p.Y);
 }
Exemple #9
0
 private static Fraction CalculateSlope(PointFr p1, PointFr p2) // Slope = (p2.y - p1.y) / (p2.x - p1.x)
 {
     if (p1.X.Equals(p2.X))
     {
         return(Fraction.PositiveInfinity);
     }
     return((p2.Y - p1.Y) / (p2.X - p1.X));
 }
Exemple #10
0
 private static Fraction CalculateConstant(PointFr p, Fraction slope) // Constant = (-Slope* p.x) + p.y
 {
     if (slope.IsInfinity())                                          // IsVertical()
     {
         return(p.X);
     }
     return(-slope * p.X + p.Y);
 }
Exemple #11
0
        public LineFr(Fraction slope, PointFr p)
        {
            if (slope.IsNaN())
            {
                throw new ArgumentException("Slope (Nachylenie) nie może przyjmować wartości NaN");
            }

            Slope    = slope.IsInfinity() ? Fraction.PositiveInfinity : slope;
            Constant = CalculateConstant(p, slope);
        }
Exemple #12
0
        public LineFr(PointFr p1, PointFr p2)
        {
            if (p1.Equals(p2))
            {
                throw new ArgumentException("Nie można utworzyć linii dla dwóch takich samych punktów");
            }

            Slope    = CalculateSlope(p1, p2);
            Constant = CalculateConstant(p1, Slope);
        }
Exemple #13
0
        public LineFr PerpendicularLine(PointFr p)
        {
            if (IsHorizontal())
            {
                return(Vertical(p.X));
            }
            if (IsVertical())
            {
                return(Horizontal(p.Y));
            }
            var newSlope = Slope.Inverse();

            return(new LineFr(newSlope, p));
        }
        public static bool IsInsideConvexPolygon(PointFr point, PolygonFr polygon)
        {
            var vertices = polygon.Vertices.ToList(); // wierzchołki posortowane przeciwnie do wskazówek zegara względem wnętrza wielokąta
            var edges    = polygon.Edges().ToList();

            if (vertices.Count < 3)
            {
                throw new ArgumentException("Polygon can't have less than 3 vertices");
            }
            if (ConvexHullJarvis(vertices).Count != vertices.Count) // Powinno wystarczyć Count
            {
                throw new ArgumentException("Polygon must be convex");
            }

            if (vertices.Count == 3)
            {
                return(!edges.Any(point.IsRightOf));
            }

            var polyCenter = polygon.Center;

            if (polyCenter == null)
            {
                throw new Exception("Can't calculate center of the Polygon");
            }
            var mid = (PointFr)polyCenter;

            while (edges.Count > 1)
            {
                var testSegment = new LineSegmentFr(mid, edges[edges.Count / 2].StartPoint);
                if (point.IsRightOf(testSegment))
                {
                    edges = edges.Take(edges.Count / 2).ToList();
                }
                else if (point.IsLeftOf(testSegment))
                {
                    edges = edges.Skip(edges.Count / 2).ToList();
                }
                else if (testSegment.Contains(point))
                {
                    return(true);
                }
                else
                {
                    throw new Exception("Invalid calculations performed, it should never happen");
                }
            }

            return(!point.IsRightOf(edges.Single())); // czyli IsLeftOf + Contains, jeżeli jest we wierzchołku to będzie spełniony ostatni warunek z while'a
        }
        public void Insert(PointFr p, EventPoint ep) // Wstawia zdarzenie ep w punkcie p do bieżącej kolejki zdarzeń zdarzenia Right są dodawane na początku, pozostałe na końcu
        {
            var existing = new C5.LinkedList <EventPoint>();

            if (_events.Exists(kvp => kvp.Key.Equals(p)))
            {
                existing = _events[p];
                _events.Remove(p);
            }

            if (ep.Type == EventPointType.Right) // Zdarzenia 'Right' powinny byc na początku listy
            {
                existing.Insert(0, ep);
            }
            else
            {
                existing.Add(ep);
            }
            _events.Add(p, existing);
        }
        public bool Contains2(PointFr point)
        {
            if (Edges(true).Any(edge => edge.Contains(point)))
            {
                return(true);
            }

            int i, j = _vertices.Count - 1;
            var contains = false;

            for (i = 0; i < _vertices.Count; i++)
            {
                if ((_vertices[i].Y < point.Y && _vertices[j].Y >= point.Y || _vertices[j].Y < point.Y && _vertices[i].Y >= point.Y) && (_vertices[i].X <= point.X || _vertices[j].X <= point.X))
                {
                    contains ^= (_vertices[i].X + (point.Y - _vertices[i].Y) / (_vertices[j].Y - _vertices[i].Y) * (_vertices[j].X - _vertices[i].X) < point.X);
                }
                j = i;
            }

            return(contains);
        }
        public static bool IsInsidePolygon(PointFr point, PolygonFr polygon)
        {
            if (polygon.Edges(true).Any(edge => edge.Contains(point)))
            {
                return(true);
            }

            var vertices = polygon.Vertices.ToList();
            int i, j = vertices.Count - 1;
            var contains = false;

            for (i = 0; i < vertices.Count; i++)
            {
                if ((vertices[i].Y < point.Y && vertices[j].Y >= point.Y || vertices[j].Y < point.Y && vertices[i].Y >= point.Y) && (vertices[i].X <= point.X || vertices[j].X <= point.X))
                {
                    contains ^= (vertices[i].X + (point.Y - vertices[i].Y) / (vertices[j].Y - vertices[i].Y) * (vertices[j].X - vertices[i].X) < point.X);
                }
                j = i;
            }

            return(contains);
        }
Exemple #18
0
        public LineSegmentFr(PointFr start, PointFr end, bool LeftMostPointFirst = false)
        {
            if (start.Equals(end))
            {
                throw new ArgumentException("Nie można utworzyć odcinka, o dwóch takich samych punktach");
            }

            StartPoint = start;
            EndPoint   = end;

            MaxX = start.X > end.X ? start.X : end.X;
            MaxY = start.Y > end.Y ? start.Y : end.Y;
            MinX = start.X < end.X ? start.X : end.X;
            MinY = start.Y < end.Y ? start.Y : end.Y;

            Line = new LineFr(start, end);

            if (LeftMostPointFirst)
            {
                NormalizeEndPoints();
            }
        }
Exemple #19
0
 public bool Contains(PointFr p)
 {
     return(StartPoint.Equals(p) || EndPoint.Equals(p) ||
            (Line.Contains(p) && (p.X >= MinX) && p.X <= MaxX && p.Y >= MinY && p.Y <= MaxY));
 }
 public bool IsBelow(PointFr that)
 {
     return(_x < that.Y);
 }
 public bool IsAbove(PointFr that)
 {
     return(_x > that.Y);
 }
 public static Fraction Slope(PointFr from, PointFr to)
 {
     return((to.Y - from.Y) / (to.X - from.X));
 }
 public bool Contains(PointFr point)
 {
     return(GeometryCalculations.IsInsidePolygon(point, this));
 }
Exemple #24
0
 public bool HasEnding(PointFr p)
 {
     return(StartPoint.Equals(p) || EndPoint.Equals(p));
 }
Exemple #25
0
 public TriangleFr(PointFr vertex1, PointFr vertex2, PointFr vertex3)
 {
     Vertex1 = vertex1;
     Vertex2 = vertex2;
     Vertex3 = vertex3;
 }
Exemple #26
0
        public bool ContainsInCircumcircle(PointFr p, TriangleFr superTriangle = null)
        {
            var vertices = Vertices().OrderBy(pnt => pnt.X).ThenBy(pnt => pnt.Y).ToList();
            var p1       = vertices[0];
            var p2       = vertices[1];
            var p3       = vertices[2];

            if ((p1.Y - p2.Y).Abs() == 0 && (p2.Y - p3.Y).Abs() == 0) // < double.Epsilon
            {
                return(false);
            }

            if (superTriangle != null)
            {
                var sharedVertices = SharedVertices(superTriangle).ToList();
                var otherVertices  = vertices.Except(sharedVertices).ToList();
                if (sharedVertices.Count == 1)
                {
                    return(!new LineSegmentFr(p, sharedVertices.Single()).Intersects(new LineFr(otherVertices[0], otherVertices[1])));
                }
                if (sharedVertices.Count == 2)
                {
                    return(!new LineSegmentFr(p, sharedVertices.First()).Intersects(new LineFr(new LineFr(sharedVertices[0], sharedVertices[1]).Slope, otherVertices[0])));
                }
            }

            Fraction m1, m2;
            Fraction mx1, mx2;
            Fraction my1, my2;
            Fraction xc, yc;

            if ((p2.Y - p1.Y).Abs() == 0) // < double.Epsilon
            {
                m2  = -(p3.X - p2.X) / (p3.Y - p2.Y);
                mx2 = (p2.X + p3.X) * 0.5;
                my2 = (p2.Y + p3.Y) * 0.5; // Oblicz środek okręgu opisanego (xc, yc)

                xc = (p2.X + p1.X) * 0.5;
                yc = m2 * (xc - mx2) + my2;
            }
            else if ((p3.Y - p2.Y).Abs() == 0) // < double.Epsilon
            {
                m1  = -(p2.X - p1.X) / (p2.Y - p1.Y);
                mx1 = (p1.X + p2.X) * 0.5;
                my1 = (p1.Y + p2.Y) * 0.5;

                xc = (p3.X + p2.X) * 0.5; // Oblicz środek okręgu opisanego (xc,yc)
                yc = m1 * (xc - mx1) + my1;
            }
            else
            {
                m1  = -(p2.X - p1.X) / (p2.Y - p1.Y);
                m2  = -(p3.X - p2.X) / (p3.Y - p2.Y);
                mx1 = (p1.X + p2.X) * 0.5;
                mx2 = (p2.X + p3.X) * 0.5;
                my1 = (p1.Y + p2.Y) * 0.5;
                my2 = (p2.Y + p3.Y) * 0.5;

                xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2); // Oblicz środek okręgu opisanego (xc,yc)
                yc = m1 * (xc - mx1) + my1;
            }

            var dx   = p2.X - xc;
            var dy   = p2.Y - yc;
            var rsqr = dx * dx + dy * dy; //double r = Math.Sqrt(rsqr); // Promień okręgu opisanego

            dx = p.X - xc;
            dy = p.Y - yc;
            var drsqr = dx * dx + dy * dy;

            return(drsqr <= rsqr);
        }
 public static double Angle(PointFr p1, PointFr p2, PointFr refp)
 {
     return(Math.Atan2((p1.Y - refp.Y).ToDouble(), (p1.X - refp.X).ToDouble()) - Math.Atan2((p2.Y - refp.Y).ToDouble(), (p2.X - refp.X).ToDouble()) * 180 / Math.PI);
 }
 public bool IsLeftOf(PointFr that)
 {
     return(_x < that.X);
 }
 public static Fraction AngleFr(PointFr p1, PointFr p2, PointFr refp)
 {
     return(Fraction.Atan2(p1.Y - refp.Y, p1.X - refp.X) - Fraction.Atan2(p2.Y - refp.Y, p2.X - refp.X) * 180 / Math.PI);
 }
 public bool IsRightOf(PointFr that)
 {
     return(_x > that.X);
 }