-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetPoints.py
43 lines (37 loc) · 1.14 KB
/
getPoints.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
'''
Code for movement of gatti along a snake or ladder
get_points fxn returns a list of all points which lies on the given line
Two points of the line i.e. origin and target must be passed
'''
def slope(origin, target):
if target['x'] == origin['x']:
return 0
else:
m = (float(target['y'] - origin['y'])) / (target['x'] - origin['x'])
return m
def line_eqn(origin, target):
x = origin['x']
y = origin['y']
c = y - (slope(origin, target)*x)
m = slope(origin, target)
return {'m':m, 'c':c}
#get y for a value of x
def get_y(x, slope, c):
# y = mx + c
y = (slope*x) + c
return y
def get_points(origin, target):
coord_list = []
if origin['x']>target['x']:
step=-1
elif origin['x']<target['x']:
step=1
else:
for i in range(origin['y'],target['y']+1):
coord_list.append([origin['x'],i])
return coord_list
for i in range(origin['x'], target['x']+step,step):
eqn = line_eqn(origin, target)
y = get_y(i, eqn['m'], eqn['c'])
coord_list.append([i, int(y)])
return coord_list