from intersect import *

def test_slope():
    point1 = Point(2.,2.)
    point2 = Point(1.,1.)
    assert slope(point1, point2) == 1
    point3 = Point(0,3.)
    point4 = Point(0,1.)
    assert slope(point3, point4) == "vert"
    p1 = Point(-2.,2.)
    p2 = Point(2.,0.)
    assert slope(p1,p2) == (-1./2.)
    
def test_b():
    p1 = Point(-2., 2.)
    p2 = Point(2., 0.)
    assert b(p1.x, p1.y, slope(p1,p2)) == 1.
    assert b(p2.x, p2.y, slope(p1,p2)) == 1.
    p3 = Point(0, 4)
    p4 = Point(0, 5)
    assert b(p3.x, p4.y, slope(p3,p4)) == "vert"
    
def test_line_class():
    point1 = Point(3.,1.)
    point2 = Point(2,0)
    line = Line(point1, point2)
    assert line.p1.x == 3
    assert line.slope == 1
    assert line.b == -2.

def test_x_intersection():
    # these two lines intersect at (3,1)
    p1 = Point(1., - 1)
    p2 = Point(0., -2.)
    line1 = Line(p1,p2)
    a1 = Point(5.,0.)
    a2 = Point (1.,2.)
    line2 = Line(a1,a2)
    assert x_intersection(line1.slope, line2.slope, line1.b, line2.b) == 3

def test_y_intersection():
    # using the same two lines as above
    p1 = Point(1., - 1)
    p2 = Point(0., -2.)
    line1 = Line(p1,p2)
    a1 = Point(5.,0.)
    a2 = Point (1.,2.)
    line2 = Line(a1,a2)
    # 3 is the result of the x_intersection
    assert y_intersection(3, line1.slope, line1.b) == 1
    # verifying it works with either line
    assert y_intersection(3, line2.slope, line2.b) == 1

def test_intersection():
    p1 = Point(1., - 1)
    p2 = Point(0., -2.)
    line1 = Line(p1,p2)
    a1 = Point(5.,0.)
    a2 = Point (1.,2.)
    line2 = Line(a1,a2)
    assert intersection(line1, line2) == [3,1]
