Python 3.x-计算绘图并计算坐标系中可能的正方形

所以,到目前为止,我有一个像这样的坐标系:

.    .    .
.    .    .

(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)这​​样,

(x,y)    (x2,y)
(x,y2)   (x2,y2)

我们知道矩形的面积大于0,所以x != x2 && y != y2

我们知道(在示例坐标系中)三个可绘制矩形坐标是:

1,0    1,0
  0,1    1,1
2,1,0    2,0
  1,1    2,1
3,1

现在,菱形不在剧中。

那么,如何在Python中获得解决方案(我希望解决方案是可绘制矩形的坐标。)?有人可以给我发送代码或说明吗?我在互联网上找不到任何东西。当然,它必须与其中包含更多坐标的坐标系统一起工作。

我只在寻找Python代码。

leikai945 回答:Python 3.x-计算绘图并计算坐标系中可能的正方形

在给定坐标系中一种非常简单的还很贪婪的计数和输出矩形数量的方法如下。

首先,定义一个函数来检查四个点是否形成矩形:

def is_rectangle(a,b,c,d):  
    # sort coordinates
    a,d = sorted([a,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,d) for (a,d) in itertools.combinations(coordinates,4)])

最后,一种输出这些矩形坐标的方法是:

def get_rectangles(coordinates):

    # return each rectangle
    return [[a,d] for (a,4) if is_rectangle(a,d)]

您的示例所得到的是:

coordinates = [(0,0),(0,1),2),(1,2)]

number_rectangles(coordinates)
# > 3

get_rectangles(coordinates)
# > [[(0,1)],# >  [(0,2)],2)]]
,

@Tibbles给出的answer向您展示了如何生成矩形。 计数(不生成它们!)很简单,您只需要一些基本的数学运算即可:

(.*-)(\d+)(\.html)

其中rect_count = w * h * (w - 1) * (h - 1) / 4 w的最大值,而x是`y的最大值。

本文链接:https://www.f2er.com/3056726.html

大家都在问