forked from dcf21/astrolabe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmother_front.py
executable file
·199 lines (165 loc) · 7.14 KB
/
mother_front.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python3
# mother_front.py
# -*- coding: utf-8 -*-
#
# The python script in this file makes the various parts of a model astrolabe.
#
# Copyright (C) 2010-2019 Dominic Ford <[email protected]>
#
# This code is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# You should have received a copy of the GNU General Public License along with
# this file; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA
# ----------------------------------------------------------------------------
"""
Render the front of the mother of the astrolabe.
"""
from math import pi, sin, cos, acos, floor
from constants import unit_deg, unit_rev, unit_cm, centre_scaling, r_1, d_12, tab_size
from graphics_context import BaseComponent
from numpy import arange
from settings import fetch_command_line_arguments
from themes import themes
class MotherFront(BaseComponent):
"""
Render the front of the mother of the astrolabe.
"""
def default_filename(self):
"""
Return the default filename to use when saving this component.
"""
return "mother_front"
def bounding_box(self, settings):
"""
Return the bounding box of the canvas area used by this component.
:param settings:
A dictionary of settings required by the renderer.
:return:
Dictionary with the elements 'x_min', 'x_max', 'y_min' and 'y_max' set
"""
r_outer = r_1 + 0.5 * unit_cm
return {
'x_min': -r_outer,
'x_max': r_outer,
'y_min': -r_outer - 2 * unit_cm,
'y_max': r_outer
}
def do_rendering(self, settings, context):
"""
This method is required to actually render this item.
:param settings:
A dictionary of settings required by the renderer.
:param context:
A GraphicsContext object to use for drawing
:return:
None
"""
is_southern = settings['latitude'] < 0
theme = themes[settings['theme']]
context.set_color(color=theme['lines'])
# Radii of circles to be drawn on front of mother
r_2 = r_1 - d_12 * 1.5
r_3 = r_2 - d_12
r_4 = r_3 - d_12 / 2
r_5 = d_12 * centre_scaling
# Draw the handle at the top of the astrolabe
ang = 180 * unit_deg - acos(unit_cm / r_1)
context.begin_path()
context.arc(centre_x=0, centre_y=-r_1, radius=2 * unit_cm,
arc_from=-ang - pi / 2, arc_to=ang - pi / 2)
context.move_to(x=0, y=-r_3)
context.line_to(x=0, y=-r_1 - 2 * unit_cm)
context.stroke()
# Draw circles 1-5 onto front of mother
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_1)
context.stroke()
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_2)
context.stroke()
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_3)
context.stroke()
# Circle four has tab cut out
context.begin_path()
context.arc(centre_x=0, centre_y=0, radius=r_4,
arc_from=tab_size - pi / 2, arc_to=360 * unit_deg - tab_size - pi / 2)
context.stroke()
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_5)
context.stroke()
# Between circles 2 and 4, mark 5-degree intervals
for theta in arange(5 * unit_deg, 359.9 * unit_deg, 5 * unit_deg):
context.begin_path()
context.move_to(x=r_2 * sin(theta), y=-r_2 * cos(theta))
context.line_to(x=r_4 * sin(theta), y=-r_4 * cos(theta))
context.stroke()
# Between circles 3 and 4, draw a fine scale of 1-degree intervals
for theta in arange(tab_size, 360.1 * unit_deg - tab_size, 1 * unit_deg):
context.begin_path()
context.move_to(x=r_3 * sin(theta), y=-r_3 * cos(theta))
context.line_to(x=r_4 * sin(theta), y=-r_4 * cos(theta))
context.stroke()
# Between circles 2 and 3, label every 10 degrees
rt_1 = (r_2 + r_3) / 2
for theta in arange(-180 * unit_deg, 179 * unit_deg, 10 * unit_deg):
if theta < -179 * unit_deg:
theta_disp = theta
elif theta < - 90 * unit_deg:
theta_disp = theta + 180 * unit_deg
elif theta < 0 * unit_deg:
theta_disp = -theta
elif theta < 90 * unit_deg:
theta_disp = theta
else:
theta_disp = -theta + 180 * unit_deg
theta_disp = floor(theta_disp / unit_deg + 0.01)
context.set_font_size(1.2)
if theta_disp == 0:
theta2 = theta
context.text(text="0",
x=rt_1 * cos(theta2), y=-rt_1 * sin(theta2),
h_align=0, v_align=0, gap=0, rotation=-theta - 90 * unit_deg)
elif theta_disp == -180:
theta2 = theta
context.set_font_size(2.1)
context.text(text="\u2720",
x=rt_1 * cos(theta2), y=-rt_1 * sin(theta2),
h_align=0, v_align=0, gap=0, rotation=-theta - 90 * unit_deg)
else:
theta2 = theta - 0.2 * unit_deg
context.text(text="{:.0f}".format(theta_disp / 10),
x=rt_1 * cos(theta2), y=-rt_1 * sin(theta2),
h_align=1, v_align=0, gap=0, rotation=-theta - 90 * unit_deg)
theta2 = theta + 0.2 * unit_deg
context.text(text="{:.0f}".format(theta_disp % 10),
x=rt_1 * cos(theta2), y=-rt_1 * sin(theta2),
h_align=-1, v_align=0, gap=0, rotation=-theta - 90 * unit_deg)
# Between circles 1 and 2, label 24 hours with large letters. A cross marks midnight.
rt_1 = r_1 * 0.55 + r_2 * 0.45
for i, t in enumerate(["\u2720", "A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "X", "Y", "Z"]):
theta = i / 24 * unit_rev
if is_southern:
theta = -theta
context.set_font_size(2.0 if i > 0 else 2.8)
context.text(text=t,
x=rt_1 * sin(theta), y=-rt_1 * cos(theta),
h_align=0, v_align=0, gap=0,
rotation=theta)
# Do it right away if we're run as a script
if __name__ == "__main__":
# Fetch command line arguments passed to us
arguments = fetch_command_line_arguments(default_filename=MotherFront().default_filename())
# Render the front of the mother
MotherFront(settings={
'latitude': arguments['latitude'],
'language': 'en'
}).render_to_file(
filename=arguments['filename'],
img_format=arguments['img_format']
)