Here are some notes about what we covered last Thursday.
First, we discussed the treatment of pointers in a two-dimensional array.
// g hagopian - 2 dimensional array handling with pointers #include <iostream> #include <ctime>
using namespace std;
int main() { srand((unsigned int)time(NULL)); // could assign random values. int **p2DArray; int M, N; cout << "\nDear User, how many rows would you like? Columns?: "; cin >> M >> N; // Allocate memory p2DArray = new int*[M]; // first allocate an array of M pointers, // the address of each first row element gets for (int i = 0; i < M; ++i) //for each of these, *(p2DArray+i) = new int[N]; // allocate space for N int
// Assign values for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) *(*(p2DArray+i)+j) = i+4*j; //rand()%10;
// output values in a matrix cout << "The matrix is " << endl; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { cout << *(*(p2DArray+i)+j) << '\t'; if(j == N - 1) cout << '\n'; } }
// output just the row header addresses: cout << endl; for(int i = 0; i < M; ++i) { cout << "\nThe address of " <<
*(p2DArray+i) cout << "\nThe content of " <<
*(p2DArray+i) }
// output the memory addresses of the int elements of array cout << '\n' << endl; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { cout << "Address " << *(p2DArray+i)+j
<< " has " if(j == N - 1) cout << '\n'; } }
// De-Allocate memory to prevent memory leak for (int i = 0; i < M; ++i) delete [] p2DArray[i]; delete [] p2DArray; return 0; } |
Let’s see how this works. The output of the program helps:
Dear User, how many rows would you like? Columns?: 3 3 The matrix is 0 4 8 1 5 9 2 6 10
The address of 00346218 is 003461E0 The content of 00346218 is 0
The address of 00346250 is 003461E4 The content of 00346250 is 1
The address of 00346420 is 003461E8 The content of 00346420 is 2
Address 00346218 has 0 Address 0034621C has 4 Address 00346220 has 8 Address 00346250 has 1 Address 00346254 has 5 Address 00346258 has 9 Address 00346420 has 2 Address 00346424 has 6 Address 00346428 has 10 |
Now for the triangles.
Here’s some code similar to what we did in class, but with a few extra member functions and using the LoopGDK() function to get continuous animation.
First, the header file.
// G. Hagopian Triangle.h
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; findCntrd(); } Triangle(int x0, int y0, int x1, int y1, int x2, int 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; findCntrd(); } //~Triangle(); void draw(); void rotateAboutZero(float); void findCntrd(); void rotateAboutCntrd(float);
private : int _vrt[3][2]; float _side1; float _side2; float _side3; float _height1; float _height2; float _height3; float _foot1[2]; float _foot2[2]; float _foot3[2]; float _median1; float _median2; float _median3; float _mdpt1[2]; float _mdpt2[2]; float _mdpt3[2]; float _bisctr1; float _bisctr2; float _bisctr3; float _circumcntr[2]; float _incntr[2]; float _cntrd[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(float theta) { _vrt[0][0] = (int)(_vrt[0][0]*cos(theta)-_vrt[0][1]*sin(theta)); _vrt[0][1] = (int)(_vrt[0][0]*sin(theta)+_vrt[0][1]*cos(theta)); _vrt[1][0] = (int)(_vrt[1][0]*cos(theta)-_vrt[1][1]*sin(theta)); _vrt[1][1] = (int)(_vrt[1][0]*sin(theta)+_vrt[1][1]*cos(theta)); _vrt[2][0] = (int)(_vrt[2][0]*cos(theta)-_vrt[2][1]*sin(theta)); _vrt[2][1] = (int)(_vrt[2][0]*sin(theta)+_vrt[2][1]*cos(theta)); }
void Triangle::findCntrd() { _cntrd[0] = (_vrt[0][0]+_vrt[1][0]+_vrt[2][0])/3.; _cntrd[1] = (_vrt[0][1]+_vrt[1][1]+_vrt[2][1])/3.; }
void Triangle::rotateAboutCntrd(float theta) { _vrt[0][0] = (int)(_cntrd[0]
+ _vrt[0][1] = (int)(_cntrd[1]
+ _vrt[1][0] = (int)(_cntrd[0]
+ _vrt[1][1] = (int)(_cntrd[1]
+ _vrt[2][0] = (int)(_cntrd[0]
+ _vrt[2][1] = (int)(_cntrd[1]
+ }
|
Now the main function:
// G. Hagopian - trnglMain.cpp
#include "DarkGDK.h" #include "Triangle.h"
void DarkGDK() { const DWORD BLACK = dbRGB(0,0,0); Triangle T0; Triangle T1(100,100,200,200,150,250); const int DELAY = 100; //Triangle T2(20,20, 30,30, 50,80); //for(int i = 0; i < 100; ++i) { while( LoopGDK() ) { T0.draw(); T0.rotateAboutZero(0.08); T1.draw(); T1.rotateAboutCntrd(0.06); dbWait(DELAY); dbCLS(BLACK);
} dbWaitKey(); }
|