INDEX
Teacher’s
[Link] Topic & Date Signature
1
PROGRAM - I
Q. Write a program in C to perform DDA algorithm to
generate a line.
Program:
#include <stdio.h>
#include <graphics.h>
void dda_algorithm(int dx, int dy, int x1, int x2, int y1, int y2, int
x, int y, int delay_time);
int main()
{
int gd = DETECT, gm;
int delay_time, x, y, dx, dy;
int x1, y1, x2, y2;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\nEnter Initial Line Coordinates:\n");
printf("\nEnter the value of x1:\t");
scanf("%d", &x1);
printf("\nEnter the value of y1:\t");
scanf("%d", &y1);
printf("\nEnter Final Line Coordinates:\n");
printf("\nEnter the value of x2:\t");
scanf("%d", &x2);
printf("\nEnter the value of y2:\t");
scanf("%d", &y2);
printf("\nEnter delay time:\t");
scanf("%d", &delay_time);
dda_algorithm(dx, dy, x1, x2, y1, y2, x, y, delay_time);
return 0;
}
2
void dda_algorithm(int dx, int dy, int x1, int x2, int y1, int y2, int
x, int y, int delay_time)
{
int count = 1;
int increment;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
if(dx >= dy)
{
increment = dx;
}
else
{
increment = dy;
}
dx = dx / increment;
dy = dy / increment;
x = x1;
y = y1;
for(count = 1; count <= increment; count++)
{
putpixel(x, y, 4);
x = x + dx;
y = y + dy;
delay(delay_time);
}
closegraph();
}
3
Program I
Output
4
PROGRAM - II
Q. Write a program in C to generate different lines style.
Program:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
int s;
char *lname[]={"SOLID LINE","DOTTED LINE","CENTER LINE",
"DASHED LINE","USERBIT LINE"};
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
clrscr();
cleardevice();
printf("Line styles:");
for (s=0;s<5;s++)
{
setlinestyle(s,1,3);
line(100,30+s*50,250,250+s*50);
outtextxy(255,250+s*50,lname[s]);
}
getch();
closegraph();
}
5
Program II
Output
6
PROGRAM - III
Q. Write a program in C to draw smiley face.
Program:
#include <graphics.h>
#include<stdio.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
//graphic mode variable
int gd=DETECT,gm;
//calling initgraph function with
//certain parameters
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
//Printing message for user
outtextxy(10, 10 + 10, "Program to draw a smiley face in C
graphics");
//setting color to yellow
setcolor(YELLOW);
//creating circle and fill it with
//yellow color using floodfill.
circle(300, 100, 40);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(300, 100, YELLOW);
//setting color to black
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
7
//using fill ellipse and ellipse for creating face.
fillellipse(310, 85, 2, 6);
fillellipse(290, 85, 2, 6);
ellipse(300, 100, 205, 335, 20, 9);
ellipse(300, 100, 205, 335, 20, 10);
ellipse(300, 100, 205, 335, 20, 11);
getch();
return 0;
}
8
Program III
Output
9
PROGRAM - IV
Q. Write a program in C to generate a moving car .
Program:
#include <graphics.h>
#include <conio.h>
#include<stdio.h>
#include<dos.h>
main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
setviewport(0,0,639,440,1);
for( i = 0 ; i <= 420 ; i = i + 10, j++ )
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);
if( i == 420 )
break;
clearviewport();
}
getch();
closegraph();
return 0;
}
10
Program IV
Output 1
OUTPUT 2
11
PROGRAM - V
Q. Write a program in C to perform Bresenham’s algorithm to
draw a circle .
Program:
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
// Function to put pixels
// at subsequence points
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
// Function for circle-generation
// using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x)
{
// for each pixel we will
// draw all eight pixels
drawCircle(xc, yc, x, y);
12
x++;
// check for decision parameter
// and correspondingly
// update d, x, y
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
}
// driver function
int main()
{
int xc = 50, yc = 50, r2 = 30;
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
13
Program V
Output
14