Triangles in 2D space
PhilipRBrenan@yahoo.com, 2004, Perl License
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');
Triangles in 2d space
Create a triangle from 3 vectors specifying the coordinates of each corner in space coordinates.
Create a triangle from 3 vectors specifying the coordinates of each corner in space coordinates - synonym for "new".
New without narrowness check
Create a triangle from the x,y components of 3 3d vectors.
Create a triangle from the x,y components of 3 3d vectors without narrowness checking - assumes caller will do thir own.
Get/Set accuracy for comparisons
Narrow (colinear) colinear?
Check its a triangle
Test its a triangle
Components of a triangle
Create a triangle from another triangle
Cyclically permute the points of a triangle
Center
Area
Add a vector to a triangle
Subtract a vector from a triangle
Multiply a triangle by a scalar
Divide a triangle by a scalar
Print triangle
Convert space to plane coordinates
Check whether point p is completely contained within triangle t.
Check whether triangle T is completely contained within triangle t.
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 of points formed by overlaying triangle t and T
Convert plane to space coordinates
Split a triangle into 4 sub triangles unless the sub triangles would be too small
Compare two triangles for equality
Operator overloads
Add operator.
Subtract operator.
Multiply operator.
Divide operator.
Equals operator.
Print a triangle
Export "triangle2", "new", "newnnc", "newV", "newVnnc"
philiprbrenan@yahoo.com
philiprbrenan@yahoo.com, 2004
Perl License.
To install Math::Zap::Draw, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::Zap::Draw
CPAN shell
perl -MCPAN -e shell install Math::Zap::Draw
For more information on module installation, please visit the detailed CPAN module installation guide.