proj home

Files   Classes   Functions   Hierarchy  

triangle< PT, PD > Class Template Reference

A triangle with ways to calculate many triangle properties. More...

#include <triangle.h>

Inheritance diagram for triangle< PT, PD >:
Inheritance graph
Collaboration diagram for triangle< PT, PD >:
Collaboration graph

List of all members.

Public Types

typedef PT PTtype
typedef PD PDtype

Public Member Functions

 triangle ()
 Unconstructed triangle.
 triangle (PT const &p0, PT const &p1, PT const &p2)
 Construct from anti clockwise point ordering.
void construct (PT const &p0, PT const &p1, PT const &p2)
 Construct from anti clockwise point ordering.
void constructUnordered (PT const &p0, PT const &p1, PT const &p2)
 No guarantee of point order because of swapping.
void bisectangle (PT &x, uintc i) const
 From vertex i extend a line bisecting the angle in half.
void centroid (PT &x) const
 Calculate the Centroid.
void circumcenter (PT &x) const
 Calculate the Circumcenter.
void equilaterali (PT &x, uintc i) const
 Gets the gradient opposite the vertex i.
void fermatpoint (PT &x) const
 From the edge construct an equilateral triangle and intersect its extreme vertexes with the opposite point.
void gergonnepoint (PT &x) const
 Intersection of bisectors with opposite points.
void incenter (PT &x) const
 Calculate the Incenter.
void incenteri (PT &x, uint i) const
 Calculate the point of the inner circle on side i.
template<typename U >
void innercircle (PD &radius, U &center, U &circlenormal) const
 Find the circle inside the triangle touching all sides.
void midpoint (PT &x, uintc i) const
 Calculate the midpoint opposite the vertex i.
template<typename U >
void normal (U &circlenormal) const
 Normal in 3D space.
void napoleanpoint (PT &x) const
 Find the centroids of equilateral triangles constructed on edges.
void orthocenter (PT &x) const
 Calculate the Orthocenter.
void orthocenteri (PT &x, uintc i) const
 From the indexed point i construct a line to the opposite side intersecting at right angles, calculate this point.
template<typename U >
void outercircle (PD &radius, U &center, U &circlenormal) const
 Find the circle passing though all the points.
void outercircle (PD &radius, PT &center) const
 Find the circle passing though all the points.
boolc valid () const
 Assert if the triangle is invalid.

Public Attributes

PT pi [3]
 The three points of the triangle.

Detailed Description

template<typename PT, typename PD>
class triangle< PT, PD >

A triangle with ways to calculate many triangle properties.

Many geometric properties such as the orthocenter, centroid and circumcenter can be calculated.

This is a monolith design. Each property could be made its own class but I want efficiency.

The class is deliberately designed without virtual functions or inheritance for efficiency.

Definition at line 25 of file triangle.h.


Member Typedef Documentation

template<typename PT, typename PD>
typedef PD triangle< PT, PD >::PDtype

Definition at line 30 of file triangle.h.

template<typename PT, typename PD>
typedef PT triangle< PT, PD >::PTtype

Definition at line 29 of file triangle.h.


Constructor & Destructor Documentation

template<typename PT, typename PD>
triangle< PT, PD >::triangle (  )  [inline]

Unconstructed triangle.

Definition at line 36 of file triangle.h.

00036 {}

template<typename PT, typename PD >
triangle< PT, PD >::triangle ( PT const &  p0,
PT const &  p1,
PT const &  p2 
) [inline]

Construct from anti clockwise point ordering.

Definition at line 533 of file triangle.h.

00538 {
00539   construct(_p0,_p1,_p2);
00540 }


Member Function Documentation

template<typename PT, typename PD >
void triangle< PT, PD >::bisectangle ( PT &  x,
uintc  i 
) const [inline]

From vertex i extend a line bisecting the angle in half.

Return where this line intersects the triangles edge.

Definition at line 349 of file triangle.h.

References triangle< PT, PD >::pi.

Referenced by trianglevisualize< T >::eval(), and triangle< PT, PD >::incenter().

