-
Notifications
You must be signed in to change notification settings - Fork 0
/
rascii.py
372 lines (299 loc) · 10.9 KB
/
rascii.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import math
import os
###########
# GLOBALS #
###########
color = '█'
border_color = '#'
fill = False
fill_color = 'f'
translateVector = [0, 0] # x, y translation
#######################
# Important Functions #
#######################
def clear_console():
"""
Clears the console with checks for operating system
"""
os.system('cls' if os.name in ('nt', 'dos') else 'clear')
def cartesian_to_idx(width, x, y):
"""
Finds the index of an (x, y) in the screen_data array
"""
return x + ((width+1)*y)
def idx_to_cartesian(width, idx):
"""
Finds the (x, y) on the screen from a given index
"""
x = (idx % (width+1)) if idx > 1 else 0
y = (idx - x)/(width+1) if idx > 1 else 0
return x, int(y)
def color_value(value: int):
"""
Maps the 255 rgb values to 70 alpha pre-specified characters to give illusion of light-level.
"""
global color
if value < 0 or value > 255: error("Color Value must be between 0 and 255")
color_range = ' .`^",:;Il!i><~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8@$█'
color = color_range[math.floor(((len(color_range)-1)/255) * value)]
##################
# Error Function #
##################
def error(Message: str):
# used function to easily keep track of error handling
print(f'<!-- Error: {Message} --!>')
exit(1)
################
# Window Class #
################
class new_window:
"""
Class to create and handle the window
"""
def __init__(self, width, height) -> None:
self.screen_data = []
self.bg_color = '_'
self.width = width
self.height = height
self.border = False
def __repr__(self):
"""
Just for testing purposes
"""
return f'bg_color:({self.bg_color}), width:{self.width}, height:{self.height}'
def setup(self):
"""
Will create the screen_data array based on a given width and height
"""
if not self.border: # if not drawing the border
self.screen_data = ['~/' if i % (self.width+1) == 0 else self.bg_color for i in range((self.width*self.height)+self.height)]
elif self.border: # if drawing a border
for row in range(self.height):
for col in range(self.width):
if row == 0 or row == self.height-1 or col == 0 or col == self.width-1:
self.screen_data.append(border_color)
else:
self.screen_data.append(self.bg_color)
self.screen_data.append('~/')
def draw(self):
"""
Iterates over every char in the window and appends to a "screen buffer"
which is then printed all at once for efficiency
special line break character = '~/'
"""
screen_buffer = ''
for idx, i in enumerate(self.screen_data):
if i != '~/':
screen_buffer += f'{i} ' # char + 'space'
elif i == '~/':
screen_buffer += '\n'
clear_console()
print(screen_buffer)
def clear(self):
"""
same as setup method, however this resets screen_data's state
"""
self.screen_data.clear()
if not self.border:
self.screen_data = ['~/' if i % (self.width+1) == 0 else self.bg_color for i in range((self.width*self.height)+self.height)]
if self.border:
for row in range(self.height):
for col in range(self.width):
if row == 0 or row == self.height-1 or col == 0 or col == self.width-1:
self.screen_data.append(border_color)
else:
self.screen_data.append(self.bg_color)
self.screen_data.append('~/')
#####################
# Drawing Functions #
#####################
def point(screen, x, y):
"""
Edits screen_data array to put char at given (x, y)
"""
index = cartesian_to_idx(screen.width, x, y)
screen.screen_data[int(index)] = color
def line(screen, x1, y1, x2, y2):
"""
* Forms an equation from the two points
* Then iterates over all probable points and checks with equation
* Due to low ascii resolution a buffer of +/- (gradient/2) is given for accuracy.
"""
x1, x2 = x1 + translateVector[0], x2 + translateVector[0]
y1, y2 = y1 + translateVector[1], y2 + translateVector[1]
translate = False
vertical = False
if (x2-x1) != 0: # try is used to fix vertical lines having undefined gradients
m = (y2-y1)/(x2-x1)
else:
m = 0
vertical = True
if y2 - y1 == 0: # fix's horizontal line error
m = 0
translate = True
c = y1 - (m * x1) # y-intercept calculation
lor, upr = -(m/2) if m > 0 else m/2, m/2 if m > 0 else -(m/2) # buffer calculation
if m < 1 and m >= 0: lor, upr = -(1/2), 1/2 # small gradient buffer
# these lines are used for calculating where to iterate over the screen, instead of iterating over the whole screen.
left_point = x1 if x1 <= x2 else x2
right_point = x1 if left_point == x2 else x2
top_point = y1 if y1 <= y2 else y2
bottom_point = y1 if top_point == y2 else y2
stai = cartesian_to_idx(screen.width, left_point, top_point)
endi = cartesian_to_idx(screen.width, right_point, bottom_point)
for i in range(stai, endi): # iteration over specified screen_data
if screen.screen_data[i] != '~/':
x, y = idx_to_cartesian(screen.width, i)
temp = x # hold of x while it is temporarily changed
if vertical: m, x, c = 1, y, 0 # vertical line manipulation to give difference of 0
difference = y - ((m*(x)) + c) if translate == False else 0
if vertical: x = temp
if difference > lor and difference < upr: # does intercept
if x >= left_point and x <= right_point: # x buffer
if y >= top_point and y <= bottom_point: # y buffer
point(screen, x, y)
def horizontal_point(screen, x, y, length):
"""
Used for rectangle to draw horizontal lines all at once to be more efficient
"""
index = cartesian_to_idx(screen.width, x, y)
screen.screen_data[index:(index+length)] = color*length
def rect(screen, rx, ry, rw, rh):
"""
draws a rectangle at given (x, y) with specified width and height
"""
global color, fill, fill_color
rx += translateVector[0]
ry += translateVector[1]
# efficiency calculations
stax, endx = rx, rx + rw
stay, endy = ry, ry + rh
stai, endi = cartesian_to_idx(screen.width, stax, stay), cartesian_to_idx(screen.width, endx, endy)
# if the rectangle is solid then more efficient function can be used
# for i in range(rh):
# horizontal_point(screen, rx, (ry+i), rw)
for i in range(stai, endi):
if screen.screen_data[i] != '~/':
x, y = idx_to_cartesian(screen.width, i)
# checks if point is between boundaries
if x >= rx and x < rx + rw and y >= ry and y < ry + rh:
if rw > 2 and rh > 2:
if x > rx and x < (rx + rw)-1 and y > ry and y < (ry + rh)-1:
if fill:
temp = color
color = fill_color
point(screen, x, y)
color = temp
else:
point(screen, x, y)
else:
point(screen, x, y)
def circle(screen, cx, cy, cr):
global color, fill, fill_color
"""
Draws a circle to the screen at a given x, y with radius
parameters
----------
cx: int
x position of circle centre
cy: int
y position of circle centre
cr: int
radius of the circle
fill: bool
Whether to only draw the circumference
"""
cx += translateVector[0]
cy += translateVector[1]
# Calculate optomised points to iterate through
stax, endx = cx - cr, cx + cr
stay, endy = cy - cr, cy + cr
stai, endi = cartesian_to_idx(screen.width, stax, stay), cartesian_to_idx(screen.width, endx, endy)
# looping over screen_data array
for i in range(stai, endi):
if screen.screen_data[i] != '~/': # Disregard line break
x, y = idx_to_cartesian(screen.width, i)
d = (cr*cr) - ((cx-x)*(cx-x) + (cy-y)*(cy-y))
if fill:
if d <= cr*2 and d > 0: # if point is on circumference using another buffer for accuracy
point(screen, x, y)
elif d > cr*2: # if point is inside circle
temp = color
color = fill_color
point(screen, x, y)
color = temp
else:
if d <= cr*2 and d > 0: # if point is on circumference using another buffer for accuracy
point(screen, x, y)
def tri_area_calc(x1, y1, x2, y2, x3, y3):
"""
function to calculate the area of a triangle given 3 points
"""
lengths = []
lengths.append((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)) # finds length of sides without sqrt by keeping them squared
lengths.append((y3-y2)*(y3-y2) + (x3-x2)*(x3-x2))
lengths.append((y3-y1)*(y3-y1) + (x3-x1)*(x3-x1))
lengths.sort()
c1, c2, c3 = lengths[2], lengths[1], lengths[0]
c1sq, c2sq = math.sqrt(c1), math.sqrt(c2) # two manditory sqrt's
try:
angle = math.acos((c1 + c2 - c3)/(2*c2sq*c1sq)) # uses cosine rule
except:
angle = 0.001
area = 0.5 * c1sq * c2sq * math.sin(angle) # 1/2absin(c)
return round(area, 1)
def triangle(screen, x1, y1, x2, y2, x3, y3, transparent=False):
"""
draws a triangle to the screen if a point is inside the triangle
method:
Get the area of the whole triangle
using the iterated point, form 3 smaller triangles
find the area of all 3 smaller triangles
if the sum of all areas adds up to the sum of the whole triangle
then that point is inside the triangle
"""
x1, x2, x3 = x1 + translateVector[0], x2 + translateVector[0], x3 + translateVector[0]
y1, y2, y3 = y1 + translateVector[1], y2 + translateVector[1], y3 + translateVector[1]
if not transparent:
# calculate the area of the whole triangle
area = tri_area_calc(x1, y1, x2, y2, x3, y3)
# points calculated for effiency of iterating
points_list = []
points_list.append(cartesian_to_idx(screen.width, x1, y1))
points_list.append(cartesian_to_idx(screen.width, x2, y2))
points_list.append(cartesian_to_idx(screen.width, x3, y3))
points_list.sort()
for i in range(points_list[0], points_list[2]):
if screen.screen_data[i] != '~/' or i not in points_list:
# get coordniates of iterated point
px, py = idx_to_cartesian(screen.width, i)
# area of 3 smaller triangles
a1 = tri_area_calc(x1, y1, px, py, x2, y2)
a2 = tri_area_calc(x2, y2, px, py, x3, y3)
a3 = tri_area_calc(x3, y3, px, py, x1, y1)
# check sum with ascii buffer
if a1+a2+a3 > round(area)-5 and a1+a2+a3 < round(area)+5:
point(screen, px, py)
elif transparent:
# If only drawing outline then just connect with 3 lines
# far more efficient as no Square Roots are needed
line(screen, x1, y1, x2, y2)
line(screen, x2, y2, x3, y3)
line(screen, x3, y3, x1, y1)
def shape(screen, point_data: list, connected=True):
"""
connects points with lines from a specified array to form a shape.
"""
if len(point_data) % 2 == 0:
for i in range(0, len(point_data) - 1, 2):
if len(point_data) >= 4:
if i == len(point_data)-2:
if connected:
line(screen, point_data[i], point_data[i+1], point_data[0], point_data[1])
else:
line(screen, point_data[i], point_data[i+1], point_data[i+2], point_data[i+3])
else:
point(screen, point_data[i]+translateVector[0], point_data[i+1]+translateVector[1])
else:
msg = "Point_data array has uneven length"
error(msg)