 # C++ // See attach for more details Anonymous

### Question Description

Starting with the code completed for Week 3, do the following:

• Implement Matrix2D.cpp corresponding to the Matrix2D.h given as a handout.
• Add to Triangle2D, two methods
• void operator *= ( const Matrix2D& matrix ) ;
• which transforms the vertices of the triangle using Matrix * Vector
• Vector2D Centroid() ;
• computes the point that is the centroid (center) of the object by adding the three vertices and multiplying by 1/3. In other words, the centroid of a triangle is the average of its three vertices: (v1+v2+v3)/3
• Animate a Triangle2D
In the constructor for MathGame (in GSP221.cpp):
• Create a scaling matrix with a uniform scale factor (e.g. of 1.01).
• Create a rotation matrix with an angle of rotation of, for example, 2 degrees.
• Compute the centroid of a triangle and save it
In Update_Model.
• Use the -centroid to translate the triangle to the origin.
• create a composite matrix from the scaling and rotation matrices and use it to transform the triangle.
• Translate the triangle by the centroid back to its original position.
• After some number of frames (e.g. 50), invert the scaling matrix.
 Deliverables
1. You will need the Week 2 iLab file with its solution. See the Week 2 iLab page.
2. Download the two header files and the .cpp file from the  Week 5 iLab 4 Handout zip folder.
3. Note: Martix2D.h and Matrix2D.cpp go in the Math project.
Triangle2D.h goes in the Geometry project.
 i L A B  S T E P S
 STEP 1: Add to Triangle2D.cpp two methods

Add the following two methods at the end of Triangle2D.cpp:

 Vector2D Triangle2D:: Centroid() {          return... write the one line of code up above that computes the centroid by adding up the three vertices and multiplying it by float(1.0/3.0) } void Triangle2D:: operator *= ( const Matrix2D& matrix ) {        for ( int i = 0 ; i < 3 ; i++ )            v[i]= ...                 write the one line of code above that will transform vertex v[i] of the triangle using the matrix* vector operator. You may see this * operator in Matrix2D.h. }
 STEP 2: Modify MathGame (in GSP221.cpp)

In GSP221.cpp add the following code:

class MathGame : public GameFramework
{
private:

Appearance solid_red ;

Note
 The following creates triangle of type Triangle2D:

Triangle2D triangle ;

Note
 The following creates two matrices. One will eventually do scaling and the other rotation. Then it creates a Vector2D type called centroid:

Matrix2D rotation, scaling ;
Vector2D centroid ;

public:
MathGame()
{
triangle = Triangle2D( Vector2D( 200, 200 ), Vector2D( 300, 200 ),
Vector2D( 300, 300 ) ) ;
solid_red = Appearance( Color( 1, 0, 0 ), true ) ;

Note
 The command U. Scaling( Vector2D( 7.0, 7.0 ) ) ; will turn matrix U into one that scales uniformly by a factor of 7. The command U. Rotation( 30 ) ; will turn matrix U into one that rotates counterclockwise 30 degrees. The command V = triangle.Centroid() ; will compute the centroid of triangle and save it as vector V.

Write the one line of code here that turns matrix scaling into one that scales uniformly by a factor of 1.01.

Write the one line of code here that turns matrix rotation into one that rotates counterclockwise 2 degrees.

Write the one line of code here that computes the centroid of triangle and saves it into vector centroid.

}

Note
 The command triangle.Translate( V ) ; will translate triangle by vector v. The command U. Rotation( 30 ) ; will turn matrix U into one that rotates counterclockwise 30 degrees. The command V = triangle.Centroid() ; will compute the centroid of triangle and save it as vector V.

void Update_Model()
{  Write the one line of code here that will translate triangle by –(centroid)
This will cause the triangle to have its centroid at the origin.

Write the one line of code here that will scale uniformly and then rotate
triangle.
triangle *= ...

Write the one line of code here that will translate triangle by centroid.
This will cause the triangle to have its centroid back where it was.

if ( frames % 50 == 0 )

Write the one line of code that will invert (i.e. turns it into its inverse) the scaling matrix. For example, inverting a scaling matrix that scales uniformly by a factor of 2 will cause it to scale by a factor of 1/2. See Matrix2D.h information on the Invert() method. The net effect is that it will invert the scaling matrix every 50 frames.

Put the rest of this code into GSP221.cpp:

}

void Render_Model()
{
triangle.Render( &solid_red ) ;
}
} ;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd )
{
Matrix2D matrix ;
matrix.Scaling( Vector2D( 2,3 ) ) ;
matrix.Rotate( 45 ) ;
Matrix2D inverse = matrix.Inverse() ;
Matrix2D identity = matrix * inverse ;

MathGame game ;

game.Open_Window( hInstance, "GSP 221 Project", 0, 0, 500, 500, 255, 255, 255 ) ;
game.Run() ;
}

In Vector2D.cpp in the Math project. Replace the negative overloaded operator (right below the Normalize() method) :

void

Vector2D::operator - ( void )

{

for

( int i = 0 ; i < dimension ; i++ )

v[i] *= Scalar( -1.0 ) ;

}

by this:

Vector2D Vector2D::operator - ( void ) const

{

Vector2D result = *this ;

for

( int i = 0 ; i < dimension ; i++ )

result.v[i] *= Scalar( -1.0 ) ;

return result ;

}

The output should look like this. It is a triangle that rotates counterclockwise about its centroid and scales the triangle as it does so. It will alternate between getting bigger and getting smaller as it rotates. More specifically, every 50 frames it will get bigger, then it will get smaller for 50 frames. Then it will get bigger for 50 frames, and so on.  GSP221_Wk5_iLab_Handout.zip

See attach for more details This question has not been answered.

Create a free account to get help with this and any other question! Brown University

1271 Tutors California Institute of Technology

2131 Tutors Carnegie Mellon University

982 Tutors Columbia University

1256 Tutors Dartmouth University

2113 Tutors Emory University

2279 Tutors Harvard University

599 Tutors Massachusetts Institute of Technology

2319 Tutors New York University

1645 Tutors Notre Dam University

1911 Tutors Oklahoma University

2122 Tutors Pennsylvania State University

932 Tutors Princeton University

1211 Tutors Stanford University

983 Tutors University of California

1282 Tutors Oxford University

123 Tutors Yale University

2325 Tutors