0

I'm trying to intersect two rectangles but I have no clue on how to do it. I've done two classes which are point and color and they work fine. Now I'm trying to get this intersection done. That's the rectangle class i've wrote

class Rectangle:
def __init__(self,Point,width,lenght,Color):
    self.x = Point.x
    self.y = Point.y
    self.x1 = self.x + lenght
    self.y1 = self.y + width
    self.r = Color.r
    self.g = Color.g
    self.b = Color.b
    self.w = width
    self.l = lenght

And as far as I've tried the intersection function it didn't work. I tried something like this

def inter(self,r):
    if   ((self.x > r.x1) or (self.x1 < r.x) or (self.y > r.y1) or (self.y1 < r.y)):
        return None
    else:
        return Rectangle(Point(x,y),w,h,Color.mix(self,r))

Where i defined the .mix function into the color class. The input of this function is the existent rectangle self and another one rectangle that is r

As an example i can put this

A is the rectangle that starts at (100,40), width 70, lenght 40 and color (200,200,200) and
  B is the rectangle that starts at (120,90), width 40, lenght 60 and color (100,100,100)
  you will get a rectangle starting at (120,90), width 20, lenght 20 and color (150,150,150)

Keep in mind that the axis that I'm working on are placed like this

0 - - - - - X

-

-

Y

And i have to do everything using only standar libraries

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
over96
  • 37
  • 5
  • What's wrong with your output? – Carcigenicate Nov 22 '16 at 14:54
  • Possible duplicate of [Determine if two rectangles overlap each other?](http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other) – OneCricketeer Nov 22 '16 at 14:56
  • 2
    @Carcigenicate It's wrong because it doesn't show the correct results , i get the right coords but i don't get the right width and lenght , i'm getting something like w:170 and l:50 if i remember well. cricket_007 It might be a duplicate , but it's not the same language. I think I've put the tag "python" on this one but I might be wrong since i'm not so handy with this website. – over96 Nov 22 '16 at 14:58
  • 1
    Agreed. Basically, the key is the conditionals. And which `x,y,w,h` values to use depend always on the "inner" rectangle (if one exists). It's not just "this rectangle"s values – OneCricketeer Nov 22 '16 at 14:59
  • Precisely, it's not a duplicate at all... the linked questions asks for a algorithm to determine if two rectangles intercept. – Haroldo_OK Nov 22 '16 at 14:59
  • The accepted answer there should be understood in any object oriented language pattern. Look at the if statements compared to yours – OneCricketeer Nov 22 '16 at 15:01
  • The questions are actually different. – Haroldo_OK Nov 22 '16 at 15:02
  • Okay. This then - finds the intersection using min and max. http://math.stackexchange.com/questions/99565/simplest-way-to-calculate-the-intersect-area-of-two-rectangles – OneCricketeer Nov 22 '16 at 15:03
  • This question is actually closer to http://stackoverflow.com/a/19753280/679240 – Haroldo_OK Nov 23 '16 at 10:48

1 Answers1

0

This should be good to get you started. Please note that I haven't thoroughly tested it, so there may be bugs.

def inter(self,r):
    x = max(self.x, r.x)
    y = max(self.y, r.y)
    x1 = min(self.x1, r.x1)
    y1 = min(self.y1, r.y1)

    if x >= x1 or y >= y1:
        return None
    else:
        return Rectangle(Point(x,y), x1-x, y1-y, Color.mix(self,r))

See also:

Community
  • 1
  • 1
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
  • It gives `none` instead of `(120,90), width 20, lenght 20 and color (150,150,150)` – over96 Nov 22 '16 at 19:14
  • Should have worked; try putting a breakpoint on the `if` statement to see what the values of `x`, `x1`, `y` and `y1` are. – Haroldo_OK Nov 23 '16 at 09:35
  • I've tried it and i have no clue why the results are like this `x=100 y=140 x1=50 y1=80` There should be `x=120 y=90 x1=140 y1=110` – over96 Nov 23 '16 at 17:30
  • That's really strange; if A and B start at `(100, 40)` and `(120, 40)`, respectively, then `x` should have been `120`, since `max(100, 120)` is `120`. I would recommend checking what are the values of `self.x`, `r.x`, `self.x1`, `r.x1`. – Haroldo_OK Nov 23 '16 at 17:44
  • 1
    I feel dumb AF , i've inverted width with length when i did the addiction for x1 and y1. Now it works like a charm – over96 Nov 23 '16 at 20:55