Module lineline
[hide private]
[frames] | no frames]

Source Code for Module lineline

  1  ################################################################################## 
  2  # Copyright (c) 2010, 2011, 2012, 2013, Daniel Urieli, Peter Stone 
  3  # University of Texas at Austin 
  4  # All right reserved 
  5  #  
  6  # Based On: 
  7  #  
  8  # Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam 
  9  # All rights reserved. 
 10  #  
 11  # Redistribution and use in source and binary forms, with or without 
 12  # modification, are permitted provided that the following conditions are met: 
 13  #  
 14  # 1. Redistributions of source code must retain the above copyright notice, this 
 15  # list of conditions and the following disclaimer. 
 16  #  
 17  # 2. Redistributions in binary form must reproduce the above copyright notice, 
 18  # this list of conditions and the following disclaimer in the documentation 
 19  # and/or other materials provided with the distribution. 
 20  #  
 21  # 3. Neither the name of the University of Amsterdam nor the names of its 
 22  # contributors may be used to endorse or promote products derived from this 
 23  # software without specific prior written permission. 
 24  #  
 25  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 26  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 27  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 28  # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 29  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 30  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 31  # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 32  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 33  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 34  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 35  ################################################################################## 
 36   
 37   
 38   
 39   
 40   
 41  ################################################################ 
 42  # The whole file is taken from: 
 43  # http://refactormycode.com/codes/1114-line-line-intersection-test 
 44  ################################################################ 
 45   
 46  # vector class from pygame cookbook http://www.pygame.org/wiki/2DVectorClass 
 47  from vec2d import * 
 48   
49 -def lineline(A,B,C,D):
50 """ Line-line intersection algorithm, 51 returns point of intersection or None 52 """ 53 # ccw from http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/ 54 def ccw(A,B,C): 55 return (C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x)
56 if ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D): 57 # formula from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ 58 ua = float(((D.x-C.x)*(A.y-C.y))-((D.y-C.y)*(A.x-C.x)))/ \ 59 float(((D.y-C.y)*(B.x-A.x))-((D.x-C.x)*(B.y-A.y))) 60 ub = float(((B.x-A.x)*(A.y-C.y))-((B.y-A.y)*(A.x-C.y)))/ \ 61 float(((D.y-C.y)*(B.x-A.x))-((D.x-C.x)*(B.y-A.y))) 62 return Vec2d( A.x+(ua*(B.x-A.x)), \ 63 A.y+(ua*(B.y-A.y))) 64 return None 65
66 -def linelinedemo():
67 """ Graphical demo showing the line line intersection algorithm. 68 Click and hold left mouse button to place first line, 69 right mouse button places second line. 70 A white circle will be draw at the point of intersection. 71 """ 72 import pygame 73 from pygame.locals import QUIT,KEYDOWN,MOUSEBUTTONDOWN,MOUSEBUTTONUP 74 75 pygame.init() 76 screen = pygame.display.set_mode((256,256)) 77 clock = pygame.time.Clock() 78 79 A,B,C,D = None,None,None,None 80 running = True 81 while running: 82 for event in pygame.event.get(): 83 if event.type in (QUIT, KEYDOWN): 84 running = False 85 elif event.type == MOUSEBUTTONDOWN and event.button == 1: 86 A = Vec2d(pygame.mouse.get_pos()) 87 B = None 88 elif event.type == MOUSEBUTTONDOWN and event.button == 3: 89 C = Vec2d(pygame.mouse.get_pos()) 90 D = None 91 elif event.type == MOUSEBUTTONUP and event.button == 1: 92 B = Vec2d(pygame.mouse.get_pos()) 93 elif event.type == MOUSEBUTTONUP and event.button == 3: 94 D = Vec2d(pygame.mouse.get_pos()) 95 96 screen.fill((0,0,0)) 97 98 if A is not None: 99 endpos = B or pygame.mouse.get_pos() 100 pygame.draw.line(screen, (255,0,0), A, endpos) 101 102 if C is not None: 103 endpos = D or pygame.mouse.get_pos() 104 pygame.draw.line(screen, (0,255,0), C, endpos) 105 106 if A and B and C and D: 107 v = lineline(A,B,C,D) 108 if v: 109 pygame.draw.circle(screen, (255,255,255), (int(v.x),int(v.y)), 5, 1) 110 111 pygame.display.flip() 112 clock.tick(100)
113 114 if __name__=="__main__": 115 linelinedemo() 116