1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import numpy as np
from matplotlib import pyplot
from matplotlib.patches import PathPatch
from matplotlib.path import Path
def create_rect():
w,h=np.random.random(2)
return(np.array([0,0,w,0,w,h,0,h]).reshape(4,2))
def draw_rect(input_rect):
input_rect =input_rect.tolist()
input_rect.append(input_rect[0])
codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path = Path(input_rect, codes)
axis = pyplot.gca()
patch = PathPatch(path)
axis.add_patch(patch)
axis.set_xlim(-0.1,1,1)
axis.set_ylim(-0.1,1,1)
pyplot.show()
for i in range(5):
draw_rect(create_rect())
|