How would I get this value? (Python)

1.9k views Asked by At

I have

class Point:

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def __str__(self):
        return "x=" + str(self.x) + ", y=" + str(self.y)


class Rectangle:

    def __init__(self, initP, initW, initH):

        self.__location = initP
        self.__width = initW
        self.__height = initH



    def getWidth(self):
        return self.__width

    def getHeight(self):
        return self.__height

    def getLocation(self):
        return self.__location

    #---------------------------------------------------------------
    #string

    def __str__(self):
        return "x=" + str(self.__location.x) + ", y=" + str(self.__location.y) +", Width=" + str(self.getWidth()) + ", Height=" +str(self.getHeight())

    def area(self):
        return self.getWidth() * self.getHeight()

    def calculatePerimeter(self):
        return self.getWidth()*2 +self.getHeight()*2

    def transpose(self):
        temp = self.__width
        self.__width = self.__height
        self.__height = temp

    def encloses(self, otherP):
        return ((self.getWidth() + self.getLocation().getX()) > otherP.getX()\
               and (self.getLocation().getX()) <otherP.getX() \
               and (self.getHeight() + self.getLocation().getY()) >otherP.getY()\
               and self.getLocation().getY() < otherP.getY())

    def computeDiagonal(self):

        d = (self.getWidth()**2 + self.getHeight()**2) ** 0.5
        return d

    def detectCollision(firstRectangle, secondRectangle):
        print(firstRectangle.getWidth())
        print(secondRectangle)


 first = Rectangle(Point(1,0), 4, 3)
 second = Rectangle(Point(4,0), 4, 3)
 Rectangle.detectCollision(first, second)

I am trying to detect a collision. I'm a bit stuck. (detectCollision) I am having trouble getting the value from the point class to the rectangle class. Does anybody have any idea?

The function detectCollision is wrong. I was testing and I could get the width, and the height with getHeight() but I could not get the values inside Point.

1

There are 1 answers

2
abarnert On

I am having trouble getting the value from the point class to the rectangle class.

I think you need to read through a good tutorial on classes. Maybe the chapter in the official tutorial, or maybe a third-party tutorial. StackOverflow is not a good place to learn basic concepts.

You don't actually want to get a value from the point class, you want to get the value from a particular point instance. After all, there are lots of points in the world, and each one has different x and y values, and you're trying to check if some particular point has collided with the rectangle.

How do you know which instance? You take one as a parameter. And then you can access that object's members, methods, etc., just like you do with a string or any other object.

class Rectangle(object):
    # existing stuff
    def collision_check(self, point):
        return (self.left <= point.getX() <= self.right and
                self.top <= point.getY() <= self.bottom)

That's it.

Except that you probably don't want getX and getY methods in the first place; better to just do point.x and point.y.

Also, I've obviously had to make some assumptions about how you defined Rectangle (left/top/bottom/right? left/right/width/top? topleftpoint/bottomrightpoint?) and about what you mean by "collision" (hitting the edge of the rectangle, or the interior of the rectangle?), etc., since you didn't explain any of that. But hopefully you can adapt this to whatever your actual design is.


So, how do you use this? You just pass a point as an argument to the method, same as you do with, say, len:

>>> rect = Rectangle(10, 10, 20, 20)
>>> point1 = Point(5, 5)
>>> rect.collision_check(point1)
False
>>> point2 = Point(15, 15)
>>> rect.collision_check(point2)
True

Now that you've shown us more of your code, it looks like you're trying to collision-check two rectangles—and, more importantly, your problem is that your rectangle uses a Point as its top-left origin, and you don't know how to access the coordinations of that.

From your description, "I was testing and I could get the width, and the height with getHeight() but I could not get the values inside Point", you seem to still be missing the key issue here. You don't want to get the values inside Point. Point is a class—a factory for creating actual point objects. You want to get the values inside one of those actual point objects, the one you've stored in a rectangle object's __location and made available through a getLocation method. (As I already explained, you should get rid of those getter methods and just have a location attribute, but let's forget that for now.)

So, the way you get the particular point object you're interested in is to call getLocation() on the rectangle, and then the way you get the x and y values for that particular point object is to call its getX and getY methods. So, here's an example of using all those methods:

firstLocation = firstRectangle.getLocation()
firstLeft = firstLocation.getX()

Or you can combine those calls into one expression:

firstLeft = firstRectangle.getLocation().getX()

So, you can do something like this:

def detectCollision(firstRectangle, secondRectangle):
    firstLeft = firstRectangle.getLocation().getX()
    firstRight = firstLeft + firstRectangle.getWidth()
    # similar code for top and bottom, and for second rectangle
    return ((firstLeft <= secondLeft <= firstRight or
             firstLeft <= secondRight <= firstRight) and
            (firstTop <= secondTop <= firstBottom or
             firstTop <= secondBottom <= firstBottom))