Skip to content

Commit 6ef731f

Browse files
committed
Add drawing file in assignment 3
1 parent 1870a50 commit 6ef731f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

assignment-3/mpl_draw.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import matplotlib
2+
3+
import matplotlib.pyplot as plt
4+
5+
from matplotlib import collections as mc
6+
7+
import sys
8+
9+
import argparse
10+
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument("input_file", help="input file")
13+
parser.add_argument("output_file", nargs="?", help="output_file")
14+
args = parser.parse_args()
15+
16+
lines = []
17+
min_x = sys.maxsize
18+
min_y = sys.maxsize
19+
max_x = -min_x
20+
max_y = -min_y
21+
22+
with open(args.input_file) as input_file:
23+
for line in input_file:
24+
points = line.strip().split(') (')
25+
point_1 = [ float(x) for x in points[0][1:].split(',') ]
26+
point_2 = [ float (x) for x in points[1][:-1].split(',') ]
27+
min_x = min(min_x, min(point_1[0], point_2[0]))
28+
min_y = min(min_y, min(point_1[1], point_2[1]))
29+
max_x = max(max_x, max(point_1[0], point_2[0]))
30+
max_y = max(max_y, max(point_1[1], point_2[1]))
31+
lines.append([point_1, point_2])
32+
33+
fig, ax = plt.subplots()
34+
ax.set_xlim(min_x - 1, max_x + 1)
35+
ax.set_ylim(min_y - 1, max_y + 1)
36+
lc = mc.LineCollection(lines, colors='black', linewidths=1)
37+
lc.set_capstyle('round')
38+
ax.add_collection(lc)
39+
ax.set_aspect('equal')
40+
ax.axis('off')
41+
plt.tight_layout()
42+
if args.output_file:
43+
plt.savefig(args.output_file, dpi=300)
44+
plt.show()

0 commit comments

Comments
 (0)