Advertisement
fevzi02

2Лаба_графика 11.12.21

Dec 11th, 2021
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.78 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import math
  4.  
  5. class Scroller:
  6.     def __init__(self, screen, x, y, length, length_ratio=None, text=""):
  7.         if length_ratio == None:
  8.             length_ratio = length
  9.  
  10.         self.screen = screen
  11.         self.x, self.y = x, y
  12.         self.length, self.length_ratio = length, length_ratio
  13.         self.text_right = text
  14.         self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
  15.         self.a, self.value = 0, 0
  16.         self.bool_znach = 0
  17.         self.font = pygame.font.SysFont('stxingkai', 20)
  18.         self.mouse_pos = (0, 0)
  19.  
  20.     def paint(self):
  21.         self.value = int(self.a/(self.length/self.length_ratio))
  22.         self.button0 = pygame.Rect(self.x , self.y, self.length, self.display_height/50)
  23.         self.button1 = pygame.Rect(self.a + self.x-self.display_width/80, self.y-self.display_height/50, self.display_width/40, self.display_height/30)
  24.  
  25.         pygame.draw.line(self.screen, (48, 38, 18),[self.a + self.x, self.y-self.display_height/50], [self.a + self.x, self.y-self.display_height/50+self.display_height/20], 10)
  26.         pygame.draw.rect(self.screen, [144, 144, 144], self.button0)  # draw button
  27.         pygame.draw.line(self.screen, (48, 38, 18),[self.a + self.x, self.y-self.display_height/50], [self.a + self.x, self.y-self.display_height/50+self.display_height/20], 2)
  28.         pygame.draw.rect(self.screen, "red", self.button1)
  29.  
  30.         text_name = self.font.render(str(self.value), True, (0, 0, 255))
  31.         text_rect = text_name.get_rect()
  32.         self.screen.blit(text_name, [self.a + self.x - text_rect.width/2, self.y - text_rect.height/2])
  33.  
  34.         text_right = self.font.render(self.text_right, True, (0, 0, 255))
  35.         text_rect_right = text_right.get_rect()
  36.         self.screen.blit(text_right, [self.x+ self.length + 10, (self.y + self.y+self.display_height/100)/2])
  37.  
  38.  
  39.     #обработчик событий
  40.     def _event_(self, event):
  41.         if event.type == pygame.MOUSEBUTTONDOWN and not self.button0.collidepoint(self.mouse_pos):
  42.             self.bool_znach = 0
  43.  
  44.         if event.type == pygame.MOUSEMOTION or event.type == pygame.MOUSEBUTTONDOWN:
  45.             self.mouse_pos = event.pos
  46.             if self.button0.collidepoint(self.mouse_pos):
  47.                 self.bool_znach = 1
  48.             if pygame.mouse.get_pressed()[0] != 0 and self.bool_znach:
  49.                 self.a = pygame.mouse.get_pos()[0] - self.x
  50.                 if self.a < 0:
  51.                     self.a = 0
  52.                 if self.a > self.length :
  53.                     self.a = self.length
  54.  
  55.  
  56. class Init:
  57.     def __init__(self, screen, list_scroller):
  58.         scl = list_scroller[0].value                            #Получение значения с ползунка "Масштаб"
  59.         DX = list_scroller[1].value                             #Получение значения с ползунка "Сдвиг по X"
  60.         DY = list_scroller[2].value                             #Получение значения с ползунка "Сдвиг по Y"
  61.         Rotate = list_scroller[3].value                         #Получение значения  ползунка "Наклон"
  62.         Rotate = (math.pi / 180) * Rotate                       #Перевод градусов в радианы
  63.  
  64.         nX = 2 * (math.sin(0) ** 3) * scl                       #Получение X координаты, для начальной точки графика
  65.         nY = 2 * (math.cos(0) ** 3) * scl                       #Получение Y координаты, для начальной точки графика
  66.         oldX = nX * math.cos(Rotate) - nY * math.sin(Rotate)    #Вычисление координаты X с учетом поворота
  67.         oldY = nX * math.sin(Rotate) + nY * math.cos(Rotate)    #Вычисление координаты Y с учетом поворота
  68.         #Отрисовка графика
  69.         t = 0
  70.         while (t < 10 * math.pi):
  71.             t += 0.01
  72.             nX = 2 * (math.sin(t) ** 3) * scl                   #Вычисление текущей X координаты, для графика
  73.             nY = 2 * (math.cos(t) ** 3) * scl                   #Вычисление текущей Y координаты, для графика
  74.             x = nX * math.cos(Rotate) - nY * math.sin(Rotate);  #Вычисление координаты X с учетом поворота
  75.             y = nX * math.sin(Rotate) + nY * math.cos(Rotate);  #Вычисление координаты Y с учетом поворота
  76.  
  77.             pygame.draw.line(screen, (0, 0, 0),(oldX + DX , oldY + DY ), (x + DX , y + DY ))
  78.             oldX = x; oldY = y;                                 #Сохраняем значения X,Y для следующей итерации
  79.  
  80. class Align:
  81.     def __init__(self, screen, list_scroller):
  82.         self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
  83.         self.screen = screen
  84.         self.list_scroller = list_scroller
  85.         self.start_znach = [50, int(self.display_width/2), self.display_height/2, 0]
  86.         self.font = pygame.font.SysFont('stxingkai', 50)
  87.  
  88.     def ran(self):
  89.         for i in range(len(self.list_scroller)):
  90.             self.list_scroller[i].a = int(self.start_znach[i] * self.list_scroller[i].length/self.list_scroller[i].length_ratio)
  91.  
  92.     def paint_button(self):
  93.         text_name = self.font.render("Выровнить", True, (0, 0, 0))
  94.         text_rect = text_name.get_rect()
  95.         self.button0 = pygame.Rect(self.display_width/2 - 10 , self.display_height - text_rect.height/2 - 35 , text_rect.width + 20, text_rect.height+10)
  96.         pygame.draw.rect(self.screen, [144, 144, 144], self.button0)
  97.         self.screen.blit(text_name, [self.display_width/2 , self.display_height - text_rect.height/2 - 30])
  98.  
  99.     def _event_(self, event):
  100.         if event.type == pygame.MOUSEBUTTONDOWN:
  101.             if self.button0.collidepoint(event.pos):
  102.                 self.ran()
  103.  
  104.  
  105. class Main:
  106.     def __init__(self, fps=60):
  107.         pygame.init()
  108.         self.screen = pygame.display.set_mode()
  109.         self.clock = pygame.time.Clock()
  110.         self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
  111.         self.a = 0
  112.  
  113.         text_list = ["Масштаб", "Сдвиг по х", "Сдвиг по у", "Наклон"]
  114.         value_list = [(self.display_height/2 + self.display_width/2)/2, self.display_width, self.display_height, 360]
  115.  
  116.         self.list_scroller = []
  117.         for i in range(4):
  118.             self.list_scroller.append(Scroller(self.screen, 15, self.display_height - 100 * i/2 - 20, 300, value_list[i], text_list[i]))
  119.  
  120.         a = Align(self.screen, self.list_scroller)
  121.  
  122.         while True:
  123.             self.screen.fill("white")
  124.             Init(self.screen, self.list_scroller)
  125.             a.paint_button()
  126.             for scroller in self.list_scroller:
  127.                 scroller.paint()
  128.             #обработчик событий
  129.             for event in pygame.event.get():
  130.                 a._event_(event)
  131.                 for scroller in self.list_scroller:
  132.                     scroller._event_(event)
  133.                 if event.type == pygame.QUIT:
  134.                     pygame.quit()
  135.                     sys.exit()
  136.                 if event.type == pygame.KEYDOWN :
  137.                     if event.key == pygame.K_ESCAPE:
  138.                         pygame.quit()
  139.                         sys.exit()
  140.                     elif event.key == pygame.K_q:
  141.                         a.ran()
  142.  
  143.             pygame.display.flip()
  144.             self.clock.tick(fps)
  145.  
  146.  
  147. #-------------------------------------------------------------------------------
  148. Main(24)
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement