473,413 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Overlapping co-ordiantes of rectangles fail to print in python

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
1 1247
dwblas
626 Expert 512MB
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

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

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
2
by: Edward K. Ream | last post by:
From the documentation for the string module at: C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html letters: The concatenation of the strings lowercase and uppercase described below....
3
by: John Hunter | last post by:
I'm trying to understand why some code from a module I'm using is failing on 2.3 but working on 2.3. Here is the minimal example that replicates the problem class Results(object): __slots__ =...
4
by: Ricardo Bugalho | last post by:
Hello, I'm using Python 2.3.4 and I noticed that, when stdout is a terminal, the 'print' statement converts Unicode strings into the encoding defined by the locales instead of the one returned by...
10
by: Noah | last post by:
I would like to package my main script and all the modules it imports into a single script that will run just as the collection would. It should not need to package standard Python lib modules --...
1
by: Sullivan WxPyQtKinter | last post by:
title:Python CGI problem: correct result, but incorrect browser response. In one of my CGI program,named 'login.py', the script return a HEADER to web browser: Set-Cookie:...
3
by: jitender001001 | last post by:
hi all i m new to python and i have a problem of printing python variable using os.system() in bash !/usr/bin/env python var = "/home/anonymous" os.system("echo $var) it is not working..
3
by: cowboyrocks2009 | last post by:
Hi, I am trying to write a Java program to plot rectangles with different colors side by side non overlapping but unfortunately I am unable to do that as of now. Suppose I want to create 3...
1
by: chintan85 | last post by:
Urgent.. I want to print my results generated by python cgi script to different html frames. Can some body help me how to do this... Thanks in advance
5
by: Jeremy Rohrer | last post by:
What is the python code for printing this sentence? "The \t is the tab escape character."
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.