Пример #1
0
        public static int RayCasting(Polygon polygon, Point point)
        {
            int   count    = 0;
            Point maxPoint = new Point {
                x = 2000 + point.x, y = 1000 + point.y
            };
            Line line1 = new Line {
                Point1 = point, Point2 = maxPoint
            };
            Line line2 = new Line();

            string[] intersections = { "Proper", "Improper" };
            foreach (Point p in polygon.Vertex)
            {
                if (polygon.Vertex.Find(p).Next != null)
                {
                    line2.Point1 = p;
                    line2.Point2 = polygon.Vertex.Find(p).Next.Value;
                }
                else
                {
                    line2.Point1 = p;
                    line2.Point2 = polygon.Vertex.First();
                }
                if (!String.IsNullOrEmpty(Lab2.CheckIntersection(line1, line2)) && !String.IsNullOrEmpty(Lab2.CheckIntersection(line2, line1)))
                {
                    count++;
                }
            }

            return(count);
        }
Пример #2
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);
        }
Пример #3
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();
        }
Пример #4
0
        static void Main(string[] args)
        {
            try
            {
                // Lab1.getData();
                //  Lab2.getData();
                Lab2.getDataInt();

                // Lab3.getData();
            }

            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Пример #5
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);
        }
Пример #6
0
        public static string PointLineClassification(Line Line1, Point Point3)
        {
            if (Lab2.calculateTriangleArea(Line1.Point1, Line1.Point2, Point3) != 0.0)
            {
                return("Point is not collinear");
            }
            else
            {
                if (Line1.Point1.y == Line1.Point2.y && Line1.Point1.x != Line1.Point2.x)
                {
                    if (Line1.Point2.x == Point3.x)
                    {
                        return("end");
                    }

                    else if (Line1.Point1.x == Point3.x)
                    {
                        return("start");
                    }

                    else if (Line1.Point1.x < Point3.x && Line1.Point2.x > Point3.x)
                    {
                        return("between");
                    }

                    else if (Line1.Point1.x > Point3.x)
                    {
                        return("behind");
                    }

                    else if (Line1.Point2.x < Point3.x)
                    {
                        return("beyond");
                    }

                    else
                    {
                        return("error");
                    }
                }
                else if (Line1.Point1.x == Line1.Point2.x && Line1.Point1.y != Line1.Point2.y)
                {
                    if (Line1.Point2.y == Point3.y)
                    {
                        return("end");
                    }

                    else if (Line1.Point1.y == Point3.y)
                    {
                        return("start");
                    }

                    else if (Line1.Point1.y < Point3.y && Line1.Point2.y > Point3.y)
                    {
                        return("between");
                    }

                    else if (Line1.Point1.y > Point3.y)
                    {
                        return("behind");
                    }

                    else if (Line1.Point2.y < Point3.y)
                    {
                        return("beyond");
                    }

                    else
                    {
                        return("error");
                    }
                }
                else
                {
                    if (Line1.Point2.x == Point3.x && Line1.Point2.y == Point3.y)
                    {
                        return("end");
                    }

                    else if (Line1.Point1.x == Point3.x && Line1.Point1.y == Point3.y)
                    {
                        return("start");
                    }

                    else if (((Line1.Point1.x < Point3.x && Line1.Point2.x > Point3.x) && (Line1.Point1.y < Point3.y && Line1.Point2.y > Point3.y)))
                    {
                        return("between");
                    }

                    else if (Line1.Point1.x > Point3.x && Line1.Point1.y > Point3.y)
                    {
                        return("behind");
                    }

                    else if (Line1.Point2.x < Point3.x && Line1.Point2.y < Point3.y)
                    {
                        return("beyond");
                    }

                    else
                    {
                        return("error");
                    }
                }
            }
        }
Пример #7
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();
        }