我目前正在用Python编写一个程序来为射影几何建模,而投影点的同余函数看起来相当讨厌。在
(对于任何感兴趣的人,如果两个投影点都位于穿过原点的一条直线上,则它们是全等的。)
class Point(object):
def __init__(self, a, b, c):
self.coords = [ a, b, c ]
def congruent(self, other):
ratio = 0
for i in range(3):
if self.coords[i] != 0 and other.coords[i] != 0:
if ratio is 0:
ratio = other.coords[i] / self.coords[i]
elif ratio != other.coords[i] / self.coords[i]:
return False
elif self.coords[i] != 0 or other.coords[i] != 0:
return False
return True
我是Python新手,但我知道通常有一种“Python”方法来完成所有事情。考虑到这一点,我该如何使它更具可读性?在
可能使用}。在Python中,任何非零值都会传递
if self.coords[i]
代替if self.coords[i] != 0
(类似的例子也是如此),用if not ratio
代替{if
子句,因此不需要检查它是否为非零,它是自动的。在可能有点像Python。。。虽然它真正的变化是如何迭代列表(它的行数相同)
这个怎么样:
zip
很方便)。在c1
非零且c2
为零时发生,因此这是一个失败。在注意:如果不使用Python3,应该在文件顶部添加
from __future__ import division
,这样就不会得到整数坐标值的错误结果。在编辑:根据@JoranBeasley添加了短路和epsilon比较。在
相关问题 更多 >
编程相关推荐