@p3artschool/

CSHS-2019-05-03-A2

Python

No description

fork
loading
Files
  • main.py
  • answer.png
  • demo.png
main.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# CSHS-2019-05-03-A2.py
# 2019-05-03
# python3
# run on https://repl.it
# run on ubuntu 18.04 LTS
#
# 要執行本程式, 請按上方run▶
#
import numpy as np
import matplotlib.pyplot as plot
from shapely import affinity
from shapely.geometry import LineString
#
# 定義圖面函數
def set_graphic_area(width,height) :
        
    cm2inch = 1/2.54    # inch per cm
    #
    # define graphic area
    #
    left_margin = 1.0   # cm
    right_margin = 1.0  # cm
    #
    figure_width  = width  # cm , from xmin to xmax
    figure_height = height # cm , from ymin to ymax
    #
    top_margin = 1.0    # cm
    bottom_margin = 1.0 # cm
    #
    box_width = left_margin + figure_width + right_margin   # cm
    box_height = top_margin + figure_height + bottom_margin # cm
    #
    top_value    = 1.0 - top_margin / box_height
    bottom_value = bottom_margin / box_height
    left_value   = left_margin / box_width
    right_value  = 1.0 - right_margin / box_width
    #
    return (box_width*cm2inch,box_height*cm2inch,top_value,bottom_value,left_value,right_value,width)
    #
# end of def
#
# Define the arc
# center position is cxy = (cx, cy)
# start_angle, end_angle is in degrees
#
def shapely_Arc(cxy, r, start_angle, end_angle) :
    #
    numsegments = 1440
    # The coordinates of the arc
    theta = np.radians(np.linspace(start_angle, end_angle, numsegments))
    x = cxy[0] + r * np.cos(theta)
    y = cxy[1] + r * np.sin(theta)
    Arc = LineString(np.column_stack([x, y]))
    return Arc
    #
# end of def
#
# ------------------------------------------------------------
#
r0 = 8
tup7 = set_graphic_area(2*r0, 2*r0)
fig = plot.figure(figsize=(tup7[0], tup7[1]))
ax = fig.add_subplot(1,1,1)
fig.subplots_adjust(
                top    = tup7[2] ,
                bottom = tup7[3] ,
                left   = tup7[4] ,
                right  = tup7[5] ,
                )
#
plot.xlim(-tup7[6]/2, tup7[6]/2)
plot.ylim(-tup7[6]/2, tup7[6]/2)
plot.gca().set_aspect('equal', adjustable='box') 
ax.grid(True)
ax.set_axis_on()
#
## -----------------------------------------------------------------------------------
#
tri_big   = LineString([(2,-6), (6,-6), (6,-2)])
tri_small = LineString([(2,-6), (0,-4), (-2,-6)])
rec = LineString([(0,-4), (2,-6), (6,-2), (4,0)])
square = LineString([(2,-2), (2,-4), (4,-4), (4,-2)])
square_big = LineString([(2,-2), (2,2), (-2,2), (-2,-2)])
#
xs, ys = tri_big.xy
ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='lightgreen', fill=True, zorder=30)
xs, ys = tri_small.xy
ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='green', fill=True, zorder=30)
xs, ys = rec.xy
ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='orange', fill=True, zorder=30)
xs, ys = square.xy
ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='cyan', fill=True, zorder=30)
xs, ys = square_big.xy
ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='magenta', fill=True, zorder=30)
for my_degree in (90, 180, 270):
    tri_b = affinity.rotate(tri_big,   my_degree, origin=(0,0))
    tri_s = affinity.rotate(tri_small, my_degree, origin=(0,0))
    rec_new    = affinity.rotate(rec,    my_degree, origin=(0,0))
    square_new = affinity.rotate(square, my_degree, origin=(0,0))
    #
    xs, ys = tri_b.xy
    ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='lightgreen', fill=True, zorder=30)
    xs, ys = tri_s.xy
    ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='green', fill=True, zorder=30)
    xs, ys = rec_new.xy
    ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='orange', fill=True, zorder=30)   
    xs, ys = square_new.xy
    ax.fill(xs, ys, alpha=1.0, closed=True, edgecolor='none', facecolor='cyan', fill=True, zorder=30)
# end for
#
## ---------------------------------------------------------------------------------------
#
plot.savefig('answer.png', format="png", dpi=300)
print('Done')