重叠的矩形坐标无法在python中打印

我正在使用以下代码,在这些代码中,我试图输出重叠矩形的坐标。然而,代码无法输出坐标。我正在定制以下代码,这是输入
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
代码是这样的:

选择 | 换行 | 行号
  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.  
选择 | 换行 | 行号
  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

我需要在哪里更改代码以输出所有重叠的矩形及其坐标?

# 回答1


是什么
X1、y1、x2、y2或x1、x2、y1、y2(我假设这些是矩形的对角点)。如果您要将此处发布的数据发送到类,那么您绝对应该打印坐标[0]、[1]等,因为它不是您认为的那样。我们也可以假设这些矩形平行于x轴和y轴,或者是一些成角度的。

标签: python

添加新评论