~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to lib/mathx/test/TestGeometry.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package mathx.test;
 
7
 
 
8
import mathx.Geometry;
 
9
import mathx.Point2;
 
10
import mathx.MatPoint;
 
11
import mathx.MathUtil;
 
12
import mathx.MatrixGeom;
 
13
 
 
14
class TestGeometry extends haxe.unit.TestCaseX
 
15
{
 
16
        public function testBasic()
 
17
        {
 
18
                var a = Point2.at( 4, 3 );
 
19
                var b = Point2.at( 2, -2 );
 
20
                
 
21
                var c = a.sub( b );
 
22
                assertEqualsF( 2.0, c.x );
 
23
                assertEqualsF( 5.0, c.y );
 
24
                c = a.add( b );
 
25
                assertEqualsF( 6.0, c.x );
 
26
                assertEqualsF( 1.0, c.y );
 
27
                
 
28
                //xAxisAngle quadrant
 
29
                var xb =  Point2.at( 0, 0 );
 
30
                var x1 =  Point2.at( 2,2 );
 
31
                var x2 =  Point2.at(-2,2);
 
32
                var x3 =  Point2.at(-2,-2);
 
33
                var x4 =  Point2.at(2,-2);
 
34
                assertEqualsF( Geometry.xAxisAngle( xb, x1 ), MathUtil.PI/4 );
 
35
                assertEqualsF( Geometry.xAxisAngleV( x2 ), 3*MathUtil.PI/4 );
 
36
                assertEqualsF( Geometry.xAxisAngle( xb, x3 ), -3*MathUtil.PI/4 );
 
37
                assertEqualsF( Geometry.xAxisAngleV( x4 ), -MathUtil.PI/4 );
 
38
                
 
39
        }
 
40
        
 
41
        public function testMatrixGeom()
 
42
        {
 
43
                var at = Point2.at( 3.5, 7.5 );
 
44
                var dir = Point2.at( 1, 0 );    //horizontal
 
45
                assertEqualsMP( MatrixGeom.nextGridFrom( at, dir ), MatPoint.at( 4, 7 ) );
 
46
                dir.y = 1;      //diagonal
 
47
                dir = dir.unit();
 
48
                assertEqualsMP( MatrixGeom.nextGridFrom( at, dir ), MatPoint.at( 4, 8 ) );
 
49
                at.x = 3.1;
 
50
                at.y = 7.1;     //diagonal, too short
 
51
                assertEqualsMP( MatrixGeom.nextGridFrom( at, dir ), MatPoint.at( 4, 8 ) );
 
52
                at.y = 7.9;     //diagonal, though down first
 
53
                assertEqualsMP( MatrixGeom.nextGridFrom( at, dir ), MatPoint.at( 3, 8 ) );
 
54
                //no motion
 
55
                assertEqualsMP( MatrixGeom.nextGridFrom( at, Point2.at( 0, 0 ) ), MatPoint.at( 3, 7 ) );
 
56
                
 
57
                for( dir in MatrixGeom.dirs4 )
 
58
                        assertEqualsMP( MatrixGeom.nextGridBase( dir.promoteF() ), dir );
 
59
                assertEqualsMP( MatrixGeom.nextGridBase( Point2.at( 0, 0 ) ), MatPoint.at( 0, 0 ) );
 
60
        }
 
61
        
 
62
        public function testAngles()
 
63
        {
 
64
                assertEqualsF( Math.PI/4, Geometry.lineOrient( 5 * Math.PI/4 ) );
 
65
                assertEqualsF( 5*Math.PI/6, Geometry.lineOrient( 5*Math.PI/6 ) );
 
66
                
 
67
                assertEqualsF( Math.PI/3, Geometry.lineOrientDiff( Math.PI/6, 5*Math.PI/6 ) );
 
68
                assertEqualsF( Math.PI/4, Geometry.lineOrientDiff( 19*Math.PI/4, -18*Math.PI/4 ) );
 
69
                
 
70
                assertEqualsAngle( 0, Geometry.combineAng( Math.PI/4, 7*Math.PI/4 ) );
 
71
                assertEqualsAngle( Math.PI, Geometry.combineAng( 2*Math.PI/3, -2*Math.PI/3 ) );
 
72
        }
 
73
        
 
74
        public function testLines()
 
75
        {
 
76
                assertEqualsP2( Point2.at( 4, 0 ),
 
77
                        Geometry.intersectLine( Point2.at( 4, 5 ), Point2.at( 4, 4 ),
 
78
                        Point2.at( -234.5, 0 ), Point2.at( 343, 0 ) )
 
79
                        );
 
80
                assertEqualsP2( Point2.at( 4, 0 ),
 
81
                        Geometry.intersectLine( 
 
82
                                Point2.at( -234.5, 0 ), Point2.at( 343, 0 ),
 
83
                                Point2.at( 4, 5 ), Point2.at( 4, 4 )
 
84
                        ) );
 
85
        }
 
86
}