Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- import numpy as np
- import random
- class Main:
- def __init__(self, fps=60):
- pygame.init()
- self.screen = pygame.display.set_mode((1080,720))
- self.clock = pygame.time.Clock()
- self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
- #line
- self.x, self.y = 200, 100 #отступ от (0, 0)
- 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)]
- self.thickness = 5 #толщина линии
- #/line
- self.triangles = []
- self.pos = (0, 0)
- self.bool_pos = 0
- self.screen.fill("white")
- self.print_polygon(self.coordinates)
- while True:
- self.triangles = []
- self.triangulate()
- 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)]
- hasPointOfPolygonMouse = self.hasPointOfPolygonMouse()
- if hasPointOfPolygonMouse[0] and self.bool_pos:
- pygame.draw.polygon(self.screen, (random.randint(0, 256)%256,random.randint(0, 256)%256, random.randint(0, 256)%256), hasPointOfPolygonMouse[1])
- self.bool_pos = 0
- #обработчик событий
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN :
- if event.key == pygame.K_ESCAPE:
- pygame.quit()
- sys.exit()
- if event.type == pygame.MOUSEBUTTONDOWN :
- self.pos = event.pos
- self.bool_pos = 1
- pygame.display.flip()
- self.clock.tick(fps)
- def print_polygon(self, coordinates):
- pygame.draw.aalines(self.screen, "black", True, coordinates)
- # 2 Левая тройка векторов?
- def isLeft(self, i):
- A = self.coordinates[i%len(self.coordinates)]
- B = self.coordinates[(i+1)%len(self.coordinates)]
- C = self.coordinates[(i+2)%len(self.coordinates)]
- AB = {"x": B[0] - A[0], "y": B[1] - A[1]}
- AC = {"x": C[0] - A[0], "y": C[1] - A[1]}
- return AB["x"] * AC["y"] - AC["x"] * AB["y"] < 0
- def hasPointOfPolygonMouse(self):
- for triangle in self.triangles:
- A = triangle[0]
- B = triangle[1]
- C = triangle[2]
- for j in range(0, len(self.coordinates)):
- VP0 = self.cross(A, B, self.pos)
- VP1 = self.cross(B, C, self.pos)
- VP2 = self.cross(C, A, self.pos)
- s_l = np.sign([VP0, VP1, VP2])
- if s_l[0] == s_l[1] and s_l[1] == s_l[2] and s_l[0] == s_l[2]:
- return (True, triangle)
- for h in range(len(s_l)):
- if s_l[h] == 0:
- if s_l[(h+1)%len(s_l)] == s_l[(h+2)%len(s_l)]:
- return (True, triangle)
- return (False, (None, None))
- def cross(self, P1, P2, P): #ABxAP, BCxBP and CAxCP.
- vector_P1_P2 = (P2[0]-P1[0], P2[1]-P1[1])
- vector_P1_P = (P[0]-P1[0], P[1]-P1[1])
- return np.cross(vector_P1_P2, vector_P1_P)
- # 3 Есть ли другие точки внутри рассматриваемого треугольника?
- def hasPointOfPolygon(self, i):
- A = self.coordinates[i%len(self.coordinates)]
- B = self.coordinates[(i+1)%len(self.coordinates)]
- C = self.coordinates[(i+2)%len(self.coordinates)]
- for j in range((i+3)%len(self.coordinates), len(self.coordinates)-i%len(self.coordinates)):
- VP0 = self.cross(A, B, self.coordinates[j%len(self.coordinates)])
- VP1 = self.cross(B, C, self.coordinates[j%len(self.coordinates)])
- VP2 = self.cross(C, A, self.coordinates[j%len(self.coordinates)])
- s_l = np.sign([VP0, VP1, VP2])
- if s_l[0] == s_l[1] and s_l[1] == s_l[2] and s_l[0] == s_l[2]:
- return False
- for h in range(len(s_l)):
- if s_l[h] == 0:
- if s_l[(h+1)%len(s_l)] == s_l[(h+2)%len(s_l)]:
- return False
- return True
- #Триангуляция
- def triangulate(self):
- i = 0
- while len(self.coordinates) >= 3:
- if self.isLeft(i) and self.hasPointOfPolygon(i):
- self.triangles.append([self.coordinates[(i)%len(self.coordinates)], self.coordinates[(i+1)%len(self.coordinates)], self.coordinates[(i+2)%len(self.coordinates)]])
- if len(self.coordinates) == 3:
- break
- del self.coordinates[(i+1)%len(self.coordinates)]
- else:
- i += 1
- i = i%len(self.coordinates)
- Main(24)
Add Comment
Please, Sign In to add comment