Frage

I'm trying to write a class circle and this is my first experience with OOP, to do so I wrote a Point class also, but when I run the contains and intersect function I get this error message:

AttributeError: 'Point' object has no attribute 'center'

This is the code:

import math


class Point():
    """ Holds data on a point (x,y) in the plane """

    def __init__(self, x=0, y=0):
        assert isinstance(x,(int, float)) and isinstance(y,(int, float)) 
        self.x = x
        self.y = y

    def __repr__(self):
        return "Point(" + str(self.x) + "," + str(self.y) + ")"


    #getters
    def x_val(self):
        return self.x

    def y_val(self):
        return self.y

#end of class Point 




class Circle():
    """ Holds data on a circle in the plane """

    def __init__(self,*args):
        if len(args)==2:
            if isinstance(args[0],Point) and isinstance(args[1],(float,int)):
                assert args[1]>0
                self.center= args[0]
                self.radius= args[1]

        if len(args)==3:
            assert args[2]>0
            self.a=args[0]
            self.b=args[1]
            self.center= Point(self.a,self.b)
            self.radius= args[2]


    def __repr__(self):
        return "Circle centered at " + str(self.center) + " with radius " + str(self.radius)



    def contains(self,check): #ERROR!!!

        if isinstance(check,(Point)):
           if math.sqrt(((Point.x_val(self.center))-(Point.x_val(check.center)))**2 + ((Point.y_val(check.center))-(Point.y_val(check.center)))**2) <= self.radius:
               return True
        if isinstance(check,Circle): 
            test= math.sqrt(((Point.x_val(self.center))-(Point.x_val(check.center)))**2 + ((Point.y_val(self.center))-(Point.y_val(check.center))**2))
            if test < (abs((self.radius)-(check.radius))):
                return True

        else:
            return False

    def intersect(self,other):  #ERROR!!!
        check= math.sqrt(((Point.x_val(self))-(Point.x_val(other)))**2 + ((Point.y_val(self))-(Point.y_val(other)))**2)
        if check >(self.radius+other.radius):
            return False
        if check < (self.radius+other.radius):
            return True


    def draw(self,mat):
        for i in mat:
            for j in i:
                if Circle.contains(i,j):
                    mat[i[j]]==1
War es hilfreich?

Lösung

instances of the class Point do not have an attribute center, in these three lines of code you assume they do:

if isinstance(check,(Point)):
           if math.sqrt(((Point.x_val(self.center))-(Point.x_val(check.center)))**2 + ((Point.y_val(check.center))-(Point.y_val(check.center)))**2) <= self.radius:
               return True

If check is an instance of Point don't refer to check.center.

BTW, please give the full stack trace when asking questions like these, not just the last line. With the full trace I could have just looked at the line with the error instead of having to look through the rest of the code.

What you probably wanted was just to access the x value of the Circle's center and the x of the check Point.

if isinstance(check,(Point)):
    if math.sqrt((self.center.x-check.x)**2 + (self.center.y-check.y)**2) <= self.radius:
           return True

Note that using a method to access an attribute is considered not to be good style in Python. If you want to access the value of an attribute just access the attribute directly. In some languages this can problems if later you need to change how an object is implemented but in Python you can change an attribute into a property at any time without breaking code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top