Reputation: 341
I am trying to write a function that takes the following arguments:
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
The first coordinate of the square is calculated that way. Note that size = h = w
As shown in the below diagram, as we increase n we should have more squares (grid-like shape), the function should returns x, y, w, h of each square in a txt file like below (if n =3):
Prediction_Region = x1, y1, w1, h1, x2, y2, w2, h2, x3, y3, w3, h3
Max_Temperture = ebt, ebt, ebt
I tried multiple attempts to implement but seems I am missing something.
that a code sample, I am trying to call this function from the terminal
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)
Upvotes: 1
Views: 182
Reputation: 80197
Coordinates of the most left top rectangle corner, when screen dimension is (width, height)
and centered grid contains n x n
rectangles, are
x0 = (width - w * n) / 2
y0 = (height - h * n) / 2
To get all rectangle coordinates, just shift them by w, h for every new rectangle
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]]
Upvotes: 1