Beispiel #1
0
        public static bool CheckPointInclusion(Polygon polygon, Point point)
        {
            bool result = true;

            foreach (Point p in polygon.Vertex)
            {
                if (p != polygon.Vertex.Last())
                {
                    string turn = Lab2.turnTest(point, p, polygon.Vertex.Find(p).Next.Value);
                    if (turn != "left")
                    {
                        result = false;
                        break;
                    }
                }
                else
                {
                    string turn = Lab2.turnTest(point, p, polygon.Vertex.First());
                    if (turn != "left")
                    {
                        result = false;
                    }
                }
            }

            return(result);
        }
Beispiel #2
0
        public static void GrahamScan(List <Point> points)
        {
            Point            MinPoint = points.OrderBy(m => m.y).First();
            List <SortPoint> sorted   = (from p in points
                                         select new SortPoint
            {
                Point = p,
                Angle = Math.Atan2(p.y - MinPoint.y, p.x - MinPoint.x)
            }).OrderBy(m => m.Angle).ToList();

            Stack <Point> Graham = new Stack <Point>();

            Graham.Push(MinPoint);
            foreach (var item in sorted)
            {
                if (item != sorted.First())
                {
                    if (Graham.Count() >= 2)
                    {
                        string turn = Lab2.turnTest(Graham.Skip(1).First(), Graham.Peek(), item.Point);
                        if (turn.ToUpper() == "RIGHT")
                        {
                            Graham.Pop();
                            foreach (var stitem in Graham.ToList())
                            {
                                if (Lab2.turnTest(Graham.Skip(1).First(), Graham.Peek(), item.Point).ToUpper() == "RIGHT")
                                {
                                    Graham.Pop();
                                }
                                else
                                {
                                    Graham.Push(item.Point);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Graham.Push(item.Point);
                        }
                    }
                    else
                    {
                        Graham.Push(item.Point);
                    }
                }
            }

            Console.WriteLine("Graham Scan Points /n");
            foreach (var item in Graham)
            {
                Console.WriteLine("Point:|" + item.x + "," + item.y + "\n");
            }
            Console.ReadKey();
        }
Beispiel #3
0
        public static bool CheckConvex(Polygon p)
        {
            bool result = true;

            foreach (Point point in p.Vertex)
            {
                var nextPoint = p.Vertex.Find(point).Next;
                if (nextPoint != null)
                {
                    if (point == p.Vertex.Find(p.Vertex.Last()).Previous.Value)
                    {
                        string turn = Lab2.turnTest(point, p.Vertex.Last(), p.Vertex.First());
                        if (turn != "left")
                        {
                            result = false;
                            break;
                        }
                    }
                    else
                    {
                        string turn = Lab2.turnTest(point, p.Vertex.Find(point).Next.Value, p.Vertex.Find(point).Next.Next.Value);
                        if (turn != "left")
                        {
                            result = false;
                            break;
                        }
                    }
                }
                else
                {
                    string turn = Lab2.turnTest(p.Vertex.Last(), p.Vertex.First(), p.Vertex.Find(p.Vertex.First()).Next.Value);
                    if (turn != "left")
                    {
                        result = false;
                    }
                    break;
                }
            }

            return(result);
        }
Beispiel #4
0
        public static void ExtremeEdges(List <Point> Points)
        {
            string[]    turns    = { "right", "left", "collinear" };
            string      turn     = "";
            List <Line> extEdges = new List <Line>();

            Console.WriteLine("Points/n");
            foreach (var item in Points)
            {
                Console.WriteLine(item.x + "," + item.y + "\n");
            }

            for (int i = 0; i < Points.Count; i++)
            {
                for (int j = 0; j < Points.Count; j++)
                {
                    if (i != j)
                    {
                        for (int k = 0; k < Points.Count; k++)
                        {
                            if (j != k)
                            {
                                if (k == 0)
                                {
                                    turn = Lab2.turnTest(Points[i], Points[j], Points[k]);
                                    continue;
                                }
                                else
                                {
                                    string currentTurn = Lab2.turnTest(Points[i], Points[j], Points[k]);
                                    Console.WriteLine("Line {0},{1}|{2},{3} Query|{4},{5}|Result: {6}",
                                                      Points[i].x, Points[i].y, Points[j].x, Points[j].y, Points[k].x,
                                                      Points[k].y, Lab2.turnTest(Points[i], Points[j], Points[k])
                                                      );
                                    if (currentTurn == "collinear" && k < (Points.Count - 1))
                                    {
                                        continue;
                                    }
                                    else if (currentTurn != "collinear" && turn == "collinear" && k < (Points.Count - 1))
                                    {
                                        turn = currentTurn;
                                        continue;
                                    }
                                    else if (turn != "collinear" && currentTurn != "collinear" && turn == currentTurn && k < (Points.Count - 1))
                                    {
                                        turn = currentTurn;
                                        continue;
                                    }
                                    else if (turn != "collinear" && currentTurn != "collinear" && turn != currentTurn && k < (Points.Count - 1))
                                    {
                                        break;
                                    }
                                    else if ((currentTurn == "collinear" || turn == currentTurn) && k == (Points.Count - 1))
                                    {
                                        List <Point> linePoint = Sort(new List <Point> {
                                            Points[i], Points[j]
                                        });
                                        Line line = new Line {
                                            Point1 = linePoint[0], Point2 = linePoint[1]
                                        };
                                        if (!extEdges.Any(m => m.Point1 == line.Point1 && m.Point2 == line.Point2))
                                        {
                                            extEdges.Add(line);
                                        }
                                        //turn = currentTurn;
                                        //continue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Extreme Edges/n");
            foreach (var item in extEdges)
            {
                Console.WriteLine("Line:|" + item.Point1.x + "," + item.Point1.y + "\t" +
                                  item.Point2.x + "," + item.Point2.y + "\n");
            }
            Console.ReadKey();
        }