By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Overlapping co-ordiantes of rectangles fail to print in python

P: 1
I am working with following code in which I am trying to output co ordinates of overlapping rectangles.. However the code fails to output the co ordinates. I am customizing the following code This is the input

1.6 1.2 7.9 3.1
1.2 1.6 3.4 7.2
2.6 11.6 6.8 14.0
9.6 1.2 11.4 7.5
9.6 1.7 14.1 2.8

This is the code:
Expand|Select|Wrap|Line Numbers
  1. from __future__ import division
  2. from collections import namedtuple
  3. from itertools import combinations
  4.  
  5. Point = namedtuple( 'Point', ['x','y'] )
  6.  
  7. class Rectangle:
  8.     def __init__( self, coordinates ):
  9.         self.min = Point( coordinates[0], coordinates[1] )
  10.         self.max = Point( coordinates[2], coordinates[3] )
  11.  
  12.     @property
  13.     def center( self ):
  14.         return Point( (self.max.x + self.min.x)/2, (self.max.y + self.min.y)/2 )
  15.  
  16.     @property
  17.     def area( self ):
  18.         return (self.max.x - self.min.x) * (self.max.y - self.min.y)
  19.  
  20.     def intersect( self, otherRect ):
  21.         x_vals = sorted([self.max.x, self.min.x, otherRect.max.x, otherRect.min.x])
  22.         y_vals = sorted([self.max.y, self.min.y, otherRect.max.y, otherRect.min.y])
  23.  
  24.         possibleIntersections = []
  25.         intersections = []
  26.  
  27.         for i in range(3):
  28.             for j in range(3):
  29.                 possibleIntersections.append( Rectangle([x_vals[i], y_vals[j], x_vals[i+1], y_vals[j+1]]) )
  30.  
  31.         for r in possibleIntersections:
  32.             if self.contains( r.center ) and otherRect.contains( r.center ) and r.area > 0:
  33.                 intersections.append( r )
  34.  
  35.         return intersections
  36.  
  37.     def contains( self, point ):
  38.         return self.min.x <= point.x and point.x <= self.max.x and self.min.y <= point.y and point.y <= self.max.y
  39.  
  40.     def __repr__( self ):
  41.         return '[{0},{1}]'.format( self.min, self.max )
  42.  
  43. def readInputconvert( filename ):
  44.     rects = []
  45.     with open(filename,'r') as f:
  46.         count = int(f.readline().rstrip());
  47.  
  48.         for _ in range( count ):
  49.             rects.append( Rectangle( map( float, f.readline().rstrip().split(' ') ) ) )
  50.  
  51.     return rects
  52.  
  53. rectangles = readInputconvert( 'input.txt' ) # read input
  54.  
  55. sign = -1
  56. area = sum( map( lambda x: x.area, rectangles) )
  57.  
  58. for i in range(2,len(rectangles)+1):
  59.     for rects in combinations( rectangles, i ):
  60.         intersections = [rects[0]]
  61.         rects = rects[1:]
  62.         for rectangle in rects:
  63.             newintersections = []
  64.             for otherR in intersections:
  65.                 newintersections.extend( rectangle.intersect(otherR) )
  66.  
  67.             intersections = newintersections
  68.             print intersections
  69.  
  70.         #intersectingArea = sum( map( lambda x: x.area, intersections ) )
  71.         #rea = area + (sign * intersectingArea)
  72.  
  73.     sign = sign*-1
  74.  

Expand|Select|Wrap|Line Numbers
  1.     def contains( self, point ):
  2.         return self.min.x <= point.x and point.x <= self.max.x and self.min.y <= point.y and point.y <= self.max.y
  3.  
  4.     def __repr__( self ):
  5.         return '[{0},{1}]'.format( self.min, self.max )
  6.  
  7. def readInputconvert( filename ):
  8.     rects = []
  9.     with open(filename,'r') as f:
  10.         count = int(f.readline().rstrip());
  11.  
  12.         for _ in range( count ):
  13.             rects.append( Rectangle( map( float, f.readline().rstrip().split(' ') ) ) )
  14.  
  15.     return rects
  16.  
  17. rectangles = readInputconvert( 'input.txt' ) # read input
  18.  
  19. sign = -1
  20. area = sum( map( lambda x: x.area, rectangles) )
  21.  
  22. for i in range(2,len(rectangles)+1):
  23.     for rects in combinations( rectangles, i ):
  24.         intersections = [rects[0]]
  25.         rects = rects[1:]
  26.         for rectangle in rects:
  27.             newintersections = []
  28.             for otherR in intersections:
  29.                 newintersections.extend( rectangle.intersect(otherR) )
  30.  
  31.             intersections = newintersections
  32.             print intersections
  33.  
  34.         #intersectingArea = sum( map( lambda x: x.area, intersections ) )
  35.         #rea = area + (sign * intersectingArea)
  36.  
  37.     sign = sign*-1
Where I need to change the code to output all overlapping rectangles and its co ordinates?
Nov 17 '16 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 621
What is
1.6 1.2 7.9 3.1
1.2 1.6 3.4 7.2
2.6 11.6 6.8 14.0
9.6 1.2 11.4 7.5
9.6 1.7 14.1 2.8
x1, y1, x2, y2 or x1, x2, y1, y2 (and I assume these are opposite corners of the rectangle). If you are sending the data as posted here, to the class, then you should definitely print coordinates[0], [1], etc. as it is not what you think it is. Also can we assume that the rectangles are parallel to the x and y axis or are some at angles.
Nov 17 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.