Python 无法编写包含3个参数并返回矩形坐标(x、y、h、w)的函数
我正在尝试编写一个函数,该函数采用以下参数:Python 无法编写包含3个参数并返回矩形坐标(x、y、h、w)的函数,python,function,oop,math,Python,Function,Oop,Math,我正在尝试编写一个函数,该函数采用以下参数: def get_square(n, size, ebt): ''' n is the number of square size is the size of the rectangle, in our case size=w=h ebt stand for elevation body temperature, it will a constant value ''' return
def get_square(n, size, ebt):
'''
n is the number of square
size is the size of the rectangle, in our case size=w=h
ebt stand for elevation body temperature, it will a constant value
'''
return
正方形的第一个坐标是这样计算的。请注意,size=h=w
如下图所示,当我们增加n时,我们应该有更多的正方形(类似网格的形状),函数应该在如下的txt文件中返回每个正方形的x、y、w、h(如果n=3):
预测区域=x1,y1,w1,h1,x2,y2,w2,h2,x3,y3,w3,h3
最高温度=ebt,ebt,ebt
我尝试了多次实现,但似乎遗漏了一些东西
这是一个代码示例,我正在尝试从终端调用此函数
import os, sys
import re
import math
import argparse
import numpy as np
FILE_NAME = 'output.txt'
def square(n, size, ebt):
xs = 320
ys = 256
nx = int(xs)/2 - size/2
ny = int(ys)/2 - size/2
# the main thing should be here, but I am not able to implement it due to few error
return
# this is to write the output of square function in a text file.
def writetoendofline(lines, line_no, append_txt):
lines[line_no] = lines[line_no].replace('\n', '') + append_txt + '\n'
# open the file
with open(FILE_NAME, 'r') as txtfile:
lines = txtfile.readlines()
# this function take the line number and append a value at the end of it
# ultimately the output of square function should be entered here
writetoendofline(lines, 1, '23 ')
# writetoendofline(lines, 0, nx)
# write to the file
with open(FILE_NAME, 'w') as txtfile:
txtfile.writelines(lines)
# write to the file
txtfile.close()
if __name__ == '__main__':
n = sys.argv[1]
size = sys.argv[2]
ebt = sys.argv[3]
square(n, size, ebt)
当屏幕尺寸为
(宽度、高度)
且居中网格包含n x n
矩形时,最左侧顶部矩形角的坐标为
x0 = (width - w * n) / 2
y0 = (height - h * n) / 2
要获得所有矩形坐标,只需将每个新矩形移动w,h
def square(width, height, n, w, h):
x0 = (width - n * w) // 2
y0 = (height - n * h) //2
coords = [[x0 + ix * w, y0 + iy * h] for iy in range(n) for ix in range(n)]
return coords
print(square(320, 240, 3, 32, 24))
[[112, 84], [144, 84], [176, 84],
[112, 108], [144, 108], [176, 108],
[112, 132], [144, 132], [176, 132]]