def test_limit(): assert v0.limit(v0, 1) == v0 assert v0.limit(v1, 1) == v0 assert v1.limit(v1, 0).equalsInRange(v0, .01) assert v1.limit(v0, 1) == v1 assert v1.limit(v1, -1) == v1 assert v1.limit(Vector(234, 0), 1).equalsInRange(Vector(1, 4), .01)
def test_from_polar(): assert fromPolar(0, 0) == Vector(0, 0) assert fromPolar(1, 0).equalsInRange(Vector(1, 0), .01) assert fromPolar(1, math.pi / 4).equalsInRange( Vector(math.sqrt(2) / 2, math.sqrt(2) / 2), .01) assert fromPolar(10, 3 * math.pi / 2).equalsInRange(Vector(0, -10), .01)
def test_rotateAbout(): assert v0.rotateAbout(math.pi, v0) == v0 assert v1.rotateAbout(0, v0).equalsInRange(v1, .01) assert v1.rotateAbout(math.pi * 1 / 2, v0).equalsInRange(Vector(-4, 3), .01) assert v1.rotateAbout(math.pi * 1 / 2, v1).equalsInRange(Vector(3, 4), .01) assert v0.rotateAbout(math.pi * 1 / 2, v1).equalsInRange(Vector(7, 1), .01)
def test_rotate(): assert c0.copy().rotate(0) == c0 assert c1.copy().rotate(0) == c1 assert c1.copy().rotate(math.pi * 1 / 2.) == c1 # About assert c0.copy().rotate(0, about=Vector(0, 0)) == c0 assert c0.copy().rotate(math.pi * 1/2., about=Vector(1,0)).equalsInRange( \ Circle(Vector(1,-1), 0), .01)
def test_area2(): assert c0.area2() == 0 assert abs(c1.area2() - math.pow(3, 4) / 2. * math.pi) < .01 # About assert c0.area2(about=Vector(1, 0)) == 0 assert abs(c1.area2(about=Vector(5, 0)) - c1.area2()) < .01 assert abs(c1.area2(about=Vector(1,0)) - \ (c1.area2() + 4*4*c1.area())) < .01
def test_move(): print "0:", str(s1) assert s0.copy().move(Vector(0,0)) == \ Segment(Vector(0,0), Vector(0,0)) assert s0.copy().move(Vector(1,1)) == \ Segment(Vector(1,1), Vector(1,1)) assert s1.copy().move(Vector(1,10)) == \ Segment(Vector(1,10), Vector(4,14))
def test_norm(): assert v0.norm() == Vector(0, 0) assert v1.norm().len() == 1 and \ v1.norm().angle() == v1.angle() assert v2.norm().len() == 1 and \ v2.norm().angle() == v2.angle() assert v3.norm().len() == 1 and \ v3.norm().angle() == v3.angle()
def test_rotate(): assert s0.copy().rotate(0) == s0 assert s0.copy().rotate(math.pi * 1. / 2) == s0 assert s1.copy().rotate(0).equalsInRange(s1, .01) assert s1.copy().rotate(math.pi * 1./2).equalsInRange( \ Segment(Vector(0,0), Vector(-4,3)), .01) # About assert s0.copy().rotate(math.pi * 1./2, Vector(1,1)).equalsInRange( \ Segment(Vector(2,0), Vector(2,0)), .01) assert s1.copy().rotate(math.pi * 1./2, Vector(1,1)).equalsInRange( \ Segment(Vector(2,0), Vector(-2,3)), .01)
def test_polygonPolygon(): assert len(intersections.between(s0,s0)) == 0 assert len(intersections.between(s0,s1)) == 0 s1s1 = intersections.between(s1, s1, pThresh=.001) assert len(s1s1) == 4 assert listEqualsInRange(s1s1, \ [Vector(5,0), Vector(10,5), Vector(5,10), Vector(0,5)], .001) s1s1c = intersections.between(s1.copy().move(Vector(1,1)), s1) assert len(s1s1c) == 2 assert listEqualsInRange(s1s1c, \ [Vector(10,1), Vector(1,10)], .001) # Literally a corner case s1s2 = intersections.between(s1, s2) assert len(s1s2) == 2 assert listEqualsInRange(s1s2, \ [Vector(0,5), Vector(5,0)], .001)
def test_getCollisionAxes(): s0 = Shape(Vector(0, 0)) s1 = Shape([Vector(0, -2), Vector(4, -2), Vector(4, 2), Vector(0, 2)]) axes0 = c0.getCollisionAxes(s1) assert len(axes0) == 1 assert axes0[0].equalsInRange(Vector(-2, 0), .01) axes1 = c1.getCollisionAxes(s1) assert len(axes1) == 1 assert axes1[0].equalsInRange(Vector(3, 0), .01)
def test_circleCircle(): assert len(intersections.between(c0,c0)) == 0 assert len(intersections.between(c1,c0)) == 0 assert len(intersections.between(c1,c1)) == 0 # Intersections c1c2 = intersections.between(c1, c2) assert len(c1c2) == 2 assert listEqualsInRange(c1c2, \ [Vector(2,4), Vector(2,-4)], .01) # Outside c1c3 = intersections.between(c1,c3) assert len(c1c3) == 0 # Inside c1c4 = intersections.between(c1,c4) assert len(c1c4) == 0 # Angled c1c1c = intersections.between( \ c1, c1.copy().move(Vector(-5, 5))) assert len(c1c1c) == 2 assert listEqualsInRange(c1c1c, \ [Vector(5,5), Vector(0,0)], .01)
def test_shadow(): assert c0.shadow(Vector(0, 0)) == Region(0, 0) assert c0.shadow(Vector(1, 1)) == Region(0, 0) assert c1.shadow(Vector(0, 0)) == Region(0, 0) assert c1.shadow(Vector(1, 0)) == Region(2, 8) assert c1.shadow(Vector(0, 1)) == Region(-3, 3) assert c1.shadow(Vector(0, -1)) == Region(-3, 3)
def test_equalsInRange(): assert v0.equalsInRange(v0, 0.01) assert v1.equalsInRange(v1, 0.01) assert v1.equalsInRange(Vector(v1.x + 0.1, v1.y), 0.2) assert v1.equalsInRange(Vector(v1.x - 0.1, v1.y), 0.2) assert not v1.equalsInRange(Vector(v1.x + 0.5, v1.y), 0.2) assert v1.equalsInRange(Vector(v1.x, v1.y + 0.1), 0.2) assert v1.equalsInRange(Vector(v1.x, v1.y - 0.1), 0.2) assert not v1.equalsInRange(Vector(v1.x, v1.y + 0.5), 0.2)
def test_project(): assert v0.project(v0) == v0 assert v1.project(v0) == v0 assert v0.project(v1) == v0 assert v1.project(v1).equalsInRange(v1, .01) assert v1.project(Vector(10.5, 0)).equalsInRange(Vector(3, 0), .01) assert v1.project(Vector(-10.5, 0)).equalsInRange(Vector(3, 0), .01) assert v1.project(Vector(0, 100)).equalsInRange(Vector(0, 4), .01) assert v1.project(v1.rotate(math.pi / 2)).equalsInRange(v0, .01)
def test_polygonCircle(): # Corner s1c1 = intersections.between(s1, Circle(Vector(0,0), 2)) assert len(s1c1) == 2 assert listEqualsInRange(s1c1, \ [Vector(2,0), Vector(0,2)], .01) # Edge s1c2 = intersections.between(s1, Circle(Vector(-4,5), 5)) assert len(s1c2) == 2 assert listEqualsInRange(s1c2, [Vector(0,2), Vector(0,8)], .01) # No intersect s1c3 = intersections.between(s1, Circle(Vector(-4, 5), 3)) assert len(s1c3) == 0
def test_overUnder(): # Over assert s1.overUnder(Vector(1, 3)) == 1 # Under assert s1.overUnder(Vector(2, 1)) == -1 # On assert s1.overUnder(s1.center(), onThresh=.001) == 1 # Out of range (right) assert s1.overUnder(Vector(3.1, 5)) == 0 # Out of range (left) assert s1.overUnder(Vector(-.1, 5)) == 0 # On segment (perfectly vertical) assert s4.overUnder(Vector(-1, 0), onThresh=.001) == 0 # Over segment (perfectly horizontal) assert s7.overUnder(Vector(1, 1)) == 1 # Under segment (perfectly horizontal) assert s7.overUnder(Vector(1, -11)) == -1
import pytest from sphericalcow.geom.segment import Segment from sphericalcow.geom.vector import Vector import math s0 = Segment(Vector(0, 0), Vector(0, 0)) s1 = Segment(Vector(0, 0), Vector(3, 4)) s2 = Segment(Vector(1, 0), Vector(2, 5)) s3 = Segment(Vector(-3, -1), Vector(1, 1)) s4 = Segment(Vector(-1, -1), Vector(-1, 1)) s5 = Segment(Vector(1, 3), Vector(5, 3)) s6 = Segment(Vector(3, 3), Vector(7, 3)) s7 = Segment(Vector(0, 0), Vector(10, 0)) def test_equalsInRange(): assert s0.equalsInRange(s0, .01) assert not s0.equalsInRange(s1, .01) assert s0.equalsInRange(s1, 5) assert s1.equalsInRange(Segment(s1.p2, s1.p1), .01) def test_copy(): s0cp = s0.copy() s1cp = s1.copy() assert s0cp is not s0 assert s1cp is not s1 assert s0 == s0cp assert s1 == s1cp
def test_contains(): assert not c0.contains(Vector(1, 1)) assert not c1.contains(Vector(1, 0)) assert not c1.contains(Vector(5, 5)) assert c1.contains(Vector(5, 2)) assert c1.contains(Vector(2.1, 0))
import pytest from sphericalcow.geom.shapes.circle import Circle from sphericalcow.geom.region import Region from sphericalcow.geom.shape import Shape from sphericalcow.geom.vector import Vector import math c0 = Circle(Vector(0, 0), 0) c1 = Circle(Vector(5, 0), 3) def test_copy(): assert c0.copy() == c0 assert not c0.copy() is c0 assert c1.copy() == c1 assert not c1.copy() is c1 def test_equalsInRange(): assert c0.equalsInRange(c0, .001) assert c0.equalsInRange(Circle(Vector(1, 1), 1), 1.5) def test_rotate(): assert c0.copy().rotate(0) == c0 assert c1.copy().rotate(0) == c1 assert c1.copy().rotate(math.pi * 1 / 2.) == c1 # About assert c0.copy().rotate(0, about=Vector(0, 0)) == c0
def test_intersect(): # Non-intersects assert len(s0.intersect(s1)) == 0 assert len(s0.intersect(s0)) == 0 assert len(s2.intersect(s3)) == 0 # Point intersects assert len(s1.intersect(s2)) == 1 assert s1.intersect(s2)[0].equalsInRange(Vector(15. / 11, 20. / 11), .01) assert len(s3.intersect(s4)) == 1 assert s3.intersect(s4)[0].equalsInRange(Vector(-1, 0), .01) # Segment intersects s1s1 = s1.intersect(s1) assert len(s1s1) == 2 assert Segment(s1s1[0], s1s1[1]).equalsInRange(s1, .01) s5s6 = s5.intersect(s6, pThresh=.001) assert len(s5s6) == 2 assert Segment(s5s6[0], s5s6[1]).equalsInRange( \ Segment(Vector(3,3), Vector(5,3)), .01) # Edge cases assert len( \ Segment(Vector(10, 0), Vector(10, 10)).intersect( \ Segment(Vector(10, 0), Vector(0, 10)))) == 0 assert len( \ Segment(Vector(10, 10), Vector(10, 0)).intersect( \ Segment(Vector(10, 0), Vector(0, 10)))) == 0
def test_rotate(): assert v0.rotate(math.pi) == v0 assert v1.rotate(0).equalsInRange(v1, .01) assert v1.rotate(math.pi * 1 / 2).equalsInRange(Vector(-4, 3), .01) assert v1.rotate(math.pi * 2 / 2).equalsInRange(Vector(-3, -4), .01) assert v1.rotate(math.pi * 3 / 2).equalsInRange(Vector(4, -3), .01)
def test_sub(): assert v0.sub(v1) == Vector(-3, -4) assert v1.sub(v0) == v1 assert v1.sub(v1) == v0
def test_add(): assert v0.add(v1) == v1 assert v1.add(v0) == v1 assert v1.add(v1) == Vector(6, 8)
def test_center(): assert s0.center() == Vector(0, 0) assert s1.center() == Vector(1.5, 2) assert s2.center() == Vector(1.5, 2.5) assert s3.center() == Vector(-1, 0) assert s4.center() == Vector(-1, 0)
def test_equalsInRange(): assert c0.equalsInRange(c0, .001) assert c0.equalsInRange(Circle(Vector(1, 1), 1), 1.5)
import pytest from sphericalcow.geom.vector import Vector, fromPolar import math v0 = Vector(0, 0) v1 = Vector(3, 4) v2 = Vector(5, -12) v3 = Vector(0, 5.1) v4 = Vector(-1000.5, 0) v5 = Vector(0, -.011) def test_equalsInRange(): assert v0.equalsInRange(v0, 0.01) assert v1.equalsInRange(v1, 0.01) assert v1.equalsInRange(Vector(v1.x + 0.1, v1.y), 0.2) assert v1.equalsInRange(Vector(v1.x - 0.1, v1.y), 0.2) assert not v1.equalsInRange(Vector(v1.x + 0.5, v1.y), 0.2) assert v1.equalsInRange(Vector(v1.x, v1.y + 0.1), 0.2) assert v1.equalsInRange(Vector(v1.x, v1.y - 0.1), 0.2) assert not v1.equalsInRange(Vector(v1.x, v1.y + 0.5), 0.2) def test_len(): assert v0.len() == 0 assert v1.len() == 5 assert v2.len() == 13 def test_angle():
def test_mul(): assert v0.mul(1) == Vector(0, 0) assert v1.mul(0) == Vector(0, 0) assert v1.mul(1) == Vector(3, 4) assert v1.mul(10) == Vector(30, 40)
def test_init(): assert Rect(0,0,1,1,0).equalsInRange( \ Shape([Vector(0,0), Vector(1,0), Vector(1,1), Vector(0,1)]), \ .01)
import pytest from sphericalcow.geom.shape import Shape from sphericalcow.geom.vector import Vector from sphericalcow.geom.shapes.circle import Circle import sphericalcow.geom.intersections as intersections from sphericalcow.util import listEqualsInRange s0 = Shape([Vector(0,0)]) s1 = Shape([Vector(0,0), Vector(10,0), Vector(10,10), Vector(0,10)]) s2 = Shape([Vector(0,0), Vector(10,0), Vector(0,10)]) c0 = Circle(Vector(0,0), 0) c1 = Circle(Vector(5,0), 5) c2 = Circle(Vector(-1,0), 5) c3 = Circle(Vector(-3,0), 2) c4 = Circle(Vector(4,0), 3) def test_polygonPolygon(): assert len(intersections.between(s0,s0)) == 0 assert len(intersections.between(s0,s1)) == 0 s1s1 = intersections.between(s1, s1, pThresh=.001) assert len(s1s1) == 4 assert listEqualsInRange(s1s1, \ [Vector(5,0), Vector(10,5), Vector(5,10), Vector(0,5)], .001) s1s1c = intersections.between(s1.copy().move(Vector(1,1)), s1) assert len(s1s1c) == 2 assert listEqualsInRange(s1s1c, \ [Vector(10,1), Vector(1,10)], .001) # Literally a corner case