All*_*tor 3 python coordinates cartesian-coordinates
我有一组基本的笛卡尔坐标,我想用Python操作它们.例如,我有以下框(坐标显示为角):
0,4 --- 4,4
0,0 --- 4,0
我希望能够找到一个以(0,2)开头并转到(4,2)的行.我是否需要将每个坐标分解为单独的X和Y值然后使用基本数学,或者有没有办法将坐标作为(x,y)对处理?例如,我想说:
New_Row_Start_Coordinate = (0,2) + (0,0)
New_Row_End_Coordinate = New_Row_Start_Coordinate + (0,4)
Run Code Online (Sandbox Code Playgroud)
听起来你正在寻找一个Point类.这是一个简单的:
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __str__(self):
return "{}, {}".format(self.x, self.y)
def __neg__(self):
return Point(-self.x, -self.y)
def __add__(self, point):
return Point(self.x+point.x, self.y+point.y)
def __sub__(self, point):
return self + -point
Run Code Online (Sandbox Code Playgroud)
然后你可以做这样的事情:
>>> p1 = Point(1,1)
>>> p2 = Point(3,4)
>>> print p1 + p2
4, 5
Run Code Online (Sandbox Code Playgroud)
您可以根据需要添加任意数量的其他操作.有关您可以实现的所有方法的列表,请参阅Python文档.
| 归档时间: |
|
| 查看次数: |
5989 次 |
| 最近记录: |