-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
33 lines (25 loc) · 885 Bytes
/
utility.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
from random import uniform
from math import sqrt, cos, sin, pi
def distance(theAPos, theBPos):
aDelta = (theAPos[0] - theBPos[0], theAPos[1] - theBPos[1])
aDist = sqrt(aDelta[0] * aDelta[0] + aDelta[1] * aDelta[1])
return int(aDist)
def add(apos, bpos):
return (apos[0] + bpos[0] , apos[1] + bpos[1])
#vector created is first term points to second
def sub(apos, bpos):
return (bpos[0] - apos[0] , bpos[1] - apos[1])
def mag(v):
amag = v[0] * v[0] + v[1] * v[1]
amag += 0.0001
amag = sqrt(amag)
return amag
def normalize(v):
amag = mag(v)
return (v[0] / amag, v[1] / amag)
def mul(adir, ascalar):
return (adir[0] * ascalar, adir[1] * ascalar)
def randPointAtDistance(thePoint, theDistance):
randAngle = uniform(0., 359.9) / 360. * pi * 2.
randPoint = add(mul((cos(randAngle), sin(randAngle)), theDistance), thePoint)
return randPoint