gpt4 book ai didi

python - 给定 4 个顶点代表一个四边形,如何将其分为 N 部分?

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:45 31 4
gpt4 key购买 nike

我正在尝试,给定代表四边形的两个维度(宽度和高度),将该四边形划分为 N 个部分,其中每个部分在比例上尽可能与其他部分相似。

例如,想象一张纸。它由4个点A、B、C、D组成现在考虑这张纸的尺寸为 800 x 800 和点:

A: {0, 0}
B: {0, 800}
C: {800, 800}
D: {800, 0}

绘图将为您提供 4 个点或 3 条线(带线图)。添加额外的点 E: {0, 0} 以关闭单元格。

我已经设法以编程方式对 N 个单元格执行此操作。不幸的是,由于某种原因,当我设置 N=4 时,我得到了 8 个单元格......我很困惑,我无法解决它,所以我正在寻找三件事:

  • A) 如何改进此代码以使其更具可读性?
  • B)如何改进此代码以使其更加 Python 化?
  • C) 你猜对了。如何解决 N=4 问题?

完整代码:

import matplotlib.pyplot as plt


class QuadPartitioner:

@staticmethod
def get_factors(number):
'''
Takes a number and returns a list of factors
:param number: The number for which to find the factors
:return: a list of factors for the given number
'''
facts = []
for i in range(1, number + 1):
if number % i == 0:
facts.append(i)
return facts

@staticmethod
def get_partitions(N, quad_width, quad_height):

'''
Given a width and height, partition the area into N parts
:param N: The number of partitions to generate
:param quad_width: The width of the quadrilateral
:param quad_height: The height of the quadrilateral
:return: a list of a list of cells where each cell is defined as a list of 5 verticies
'''

# We reverse only because my brain feels more comfortable looking at a grid in this way
factors = list(reversed(QuadPartitioner.get_factors(N)))

# We need to find the middle of the factors so that we get cells
# with as close to equal width and heights as possible
split = int(len(factors)/2)
factors = factors[split-1:split+1]

# The width and height of an individual cell
cell_width = quad_width / factors[0]
cell_height = quad_height / factors[1]

number_of_cells_in_a_row = factors[0]
rows = factors[1]
row_of_cells = []

# We build just a single row of cells
# then for each additional row, we just duplicate this row and offset the cells
for n in range(0, number_of_cells_in_a_row):
cell_points = []

for i in range(0, 5):

cell_y = 0
cell_x = n * cell_width

if i == 2 or i == 3:
cell_x = n * cell_width + cell_width

if i == 1 or i == 2:
cell_y = cell_height

cell_points.append((cell_x, cell_y))

row_of_cells.append(cell_points)

rows_of_cells = [row_of_cells]

# With that 1 row of cells constructed, we can simply duplicate it and offset it
# by the height of a cell multiplied by the row number
for index in range(1, rows):
new_row_of_cells = [[ (point[0],point[1]+cell_height*index) for point in square] for square in row_of_cells]
rows_of_cells.append(new_row_of_cells)

return rows_of_cells


if __name__ == "__main__":

QP = QuadPartitioner()
partitions = QP.get_partitions(4, 800,800)

for row_of_cells in partitions:
for cell in row_of_cells:
x, y = zip(*cell)
plt.plot(x, y, marker='o')

plt.show()

最佳答案

我通过更改解决了这个问题:

split = int(len(factors)/2)
factors = factors[split-1:split+1]

至:

factor_count = len(factors)

if factor_count % 2 == 0:
split = int(factor_count/2)
factors = factors[split-1:split+1]
else:
factors = []
split = ceil(factor_count/2)
factors.append(split)
factors.append(split)

关于python - 给定 4 个顶点代表一个四边形,如何将其分为 N 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797107/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com