我对python3.6使用了anaconda/spider图形界面。我已经安装了planar 0.4库。我想要构建一个矩形,我想检查(0.5,0.5)点是否包含在多边形中。你能帮帮我吗?
import from planar Polygon
import from planar Point
poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])例外:
Traceback (most recent call last):
File "<ipython-input-39-0b13ff57af32>", line 1, in <module>
poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])
File "C:/Users/pama671/AppData/Local/Continuum/anaconda3/lib/site-
packages/planar/polygon.py", line 80, in __init__
if len(self) < 3:
File "C:\Users\pama671\AppData\Local\Continuum\anaconda3\lib\site-packages\planar\vector.py", line 490, in __len__
return len(self._vectors)
AttributeError: 'Polygon' object has no attribute '_vectors'发布于 2019-05-17 12:40:08
首先,您的import语句是错误的。
>>> from planar import Polygon, Point
>>>
>>> poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])
>>> 现在,先快速看一下下面的检查
>>> poly
Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
>>>
>>> for location in poly:
... print(location)
...
Vec2(0.00, 0.00)
Vec2(1.00, 0.00)
Vec2(1.00, 1.00)
Vec2(0.00, 1.00)
>>>
>>> for location in poly:
... print(dir(location))
... break
...
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__ifloordiv__', '__imul__', '__init__', '__init_subclass__', '__isub__', '__itruediv__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmul__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'almost_equals', 'angle', 'angle_to', 'clamped', 'cross', 'distance_to', 'dot', 'is_null', 'length', 'length2', 'lerp', 'normalized', 'perpendicular', 'polar', 'project', 'reflect', 'rotated', 'scaled_to', 'x', 'y']
>>>
>>> for location in poly:
... print(location, location.x, location.y)
...
Vec2(0.00, 0.00) 0.0 0.0
Vec2(1.00, 0.00) 1.0 0.0
Vec2(1.00, 1.00) 1.0 1.0
Vec2(0.00, 1.00) 0.0 1.0
>>>
>>> # Create a rectangle => of height 5(y-axis), width 10(x-axis) with bottom left corner at origin (0, 0)
...
>>> rectangle = Polygon([Point(0, 0),Point(10,0),Point(10, 5),Point(0, 5)])
>>> rectangle
Polygon([(0, 0), (10, 0), (10, 5), (0, 5)])
>>>
>>> rectangle[0]
Vec2(0, 0)
>>>
>>> rectangle[0].x
0.0
>>> rectangle[0].y
0.0
>>>
>>> rectangle[1].x, rectangle[1].y
(10.0, 0.0)
>>> 最后,您可以像这样尝试
>>> # Create a reactangle of your choice i.e. at least with 1 co-ordinate (0.5, 0.5)
...
>>> rect = Polygon([Point(0, 0),Point(0.5, 0),Point(0.5, 0.5),Point(0, 0.5)])
>>> rect
Polygon([(0, 0), (0.5, 0), (0.5, 0.5), (0, 0.5)])
>>>
>>> found = False
>>> for vertex in rect:
... if vertex.x == 0.5 and vertex.y == 0.5:
... found = True
... break
...
>>> if found:
... print("Found a vertex with x=0.5, y=0.5")
... else:
... print("Couldn't find any vertex with x=0.5, y=0.5")
...
Found a vertex with x=0.5, y=0.5
>>>
>>> https://stackoverflow.com/questions/56165714
复制相似问题