Physics 5
 Assignment DarkGDK005
 Triangle Class

In class we developed code which I’ve since modified a bit.  Here’s the header file:

// G. Hagopian Triangle.h

#include <ctime>

 

class Triangle {

    public :

        Triangle() {

            _vrt[0][0] = 320;

            _vrt[0][1] = 240;

            _vrt[1][0] = 330;

            _vrt[1][1] = 240;

            _vrt[2][0] = 320;

            _vrt[2][1] = 230;

            findCentroid();

        }

        Triangle(double x0, double y0,
                 double x1, double y1,
                 double x2, double y2) {

            _vrt[0][0] = x0;

            _vrt[0][1] = y0;

            _vrt[1][0] = x1;

            _vrt[1][1] = y1;

            _vrt[2][0] = x2;

            _vrt[2][1] = y2;

            findCentroid();

        }

        //~Triangle();

        void findSides(); // use distance formula for triangle sides

        void findAngles();  // use law of cosines for interior angles      

        void findArea();

        void findSlopes(); // slopes of the sides

        void findBisectors(); // angle bisectors

        void findIncenter(); // intrsctn angle bisctrs (also incircle cntr)

        void findCentroid(); // compute _centroid[3] - the coords of centroid

        void findHeights(); // altitudes

        void findInradius(); // radius of inscribed circle             

        void findOrthocenter(); // intersection of altitudes

        void findCircumCircle(); // intersection of perpendicular bisectors  

        void find9PtCircleCenter(); // center of nine point circle

        // drawing

        void draw(); // draw triangle          

        void drawIncircle() {dbCircle(_incenter[0],_incenter[1],_inradius);}

        void drawMedians(); // a median connects a vertex to midpt of opp

        void drawAngleBisectors(); // these intersect at incenter

        void drawPerpBisectors(); // these intersect at circumcenter

        void drawCircumcircle(); //

        void drawEulerLine(); // line thru orthocntr, circumcntr & cntrd

        void drawNinePointCircle(); // cntr

        // rotations

        void rotateAboutZero(double); // zero is upper left corner

        void rotateAboutCentroid(double); // circle rotates in place

        void rotateAboutPoint(double, double pt[]); // rotate any point



    private :

        double _vrt[3][2]; // coords of vertices

        double _sides[3];  // dist formula computes side lengths

        double _angles[3]; // law of cosines for angles

        double _area; // triangle's area

        double _slopes[3]; // slope of each side or 1.e8 for vertical

        double _heights[3]; // simple right triangle trig for altitudes

        double _feet[3][2]; // ???

        double _midpoints[3][2]; // ???

        double _incenter[2]; // weighted average of three sides

        double _inradius; // 1/(1/_heights[0]+1/_heights[1]+1/_heights[2])

        double _circumCenter[2]; // center of circumcircle

        double _circumRadius; // radius of circumcircle

        double _centroid[2]; // intersection of medians

        double _orthocenter[2]; // intersection of perpendicular bisectors

        double ninePtCircleCenter[2]; //

};

 

void Triangle::draw() {

      dbLine(_vrt[1][0],_vrt[1][1],_vrt[2][0],_vrt[2][1]);

      dbLine(_vrt[0][0],_vrt[0][1],_vrt[2][0],_vrt[2][1]);

      dbLine(_vrt[0][0],_vrt[0][1],_vrt[1][0],_vrt[1][1]);

}

 

void Triangle::rotateAboutZero(double theta) {
      /* multiply point by rotation matrix
         | cos(t)   -sin(t) | x
         | sin(t)    cos(t) |
y   */

      _vrt[0][0] = (_vrt[0][0]*dbCOS(theta)-_vrt[0][1]*dbSIN(theta));

      _vrt[0][1] = (_vrt[0][0]*dbSIN(theta)+_vrt[0][1]*dbCOS(theta));

      _vrt[1][0] = (_vrt[1][0]*dbCOS(theta)-_vrt[1][1]*dbSIN(theta));

      _vrt[1][1] = (_vrt[1][0]*dbSIN(theta)+_vrt[1][1]*dbCOS(theta));

      _vrt[2][0] = (_vrt[2][0]*dbCOS(theta)-_vrt[2][1]*dbSIN(theta));

      _vrt[2][1] = (_vrt[2][0]*dbSIN(theta)+_vrt[2][1]*dbCOS(theta));

}

 

