Pages Menu
Chitu Okoli chitu.okoli.org

trad-shapes.cpp

/* trad-shapes.cpp. Code generated using Microsoft Visual C++.

Using traditional structured programming, this program creates shapes,
displays their properties, and prints them.
*/

#include <iostream>
#include <math.h>
using namespace std;

/* printLine is a useful function for line printing.

For simplicity, this implementation outputs the end
points of the line; going into graphics programming
would distract from the purpose of this demonstration.
*/
void printLine( int x1, int y1, int x2, int y2 ) {

cout << "Line from (" << x1 << "," << y1 << ") ";
cout << "to (" << x2 << "," << y2 << ")n";
}


/* Rectangle

A rectangle basically looks like this:

|------ length -----|
1 --------------------- 2 -
| | |
| | |
| | | height
| | |
4 --------------------- 3 -

The code for Rectangle follows (28 lines of code).
*/
struct Rectangle {
int x1, y1, x2, y2, x3, y3, x4, y4;
};

void createRect( Rectangle* r, int x, int y, int length, int height) {
r->x1 = x;
r->y1 = y;
r->x2 = x + length;
r->y2 = y;
r->x3 = x + length;
r->y3 = y - height;
r->x4 = x;
r->y4 = y - height;
}

void displayRect( Rectangle r ) { // displays properties
cout << "Rectangle Properties: ";
cout << "Top-left corner is (" << r.x1 << "," << r.y1 << ")n";
cout << "Line 1-2 is " << (r.x2 - r.x1) << " points long.n";
cout << "Line 2-3 is " << (r.y2 - r.y3) << " points long.n";
cout << "Line 3-4 is " << (r.x3 - r.x4) << " points long.n";
cout << "Line 4-1 is " << (r.y1 - r.y4) << " points long.n";
}

void printRect( Rectangle r ) {
cout << "Rectangle:n";
printLine( r.x1, r.y1, r.x2, r.y2 );
printLine( r.x2, r.y2, r.x3, r.y3 );
printLine( r.x3, r.y3, r.x4, r.y4 );
printLine( r.x4, r.y4, r.x1, r.y1 );
}


/* Isosceles

An isosceles triangle basically looks like this:

1
^ -
/ |
/ | height
/ |
3 /------- 2 -
|--base-|

The code for Isosceles follows. With 27 lines of code, it about as
long as Rectangle, with some time savings for structure and typing.
However, these time savings were partially negated by the time it took
to debug the bugs introduced by the cut-and-paste method of reuse. For
a small program like this, the typo effect is minor, but in large
programs, the bugs introduced can be quite significant.
*/
struct Isosceles {
int x1, y1, x2, y2, x3, y3;
};

void createIso( Isosceles* i, int x, int y, int height, int base) {
if ( base%2 == 1 )
// base is odd, so increment to allow even division by two
base++;
i->x1 = x;
i->y1 = y;
i->x2 = x + (base/2);
i->y2 = y - height;
i->x3 = x - (base/2);
i->y3 = y - height;
}

void displayIso( Isosceles i ) { // displays properties
cout << "Isosceles Triangle Properties: ";
cout << "Topmost point is (" << i.x1 << "," << i.y1 << ")n";

float slope = sqrt( pow(i.y1-i.y2, 2) + pow(i.x2-i.x1, 2) ); // Pythagoran formula
cout << "Line 1-2 is " << slope << " points long.n";
cout << "Line 2-3 is " << (i.x2 - i.x3) << " points long.n";
cout << "Line 3-1 is " << slope << " points long.n";
}

void printIso( Isosceles i ) {
cout << "Isosceles Triangle:n";
printLine( i.x1, i.y1, i.x2, i.y2 );
printLine( i.x2, i.y2, i.x3, i.y3 );
printLine( i.x3, i.y3, i.x1, i.y1 );
}


/* Parallelogram

A parallelogram basically looks like this:

|------ base -----|
1 --------------------- 2




4 --------------------- 3

The code for Parallelogram follows. With 29 lines of code, it about as
long as Rectangle and Triangle. Again, the code was primarily composed
by cutting and pasting, which introduced a number of compiler errors
that had to be debugged. By this time, we can tell that each new shape
of this level of simplicity takes just under 30 lines of code.
*/
struct Parallelogram {
int x1, y1, x2, y2, x3, y3, x4, y4;
};

void createPar( Parallelogram* p, int x1, int y1, int base, int x4, int y4 ) {
p->x1 = x1;
p->y1 = y1;
p->x2 = x1 + base;
p->y2 = y1;
p->x3 = x4 + base;
p->y3 = y4;
p->x4 = x4;
p->y4 = y4;
}

void displayPar( Parallelogram p ) { // displays properties
cout << "Parallelogram Properties: ";
cout << "Top-left corner is (" << p.x1 << "," << p.y1 << ")n";

float slope = sqrt( pow(p.y1-p.y4, 2) + pow(p.x1-p.x4, 2) ); // Pythagoran formula
cout << "Line 1-2 is " << (p.x2 - p.x1) << " points long.n";
cout << "Line 2-3 is " << slope << " points long.n";
cout << "Line 3-4 is " << (p.x3 - p.x4) << " points long.n";
cout << "Line 4-1 is " << slope << " points long.n";
}

void printPar( Parallelogram p ) {
cout << "Parallelogram:n";
printLine( p.x1, p.y1, p.x2, p.y2 );
printLine( p.x2, p.y2, p.x3, p.y3 );
printLine( p.x3, p.y3, p.x4, p.y4 );
printLine( p.x4, p.y4, p.x1, p.y1 );
}



int main() {

Rectangle r;
createRect( &r, 3,10, 6, 8 ); // anchor point, length, height
cout << "n";
displayRect( r );
cout << "n";
printRect( r );

Isosceles i;
createIso( &i, 3,10, 6, 8 ); // anchor point, height, base
cout << "n";
displayIso( i );
cout << "n";
printIso( i );

Parallelogram p;
createPar( &p, 3,10, 6, 5,7 ); // upper anchor, base, lower anchor
cout << "n";
displayPar( p );
cout << "n";
printPar( p );

return 0;
}

Post a Reply

Your email address will not be published. Required fields are marked *