from math import *
def Normalize(vec):
    mag = VectorLen(vec)
    vec2 = [float(vec[0])/float(mag),
            float(vec[1])/float(mag)]
    return vec2
def rndint(num):
    return int(round(num))
def VectorMultiply(vec1,vec2):
    return [vec1[0]*vec2[0],
            vec1[1]*vec2[1]]
def VectorDivide(vec1,vec2):
    if vec2[0] == 0: vec2[0] = 0.0000001
    if vec2[1] == 0: vec2[1] = 0.0000001
    return [float(vec1[0])/float(vec2[0]),
            float(vec1[1])/float(vec2[1])]
def VectorLen(vec):
    return sqrt((vec[0]**2)+(vec[1]**2))
def VectorNegate(vec):
    return [-vec[0],-vec[1]]
def VectorAdd(vec1,vec2):
    return [vec1[0]+vec2[0],vec1[1]+vec2[1]]
def VectorSubtract(vec1,vec2):
    return [vec1[0]-vec2[0],vec1[1]-vec2[1]]
def ScalarVector(s,v):
    return [s*v[0],s*v[1]]
def absVector(vec):
    return [abs(vec[0]),abs(vec[1])]
def DotProduct(vec1,vec2):
    return (vec1[0]*vec2[0])+(vec1[1]*vec2[1])
def DistToPoint(ray,dist):
    point = [ray.pos[0],ray.pos[1]]
    point[0] += ray.dir[0]*dist
    point[1] += ray.dir[1]*dist
    return point
def CollideCircle(ray,obj):
    p = VectorSubtract(ray.pos,obj.pos)
    d = ray.dir
    r = obj.size
    b = -DotProduct(p,d)
    det = b*b - DotProduct(p,p) + r*r
    if det < 0:
        return False
    det = sqrt(det)
    i1 = b - det
    i2 = b + det
    if i2 < 0: return False
    if i1 < 0: return i2
    i = min(i1,i2)
    return i
def CollideLine(ray,obj):
    return (\
        (ray.dir[0]*ray.pos[1] + ray.dir[1]*(obj.p1[0]-ray.pos[0])) -\
        (ray.dir[0] * obj.p2[1])) /\
        (ray.dir[1]*(obj.p1[0]+obj.p2[0])-\
         ray.dir[0]*(obj.p1[1]+obj.p2[1]))











        
