Maharishi Markandeshwar University, Sadopur, Ambala Date: Program No.: 1 Aim: Write A Program To Implement DDA Line Drawing Algorithm
Maharishi Markandeshwar University, Sadopur, Ambala Date: Program No.: 1 Aim: Write A Program To Implement DDA Line Drawing Algorithm
Maharishi Markandeshwar University, Sadopur, Ambala Date: Program No.: 1 Aim: Write A Program To Implement DDA Line Drawing Algorithm
Date :
Program No. : 1
Aim : Write a program to implement DDA line drawing algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,":");
printf("Enter First Point : ");
scanf("%d%d",&x1,&y1);
printf("Enter Second Point : ");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,6);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,7);
}
getch();
closegraph();
}
Program No. : 2
Aim : Write a program to implement Bresenhams line drawing algorithm.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,x1,y1,x2,y2,dx,dy,p;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
printf("Enter values of x1 and y1: ");
scanf("%d %d",&x1,&y1);
printf("Enter values of x2 and y2 : ");
scanf("%d %d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
p = 2 * (dy) - (dx);
x=x1;
y=y1;
putpixel(x,y,WHITE);
while(x<=x2)
{
if(p<0)
p=p+(2*dy);
else
{
p=p+(2*dy)-(2*dx);
y++;
}
x++;
putpixel(x,y,WHITE);
}
getch();
}
Output :
Output :
Output :
10
11
2) Scaling
3) Rotation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate(int,int);
void scale(float,float);
void rotate(float);
void main()
{
int ch;
int gd=DETECT,gm;
int tx,ty;
float sx,sy;
float theta;
initgraph(&gd,&gm,"c:\\tc\\bgi");
outtextxy(20,100,"Object.");
outtextxy(20,110,"-------");
rectangle(100,250,150,200);
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1:
outtextxy(10,45,"Enter value of tx and ty:");
scanf("%d %d",&tx,&ty);
translate(tx,ty);
break;
case 2:
outtextxy(10,45,"Enter the value of sx and sy:");
scanf("%f%f",&sx,&sy);
scale(sx,sy);
break;
case 3:
outtextxy(10,50,"Enter the angle for rotation: ");
scanf("%f",&theta);
rotate(theta);
break;
default:
printf("Invalid choice!!!!");
break;
}
getch();
closegraph();
12
13
14
15
16
17
18
Output :
19
20
21