Triangle2

Triangles in 2D space

PhilipRBrenan@yahoo.com, 2004, Perl License

Synopsis

Example t/triangle2.t

#_ Triangle ___________________________________________________________
# Test 2d triangles    
# philiprbrenan@yahoo.com, 2004, Perl License    
#______________________________________________________________________

use Math::Zap::Triangle2;
use Math::Zap::Vector2;
use Test::Simple tests=>27;
 
$a = triangle2
 (vector2(0, 0), 
  vector2(2, 0), 
  vector2(0, 2),
 );
 
$b = triangle2
 (vector2( 0,  0), 
  vector2( 4,  0), 
  vector2( 0,  4),
 );
 
$c = triangle2
 (vector2( 0,  0), 
  vector2(-4,  0), 
  vector2( 0, -4),
 );
 
$d = $b - vector2(1,1);
$e = $c + vector2(1,1);

#print "a=$a\nb=$b\nc=$c\nd=$d\ne=$e\n";

ok($a->containsPoint(vector2( 1,  1)));
ok($a->containsPoint(vector2( 1,  1)));
ok($b->containsPoint(vector2( 2,  0)));
ok($b->containsPoint(vector2( 1,  0)));
ok($c->containsPoint(vector2(-1,  0)));
ok($c->containsPoint(vector2(-2,  0)));
ok($d->containsPoint(vector2( 1, -1)));

ok(!$a->containsPoint(vector2( 9,  1)));
ok(!$a->containsPoint(vector2( 1,  9)));
ok(!$b->containsPoint(vector2( 2,  9)));
ok(!$b->containsPoint(vector2( 9,  0)));
ok(!$c->containsPoint(vector2(-9,  0)));
ok(!$c->containsPoint(vector2(-2,  9)));
ok(!$d->containsPoint(vector2( 9, -1)));

ok( $a->containsPoint(vector2(0.5, 0.5)));
ok(!$a->containsPoint(vector2( -1,  -1)));

ok(vector2(1,2)->rightAngle == vector2(-2, 1));
ok(vector2(1,0)->rightAngle == vector2( 0, 1));

ok($a->area == 2);
ok($c->area == 8);

eval { triangle2(vector2(0, 0), vector2(3, -6), vector2(-3, 6))};
ok($@ =~ /^Narrow triangle2/, 'Narrow triangle');

$t = triangle2(vector2(0,0),vector2(0,10),vector2( 10,0));
$T = triangle2(vector2(0,0),vector2(0,10),vector2(-10,10))+vector2(5, -2);
@p = $t->ring($T);
#print "$_\n" for(@p);
ok($p[0] == vector2(0, 8), 'Ring 0');
ok($p[1] == vector2(2, 8), 'Ring 1');
ok($p[2] == vector2(5, 5), 'Ring 2');
ok($p[3] == vector2(5, 0), 'Ring 3');
ok($p[4] == vector2(3, 0), 'Ring 4');
ok($p[5] == vector2(0, 3), 'Ring 5');

Description

Triangles in 2d space

Constructors

new

Create a triangle from 3 vectors specifying the coordinates of each corner in space coordinates.

triangle2

Create a triangle from 3 vectors specifying the coordinates of each corner in space coordinates - synonym for "new".

newnnc

New without narrowness check

newV

Create a triangle from the x,y components of 3 3d vectors.

newVnnc

Create a triangle from the x,y components of 3 3d vectors without narrowness checking - assumes caller will do thir own.

Methods

accuracy

Get/Set accuracy for comparisons

narrow

Narrow (colinear) colinear?

check

Check its a triangle

is

Test its a triangle

components

Components of a triangle

clone

Create a triangle from another triangle

permute

Cyclically permute the points of a triangle

center

Center

area

Area

add

Add a vector to a triangle

subtract

Subtract a vector from a triangle

multiply

Multiply a triangle by a scalar

divideBy

Divide a triangle by a scalar

print

Print triangle

convertSpaceToPlane

Convert space to plane coordinates

containsPoint

Check whether point p is completely contained within triangle t.

contains

Check whether triangle T is completely contained within triangle t.

pointsInCommon

Find points in common to two triangles. A point in common is a point on the border of one triangle touched by the border of the other triangle.

ring

Ring of points formed by overlaying triangle t and T

convertPlaneToSpace

Convert plane to space coordinates

split

Split a triangle into 4 sub triangles unless the sub triangles would be too small

equals

Compare two triangles for equality

Operators

Operator overloads

add

Add operator.

subtract

Subtract operator.

multiply

Multiply operator.

divide

Divide operator.

equals

Equals operator.

print

Print a triangle

Exports

Export "triangle2", "new", "newnnc", "newV", "newVnnc"

Credits

Author

philiprbrenan@yahoo.com

philiprbrenan@yahoo.com, 2004

License

Perl License.