如何使用两组点绘制矩形 (X1 = 10,Y1 = 20)和(X2 = 100,Y2 = 200),使用dc.DrawRectangle()方法。它只需要一组点,但我需要它使用两组点绘制矩形?
答案 0 :(得分:1)
pt1 = (10, 20)
pt2 = (100, 200)
# calculate top left corner coords, width, height
min_x = min(pt1[0], pt2[0]) # left
min_y = min(pt1[1], pt2[1]) # top
width = max(pt1[0], pt2[0])-min_x
height = max(pt1[1], pt2[1])-min_y
# draw rectangle
dc.DrawRectangle(min_x, min_y, width, height)
答案 1 :(得分:1)
如果您知道(X1,Y1)是左上角,(X2,Y2)是右下角,那么您可以使用:
dc.DrawRectangle(X1, Y1, X2-X1, Y2-Y1)
更一般地说,
dc.DrawRectangle(min(X1,X2), min(Y1,Y2), abs(X1-X2), abs(Y1-Y2))
应始终有效(至少对于与x和y轴对齐的矩形)。