00350 {
00351   PT A,B;
00352   PT X;
00353 
00354   switch(i)
00355   {
00356     case 0:
00357       A = pi[1];
00358       B = pi[2];
00359       X = pi[0];
00360       break;
00361 
00362     case 1:
00363       A = pi[0];
00364       B = pi[2];
00365       X = pi[1];
00366       break;
00367 
00368     case 2:
00369       A = pi[1];
00370       B = pi[0];
00371       X = pi[2];
00372       break;
00373 
00374 
00375     default:
00376       assert(false);
00377   }
00378 
00379   PD a = (A-X).distance();
00380   PD b = (B-X).distance();
00381 
00382   x = (A*b+B*a)/(a+b);
00383 }

template<typename PT, typename PD >
void triangle< PT, PD >::centroid ( PT &  x  )  const [inline]

Calculate the Centroid.

Definition at line 340 of file triangle.h.

References triangle< PT, PD >::pi.

Referenced by triangle< PT, PD >::circumcenter(), and trianglevisualize< T >::eval().

00341 {
00342   x =  pi[0];
00343   x += pi[1];
00344   x += pi[2];
00345   x /= 3.0;
00346 }

template<typename PT, typename PD >
void triangle< PT, PD >::circumcenter ( PT &  x  )  const [inline]

Calculate the Circumcenter.

Definition at line 314 of file triangle.h.

References triangle< PT, PD >::centroid(), and triangle< PT, PD >::orthocenter().

Referenced by trianglevisualize< T >::eval().

00315 {
00316   //x =(G*3.0-H)*0.5;
00317   centroid(x);
00318   x *= 3.0;
00319   PT H;
00320   orthocenter(H);
00321   x -= H;
00322   x *= 0.5;
00323 }

template<typename PT, typename PD >
void triangle< PT, PD >::construct ( PT const &  p0,
PT const &  p1,
PT const &  p2 
) [inline]

Construct from anti clockwise point ordering.

Definition at line 518 of file triangle.h.

00523 {
00524   pi[0] = _p0;
00525   pi[1] = _p1;
00526   pi[2] = _p2;
00527 
00528   assert(valid());
00529 }

template<typename PT, typename PD >
void triangle< PT, PD >::constructUnordered ( PT const &  p0,
PT const &  p1,
PT const &  p2 
) [inline]

No guarantee of point order because of swapping.

Definition at line 148 of file triangle.h.

00153 {
00154   pi[0] = p0;
00155   pi[1] = p1;
00156   pi[2] = p2;
00157 
00158   if (valid()==false)
00159   {
00160     pi[1] = p2;
00161     pi[2] = p1;
00162 
00163     assert(valid());
00164   }
00165 }

template<typename PT, typename PD >
void triangle< PT, PD >::equilaterali ( PT &  x,
uintc  i 
) const [inline]

Gets the gradient opposite the vertex i.

Gets invards pointing normal opposite the vertex i. Get the point forming an equilateral triangle with the opposite side.

Definition at line 386 of file triangle.h.

References triangle< PT, PD >::pi.

Referenced by triangle< PT, PD >::fermatpoint(), and triangle< PT, PD >::napoleanpoint().

00387 {
00388   PT g;
00389   PT n1;
00390   switch(i)
00391   {
00392     case 0:
00393       g = (pi[2]+pi[1])*0.5;
00394       n1=pi[2]-pi[1];
00395       break;
00396     case 1:
00397       g = (pi[2]+pi[0])*0.5;
00398       n1=pi[0]-pi[2];
00399       break;
00400     case 2:
00401       g = (pi[0]+pi[1])*0.5;
00402       n1=pi[1]-pi[0];
00403       break;
00404     default:
00405       assert(false);
00406   }
00407 
00408   // Outwards normal.
00409   PT n2(n1.y,-n1.x);
00410   n2.normalize();
00411 
00412   x = g+n2*sqrt(3.0)*0.5*n1.distance();
00413 }

template<typename PT, typename PD >
void triangle< PT, PD >::fermatpoint ( PT &  x  )  const [inline]

From the edge construct an equilateral triangle and intersect its extreme vertexes with the opposite point.

Definition at line 202 of file triangle.h.

References d2matsolve(), triangle< PT, PD >::equilaterali(), and triangle< PT, PD >::pi.

