我正在根据文件中的信息创建矩形对象。我需要遍历文件中的每一行并制作各种矩形,然后将它们相互比较并找出它们是否相交。单个矩形的值存储在它们自己的行上,如:A,10,12,3,4,然后是B,3,5,62,2,依此类推。
这就是我获取该信息的方式(我的班级中有一个打印出值的方法),但我无法弄清楚如何为它们分配变量名,这样我就可以做一些像A这样的事情了。 .intersection(B),因为现在我不得不说 sl [0] .intersection(sl [0])这只会让他们相互完美地相互交叉,因为很明显它们是确切的相同的矩形。
def opens(f, cls):
try:
inpFile = open(f)
except IOError, e:
print "Failed to open %s for reading: %s" % ("inputRectangles.txt", e)
i = 0
for line in inpFile:
i+= 1
sl = line.split(",")
sl[1] = int(sl[1])
sl[2] = int(sl[2])
sl[3] = int(sl[3])
sl[4] = int(sl[4])
sl[0] = cls(sl[1], sl[2], sl[3], sl[4])
print sl[0]
#MAIN
opens("inputRectangles.txt", Rectangle)
到目前为止,我已经尝试将每个类放入它自己的列表中,并使用随机字母分配来命名它们,但我的潜在解决方案都没有奏效。问题的一部分是我试图以一种无论有多少矩形都有效的方式编写代码。任何帮助是极大的赞赏。
EDIT 这是我的交集方法:
def intersection(self, other):
#find unkown values of corners
#far right x (self)
maxXS = self.__left + self.__width
#top y (self)
maxYS = self.__bottom + self.__height
#far right x (other)
maxXO = other.__left + other.__width
#top y (other)
maxYO = other.__bottom + other.__height
#find width(wx) and height(hy)
wx = min(maxXS, maxXO) - max(self.__left, other.__left)
hy = min(maxYS, maxYO) - max(self.__bottom, other.__bottom)
#if width less than 0
if wx < 0:
wx = 0
#ENDIF
#if height less than 0
if hy < 0:
hy = 0
#ENDIF
#if min left point is self
if min(self.__left, other.__left) == self.__left:
biggie = other
smalls = self
else:
biggie = self
smalls = other
#ENDIF
#find lower left point of new rect
miniLY = max(smalls.__left, biggie.__left)
miniLX = max(smalls.__bottom, biggie.__bottom)
#if width and height are 0
#then minimum points 0
if wx == 0 and hy == 0:
miniLX = 0
miniLY = 0
#ENDIF
#return new rect with calculated values
return Rectangle(miniLX, miniLY, wx, hy)
#ENDMETHOD
答案 0 :(得分:0)
您应该在列表中收集矩形:
rects = []
for line in inpFile:
sl = line.split(",")
rects.append(cls(*map(int, sl[1:5])))
# now that you have them all, you can do
rects[0].intersection(rects[1])
答案 1 :(得分:0)
如果我理解正确,你只需要在你的功能中创建一个矩形列表,最后返回,然后你可以在有不同的矩形后调用交叉方法。
def opens(f, cls):
try:
inpFile = open(f)
except IOError, e:
print "Failed to open %s for reading: %s" % ("inputRectangles.txt", e)
list_rectangles = []
for line in inpFile:
sl = line.split(",")
sl[1] = int(sl[1])
sl[2] = int(sl[2])
sl[3] = int(sl[3])
sl[4] = int(sl[4])
rectangle = (cls(sl[1], sl[2], sl[3], sl[4])
list_rectangles.append(rectangle)
return list_rectangles
#MAIN
list_rectangles = opens("inputRectangles.txt", Rectangle)
rectangle_1 = list_rectangles[0]
rectangle_2 = list_rectangles[1]
rectangle_1 .intersection(rectangle_2)
答案 2 :(得分:0)
您没有指定如何收集交叉矩形,因此这里有一个解决方案,可以生成相交的矩形对。
from Rectangle import Rectangle
def readfile(filename):
try:
with open(filename) as f:
for line in f:
yield line
except IOError as e:
print "Failed to open %s for reading: %s" % ("inputRectangles.txt", e)
def find_intersections(rectangles):
rectangle_list = list(rectangles)
r1 = rectangle_list.pop(0)
while rectangle_list:
for r2 in rectangle_list:
if r1.intersection(r2):
yield r1, r2
r1 = rectangle_list.pop(0)
lines = readfile("inputRectangles.txt")
rectangles = [Rectangle(line.split(',')) for line in lines]
intersecting_rectangles = list(find_intersections(rectangles))