public Wall CreateWall(Polygon2D area)
        {
            var w = new Wall();

            w.Area = area;

            return w;
        }
        public Corridor CreateCorrdior(Polygon2D area, double floorHeight, double ceilingHeight)
        {
            var c = new Corridor();

            c.Area = area;
            c.FloorHeight = floorHeight;
            c.CeilingHeight = ceilingHeight;

            return c;
        }
        public Platform CreatePlatform(Polygon2D area, double ceilingHeight, double floorHeight)
        {
            var c = new Platform();

            c.Area = area;
            c.FloorHeight = floorHeight;
            c.CeilingHeight = ceilingHeight;

            return c;
        }
        // find the type of a specific vertex in a polygon, either Concave or Convex.
        public Polygon2DType VertexType(int vertexNo)
        {
            Polygon2D triangle;
            if (vertexNo == 0)
            {
                triangle =
                    new Polygon2D(new List<Vector2D>
                                      {ClosedPoints[ClosedPoints.Count - 2], ClosedPoints[0], ClosedPoints[1]});
                    // the polygon is always closed so the last point is the same as the first
            }
            else
            {
                triangle =
                    new Polygon2D(new List<Vector2D>
                                      {ClosedPoints[vertexNo - 1], ClosedPoints[vertexNo], ClosedPoints[vertexNo + 1]});
            }

            if (Math.Sign(triangle.Area) == Math.Sign(Area))
                return Polygon2DType.Convex;
            else
                return Polygon2DType.Concave;
        }
 private static int FindEar(Polygon2D poly)
 {
     for (int i = 0; i < poly.ClosedPoints.Count - 2; i++)
     {
         if (poly.VertexType(i + 1) == Polygon2DType.Convex)
         {
             // get the three points of the triangle we are about to test
             Vector2D a = poly.ClosedPoints[i];
             Vector2D b = poly.ClosedPoints[i + 1];
             Vector2D c = poly.ClosedPoints[i + 2];
             bool foundAPointInTheTriangle = false;
                 // see if any of the other points in the polygon are in this triangle
             for (int j = 0; j < poly.Points.Count; j++)
                 // don't check the last point, which is a duplicate of the first
             {
                 if (j != i && j != i + 1 && j != i + 2 && Intersections.PointInTriangle(poly.ClosedPoints[j], a, b, c))
                     foundAPointInTheTriangle = true;
             }
             if (!foundAPointInTheTriangle)
                 // the middle point of this triangle is convex and none of the other points in the polygon are in this triangle, so it is an ear
                 return i + 1; // EXITING HERE!
         }
     }
     throw new ApplicationException("Improperly formed polygon");
 }
 public List<Polygon2D> Triangulate()
 {
     Polygon2D poly = new Polygon2D(this.Points);
     //poly.Points.Reverse();
     var triangles = new List<Polygon2D>(); // accumulate the triangles here
     // keep clipping ears off of poly until only one triangle remains
     while (poly.Points.Count > 3) // if only 3 points are left, we have the final triangle
     {
         int midvertex = FindEar(poly); // find the middle vertex of the next "ear"
         triangles.Add(new Polygon2D(new List<Vector2D>
                           {
                               poly.ClosedPoints[midvertex - 1], poly.ClosedPoints[midvertex],
                               poly.ClosedPoints[midvertex + 1]
                           }));
         // create a new polygon that clips off the ear; i.e., all vertices but midvertex
         var newPts = new List<Vector2D>(poly.Points);
         newPts.RemoveAt(midvertex); // clip off the ear
         poly = new Polygon2D(newPts); // poly now has one less point
     }
     // only a single triangle remains, so add it to the triangle list
     triangles.Add(poly);
     return triangles;
 }
 public static bool PolygonPolygonIntersection(Polygon2D a, Polygon2D b)
 {
     return
         (from line2D in a.Lines
          from line in b.Lines
          where LineLineIntersection(line2D, line) != null
          select line2D).Any();
 }
 public static List<Vector2D> PolygonLineIntersection(Polygon2D polygon, Line2D intersectLine)
 {
     return
         polygon.Lines.Select(line => LineLineIntersection(line, intersectLine)).Where(point => point != null).
             ToList();
 }
        // this is sloppy as f**k... but should work... rewrite later
        public static bool IsPointInPolygon(Polygon2D polygon, Vector2D point)
        {
            //double minX = polygon.Points[0].X;
            //double minY = polygon.Points[0].Y;
            //double maxX = polygon.Points[0].X;
            //double maxY = polygon.Points[0].Y;

            //for (int i = 1; i < polygon.Points.Count; i++)
            //{
            //    if (polygon.Points[i].X < minX)
            //        minX = polygon.Points[i].X;
            //    if (polygon.Points[i].Y < minY)
            //        minY = polygon.Points[i].Y;
            //    if (polygon.Points[i].X > maxX)
            //        maxX = polygon.Points[i].X;
            //    if (polygon.Points[i].Y > maxY)
            //        maxY = polygon.Points[i].Y;
            //}

            //var r = new Random();
            //var p = new Vector2D();
            //while (true)
            //{
            //    var rx = r.NextDouble() * double.MaxValue;
            //    var ry = r.NextDouble() * double.MaxValue;

            //    if ((minX <= rx && rx <= maxX) || (minY <= ry && ry <= maxY)) continue;
            //    p.X = rx;
            //    p.Y = ry;
            //    break;
            //}
            //var q = PolygonLineIntersection(polygon, new Line2D(p, point));
            //return q.Count % 2 != 0;

            List<Polygon2D> tris = polygon.Triangulate();

            foreach (Polygon2D polygon2D in tris)
            {
                if (PointInTriangle(point, polygon2D.Points[0], polygon2D.Points[1], polygon2D.Points[2]))
                    return true;
            }
            return false;
        }
        //    private static int FindLineCircleIntersections(double cx, double cy, double radius,
        //Vector2D point1, Vector2D point2)
        //    {
        //        double dx, dy, A, B, C, det;
        //        dx = point2.X - point1.X;
        //        dy = point2.Y - point1.Y;
        //        A = dx * dx + dy * dy;
        //        B = 2 * (dx * (point1.X - cx) + dy * (point1.Y - cy));
        //        C = (point1.X - cx) * (point1.X - cx) + (point1.Y - cy) * (point1.Y - cy) - radius * radius;
        //        det = B * B - 4 * A * C;
        //        if ((A <= 0.0000001) || (det < 0))
        //        {
        //            return 0;
        //        }
        //        else if (det == 0)
        //        {
        //            return 1;
        //        }
        //        else
        //        {
        //            return 2;
        //        }
        //    }
        //    public static bool CirclePolygonIntersecton(Circle2D c, Polygon2D p)
        //    {
        //        foreach (var l in p.Lines)
        //        {
        //            var count = FindLineCircleIntersections(c.Point.X, c.Point.Y, c.Radius, l.A, l.B);
        //            if (count == 2)
        //                return true;
        //            else if (count == 1 && IsPointInPolygon(p, c.Point))
        //                return true;
        //        }
        //        if (IsPointInPolygon(p, c.Point))
        //            return true;
        //        return false;
        //    }
        public static bool CirclePolygonIntersection(Circle2D c, Polygon2D p)
        {
            if (IsPointInPolygon(p, c.Point))
                return true;

            foreach (var l in p.Lines)
            {
                if (CircleLineIntersection(c, l))
                    return true;
            }

            return false;
        }