Graphics Window Coordinates
#include <graphics.h>
#include <iostream>
using namespace std;
//---------------------------------------------------------
// M A I N D R I V E R
//---------------------------------------------------------
int main()
{
int grErrCode;
// Initialize the graphics window.
initwindow(200, 150, "200 x 150");
grErrCode = graphresult(); // Get result of initialization
if( grErrCode != grOk ) // Error occured during init
{
cout << " Graphics System Error: " << grapherrormsg(grErrCode) << endl;
exit( 1 );
}
// Display corner coordinates
// Upper-left corner
line(1, 1, 1, 6);
line(1, 1, 6, 1);
line(1, 1, 10, 10);
bgiout << "(0, 0)";
outstreamxy(14, 12);
// Upper-right corner
line(198, 1, 198, 6);
line(198, 1, 193, 1);
line(198, 1, 189, 10);
bgiout << "(199, 0)";
outstreamxy(138, 12);
// Lower-right corner
line(198, 148, 198, 143);
line(198, 148, 193, 148);
line(198, 148, 189, 139);
bgiout << "(199, 149)";
outstreamxy(122, 119);
// Lower-left corner
line(1, 148, 1, 143);
line(1, 148, 6, 148);
line(1, 148, 10, 139);
bgiout << "(0, 149)";
outstreamxy(14, 119);
// Terminate the program after user taps almost any key.
getch();
closegraph();
return 0;
}
Simple Graphics Program
//-----------------------------------------------
// HelloWorld
//
// A very simple graphics program.
//-----------------------------------------------
#include <graphics.h>
#include <iostream>
using namespace std;
int main()
{
int grErrCode;
// Initialize the graphics window.
initwindow(200, 200, "Hello World");
grErrCode = graphresult();
if( grErrCode != grOk )
{
cout << " Graphics System Error: " << grapherrormsg(grErrCode) << endl;
exit( 1 );
}
// Draw a yellow square in the center of the screen.
setcolor(YELLOW);
line( 50, 50, 150, 50); // top
line(150, 50, 150, 150); // right side
line(150, 150, 50, 150); // bottom
line( 50, 150, 50, 50); // left side
// Draw a cyan square in the center of the screen.
getch();
cleardevice();
setcolor(CYAN);
moveto( 50, 50);
lineto(150, 50); // top
lineto(150, 150); // right side
lineto( 50, 150); // bottom
lineto( 50, 50); // left side
// Draw a light green square in the center of the screen.
getch();
cleardevice();
setcolor(LIGHTGREEN);
moveto( 50, 50);
linerel( 100, 0); // top
linerel( 0, 100); // right side
linerel(-100, 0); // bottom
linerel( 0, -100); // left side
// Draw a white square in the center of the screen.
getch();
cleardevice();
setcolor(WHITE);
int coord[10] = {50, 50, 150, 50, 150, 150, 50, 150, 50, 50};
drawpoly(5, coord);
// Fill the interior of the square with light blue.
setfillstyle(SOLID_FILL, LIGHTBLUE);
floodfill(100, 100, WHITE);
// Display the text "Hello World" in the middle of the square in black.
setcolor(BLACK);
setbkcolor(LIGHTBLUE);
bgiout << "Hello World";
outstreamxy(61, 92);
// Terminate the program after user taps almost any key.
getch();
closegraph();
return 0;
}
//-----------------------------------------------
// Hexagon
//
// Draw a regular hexagon
//-----------------------------------------------
#include <graphics.h>
#include <cmath>
#include <iostream>
using namespace std;
const double PI = 3.14159265358979;
Draw Hexagons
//----------------------------------------------
// drawHexagon1
//
// Draw a regular hexagon. This version uses the
// line function to draw each side. This version
// also relies on a set of predetermined points.
// As a result its usefulness is severly limited.
//----------------------------------------------
void drawHexagon1()
{
// Center at (300,300)
// Radius of 190
setcolor(LIGHTBLUE);
line(490, 300, 395, 465);
line(395, 465, 205, 465);
line(205, 465, 110, 300);
line(110, 300, 205, 135);
line(205, 135, 395, 135);
line(395, 135, 490, 300);
}
//----------------------------------------------
// drawHexagon2
//
// Draw a regular hexagon. This version uses the
// moveto and lineto functions to draw the sides.
// This version also relies on a set of
// predetermined points. As a result its
// usefulness is severly limited.
//----------------------------------------------
void drawHexagon2()
{
// Center at (300,300)
// Radius of 180
setcolor(LIGHTRED);
moveto(480, 300);
lineto(390, 456);
lineto(210, 456);
lineto(120, 300);
lineto(210, 144);
lineto(390, 144);
lineto(480, 300);
}
//----------------------------------------------
// drawHexagon3
//
// This function draws a regular hexagon using
// the specified color. It will have the
// specified (circumscribed) radius and be
// centered on the specified point. Because the
// characteristics of the hexagon are parameters
// to this function, the function is very
// versatile.
//----------------------------------------------
void drawHexagon3(int color,
int centerX, int centerY,
int radius)
{
double angle; // radians
int oldColor;
oldColor = getcolor();
setcolor(color);
angle = 0;
moveto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = PI / 3.0;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = 2.0 * PI / 3.0;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = PI;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = 4.0 * PI / 3.0;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = 5.0 * PI / 3.0;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
angle = 0;
lineto(centerX + int(double(radius) * cos(angle) + 0.5),
centerY + int(double(radius) * sin(angle) + 0.5));
setcolor(oldColor);
}
//---------------------------------------------------------
// M A I N D R I V E R
//---------------------------------------------------------
int main()
{
int grErrCode;
// Initialize a 600 by 600 graphics window.
initwindow(600, 600, "Hexagons");
grErrCode = graphresult(); // Get result of initialization
if( grErrCode != grOk ) // Error occured during init
{
cout << " Graphics System Error: " << grapherrormsg(grErrCode) << endl;
exit( 1 );
}
drawHexagon1();
getch();
drawHexagon2();
getch();
drawHexagon3(LIGHTGREEN, 300, 300, 170);
getch();
cleardevice();
for (int i = 0; i <= 400; i = i + 8)
{
drawHexagon3(COLOR(rand()%256,rand()%256,rand()%256), 300, 300, i);
}
getch();
closegraph();
return 0;
}