首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用planar.Polygon

如何使用planar.Polygon
EN

Stack Overflow用户
提问于 2019-05-16 17:51:00
回答 1查看 217关注 0票数 1

我对python3.6使用了anaconda/spider图形界面。我已经安装了planar 0.4库。我想要构建一个矩形,我想检查(0.5,0.5)点是否包含在多边形中。你能帮帮我吗?

代码语言:javascript
复制
import from planar Polygon
import from planar Point
poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])

例外:

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2019-05-17 12:40:08

首先,您的import语句是错误的。

代码语言:javascript
复制
>>> from planar import Polygon, Point
>>> 
>>> poly = Polygon([Point(0, 0),Point(1,0),Point(1, 1),Point(0, 1)])
>>> 

现在,先快速看一下下面的检查

代码语言:javascript
复制
>>> 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)
>>> 

最后,您可以像这样尝试

代码语言:javascript
复制
>>> # 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
>>> 
>>> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56165714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档