Advertisement
Windspar

Pygame Line Intersect Line

Jun 25th, 2021
1,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. # Line Intersect Line
  2. import pygame
  3.  
  4. class Line:
  5.     def __init__(self, x1, y1, x2, y2, color, width=1):
  6.         self.a = pygame.Vector2(x1, y1)
  7.         self.b = pygame.Vector2(x2, y2)
  8.         self.color = color
  9.         self.width = width
  10.  
  11.     def draw(self, surface):
  12.         x1, y1, x2, y2 = self.get_points()
  13.  
  14.         pygame.draw.aaline(surface, self.color, (x1, y1), (x2, y2), self.width)
  15.  
  16.     def get_points(self):
  17.         x1, y1 = self.a
  18.         x2, y2 = self.b
  19.         return x1, y1, x2, y2
  20.  
  21.     # @return None or Vector2 of intersect
  22.     def intersect_line(self, line):
  23.         if not isinstance(line, Line):
  24.             return
  25.  
  26.         x1, y1, x2, y2 = line.get_points()
  27.         x3, y3, x4, y4 = self.get_points()
  28.  
  29.         den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
  30.         if den == 0:
  31.             return
  32.  
  33.         t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den
  34.         u = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / den
  35.         if 0 < t < 1 and 0 < u < 1:
  36.             return pygame.Vector2(x1 + t * (x2 - x1),
  37.                            y1 + t * (y2 - y1))
  38.  
  39. class Main:
  40.     def __init__(self, caption, width, height, flags=0):
  41.         pygame.display.set_caption(caption)
  42.         self.surface = pygame.display.set_mode((width, height), flags)
  43.         self.rect = self.surface.get_rect()
  44.         self.clock = pygame.time.Clock()
  45.         self.running = False
  46.         self.delta = 0
  47.         self.fps = 60
  48.  
  49.         self.line_color = pygame.Color("snow")
  50.         self.lines = (Line(150, 200, 350, 200, self.line_color),
  51.                       Line(200, 100, 250, 300, self.line_color))
  52.  
  53.         self.point = self.lines[0].intersect_line(self.lines[1])
  54.  
  55.     def draw(self):
  56.         for line in self.lines:
  57.             line.draw(self.surface)
  58.  
  59.         if self.point:
  60.             pygame.draw.circle(self.surface, self.line_color, self.point, 5)
  61.  
  62.  
  63.     def mainloop(self):
  64.         self.running = True
  65.         while self.running:
  66.             for event in pygame.event.get():
  67.                 if event.type == pygame.QUIT:
  68.                     self.running = False
  69.  
  70.             self.surface.fill(pygame.Color("black"))
  71.             self.draw()
  72.             pygame.display.flip()
  73.             self.delta = self.clock.tick(self.fps)
  74.  
  75. def main():
  76.     pygame.init()
  77.     app = Main("Line Intersection Line", 400, 400)
  78.     app.mainloop()
  79.     pygame.quit()
  80.  
  81. main()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement