Sunteți pe pagina 1din 11

Computer Graphics (CG) Programs

Computer Graphics Programs in C - What you will learn?

You are the one who is interested in learning the fundamentals of the graphical
programming languages. This Page contains a list of fundamental graphics program for
various shapes. You will learn basics to draw geometrical shapes like circle, ellipse,
rectangle, etc.
We will also learn graphics programming in c to draw curves, objects with various
colors, 3d graphics programming, simple animation programs, and algorithms
programs.

How to start writing Computer Graphics Code in C: Instructions

When you start writing computer graphics code. The first step is to initialize
the graphics drivers using the initgraph() method given in graphics.h Library.

Instruction:
For all the Graphics Program Do this before you run the program.
1. Find initgraph(&gd, &gm, ""); in Program.
2. Write the path of BGI Folder, which there inside TC.
Like, initgraph(&gd, &gm, "C://TC//BGI");
or initgraph(&gd, &gm, "C://TurboC++//Disk//TurboC3//BGI");
3. Copy "EGAVGA.BGI" File from BGI Folder and Paste it to BIN Folder.

Arc Function - Computer Graphics CG


Program
on - September 18, 2014
Declare :- void arc(int x, int y, int stangle, int endangle, int sweep);

curve capacity is utilized to draw a circular segment with focus (x,y) and stangle
determines beginning edge, endangle details the end plot and last parameter points out
the sweep of the bend. curve capacity can additionally be utilized to draw a loop yet for
that beginning point and end edge ought to be 0 and 360 individually.
In the below program (100,100) are directions of middle of circular segment, 0 is the
beginning plot, 135 is the end edge and 50 indicates the sweep of the curve.
#include <graphics.h>
#include <conio.h>

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

arc(100, 100, 0, 135, 50);

getch();
closegraph();
return 0;
}

Bar Function - Computer Graphics CG


Program
on - September 18, 2014
#include <graphics.h>
#include <conio.h>

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

bar(100, 100, 200, 200);

getch();
closegraph();
return 0;
}

Declaration :- void bar(int left, int top, int right, int base);

Bar capacity is utilized to draw a 2-dimensional, rectangular filled in bar . Directions of


left top and right bottom corner are obliged to draw the bar. Left tags the X-direction of
upper left corner, top details the Y-direction of upper left corner, right determines the
X-direction of right bottom corner, lowest part points out the Y-direction of right base
corner. Current fill example and fill color is utilized to fill the bar. To change fill example
and fill shade use set fill-style.

Circle Function - Computer Graphics CG


Program
on - July 06, 2016
Statement: void circle (int x,int y,int radius);

Circle function is used to draw a circle with center (x, y) and the third parameter is the
radius of the circle. The following code draws a circle.

In the following program (100, 100), the coordinates of the circle center and 50 is the
radius of the circle.

#include<graphics.h>
#include<conio.h>

main() {
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

circle(100, 100, 50);

getch();

closegraph();
return 0;
}

Program to Draw a Line using DDA


Algorithm - CG
on - April 05, 2013

Program :
#include <stdio.h>
#include <dos.h>
#include <graphics.h>

void lineDDA(int, int, int, int);

void main()
{

int x1, y1, xn, yn;


int gd = DETECT, gm;
initgraph(&gd, &gm, "");

printf("Enter the starting coordinates of line: ");


scanf("%d %d", &x1, &y1);

printf("Enter the ending coordinates of line: ");


scanf("%d %d", &xn, &yn);

lineDDA(x1, y1, xn, yn);


getch();
}

void lineDDA(int x1, int y1, int xn, int yn)


{

int dx, dy, m, i;


m = (yn-y1)/(xn-x1);

for (i=x1; i<=xn; i++)


{
if (m <= 1)
{
dx = 1;
dy = m * dx;
}
else
{
dy = 1;
dx = dy / m;
}

x1 = x1 + dx;
y1 = y1 + dy;

putpixel(x1, y1, RED);

delay(20);
}

Program to Draw a Line using Bresenham's


Algorithm - CG
on - April 05, 2013
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void lineBres(int, int, int, int);

void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineBres(x1, y1, xn, yn);
getch();
}

void lineBres(int x1, int y1, int xn, int yn)


{
int dx = xn - x1, dy = yn - y1;
int di = 2 * dy - dx;
int ds = 2 * dy, dt = 2 * (dy - dx);
putpixel(x1, y1, RED);
while (x1 < xn)
{
x1++;
if (di < 0)
di = di + ds;
else
{
y1++;
di = di + dt;
}
putpixel(x1, y1, RED);
delay(20);
}
}

rogram to draw a line using Cartesian


Slope-Intercept Equation
on - October 15, 2016
Below is the C++ program to draw a line using Cartesian Slope-Intercept Equation.
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>

//____ Function Prototypes ____//


void show_screen( );
void slope_intercept_line(const int,const int,const int,const int);

//____ main( ) ____//

int main( )
{
int driver=VGA;
int mode=VGAHI;

int p1=0;
int q1=0;

int p2=0;
int q2=0;

do
{
show_screen( );

gotoxy(8,10);
cout<<"Coordinates of Point_I (p1,q1) :";

gotoxy(8,11);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";

gotoxy(12,13);
cout<<"Enter the value of p1 = ";
cin>>p1;
gotoxy(12,14);
cout<<"Enter the value of q1 = ";
cin>>q1;

gotoxy(8,18);
cout<<"Coordinates of Point_II (p2,q2) :";

gotoxy(8,19);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";

gotoxy(12,21);
cout<<"Enter the value of p2 = ";
cin>>p2;

gotoxy(12,22);
cout<<"Enter the value of q2 = ";
cin>>q2;

initgraph(&driver,&mode,"..\\Bgi");

setcolor(15);
slope_intercept_line(p1,q1,p2,q2);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");

int key=int(getch( ));

if(key!=13)
break;
}
while(1);

return 0;
}
//____ Funcion Definitions ____//

//____ slope_intercept_line( ) function ____//

void slope_intercept_line(const int p1,const int q1,


const int p2,const int q2)
{
int color=getcolor( );

float x=p1;
float y=q1;

float dx=(p2_p1);
float dy=(q2_q1);

float m=(dy/dx);
float b=(y_(m#x));

float x_inc=((p2>=p1)?1:_1);

putpixel(x,y,color);

while((int)(x+0.5)!=p2)
{
x+=x_inc;
y=((m#x)+b);

putpixel((int)(x+0.5),(int)(y+0.5),color);
}
}

//____ show_screen( ) function ____//

void show_screen( )
{
restorecrtmode( );
textmode(C4350);

cprintf("\n##################################################
##############################");
cprintf("####################_ _###############
#####");
cprintf("#____________________ ");

textbackground(1);
cprintf(" Cartesian Slope Intercept Equation ");
textbackground(8);

cprintf(" ____________________#");
cprintf("#_##################_ _###############
###_#");
cprintf("#_##################################################
##########################_#");

for(int count=0;count<42;count++)
cprintf("#_# #_#");

gotoxy(1,46);
cprintf("#_##################################################
##########################_#");
cprintf("#___________________________________________________
___________________________#");
cprintf("####################################################
############################");

gotoxy(8,40);
cout<<"Note :";

gotoxy(8,41);
cout<<"IIIIII";
gotoxy(10,43);
cout<<"This program is better for those lines with é<ñ45ø with x_axis.";

gotoxy(1,2);
}

//____ THE END ____//

S-ar putea să vă placă și