Python中不支持的Operand定义了运算符

时间:2020-07-16 23:56:04

标签: python operator-overloading overloading typeerror operands

我正在研究几个基本的数学课程,以二维方式执行线冲突。

这是我的python代码:

collisions.py

import math

class Vector:
    def __init__(self, x, y):
        self._x = x
        self._y = y
        
    def __str__(self):
        return '(%d, %d)' %(self._x, self._y)
        
    def __add__(self, other):
        return Vector(self._x + other._x, self._y + other._y)
        
    def __sub__(self, other):
        return Vector(self._x - other._x, self._y - other._y)
    
    def __mul__(self, val):
        return Vector(self._x * val, self._y * val)
        
    # same as dot product
    #def __mul__ (self, other):
    #    return self.dot(other)
    
    # same as cross product
    #def __pow__(self, other):
    #    return self.cross(other)
    
    def x(self):
        return self._x
    
    def y(self):
        return self._y
    
    def cross(self, other):
        return (self._x * other._y - self._y * other._x)
    
    def dot(self, other):
        return (self._x * other._x + self._y * other._y)        
    
class Line:
    def __init__(self, p1, p2):
       self._p1 = p1
       self._p2 = p2
       
    def slope(self):
        if self._p1.x() == self._p2.x():
            if self._p1.y() == self._p2.y():
                return str("both points coincide") 
            else:
                if self._p1.y() < self._p2.y():
                    return float('inf')
                else:
                    return float('-inf')
        self._m = float((self._p2.y() - self._p1.y()) / (self._p2.x() - self._p1.x()))  
        return self._m
    
    def p1(self):
        return self._p1
        
    def p2(self):
        return self._p2
        
def LineIntersect(l1, l2):
    a = l1.p1()
    b = l1.p2()
    c = l2.p1()
    d = l2.p2()

    #r = l1.p2() - l1.p1()
    r = b - a
    #s = l2.p2() - l2.p1()
    s = d - c
    
    d = r.cross(s)
    #u = ((l2.p2.x() - l1.p1.x()) * r.y() - (l2.p2.y() - l1.p1.y()) * r.x()) / d
    u = ((c.x() - a.x()) * r.y() - (c.y() - a.y()) * r.x()) / d
    #t = ((l2.p2.x() - l1.p1.x()) * s.y() - (l2.p2.y() - l1.p1.y()) * s.x()) / d
    t = ((c.x() - a.x()) * s.y() - (c.y() - a.y()) * s.x()) / d
    
    if (0 <= u and u <= 1 and 0 <= t and t <= 1):
        return (a + t * r)
    else:
        return False;
        
l1 = Line(Vector(0, 0), Vector(3, 3))
l2 = Line(Vector(3, 0), Vector(0, 3))

intersected = LineIntersect(l1, l2)
print(intersected)

当我通过python collisions.py使用Python 3.7的解释器通过Windows 7命令提示符运行此命令时,

它正在生成此错误消息:

D:\Dev\Languages\Python\Projects\Test>python collisions.py
Traceback (most recent call last):
  File "collisions.py", line 88, in <module>
    intersected = LineIntersect(l1, l2)
  File "collisions.py", line 81, in LineIntersect
    return (a + t * r)
TypeError: unsupported operand type(s) for *: 'float' and 'Vector'

我更喜欢C语言,尤其是C ++。我已经学习Python大约一年了,我开始习惯它,但是类似这样的事情,我理解错误消息,我只是不知道如何解决它们。我已经习惯了像Python这样的解释型语言上的强类型化和编译型语言。

在这里,我假设我在Vector类中的运算符已重载,从而能够将标量乘以矢量。。。但是,Python却对我大喊,它是不受支持的操作数。我该怎么做才能解决这个问题?作为旁注,如果您发现任何其他潜在的错误或代码问题,请随时在评论中提及它们。我还在学习这种语言!

0 个答案:

没有答案
相关问题