невозможно написать функцию, которая принимает 3 аргумента и возвращает координату прямоугольника (x, y, h, w) - PullRequest
1 голос
/ 02 апреля 2020

Я пытаюсь написать функцию, которая принимает следующие аргументы:

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 

Первая координата квадрата вычисляется таким образом. Обратите внимание, что размер = h = w

enter image description here

Как показано на диаграмме ниже, при увеличении n у нас должно быть больше квадратов (форма сетки) , функция должна возвращать x, y, w, h каждого квадрата в текстовом файле, как показано ниже (если n = 3):

Prediction_Region = x1, y1, w1, h1, x2, y2, w2, h2, x3, y3, w3, h3

Max_Temperture = 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)

enter image description here

1 Ответ

1 голос
/ 03 апреля 2020

Координаты самого левого верхнего угла прямоугольника, когда размер экрана равен (width, height), а центрированная сетка содержит n x n прямоугольников, равных

x0 = (width - w * n) / 2
y0 = (height - h * n) / 2

Чтобы получить все координаты прямоугольника, просто сдвиньте их на w, ч для каждого нового прямоугольника

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]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...