/**********************************************************
Sample C++ Program illustrating the code developed in
Part 12 of the 240 Lecture Notes. See Notes for
explanations and documentation of the Line class.
Jim Henry 7.23.03
**********************************************************/
#include ...
// Line class represents a drawable on-screen line
// with a color attribute.
// Action methods are drawLn and eraseLn
// Screen size is hardcoded as 640 x 480 for simplicity
class Line
{
private:
long int x1, y1, x2, y2;
int color;
public:
Line(int, int, int, int);
void drawLn();
void eraseLn();
double getLength();
void setTop(int);
void setLeft(int);
void setBot(int);
void setRight(int);
void setLine(int, int, int, int);
int getTop();
int getLeft();
int getBot();
int getRight();
void setColor(int);
}; // end class Line
//*******************************************************
Line::Line(int left, int top, int right, int bot)
{
color = 50; // default color
setLeft(left);
setTop(top);
setRight(right);
setBot(bot);
}
//*******************************************************
void Line::drawLn()
{
line(screen, x1, y1, x2, y2, color);
}
//*******************************************************
void Line::eraseLn()
{
line(screen, x1, y1, x2, y2, 0); // 0 = black
}
//*******************************************************
double Line::getLength()
{
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
//*******************************************************
void Line::setTop(int x)
{
if (x < 0 || x > 479)
y1 = 0;
else
y1 = x;
}
//*******************************************************
void Line::setLeft(int x)
{
if (x < 0 || x > 639)
x1 = 0;
else
x1 = x;
}
//*******************************************************
void Line::setBot(int x)
{
if (x < 0 || x > 479)
y2 = 0;
else
y2 = x;
}
//*******************************************************
void Line::setRight(int x)
{
if (x < 0 || x > 639)
x2 = 0;
else
x2 = x;
}
//*******************************************************
void Line::setLine(int left, int top, int right, int bot)
{
setLeft(left);
setTop(top);
setRight(right);
setBot(bot);
}
//*******************************************************
int Line::getTop()
{
return y1;
}
//*******************************************************
int Line::getLeft()
{
return x1;
}
//*******************************************************
int Line::getBot()
{
return y2;
}
//*******************************************************
int Line::getRight()
{
return x2;
}
void Line::setColor(int c)
{
if (c < 0 || c > 15)
color = 15;
else
color = c;
}
// see lecture notes for these functions:
void initGr();
void wait_for_keypress();
//*******************************************************
//
// Driver/test program for the Line class
//
//*******************************************************
void main()
{
int i;
// create 2 Lines
Line L1 = Line(10, 10, 200, 200);
Line L2 = Line(20, 30, 300, 310);
// switch to graphics mode
initGr();
// draw the 2 lines and pause for <Enter>
L1.drawLn();
L2.drawLn();
wait_for_keypress();
// erase L1, change its right hand endpoint, set its
// color to RED, redraw it, and pause...
L1.eraseLn();
L1.setLine(L1.getLeft(), L1.getTop(), 300, L1.getBot());
L1.setColor(200);
L1.drawLn();
wait_for_keypress();
// erase L1 again, change its color, and reset its
// position
L1.eraseLn();
L1.setColor(100);
L1.setLine(20, 200, 30, 210);
// loop to erase, move, draw L1 50 times = "animation"
for (i = 1; i < 150; i++)
{
L1.eraseLn();
L1.setLine(L1.getLeft() + 2,
L1.getTop(),
L1.getRight() + 2,
L1.getBot());
L1.drawLn();
}
wait_for_keypress();
// switch back to textmode - NOT TESTED
release_screen();
// print lengths of L1 and L2
cout << "L1: " << L1.getLength() << " L2: " << L2.getLength();
wait_for_keypress();
} // end main()
//*******************************************************
void initGraphics()
{
int gdriver = DETECT,
gmode,
errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("error on grinit");
exit(0);
}
}
//*******************************************************
void pause()
{
flushall();
getch();
}