Think of the computer screen as a blank piece of paper. On the screen we want to draw points, lines, polygons, and translate them, rotate them, clip them and scale them. Matrices and matrix operationscome in very handy here.
Definition: A point is position in a 2-d plane
Program 1. A Program to Plot Points
#include <stdipo.h>
// this includes the basics
#include <graph.h>
// this includes Microsoft's graphics header
void main(void)
{
int x,y,color,index;
//put the computer into graphics mode
_setvideomode(_vres16color); //this sets mode to 640x480 in 16 colors
//let us draw10,000 points randomly on the screen
for (index
= 0; index < 10000; index++
{
// geta random position and color and plot the point there
x = rand()%640;
y = rand()%480;
color = rand()%16;
_setcolor(color); //set the color of the pixel to be drawn
_setpixel(x,y); //draw the pixel;
}// end for index
//wait for the
user to hit a key
while(!kbhit()){}
//put the computer back into text mode
_setvideomode (_defaultmode);
}//end main
Note: rand() is a psedo random number generator! So it will generate the same random number every time it is invoked. So all points will be identical. We can overcome this problem by using srand() - a RNG that uses a different seed everytime it is invoked.
Program 2. A Program to Draw a Line.
#include <stdipo.h>
// this includes the basics
#include <graph.h>
// this includes Microsoft's graphics header
void main(void)
{
int x1,y1,x2,y2,color,index;
//put the computer into graphics mode
_setvideomode(_vres16color); //this sets mode to 640x480 in 16 colors
//let us draw 1000 lines randomly on the screen
for (index
= 0; index < 1000; index++
{
// geta random position and color and draw a line there
x1 = rand()%640;//x-axis location of the starting point
y1 = rand()%480;//y-axis location of the starting point
x2 = rand()%640;//x-axis location of the end point
y2 = rand()%480;//y-axis location of the end point
color = rand()%16;
_setcolor(color); //set the color of the pixel to be drawn
_moveto(x1,y1); //move to the start of the line;
_lineto(x2,y2); //draw the line;
}// end for index
//wait for the
user to hit a key
while(!kbhit()){}
//put the computer back into text mode
_setvideomode (_defaultmode);
}//end main
Program 3. A Program to Draw a Polygon
#include <stdio.h>
// this includes the basics
#include <graph.h>
// this includes Microsoft's graphics header
void main(void)
{
int x1,y1,x2,y2,color,index;
//put the computer into graphics mode
_setvideomode(_vres16color); //this sets mode to 640x480 in 16 colors
//draw a simple polygon
_setcolor(1); //make it blue
_moveto(100,100); //define vertex 1;
_lineto(120,120); //define vertex 2;
_lineto(150,200); //define vertex 3;
_lineto(80,190); //define vertex 4;
_lineto(100,100); //back to vertex 1;
//wait for the user to hit a key
while(!kbhit()){}
//put the computer back into text mode
_setvideomode (_defaultmode);
}//end main
A data structure to define veritices and objects.
//Defining the structureof a vertex
typedef struct vertex_typ
{
float x, y; //this is a single point in 2-d plane
} vertex, *vertex_ptr;
//Defining the structure of an object
typedef struct object_typ
{
int num_vertices; //this is the number of verticies in the object
int color; //this is the color of the object
float x0, y0; //this is the position of this object
vertex vertices[16]; //this defines 16 vertices
} object, *object_ptr;