*后面的Python:TypeError:det()参数必须是iterable,而不是Line

*后面的Python:TypeError:det()参数必须是iterable,而不是Line,python,Python,我想得到两条线之间的交点,这是我的错误 我认为在这条直线中,d=(det(*self),det(*other))这就是产生这个错误的原因,我已经计算了斜率,并检查了它的垂直线是否平行,如果它们相等,所以这个错误不能让我完成我的问题,如果有人能认识到这个错误,它会让我更好地知道这是错误 import math import sys class Point: '''Creates a point on a coordinate plane with values x and y.'''

我想得到两条线之间的交点,这是我的错误

我认为在这条直线中,d=(det(*self),det(*other))这就是产生这个错误的原因,我已经计算了斜率,并检查了它的垂直线是否平行,如果它们相等,所以这个错误不能让我完成我的问题,如果有人能认识到这个错误,它会让我更好地知道这是错误

import math
import sys

class Point:
     '''Creates a point on a coordinate plane with values x and y.'''
     def __init__(self, x, y):
        '''Defines x and y variables'''
        if not isinstance(x, (float,int)):
            raise ValueError("the x must be an number", x)
        if not isinstance(y, (float,int)):
            raise ValueError("the x must be an number", y)
        self.__x = x
        self.__y = y  

     def __str__(self):
        return "Point(%.2f,%.2f)"%(self.__x, self.__y)

     def setX(self,x):
         self.__x=x   
     def setY(self,y):
         self.__y=y 

     def getX(self):
        return self.__x
     def getY(self):
        return self.__y

class Line:
    ''' calculate the y=ax+b '''
    def __init__(self, point_1, point_2):
        self.__point_1=point_1
        self.__point_2=point_2

    def __str__(self):# print the object
        if self.slope()==None:
            return 'x={0}'.format(self.__point_1.getX())
        else:
            return 'y={0}x+{1}'.format(self.slope(),self.y_intersect())

    ''' set point 1 and 2 '''
    def setP1(self,point_1):
        self.__point_1=point_1
    def setP2(self,point_2):
        self.__point_2=point_2

    ''' get point 1 and 2'''
    def getP1(self):
        return self.__point_1
    def getP2(self):
        return self.__point_2

    def is_vertical(self):
        ''' check if the line is vertical '''
        if (self.__point_1.getX() == self.__point_2.getX()):
           return True
        else:
           return False

    def slope(self):
        ''' the slope of the line '''
        if self.is_vertical() == False:
            return( (self.__point_1.getY())-(self.__point_2.getY()) )   /  ( (self.__point_1.getX())-(self.__point_2.getX()) )    
        else:
            return None

    def y_intersect(self):
        ''' get the intersecton with Y '''
        b=-(self.slope()) * self.__point_1.getX() + self.__point_1.getY()
        # y=mx-mx1+y1 --> b=-mx1+y1 this is the y intersection
        return b

    def parallel(self,other):
        if self.slope()==other.slope():
            ''' check if the two line are parellel '''
            return True

    def equals(self,other):
        ''' function check if two line are equals '''
        if self.slope()==other.slope() and self.y_intersect()==other.y_intersect():
            ''' check if two line are the same '''
            return True

    def line_intersection(self, other):
        xdiff = (self.__point_1.getX() - self.__point_2.getX(), other.__point_1.getX() - other.__point_2.getX())
        ydiff = (self.__point_1.getY() - self.__point_2.getY(), other.__point_1.getY() - other.__point_2.getY())
        def det(a, b):
            return a[0] * b[1] - a[1] * b[0]
        div = det(xdiff, ydiff)
        if div == 0:
            raise Exception('lines do not intersect')
        d = (det(*self), det(*other))
        x = det(d, xdiff) / div
        y = det(d, ydiff) / div
        return x, y 


def main():

    p1=Point(-2,0)
    p2=Point(6,4)
    l=Line(p1,p2)

    p11=Point(6,0)
    p22=Point(0,6)
    l1=Line(p11,p22)

    print(l.line_intersection(l1))

main()

为了在
对象上使用解包,您需要定义
\uuu iter\uuu
魔术函数。比如:

class Line:
    def __iter__(self):
        for p in (self.point1, self.point2):
            yield p

另一方面,你可能想用它来定义你的类。

你期望的
*self
是什么?(点1,点2)并且每个点都有x,在这个例子中,l.line\u交叉点(l1),l是self,l1是other。请用a来说明你的问题。
对象也不适用。请确保您了解
xdiff
表达式的工作原理,以及为什么它可以用于
det
我使用它,我不知道收益率是什么,我可以看到它的返回类型,现在我有了TypeError:“Point”对象不是subscribableYes,因为您的类
Point
不能用
[0]
[1]
进行索引。也许你的意思是
a.getX
?我试试这个,d=(det(self.\uu point\u 1,self.\uu point\u 2),det(other.\uu point\u 1,other.\uu point\u 2)),并给出了与iter给出的mealready try getX和gety相同的错误,它显示了AttributeError:“tuple”对象没有属性“getX”