void Triangle::findCentroid() {  // averages

      _centroid[0] = (_vrt[0][0]+_vrt[1][0]+_vrt[2][0])/3.;

      _centroid[1] = (_vrt[0][1]+_vrt[1][1]+_vrt[2][1])/3.;

}

 

void Triangle::rotateAboutCentroid(double theta) { // dbCOS vs cos?

      _vrt[0][0] = (_centroid[0] +

                        (_vrt[0][0]-_centroid[0])*dbCOS(theta)-

                        (_vrt[0][1]-_centroid[1])*dbSIN(theta));

      _vrt[0][1] = (_centroid[1] +

                         (_vrt[0][0]-_centroid[0])*dbSIN(theta)+

                         (_vrt[0][1]-_centroid[1])*dbCOS(theta));

      _vrt[1][0] = (_centroid[0] +

                         (_vrt[1][0]-_centroid[0])*dbCOS(theta)-

                         (_vrt[1][1]-_centroid[1])*dbSIN(theta));

      _vrt[1][1] = (_centroid[1] +

                         (_vrt[1][0]-_centroid[0])*dbSIN(theta)+

                         (_vrt[1][1]-_centroid[1])*dbCOS(theta));

      _vrt[2][0] = (_centroid[0] +

                         (_vrt[2][0]-_centroid[0])*dbCOS(theta)-

                         (_vrt[2][1]-_centroid[1])*dbSIN(theta));

      _vrt[2][1] = (_centroid[1] +

                         (_vrt[2][0]-_centroid[0])*dbSIN(theta)+

                         (_vrt[2][1]-_centroid[1])*dbCOS(theta));

}

 

void Triangle::findArea() {

      // need code for this still

}

 

void Triangle::findSlopes() {

      if(_vrt[1][0] != _vrt[2][0])

            _slopes[0] = (_vrt[1][1] - _vrt[2][1])/(_vrt[1][0] - _vrt[2][0]);

      else _slopes[0] = 1.e8; // arbitrary very large, finite quantity

      if(_vrt[0][0] != _vrt[2][0])

            _slopes[1] = (_vrt[0][1] - _vrt[2][1])/(_vrt[0][0] - _vrt[2][0]);

      else _slopes[1] = 1.e8;

      if(_vrt[0][0] != _vrt[1][0])

            _slopes[2] = (_vrt[0][1] - _vrt[1][1])/(_vrt[0][0] - _vrt[1][0]);

      else _slopes[2] = 1.e8;

}

 

void Triangle::findSides() {

      _sides[0] = sqrt((_vrt[2][0]-_vrt[1][0])*(_vrt[2][0]-_vrt[1][0])+

                         (_vrt[2][1]-_vrt[1][1])*(_vrt[2][1]-_vrt[1][1]));

      _sides[1] = sqrt((_vrt[2][0]-_vrt[0][0])*(_vrt[2][0]-_vrt[0][0])+

                         (_vrt[2][1]-_vrt[0][1])*(_vrt[2][1]-_vrt[0][1]));

      _sides[2] = sqrt((_vrt[1][0]-_vrt[0][0])*(_vrt[1][0]-_vrt[0][0])+

                         (_vrt[1][1]-_vrt[0][1])*(_vrt[1][1]-_vrt[0][1]));

}

 

void Triangle::findIncenter() {

      double perimeter = _sides[0]+_sides[1]+_sides[2];

      _incenter[0] = (_sides[0]*_vrt[0][0]

                     +_sides[1]*_vrt[1][0]

                           +_sides[2]*_vrt[2][0])/perimeter;

      _incenter[1] = (_sides[0]*_vrt[0][1]

                     +_sides[1]*_vrt[1][1]

                           +_sides[2]*_vrt[2][1])/perimeter;

}

 

