Python3.x-计数绘图&&;在坐标系中计算可能的正方形
现在,我有一个坐标系,比如:Python3.x-计数绘图&&;在坐标系中计算可能的正方形,python,python-3.x,drawing,coordinates,coordinate-systems,Python,Python 3.x,Drawing,Coordinates,Coordinate Systems,现在,我有一个坐标系,比如: . . . . . . (x=0, y=0) (x=1, y=0) (x=2, y=0) (x=0, y=1) (x=1, y=1) (x=2, y=1) 我们知道有3个可能的矩形可以绘制 我们知道每个元素的坐标。(左上角为0,0(x,y))(右下角为2,1(x,y)) 我们知道一个矩形有4个点,现在,它是:(x,y),(x2,y),(x,y2),(x2,y2)所以 我们知道矩形的面积大于0,所以x!=x2&&y!
. . .
. . .
(x=0, y=0) (x=1, y=0) (x=2, y=0)
(x=0, y=1) (x=1, y=1) (x=2, y=1)
我们知道有3个可能的矩形可以绘制
我们知道每个元素的坐标。(左上角为0,0(x,y))(右下角为2,1(x,y))
我们知道一个矩形有4个点,现在,它是:(x,y),(x2,y),(x,y2),(x2,y2)所以
我们知道矩形的面积大于0,所以x!=x2&&y!=y2
我们知道(在示例坐标系中)三个可绘制矩形坐标为:
1,
0,0 1,0
0,1 1,1
2,
1,0 2,0
1,1 2,1
3,
0,0 2,0
0,1 2,1
现在,菱形不在剧中
那么,如何在Python中获得解决方案(我希望解决方案是可绘制矩形的坐标)?有人能给我发个代码或说明吗?我在网上找不到。当然,它必须使用坐标系,坐标系中有更多的坐标
我只寻找Python代码。下面是一种非常简单但贪婪的方法来计算和输出给定坐标系中的矩形数 首先,定义一个函数来检查四个点是否形成一个矩形:
def is_rectangle(a, b, c, d):
# sort coordinates
a, b, c, d = sorted([a, b, c, d])
# check rectangle
return a[0] == b[0] and c[0] == d[0] and a[1] == c[1] and b[1] == d[1]
然后,使用函数计算坐标系所有可能的四点组合中的矩形:
def number_rectangles(coordinates):
# output the number of rectangles
return sum([is_rectangle(a, b, c, d) for (a, b, c, d) in itertools.combinations(coordinates, 4)])
最后,输出这些矩形坐标的方法是:
def get_rectangles(coordinates):
# return each rectangle
return [[a, b, c, d] for (a, b, c, d) in itertools.combinations(coordinates, 4) if is_rectangle(a, b, c, d)]
您将从您的示例中得到:
coordinates = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
number_rectangles(coordinates)
# > 3
get_rectangles(coordinates)
# > [[(0, 0), (0, 1), (1, 0), (1, 1)],
# > [(0, 0), (0, 2), (1, 0), (1, 2)],
# > [(0, 1), (0, 2), (1, 1), (1, 2)]]
下面是一种计算和输出给定坐标系中矩形数量的非常简单但贪婪的方法 首先,定义一个函数来检查四个点是否形成一个矩形:
def is_rectangle(a, b, c, d):
# sort coordinates
a, b, c, d = sorted([a, b, c, d])
# check rectangle
return a[0] == b[0] and c[0] == d[0] and a[1] == c[1] and b[1] == d[1]
然后,使用函数计算坐标系所有可能的四点组合中的矩形:
def number_rectangles(coordinates):
# output the number of rectangles
return sum([is_rectangle(a, b, c, d) for (a, b, c, d) in itertools.combinations(coordinates, 4)])
最后,输出这些矩形坐标的方法是:
def get_rectangles(coordinates):
# return each rectangle
return [[a, b, c, d] for (a, b, c, d) in itertools.combinations(coordinates, 4) if is_rectangle(a, b, c, d)]
您将从您的示例中得到:
coordinates = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
number_rectangles(coordinates)
# > 3
get_rectangles(coordinates)
# > [[(0, 0), (0, 1), (1, 0), (1, 1)],
# > [(0, 0), (0, 2), (1, 0), (1, 2)],
# > [(0, 1), (0, 2), (1, 1), (1, 2)]]
@Tibbles给出的示例演示了如何生成矩形。计算它们(不生成它们!!)很容易,您只需要一些基本的数学知识:
rect_count = w * h * (w - 1) * (h - 1) / 4
其中,w
是x
的最大值,h
是'y'的最大值。由@Tibbles给出的说明如何生成矩形。计算它们(不生成它们!!)很容易,您只需要一些基本的数学知识:
rect_count = w * h * (w - 1) * (h - 1) / 4
其中,
w
是x
的最大值,h
是'y'的最大值。只是澄清一下:你是指所有矩形还是所有正方形?在您提供的示例中,我看到了3个可能的矩形,其中只有2个是正方形。是的,您是对的。我指的是矩形!:)澄清一下:你是指所有的矩形还是所有的正方形?在您提供的示例中,我看到了3个可能的矩形,其中只有2个是正方形。是的,您是对的。我指的是矩形!:)你不需要编程来解决这个问题,只需要数学…这是真的,但就像@Marci专门要求Python代码一样…你不需要编程来解决这个问题,只需要数学…这是真的,但就像@Marci专门要求Python代码一样…谢谢!现在我明白了背后的数学!“感谢您的反馈!声誉低于15的人所投的票会被记录下来,但不会改变公开显示的帖子分数。”我只需要再多投10票!:)谢谢现在我明白了背后的数学!“感谢您的反馈!声誉低于15的人所投的票会被记录下来,但不会改变公开显示的帖子分数。”我只需要再多投10票!:)