fevzi02

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

Dec 11th, 2021 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import pygame, sys
  2. import numpy as np
  3. import random
  4.  
  5. class Main:
  6.     def __init__(self, fps=60):
  7.         pygame.init()
  8.         self.screen = pygame.display.set_mode((1080,720))
  9.         self.clock = pygame.time.Clock()
  10.         self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
  11.  
  12.         #line
  13.         self.x, self.y = 200, 100  #отступ от (0, 0)
  14.         self.coordinates = [(100+self.x, 400+self.y),(100+self.x, 100+self.y),(200+self.x, 600+self.y),(300+self.x, 400+self.y),(200+self.x, 300+self.y),(500+self.x, 300+self.y),(300+self.x, 500+self.y),(600+self.x, 500+self.y),(600+self.x, 0+self.y),(400+self.x, 100+self.y),(0+self.x, 0+self.y)]
  15.         self.thickness = 5  #толщина линии
  16.         #/line
  17.         self.triangles = []
  18.         self.pos = (0, 0)
  19.         self.bool_pos = 0
  20.  
  21.         self.screen.fill("white")
  22.         self.print_polygon(self.coordinates)
  23.  
  24.         while True:
  25.             self.triangles = []
  26.             self.triangulate()
  27.             self.coordinates = [(100+self.x, 400+self.y),(100+self.x, 100+self.y),(200+self.x, 600+self.y),(300+self.x, 400+self.y),(200+self.x, 300+self.y),(500+self.x, 300+self.y),(300+self.x, 500+self.y),(600+self.x, 500+self.y),(600+self.x, 0+self.y),(400+self.x, 100+self.y),(0+self.x, 0+self.y)]
  28.             hasPointOfPolygonMouse = self.hasPointOfPolygonMouse()
  29.             if hasPointOfPolygonMouse[0] and self.bool_pos:
  30.                 pygame.draw.polygon(self.screen, (random.randint(0, 256)%256,random.randint(0, 256)%256, random.randint(0, 256)%256), hasPointOfPolygonMouse[1])
  31.             self.bool_pos = 0
  32.             #обработчик событий
  33.             for event in pygame.event.get():
  34.                 if event.type == pygame.QUIT:
  35.                     pygame.quit()
  36.                     sys.exit()
  37.                 if event.type == pygame.KEYDOWN :
  38.                     if event.key == pygame.K_ESCAPE:
  39.                         pygame.quit()
  40.                         sys.exit()
  41.                 if event.type == pygame.MOUSEBUTTONDOWN :
  42.                     self.pos = event.pos
  43.                     self.bool_pos = 1
  44.             pygame.display.flip()
  45.             self.clock.tick(fps)
  46.  
  47.     def print_polygon(self, coordinates):
  48.         pygame.draw.aalines(self.screen, "black", True, coordinates)
  49.  
  50.     # 2 Левая тройка векторов?
  51.     def isLeft(self, i):
  52.         A = self.coordinates[i%len(self.coordinates)]
  53.         B = self.coordinates[(i+1)%len(self.coordinates)]
  54.         C = self.coordinates[(i+2)%len(self.coordinates)]
  55.  
  56.         AB = {"x": B[0] - A[0], "y": B[1] - A[1]}
  57.         AC = {"x": C[0] - A[0], "y": C[1] - A[1]}
  58.  
  59.         return AB["x"] * AC["y"] - AC["x"] * AB["y"] < 0
  60.  
  61.     def hasPointOfPolygonMouse(self):
  62.         for triangle in self.triangles:
  63.             A = triangle[0]
  64.             B = triangle[1]
  65.             C = triangle[2]
  66.             for j in range(0, len(self.coordinates)):
  67.                 VP0  = self.cross(A, B, self.pos)
  68.                 VP1  = self.cross(B, C, self.pos)
  69.                 VP2  = self.cross(C, A, self.pos)
  70.                 s_l = np.sign([VP0, VP1, VP2])
  71.  
  72.                 if s_l[0] == s_l[1] and s_l[1] == s_l[2] and s_l[0] == s_l[2]:
  73.                      return (True, triangle)
  74.  
  75.                 for h in range(len(s_l)):
  76.                     if s_l[h] == 0:
  77.                         if s_l[(h+1)%len(s_l)] == s_l[(h+2)%len(s_l)]:
  78.                             return (True, triangle)
  79.         return (False, (None, None))
  80.  
  81.     def cross(self, P1, P2, P): #ABxAP, BCxBP and CAxCP.
  82.         vector_P1_P2 = (P2[0]-P1[0], P2[1]-P1[1])
  83.         vector_P1_P = (P[0]-P1[0], P[1]-P1[1])
  84.  
  85.         return np.cross(vector_P1_P2, vector_P1_P)
  86.  
  87.     # 3 Есть ли другие точки внутри рассматриваемого треугольника?
  88.     def hasPointOfPolygon(self, i):
  89.         A = self.coordinates[i%len(self.coordinates)]
  90.         B = self.coordinates[(i+1)%len(self.coordinates)]
  91.         C = self.coordinates[(i+2)%len(self.coordinates)]
  92.  
  93.         for j in range((i+3)%len(self.coordinates), len(self.coordinates)-i%len(self.coordinates)):
  94.             VP0  = self.cross(A, B, self.coordinates[j%len(self.coordinates)])
  95.             VP1  = self.cross(B, C, self.coordinates[j%len(self.coordinates)])
  96.             VP2  = self.cross(C, A, self.coordinates[j%len(self.coordinates)])
  97.             s_l = np.sign([VP0, VP1, VP2])
  98.  
  99.             if s_l[0] == s_l[1] and s_l[1] == s_l[2] and s_l[0] == s_l[2]:
  100.                  return False
  101.  
  102.             for h in range(len(s_l)):
  103.                 if s_l[h] == 0:
  104.                     if s_l[(h+1)%len(s_l)] == s_l[(h+2)%len(s_l)]:
  105.                         return False
  106.         return True
  107.  
  108.     #Триангуляция
  109.     def triangulate(self):
  110.         i = 0
  111.         while len(self.coordinates) >= 3:
  112.             if self.isLeft(i) and self.hasPointOfPolygon(i):
  113.                 self.triangles.append([self.coordinates[(i)%len(self.coordinates)], self.coordinates[(i+1)%len(self.coordinates)], self.coordinates[(i+2)%len(self.coordinates)]])
  114.                 if len(self.coordinates) == 3:
  115.                     break
  116.                 del self.coordinates[(i+1)%len(self.coordinates)]
  117.             else:
  118.                 i += 1
  119.                 i = i%len(self.coordinates)
  120.  
  121. Main(24)
  122.  
Add Comment
Please, Sign In to add comment