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: -
from __future__ import division
-
from collections import namedtuple
-
from itertools import combinations
-
-
Point = namedtuple( 'Point', ['x','y'] )
-
-
class Rectangle:
-
def __init__( self, coordinates ):
-
self.min = Point( coordinates[0], coordinates[1] )
-
self.max = Point( coordinates[2], coordinates[3] )
-
-
@property
-
def center( self ):
-
return Point( (self.max.x + self.min.x)/2, (self.max.y + self.min.y)/2 )
-
-
@property
-
def area( self ):
-
return (self.max.x - self.min.x) * (self.max.y - self.min.y)
-
-
def intersect( self, otherRect ):
-
x_vals = sorted([self.max.x, self.min.x, otherRect.max.x, otherRect.min.x])
-
y_vals = sorted([self.max.y, self.min.y, otherRect.max.y, otherRect.min.y])
-
-
possibleIntersections = []
-
intersections = []
-
-
for i in range(3):
-
for j in range(3):
-
possibleIntersections.append( Rectangle([x_vals[i], y_vals[j], x_vals[i+1], y_vals[j+1]]) )
-
-
for r in possibleIntersections:
-
if self.contains( r.center ) and otherRect.contains( r.center ) and r.area > 0:
-
intersections.append( r )
-
-
return intersections
-
-
def contains( self, point ):
-
return self.min.x <= point.x and point.x <= self.max.x and self.min.y <= point.y and point.y <= self.max.y
-
-
def __repr__( self ):
-
return '[{0},{1}]'.format( self.min, self.max )
-
-
def readInputconvert( filename ):
-
rects = []
-
with open(filename,'r') as f:
-
count = int(f.readline().rstrip());
-
-
for _ in range( count ):
-
rects.append( Rectangle( map( float, f.readline().rstrip().split(' ') ) ) )
-
-
return rects
-
-
rectangles = readInputconvert( 'input.txt' ) # read input
-
-
sign = -1
-
area = sum( map( lambda x: x.area, rectangles) )
-
-
for i in range(2,len(rectangles)+1):
-
for rects in combinations( rectangles, i ):
-
intersections = [rects[0]]
-
rects = rects[1:]
-
for rectangle in rects:
-
newintersections = []
-
for otherR in intersections:
-
newintersections.extend( rectangle.intersect(otherR) )
-
-
intersections = newintersections
-
print intersections
-
-
#intersectingArea = sum( map( lambda x: x.area, intersections ) )
-
#rea = area + (sign * intersectingArea)
-
-
sign = sign*-1
-
-
def contains( self, point ):
-
return self.min.x <= point.x and point.x <= self.max.x and self.min.y <= point.y and point.y <= self.max.y
-
-
def __repr__( self ):
-
return '[{0},{1}]'.format( self.min, self.max )
-
-
def readInputconvert( filename ):
-
rects = []
-
with open(filename,'r') as f:
-
count = int(f.readline().rstrip());
-
-
for _ in range( count ):
-
rects.append( Rectangle( map( float, f.readline().rstrip().split(' ') ) ) )
-
-
return rects
-
-
rectangles = readInputconvert( 'input.txt' ) # read input
-
-
sign = -1
-
area = sum( map( lambda x: x.area, rectangles) )
-
-
for i in range(2,len(rectangles)+1):
-
for rects in combinations( rectangles, i ):
-
intersections = [rects[0]]
-
rects = rects[1:]
-
for rectangle in rects:
-
newintersections = []
-
for otherR in intersections:
-
newintersections.extend( rectangle.intersect(otherR) )
-
-
intersections = newintersections
-
print intersections
-
-
#intersectingArea = sum( map( lambda x: x.area, intersections ) )
-
#rea = area + (sign * intersectingArea)
-
-
sign = sign*-1
Where I need to change the code to output all overlapping rectangles and its co ordinates?
1 1247
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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__ =...
|
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...
|
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 --...
|
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:...
|
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..
|
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...
|
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
|
by: Jeremy Rohrer |
last post by:
What is the python code for printing this sentence? "The \t is the tab escape character."
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |