public static int pointInPolygon(Point a, Polygon p) { int parity = 0; for (int i = 0; i < p.size(); i++, p.advance((int)Clock.CLOCKWISE)) { Edge e = p.edge(); int result = edgeType(a, e); if (result == (int)EdgePozition.TOUCHING) return (int)PointToFigurePozition.BOUNDARY; if (result == (int)EdgePozition.CROSSING) parity = 1 - parity; } return ((parity != 0) ? (int)PointToFigurePozition.INSIDE : (int)PointToFigurePozition.OUTSIDE); }
public void TestMethodX() { Vertex s = new Vertex(4, -7); s.insert(new Vertex(4, 0)); s.insert(new Vertex(9, 0)); s.insert(new Vertex(9, -2)); s.insert(new Vertex(16, -2)); s.insert(new Vertex(16, 0)); s.insert(new Vertex(21, 0)); s.insert(new Vertex(21, -7)); s.insert(new Vertex(16, -7)); s.insert(new Vertex(16, -5)); s.insert(new Vertex(9, -5)); s.insert(new Vertex(9, -7)); Polygon pol = new Polygon(s); Point p1 = new Point(17, 1); Point p2 = new Point(5, -10); Point p3 = new Point(9, -5); Point p4 = new Point(10, -4); int result = _Polygon.Action.pointInPolygon(p1, pol); Assert.AreEqual(result, (int)PointToFigurePozition.OUTSIDE); int result1 = _Polygon.Action.pointInPolygon(p2, pol); Assert.AreEqual(result1, (int)PointToFigurePozition.OUTSIDE); int result2 = _Polygon.Action.pointInPolygon(p3, pol); Assert.AreEqual(result2, (int)PointToFigurePozition.BOUNDARY); int result3 = _Polygon.Action.pointInPolygon(p4, pol); Assert.AreEqual(result3, (int)PointToFigurePozition.INSIDE); }
static void Main(string[] args) { int n; Console.Write("Число вершин многоугольника = "); n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите {0} Точек", n); int a, b; Console.WriteLine("Точка 1 "); Console.Write(" X = "); a = Convert.ToInt32(Console.ReadLine()); Console.Write(" Y = "); b = Convert.ToInt32(Console.ReadLine()); Vertex s = new Vertex(a, b); // заводим вершину и все другие будем добавлять к текущей. for (int i = 2; i <= n; i++) { Console.WriteLine("Точка {0} ", i); Console.Write(" X = "); a = Convert.ToInt32(Console.ReadLine()); Console.Write(" Y = "); b = Convert.ToInt32(Console.ReadLine()); s.insert(new Vertex(a, b)); // новые вершины добавляем к текущей } Polygon d = new Polygon(s); char stop = 'n'; do { Console.WriteLine("Введите точку для проверки принадлежности многоугольнику"); int x, y; Console.Write(" X = "); x = Convert.ToInt32(Console.ReadLine()); Console.Write(" Y = "); y = Convert.ToInt32(Console.ReadLine()); Console.Write(" Точка лежит : "); Point z = new Point(x, y); int result = Action.pointInPolygon(z, d); switch (result) { case (int)PointToFigurePozition.INSIDE: Console.WriteLine("Внутри многоугольника"); break; case (int)PointToFigurePozition.OUTSIDE: Console.WriteLine("Вне многоугольника"); break; case (int)PointToFigurePozition.BOUNDARY: Console.WriteLine("На границе многоугольника"); break; } Console.WriteLine("Остановиться? (y/n)"); stop = Convert.ToChar(Console.ReadLine()); } while (stop == 'n' || stop == 'N'); Console.ReadKey(); }
public Polygon(Polygon p) { _size = p._size; if (_size == 0) _v = null; else { _v = new Vertex(p.point()); for (int i = 1; i < _size; i++) { p.advance((int)Clock.CLOCKWISE); _v = _v.insert(new Vertex(p.point())); } p.advance((int)Clock.CLOCKWISE); _v = _v.cw(); } }