# graphicsLibrary.py

Jan 10th, 2022
662
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import pygame
2. import math
3. #-------------------------------------------------------------------------------
4.
5. def pixel(surface, color, pos):
6.     surface.fill(color, (pos, (1, 1)))
7. #-------------------------------------------------------------------------------
8. # №1 Прямоугольник
9. def draw(surface, pos, color="white", borderFull=1, borderColor="black" , borderLeft=None, borderRight=None, borderTop=None, borderBottom=None, transparency=0):
10.     x1 = pos[0][0]
11.     x2 = pos[1][0]
12.     y1 = pos[0][1]
13.     y2 = pos[1][1]
14.
15.     #Если пармаметтры не заполнены, то примут значение borderFull
16.     if borderLeft == None:
17.          borderLeft = borderFull
18.     if borderRight == None:
19.          borderRight = borderFull
20.     if borderTop == None:
21.          borderTop = borderFull
22.          borderBottom = borderFull
23.
24.     #Внутреняя область
25.     listBorder = [borderLeft, borderRight, borderTop, borderBottom, borderFull]
26.     min = borderFull
27.     summ = 0
28.     for i in listBorder:
29.         summ += i
30.         if i < min:
31.             min = i
32.     if not((x2-x1)/2>borderFull and (y2-y1)/2>borderFull):
33.         min = 0
34.
35.     if not transparency:
36.         for x in range(x1+min, x2+1-min):
37.             for y in range(y1+min, y2+1-min):
38.                 pixel(surface, color, (x, y))
39.
40.     #Рамка
41.     if summ != 0 and (x2-x1)/2>borderFull and (y2-y1)/2>borderFull :
42.         draw(surface, [[x1,y1], [x2,y1+borderTop]], color=borderColor, borderFull=0)
43.         draw(surface, [[x2-borderRight,y1], [x2,y2]], color=borderColor, borderFull=0)
44.         draw(surface, [[x1,y2-borderBottom], [x2,y2]], color=borderColor, borderFull=0)
45.         draw(surface, [[x1,y1], [x1+borderLeft,y2]], color=borderColor, borderFull=0)
46.
47. #-------------------------------------------------------------------------------
48. # №2 Окружность
49. def circle(surface, x0, y0, radius, color="white", T_F=0):
51.         x = 0; y = radius; gap = 0; delta = (2 - 2 * radius)
52.         while (y >= 0):
53.             pixel(surface, color, [x0 + x, y0 + y])
54.             pixel(surface, color, [x0 + x, y0 - y])
55.             pixel(surface, color, [x0 - x, y0 - y])
56.             pixel(surface, color, [x0 - x, y0 + y])
57.             gap = 2 * (delta + y) - 1
58.             if (delta < 0 and gap <= 0):
59.                 x += 1
60.                 delta += 2 * x + 1
61.                 continue
62.             if (delta > 0 and gap > 0):
63.                 y -= 1
64.                 delta -= 2 * y + 1
65.                 continue
66.             x += 1
67.             delta += 2 * (x - y)
68.             y -= 1
70.
71. #-------------------------------------------------------------------------------
72. def alines(surface, borderColor, list_ccord, T_F=0):
73.     if T_F:
74.         draw_line(surface, borderColor, x1 = list_ccord[0][0], y1=list_ccord[0][1], x2=list_ccord[-1][0], y2=list_ccord[-1][1])
75.     for i in range(len(list_ccord)-1):
76.         draw_line(surface, borderColor, x1 = list_ccord[i][0], y1=list_ccord[i][1], x2=list_ccord[i+1][0], y2=list_ccord[i+1][1])
77. #-------------------------------------------------------------------------------
78.
79. def polygon(surface, borderColor, list_ccord):
80.     for x in range( min(list_ccord, 0), max(list_ccord, 0) ):
81.         for y in range( min(list_ccord, 1), max(list_ccord, 1) ):
82.             if point_in_polygon(list_ccord, [x, y]):
83.                 pixel(surface, borderColor, [x, y])
84. def min(l, b):
85.     minimum = l[0][b]
86.     for i in l:
87.         if i[b] < minimum:
88.             minimum = i[b]
89.     return minimum
90.
91. def max(l, b):
92.     maximum = l[0][b]
93.     for i in l:
94.         if i[b] > maximum:
95.             maximum = i[b]
96.     return maximum
97.
98. def point_in_polygon(p, point):
99.     result = False
100.     size = len(p)
101.     j = size - 1
102.     for i in range(size):
103.         if (p[i][1] < point[1] and p[j][1] >= point[1] or p[j][1] < point[1]
104.         and p[i][1] >= point[1]) and (p[i][0] + (point[1] - p[i][1]) / (p[j][1] - p[i][1]) * (p[j][0] - p[i][0]) < point[0]):
105.             result = not result
106.         j = i
107.     return result
108. #-------------------------------------------------------------------------------
109.
110. def triangle(surface, borderColor, list_ccord):
111.     if len(list_ccord)==3:
112.         alines(surface, borderColor, list_ccord, T_F=1)
113.
114. #-------------------------------------------------------------------------------
115. def draw_line(surface, borderColor, x1=0, y1=0, x2=0, y2=0):
116.
117.         dx = x2 - x1
118.         dy = y2 - y1
119.
120.         sign_x = 1 if dx>0 else -1 if dx<0 else 0
121.         sign_y = 1 if dy>0 else -1 if dy<0 else 0
122.
123.         if dx < 0: dx = -dx
124.         if dy < 0: dy = -dy
125.
126.         if dx > dy:
127.             pdx, pdy = sign_x, 0
128.             es, el = dy, dx
129.         else:
130.             pdx, pdy = 0, sign_y
131.             es, el = dx, dy
132.
133.         x, y = x1, y1
134.
135.         error, t = el/2, 0
136.
137.         pixel(surface, borderColor, (x, y))
138.
139.         while t < el:
140.             error -= es
141.             if error < 0:
142.                 error += el
143.                 x += sign_x
144.                 y += sign_y
145.             else:
146.                 x += pdx
147.                 y += pdy
148.             t += 1
149.             pixel(surface, borderColor, (x, y))
150.
151. #-------------------------------------------------------------------------------
152. def drawSpline(surface, color, args):
153.     coords = [[args[0], args[1]]]
154.     num = 0
155.     for i in range(0, len(args), 2):
156.         coords.append([args[i], args[i+1]])
157.         if i > 0:
158.             deltaX = coords[int(i/2+1)][0] - coords[int(i/2)][0]
159.             deltaY = coords[int(i/2+1)][1] - coords[int(i/2)][1]
160.             num += math.sqrt(deltaX * deltaX + deltaY * deltaY)
161.
162.     coords.append(coords[-1])
163.
164.     for i in range(1, len(coords)-2):
165.         a = []
166.         b = []
167.         arrs = _SplineCoefficient(i, coords)   # считаем коэффициенты q
168.
169.         for j in range(int(num)):
170.             points = []                           # создаём массив промежуточных точек
171.             t = j / num                       # шаг интерполяции
172.                                               # передаём массиву точек значения по методу beta-spline
173.             points.append(arrs[0][0] + t * (arrs[0][1] + t * (arrs[0][2] + t * arrs[0][3])))
174.             points.append(arrs[1][0] + t * (arrs[1][1] + t * (arrs[1][2] + t * arrs[1][3])))
175.
176.             pixel(surface, color, (points[0], points[1]))
177.
178.
179. def _SplineCoefficient(i, coords):      # в функции рассчитываются коэффициенты a0-a3, b0-b3
180.     arrs = [[], []]
181.
182.     arrs[0].append((coords[i - 1][0] + 4*coords[i][0] + coords[i + 1][0])/6)
183.     arrs[0].append((-coords[i - 1][0] + coords[i + 1][0])/2)
184.     arrs[0].append((coords[i - 1][0] - 2*coords[i][0] + coords[i + 1][0])/2)
185.     arrs[0].append((-coords[i - 1][0] + 3*coords[i][0] - 3*coords[i + 1][0] + coords[i + 2][0])/6)
186.
187.     arrs[1].append((coords[i - 1][1] + 4*coords[i][1] + coords[i + 1][1])/6)
188.     arrs[1].append((-coords[i - 1][1] + coords[i + 1][1])/2)
189.     arrs[1].append((coords[i - 1][1] - 2*coords[i][1] + coords[i + 1][1])/2)
190.     arrs[1].append((-coords[i - 1][1] + 3*coords[i][1] - 3*coords[i + 1][1] + coords[i + 2][1])/6)
191.
192.     return arrs
193. #-------------------------------------------------------------------------------
194.