Advertisement
Windspar

Example drawing line 90 degree from other line

Nov 14th, 2022
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. import pygame
  2.  
  3. class Line:
  4.     def __init__(self, color, start_point, end_point):
  5.         self.start_point = start_point
  6.         self.end_point = end_point
  7.         self.color = color
  8.  
  9.     def draw(self, surface):
  10.         pygame.draw.line(surface, self.color, self.start_point, self.end_point)
  11.  
  12.     def get_line_angle_front(self, angle, color, length=0):
  13.         vector = pygame.Vector2(self.end_point) - pygame.Vector2(self.start_point)
  14.         vector.normalize_ip()
  15.         vector.rotate_ip(angle)
  16.         return self._get_line_angle(vector, self.start_point, angle, color, length)
  17.  
  18.     def get_line_angle_back(self, angle, color, length=0):
  19.         vector = pygame.Vector2(self.start_point) - pygame.Vector2(self.end_point)
  20.         vector.normalize_ip()
  21.         vector.rotate_ip(angle)
  22.         return self._get_line_angle(vector, self.end_point, angle, color, length)
  23.  
  24.     def _get_line_angle(self, vector, point, angle, color, length):
  25.         if length < 1:
  26.             length = pygame.Vector2(self.start_point).distance_to(pygame.Vector2(self.end_point))
  27.  
  28.         end_point = pygame.Vector2(point) + vector * length
  29.         return Line(color, point, vec_to_int(end_point))
  30.  
  31. def vec_to_int(vector):
  32.     return tuple(map(int, vector))
  33.  
  34. def main(caption, size, flags=0):
  35.     pygame.display.set_caption(caption)
  36.     surface = pygame.display.set_mode(size, flags)
  37.     clock = pygame.time.Clock()
  38.     rect = surface.get_rect()
  39.     running = True
  40.     delta = 0
  41.     fps = 30
  42.  
  43.     line = Line("dodgerblue", rect.center, (rect.w - 100, rect.h - 100))
  44.     lines = [line,
  45.              line.get_line_angle_front(90, "lawngreen", 0),
  46.              line.get_line_angle_back(90, "firebrick", 30)
  47.              ]
  48.  
  49.     while running:
  50.         for event in pygame.event.get():
  51.             if event.type == pygame.QUIT:
  52.                 running = False
  53.  
  54.         surface.fill("gray10")
  55.         for l in lines:
  56.             l.draw(surface)
  57.  
  58.         pygame.display.flip()
  59.         delta = clock.tick(fps) * 0.001
  60.  
  61. if __name__ == "__main__":
  62.     pygame.init()
  63.     main("Alter Image", (400, 300))
  64.     pygame.quit()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement