-
Notifications
You must be signed in to change notification settings - Fork 0
/
linex.py
151 lines (127 loc) · 4.76 KB
/
linex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env python
import pygame
def slope(p1, p2) :
return (p2[1] - p1[1]) * 1. / (p2[0] - p1[0])
def y_intercept(slope, p1) :
return p1[1] - 1. * slope * p1[0]
def intersect(line1, line2) :
min_allowed = 1e-5 # guard against overflow
big_value = 1e10 # use instead (if overflow would have occurred)
m1 = slope(line1[0], line1[1])
print( 'm1: %d' % m1 )
b1 = y_intercept(m1, line1[0])
print( 'b1: %d' % b1 )
m2 = slope(line2[0], line2[1])
print( 'm2: %d' % m2 )
b2 = y_intercept(m2, line2[0])
print( 'b2: %d' % b2 )
if abs(m1 - m2) < min_allowed :
x = big_value
else :
x = (b2 - b1) / (m1 - m2)
y = m1 * x + b1
y2 = m2 * x + b2
print( '(x,y,y2) = %d,%d,%d' % (x, y, y2) )
return (int(x),int(y))
def segment_intersect(line1, line2) :
intersection_pt = intersect(line1, line2)
print( line1[0][0], line1[1][0], line2[0][0], line2[1][0], intersection_pt[0] )
print( line1[0][1], line1[1][1], line2[0][1], line2[1][1], intersection_pt[1] )
if (line1[0][0] < line1[1][0]) :
if intersection_pt[0] < line1[0][0] or intersection_pt[0] > line1[1][0] :
print( 'exit 1' )
return None
else :
if intersection_pt[0] > line1[0][0] or intersection_pt[0] < line1[1][0] :
print( 'exit 2' )
return None
if (line2[0][0] < line2[1][0]) :
if intersection_pt[0] < line2[0][0] or intersection_pt[0] > line2[1][0] :
print( 'exit 3' )
return None
else :
if intersection_pt[0] > line2[0][0] or intersection_pt[0] < line2[1][0] :
print( 'exit 4' )
return None
return intersection_pt
# window dimensions
w = 800
h = 800
# slightly off-white background
bgcolor = (0xf1,0xf2,0xf3)
# black for drawing
fgcolor = (0,0,0)
# red for "active" segment endpt
redcolor = (0xff,0,0)
# CPU throttle, higher number = less CPU hogging
delay = 200
# key repeat delay, interval
key_delay = 20
key_interval = 20
# using +1 here to avoid using -1 elsewhere
screen = pygame.display.set_mode((w+1, h+1))
clock = pygame.time.Clock()
pygame.key.set_repeat(key_delay, key_interval)
line1 = [(100.,100.),(700.,700.)]
line2 = [(100.,700.),(700.,100.)]
intersection = segment_intersect(line1, line2)
screen.fill(bgcolor)
pygame.draw.aaline(screen, fgcolor, line1[0], line1[1])
pygame.draw.aaline(screen, fgcolor, line2[0], line2[1])
if intersection is not None :
pygame.draw.circle(screen, fgcolor, intersection, 10, 2)
pygame.draw.circle(screen, redcolor, (int(line1[0][0]), int(line1[0][1])), 5, 1)
pygame.display.flip()
active_pt = 0
prev_active_pt = 0
running = True
while running:
for event in pygame.event.get() :
if event.type == pygame.QUIT :
running = False
elif event.type == pygame.KEYDOWN :
if event.key == pygame.K_q :
# quit
running = False
elif event.type == pygame.KEYUP :
if event.key == pygame.K_0 :
active_pt = 0
elif event.key == pygame.K_1 :
active_pt = 1
elif event.key == pygame.K_2 :
active_pt = 2
elif event.key == pygame.K_3 :
active_pt = 3
if prev_active_pt != active_pt :
prev_active_pt = active_pt
screen.fill(bgcolor)
pygame.draw.aaline(screen, fgcolor, line1[0], line1[1])
pygame.draw.aaline(screen, fgcolor, line2[0], line2[1])
if intersection is not None :
pygame.draw.circle(screen, fgcolor, intersection, 10, 2)
if (active_pt < 2) :
red_pt = (int(line1[active_pt][0]), int(line1[active_pt][1]))
else :
red_pt = (int(line2[active_pt - 2][0]), int(line2[active_pt - 2][1]))
pygame.draw.circle(screen, redcolor, red_pt, 5, 1)
pygame.display.flip()
elif event.type == pygame.MOUSEBUTTONUP :
if (active_pt < 2) :
line1[active_pt] = pygame.mouse.get_pos()
print( line1[active_pt] )
else :
line2[active_pt - 2] = pygame.mouse.get_pos()
print( line2[active_pt - 2] )
intersection = segment_intersect(line1, line2)
screen.fill(bgcolor)
pygame.draw.aaline(screen, fgcolor, line1[0], line1[1])
pygame.draw.aaline(screen, fgcolor, line2[0], line2[1])
if intersection is not None :
pygame.draw.circle(screen, fgcolor, intersection, 10, 2)
if (active_pt < 2) :
pygame.draw.circle(screen, redcolor, line1[active_pt], 5, 1)
else :
pygame.draw.circle(screen, redcolor, line2[active_pt - 2], 5, 1)
pygame.display.flip()
# sleep
clock.tick(delay)