如何使用平面多边形

2024-05-26 17:42:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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'

Tags: infromimportlenlocallineusersappdata
1条回答
网友
1楼 · 发布于 2024-05-26 17:42:09

第一件事是你的进口声明是错误的。你知道吗

>>> from planar import Polygon, Point
>>> 
>>> poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])
>>> 

Now, first have a quick look at the below inspection

>>> 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)
>>> 

Finally, you can try like this

>>> # 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
>>> 
>>> 

相关问题 更多 >

    热门问题