00203 {
00204   PT e0;
00205   equilaterali(e0,0);
00206   PT e1;
00207   equilaterali(e1,1);
00208 
00209   PT t;
00210   d2matsolve(t,pi[0]-e0,e1-pi[1],e1-e0);
00211 
00212   x = e0 + (pi[0]-e0)*t.x;
00213 }

template<typename PT, typename PD >
void triangle< PT, PD >::gergonnepoint ( PT &  x  )  const [inline]

Intersection of bisectors with opposite points.

Definition at line 168 of file triangle.h.

References d2matsolve(), triangle< PT, PD >::incenteri(), and triangle< PT, PD >::pi.

00169 {
00170   PT c0; 
00171   incenteri(c0,0);
00172   PT c1; 
00173   incenteri(c1,1);
00174 
00175   PT t;
00176   d2matsolve(t,pi[0]-c0,c1-pi[1],c1-c0);
00177 
00178   x = c0 + (pi[0]-c0)*t.x;
00179 }

template<typename PT, typename PD >
void triangle< PT, PD >::incenter ( PT &  x  )  const [inline]

Calculate the Incenter.

Definition at line 216 of file triangle.h.

References triangle< PT, PD >::bisectangle(), d2matsolve(), and triangle< PT, PD >::pi.

00217 {
00218   PT b0;
00219   bisectangle(b0,0);
00220   PT b1;
00221   bisectangle(b1,1);
00222 
00223   PT v;  // The two variables. 
00224   d2matsolve
00225   (
00226     v,
00227     b0 - pi[0], pi[1] - b1,
00228     pi[1] - pi[0]
00229   );
00230 
00231   x = pi[0]+(b0-pi[0])*v.x;
00232 }

template<typename PT, typename PD>
void triangle< PT, PD >::incenteri ( PT &  x,
uint  i 
) const

Calculate the point of the inner circle on side i.

Referenced by triangle< PT, PD >::gergonnepoint().

template<typename PT , typename PD>
template<typename U >
void triangle< PT, PD >::innercircle ( PD &  radius,
U &  center,
U &  circlenormal 
) const [inline]

Find the circle inside the triangle touching all sides.

Definition at line 246 of file triangle.h.

References line< PT, PD >::nearestpointonline().

Referenced by trianglevisualize< T >::eval().

00251 {
00252   PT c;
00253   incenter(c);
00254   center.x = c.x;
00255   center.y = c.y;
00256 
00257   PT w;
00258   line<PT,PD>::nearestpointonline(w,c,pi[0],pi[1]-pi[0]);
00259   w -= c;
00260 
00261   radius = w.distance();
00262   normal(circlenormal);
00263 }

template<typename PT, typename PD >
void triangle< PT, PD >::midpoint ( PT &  x,
uintc  i 
) const [inline]

Calculate the midpoint opposite the vertex i.

Definition at line 493 of file triangle.h.

References triangle< PT, PD >::pi.

Referenced by trianglevisualize< T >::eval().

00494 {
00495   switch(i)
00496   {
00497     case 0:
00498       x=pi[1]; x += pi[2]; 
00499       break;
00500 
00501     case 1:
00502       x=pi[0]; x += pi[2]; 
00503       break;
00504 
00505     case 2:
00506       x=pi[1]; x += pi[0];
00507       break;
00508       
00509     default:
00510       assert(false);
00511   }
00512 
00513   x *= 0.5;
00514 }

template<typename PT, typename PD >
void triangle< PT, PD >::napoleanpoint ( PT &  x  )  const [inline]

Find the centroids of equilateral triangles constructed on edges.

Intersect lines from centroid to opposite point on triangle.

Definition at line 182 of file triangle.h.

References d2matsolve(), triangle< PT, PD >::equilaterali(), and triangle< PT, PD >::pi.

00183 {
00184   PT c0;
00185   equilaterali(c0,0);
00186   c0 += pi[1];
00187   c0 += pi[2];
00188   c0 /= 3.0;
00189   PT c1;
00190   equilaterali(c1,1);
00191   c1 += pi[0];
00192   c1 += pi[2];
00193   c1 /= 3.0;
00194 
00195   PT t;
00196   d2matsolve(t,pi[0]-c0,c1-pi[1],c1-c0);
00197 
00198   x = c0 + (pi[0]-c0)*t.x;
00199 }

