中级农民
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-7-5
- 最后登录
- 1970-1-1
|
- #!/usr/bin/python
- """
- Module to test function "_is_point_in_poly".
- """
- from unittest import TestCase
- class TestIsPointInPoly(TestCase):
- """
- Unit tests for function "_is_point_in_poly".
- """
- def _is_point_in_poly(self, x, y, poly):
- """
- This is the function we want to test.
- """
- n = len(poly)
- inside = False
- p1x, p1y = poly[0]
- for i in range(n+1):
- p2x, p2y = poly[i % n]
- if y > min(p1y, p2y):
- if y <= max(p1y, p2y):
- if x <= max(p1x, p2x):
- if p1y != p2y:
- xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
- if p1x == p2x or x <= xints:
- inside = not inside
- p1x, p1y = p2x, p2y
- return inside
- def test_when_poly_is_none(self):
- """
- When poly is None, any point should be
- judged as not inside the polygon.
- """
- x = 0
- y = 0
- poly = None
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_when_poly_is_a_point_and_different_from_the_input_point(self):
- """
- When poly is a point (not a real polygon),
- a different point should be judged as not inside the polygon.
- """
- x = 0
- y = 0
- poly = [(1, 1)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_when_poly_is_a_point_and_as_same_as_the_input_point(self):
- """
- When poly is a point (not a real polygon),
- the same point should be judged as not inside the polygon.
- """
- x = 1
- y = 1
- poly = [(1, 1)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_when_poly_is_a_line_and_the_input_point_is_not_on_the_line(self):
- """
- When poly is a line (not a real polygon),
- a point on the line should be judged as not inside the polygon.
- """
- x = 1
- y = 2
- poly = [(1, 1), (1, 3)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_input_point_on_poly_vertex(self):
- """
- When the input point is on the polygon's vertex,
- the point should be judged as not inside the polygon.
- """
- x = 2
- y = 4
- poly = [(2, 4), (6, 4), (6, 2), (2, 2)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_input_point_on_poly_edge(self):
- """
- When the input point is on the polygon's edge,
- the point should be judged as not inside the polygon.
- """
- x = 4
- y = 4
- poly = [(2, 4), (6, 4), (6, 2), (2, 2)]
- self.assertEqual(True, self._is_point_in_poly(x, y, poly))
- def test_input_point_inside_poly(self):
- """
- When the input point is really inside the polygon,
- the point should be judged as inside the polygon.
- """
- x = 3
- y = 3
- poly = [(2, 4), (6, 4), (6, 2), (2, 2)]
- self.assertEqual(True, self._is_point_in_poly(x, y, poly))
- def test_input_point_outside_poly(self):
- """
- When the input point is really outside the polygon,
- the point should be judged as outside the polygon.
- """
- x = 7
- y = 7
- poly = [(2, 4), (6, 4), (6, 2), (2, 2)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
- def test_input_point_outside_concave_polygon(self):
- """
- When the polygon is a concave polygon, and the input point
- is outside the polygon, but inside the concave part.
- the point should be judged as outside the polygon.
- """
- x = 4
- y = 5
- poly = [(2, 2), (2, 6), (3, 6), (3, 4), (5, 4), (5, 6), (6, 6), (6, 2)]
- self.assertEqual(False, self._is_point_in_poly(x, y, poly))
复制代码
仅供参考 |
|