void Triangle::findAngles() {  // maybe change to use law of sines for 2nd?

      _angles[0] = dbACOS((_sides[1]*_sides[1]+
                           _sides[2]*_sides[2]-
                           _sides[0]*_sides[0])/(2*_sides[1]*_sides[2]));

      _angles[1] = dbACOS((_sides[0]*_sides[0]+
                           _sides[2]*_sides[2]-
                           _sides[1]*_sides[1])/(2*_sides[0]*_sides[2]));

      _angles[2] = dbACOS((_sides[1]*_sides[1]+

                           _sides[0]*_sides[0]-

                           _sides[2]*_sides[2])/(2*_sides[1]*_sides[0]));

}

 

void Triangle::findHeights() {

      _heights[0]=_sides[2]*dbSIN(_angles[1]);

      _heights[1]=_sides[0]*dbSIN(_angles[2]);

      _heights[2]=_sides[1]*dbSIN(_angles[0]);

}

 

void Triangle::findInradius() {

      _inradius = 1/(1/_heights[0]+1/_heights[1]+1/_heights[2]);

}

 

void Triangle::findCircumCircle() {

      // need code for this still

}

 

void Triangle::drawCircumcircle() {

      // need code for this still

}

And the DarkGDK() (or main()) function code is below:

// G. Hagopian - trnglMain.cpp

#include "DarkGDK.h"

#include "Triangle03.h"

#include <ctime>

 

void updateTriangle(Triangle *);

const DWORD BLACK = dbRGB(0,0,0);

const DWORD WHITE = dbRGB(255,255,255);

 

void DarkGDK() {

    srand((unsigned int)time(NULL)); // seed random number generator

    Triangle T0; // create triangle with default constructor

    Triangle T1(100,100,200,200,150,250);// triangle with specified vertices

    //Create a dynamic array with 81 triangles

    Triangle * trngls[81];

    Triangle * tPtr;

    for (int i = 0; i < 9; i++) {

        for(int j = 0; j < 9; j++) {

            tPtr = new Triangle((double)(64*(i+1)+(rand()%50)),
                                (double)(48*j+rand()%50),

                                (double)(64*(i+1)+(rand()%50)),
                                (double)(48*j+rand()%50),

                                (double)(64*(i+1)+(rand()%50)),
                                (double)((48*j)+rand()%50));

            // note use of place value here for array indexing

            trngls[9*i+j] = tPtr;

        }

    }

    dbSyncOn();

    dbSyncRate(60);

    while( LoopGDK() ) {

        T0.draw();

        T0.rotateAboutZero(1);

        T1.draw();

        T1.drawIncircle();

        T1.rotateAboutCentroid(1);

        for(int i = 0; i < 81; ++i) {

            updateTriangle(trngls[i]);

        }

        dbSync();

        dbCLS(WHITE);

    } // end game loop.

    for (int i = 0; i < 81; i++)

    {

        delete trngls[i];

        trngls[i] = NULL;

    } // end for

} // end DarkGDK().

 

void updateTriangle(Triangle * T) { // passing pointer like passing reference

    dbInk(dbRGB(50+rand()%200,50+rand()%200,50+rand()%200),WHITE);

    T->draw();

    T->drawIncircle();

    T->rotateAboutCentroid(1);                 

    T->findSides();

    T->findAngles();

    T->findHeights();

    T->findIncenter();

    T->findInradius();

    T->findCircumCircle(); // need code for this still

    T->drawCircumcircle(); // need code for this still

}


Your job here is to add code for the following:

1.       findCircumCircle();   You can find formulas for the center and radius of the circumcircle at various internet sites including http://en.wikipedia.org/wiki/Circumscribed_circle
Implement the code by showing how the array of random triangles, their incircles and circumcircles rotate to produce pictures like these:

triangles01.jpg

triangles02.jpg

 

2.  Provide code to define the following functions and use them also i
void
drawMedians(); // a median connects a vertex to midpt of opp 
void drawAngleBisectors(); // these intersect at incenter

   void drawPerpBisectors(); // these intersect at circumcenter

   void drawCircumcircle(); //

   void drawEulerLine(); // line thru orthocntr, circumcntr & cntrd

   void drawNinePointCircle(); // google it

3.       Modify the code to allow the user to enter the number of rows and columns of triangles to display and then rotate. In the code it is an evenly spaced 9 by 9 array with random coordinates varying by as much as 50.

4.       Try something I wouldn’t have guessed you would invent.