template<typename PT , typename PD >
template<typename U >
void triangle< PT, PD >::normal ( U &  circlenormal  )  const [inline]

Normal in 3D space.

Definition at line 235 of file triangle.h.

00236 {
00237   circlenormal.x = 0;
00238   circlenormal.y = 0;
00239   circlenormal.z = 1;
00240 }

template<typename PT, typename PD >
void triangle< PT, PD >::orthocenter ( PT &  x  )  const [inline]

Calculate the Orthocenter.

Definition at line 326 of file triangle.h.

References d2matsolve(), triangle< PT, PD >::orthocenteri(), and triangle< PT, PD >::pi.

Referenced by triangle< PT, PD >::circumcenter(), and trianglevisualize< T >::eval().

00327 {
00328   PT p0w;
00329   orthocenteri(p0w,0);
00330   PT p1w;
00331   orthocenteri(p1w,1);
00332   PT t;
00333 
00334   d2matsolve(t,p0w-pi[0],pi[1]-p1w,pi[1]-pi[0]);
00335 
00336   x = pi[0] + (p0w-pi[0])*t.x;
00337 }

template<typename PT, typename PD >
void triangle< PT, PD >::orthocenteri ( PT &  x,
uintc  i 
) const [inline]

From the indexed point i construct a line to the opposite side intersecting at right angles, calculate this point.

Definition at line 452 of file triangle.h.

References d2matsolve(), and triangle< PT, PD >::pi.

Referenced by trianglevisualize< T >::eval(), and triangle< PT, PD >::orthocenter().

00453 {
00454   PT z;
00455   PT m0;
00456   PT w0;
00457 
00458   switch(i)
00459   {
00460     case 0:
00461       w0 = pi[1];
00462       m0 = pi[2] - pi[1];
00463 
00464       z = pi[0];
00465       break;
00466 
00467     case 1:
00468       w0 = pi[2];
00469       m0 = pi[0] - pi[2];
00470 
00471       z = pi[1];
00472       break;
00473 
00474     case 2:
00475       w0 = pi[0];
00476       m0 = pi[1] - pi[0];
00477 
00478       z = pi[2];
00479       break;
00480   
00481     default:
00482       assert(false);
00483   }
00484 
00485   PT m1(-m0.y,m0.x);
00486   PT t;
00487   d2matsolve(t,m0,m1*-1.0,z-w0);
00488 
00489   x = z + m1*t.y;
00490 }

template<typename PT, typename PD>
void triangle< PT, PD >::outercircle ( PD &  radius,
PT &  center 
) const [inline]

Find the circle passing though all the points.

Definition at line 300 of file triangle.h.

00304 {
00305   PT c;
00306   circumcenter(c);
00307   center.x = c.x;
00308   center.y = c.y;
00309   radius = (c-pi[0]).distance();
00310 }

template<typename PT , typename PD>
template<typename U >
void triangle< PT, PD >::outercircle ( PD &  radius,
U &  center,
U &  circlenormal 
) const [inline]

Find the circle passing though all the points.

Definition at line 284 of file triangle.h.

Referenced by tessD2draw02circles< TESS, PT, INDX >::draw(), writecirclesobj::draw(), writecpcircleobj::draw(), and tessD2disp01< PT >::eval().

00289 {
00290   PT c;
00291   circumcenter(c);
00292   center.x = c.x;
00293   center.y = c.y;
00294   radius = (c-pi[0]).distance();
00295   normal(circlenormal);
00296 }

template<typename PT , typename PD >
boolc triangle< PT, PD >::valid (  )  const [inline]

Assert if the triangle is invalid.

Definition at line 543 of file triangle.h.

References triangle< PT, PD >::pi.

00544 {
00545   PT a0(pi[1]-pi[0]);
00546   PT a1;
00547   a1.x=-a0.y;
00548   a1.y=a0.x;
00549   PT q(pi[2]-pi[0]); 
00550   bool res = (a1.x*q.x+a1.y*q.y>0);
00551 
00552   return res;
00553 }


Member Data Documentation

template<typename PT, typename PD>
PT triangle< PT, PD >::pi[3]

The documentation for this class was generated from the following file:

Generated on 27 Nov 2012 for Chelton Evans Source by  doxygen 1.6.1