I try smth like this:
def main_rec():
width = random.randint(150, 250)
height = random.randint(150, 250)
angle = rand_angle()
c, s = np.cos(angle), np.sin(angle)
R = np.array(((c, -s), (s, c)))
center = (random.randint(0, 640), random.randint(0, 480))
x1y10 = (center[0] - width / 2, center[1] height / 2)
x2y20 = (x1y10[0] width, x1y10[1])
x3y30 = (x2y20[0], x2y20[1] - height)
x4y40 = (x3y30[0] - width, x3y30[1])
x1y1 = (x1y10[0] * R[0][0] x1y10[1] * R[0][1], x1y10[0] * R[1][0] x1y10[1] * R[1][1])
x2y2 = (x2y20[0] * R[0][0] x2y20[1] * R[0][1], x1y10[1] * R[0][1] x2y20[1] * R[1][1])
x3y3 = (x3y30[0] * R[0][0] x3y30[1] * R[0][1], x3y30[0] * R[1][0] x3y30[1] * R[1][1])
x4y4 = (x4y40[0] * R[0][0] x4y40[1] * R[0][1], x4y40[1] * R[0][1] x4y40[1] * R[1][1])
points = [x1y1, x2y2, x3y3, x4y4]
return points, angle / 3.14159 * 180
but I don't know how to set a condition for the corners to be right. I try to use rotation matrix. It makes normal rectangles only for angle = 0
CodePudding user response:
using numpy and rotation matrix code would be:
import numpy as np
import matplotlib.pyplot as plt
def create_rect(width,height,center):
x,y = center
w,h = width,height
return np.array([[x-w/2,y-h/2],
[x w/2,y-h/2],
[x w/2,y h/2],
[x-w/2,y h/2],
[x-w/2,y-h/2]]).T
width = np.random.randint(150,250)
height = np.random.randint(150,250)
center = (np.random.randint(0, 640), np.random.randint(0, 480))
angle = np.random.randint(0,360)/360.0*2*np.pi
rotmat = np.array([[np.cos(angle),-np.sin(angle)],
[np.sin(angle),np.cos(angle)]])
rect = create_rect(width,height,center)
rot_rect = rotmat @ rect
plt.imshow(*rot_rect)