0
class Point:
    'class that represents a point in the plane'

    def __init__(self, xcoord=0, ycoord=0):
        ''' (Point,number, number) -> None
        initialize point coordinates to (xcoord, ycoord)'''
        self.x = xcoord
        self.y = ycoord


class Rectangle:
    def __init__(self, bottom_left, top_right, colour):
        self.bottom_left = bottom_left
        self.top_right = top_right
        self.colour = colour
        self.length = self.top_right.x - self.bottom_left.x
        self.width = self.top_right.y - self.bottom_left.y

    def intersects(self, given_rectangle):
        if (self.bottom_left.x <= given_rectangle.bottom_left.x <= self.top_right.x) and (
                        self.bottom_left.y <= given_rectangle.bottom_left.y <= self.top_right.y):
            return True

        if (self.bottom_left.x <= given_rectangle.top_right.x <= self.top_right.x) and (
                        self.bottom_left.y <= given_rectangle.top_right.y <= self.top_right.y):
            return True

        return False

I am trying to see if two rectangles intersect. at least one point has to be in the rectangle for them to intersect. When I run my code in python shell:

r1=Rectangle(Point(1,1), Point(2,2), "blue")
r2=Rectangle(Point(2,2.5), Point(3,3), "blue")
r3=Rectangle(Point(1.5,0),Point(1.7,3),"red")
r1.intersects(r3)

This should print True however it prints False

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Garret Ulrich
  • 341
  • 1
  • 3
  • 12
  • What is your question? – DYZ Dec 02 '16 at 01:12
  • I am trying to see if two rectangles intersect my code is giving me an answer of False and the answer should be True I was wondering how can I fix it so that it returns true – Garret Ulrich Dec 02 '16 at 01:14
  • 1
    [Here's a language-independent solution](http://stackoverflow.com/questions/13390333/two-rectangles-intersection). – DYZ Dec 02 '16 at 01:20
  • I have only two points not four though – Garret Ulrich Dec 02 '16 at 01:26
  • 1
    Possible duplicate of [Algorithm to detect intersection of two rectangles?](http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles) – Prune Dec 02 '16 at 01:30
  • You can trivially generate the other two points; you use that property in the logic you posted. – Prune Dec 02 '16 at 01:31

0 Answers0