Files
@ 5e5a8c4642c5
Branch filter:
Location: OneEye/exp/tests/test_geometry.py - annotation
5e5a8c4642c5
1.8 KiB
text/x-python
presenting detected peaks
7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 7c268e382b96 | import math
import random
from unittest import TestCase
from geometry import Line,EPoint
random.seed(361)
class TestLine(TestCase):
def testFromNormal(self):
p=Line.fromNormal(1,0,-1) # x-1=0
self.assertEqual(p._alpha,0)
self.assertEqual(p._d,1)
q=Line.fromNormal(1,1,-2) # x+y-2=0
self.assertAlmostEqual(q._alpha,math.pi/4)
self.assertAlmostEqual(q._d,math.sqrt(2))
r=Line.fromNormal(0,1,1) # y+1=0
self.assertAlmostEqual(r._alpha,math.pi*3/2)
self.assertEqual(r._d,1)
def testFromPoints(self):
ab=Line.fromPoints(EPoint(1,3),EPoint(1,-1))
self.assertEqual(ab._alpha,0)
self.assertEqual(ab._d,1)
cd=Line.fromPoints(EPoint(0,2),EPoint(-1,3))
self.assertAlmostEqual(cd._alpha,math.pi/4)
self.assertAlmostEqual(cd._d,math.sqrt(2))
ef=Line.fromPoints(EPoint(-2,-1),EPoint(-4,-1))
self.assertAlmostEqual(ef._alpha,math.pi*3/2)
self.assertEqual(ef._d,1)
def testIntersect(self):
for i in range(10):
a=EPoint(random.randint(-100,100),random.randint(-100,100))
b=EPoint(random.randint(-100,100),random.randint(-100,100))
c=EPoint(random.randint(-100,100),random.randint(-100,100))
ab=Line.fromPoints(a,b)
ac=Line.fromPoints(a,c)
a_=ab.intersect(ac)
self.assertAlmostEqual(a.x,a_.x)
self.assertAlmostEqual(a.y,a_.y)
def testDistanceTo(self):
p=Line(0,1)
q=Line(math.pi/4,math.sqrt(2))
r=Line(math.pi*3/2,1)
a=EPoint(0,0)
b=EPoint(1,0)
c=EPoint(-1,-1)
self.assertAlmostEqual(p.distanceTo(a),1)
self.assertAlmostEqual(p.distanceTo(b),0)
self.assertAlmostEqual(p.distanceTo(c),2)
self.assertAlmostEqual(q.distanceTo(a),math.sqrt(2))
self.assertAlmostEqual(q.distanceTo(b),math.sqrt(2)/2)
self.assertAlmostEqual(q.distanceTo(c),2*math.sqrt(2))
self.assertAlmostEqual(r.distanceTo(a),1)
self.assertAlmostEqual(r.distanceTo(b),1)
self.assertAlmostEqual(r.distanceTo(c),0)
|