0% found this document useful (0 votes)
18 views6 pages

Dda

The document contains multiple C programs that demonstrate various graphics algorithms, including DDA line drawing, Bresenham's line algorithm, Bezier curves, bit mapping, polygon filling, and circle generation using the midpoint algorithm. Each program initializes the graphics mode, takes user input for coordinates or control points, and visually represents the specified shapes on the screen. The code utilizes the graphics.h library for rendering and includes delays to control the drawing speed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Dda

The document contains multiple C programs that demonstrate various graphics algorithms, including DDA line drawing, Bresenham's line algorithm, Bezier curves, bit mapping, polygon filling, and circle generation using the midpoint algorithm. Each program initializes the graphics mode, takes user input for coordinates or control points, and visually represents the specified shapes on the screen. The code utilizes the graphics.h library for rendering and includes delays to control the drawing speed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

dda

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
int main()
{
float x,y,y1,x1,x2,y2,dx,dy,pixel;
int i,gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\BGI");
printf("Enter the value of x1:");
scanf("%f",&x1);
printf("Enter the value of y1:");
scanf("%f",&y1);
printf("Enter the value of x2:");
scanf("%f",&x2);
printf("Enter the value of y2:");
scanf("%f",&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,YELLOW);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int x1,y1,x2,y2,x,y,p1,p2,p0,dx,dy;
int i,gm; int gd=DETECT;
initgraph(&gd,&gm,"..\\bgi");
printf("Enter value of x1 and y1");
scanf("%d %d",&x1,&y1);
printf("Enter value of x2 and y2");
scanf("%d %d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
p1=2*dy;
p2=2*dy-2*dx;
p0=2*dy-dx;
for(i=0;i<=dx;i++)
{
if(p0<0)
{
x=x+1; y=y; p0=p0+p1;
}
else
{
x=x;
y=y+1;
p0=p0+p2;
}
putpixel(x,y,BLUE); delay(100);
}
getch();
return 0;
}
Bezier Curve
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x[4],y[4],px,py,i,n;
double t;
clrscr();
initgraph (&gd, &gm, "..\\bgi");
printf("Enter the no of control points");
scanf("%d",&n);
printf("Enter the control points of bezier curve: ");
for(i=0;i<n;i++)
{ scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],4); }
for(t=0.0;t<=1.0;t+=0.001){
px=(1-t)*(1-t)*(1-t)*x[0]+3*t*(1-t)*(1-t)*x[1]+3*t*t*(1-t)*x[2]+t*t*t*x[3];
py=(1-t)*(1-t)*(1-t)*y[0]+3*t*(1-t)*(1-t)*y[1]+3*t*t*(1-t)*y[2]+t*t*t*y[3];
putpixel(px,py,WHITE);
delay(2);
}
getch();
closegraph();
}
Bit mapping
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int i,j,k,x,y;
int gd=DETECT,gm;
int ch1[][10]={
{1,1,1,0,0,0,0,1,1,1},
{1,1,1,0,0,0,0,1,1,1},
{0,1,1,0,0,0,0,1,1,0},
{0,0,1,1,0,0,0,1,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0} };
int ch2[][10]={
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1} } ;
int ch3[][10]={
{0,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0,1,1},
{0,1,1,0,0,0,0,1,1,1},
{0,0,1,1,1,1,1,1,1,1},
{0,0,0,0,1,1,1,1,1,1},
{0,0,0,0,0,0,0,1,1,1} };
initgraph(&gd,&gm,"..\\BGI");
setbkcolor(BLUE);
for(k=0;k<6;k++)
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(k==0)
{
if(ch1[i][j]==1)
{
putpixel(j+200,i+230,RED);
}
}
if(k==1)
{
if(ch2[i][j]==1)
{
putpixel(j+250,i+230,RED);
}
}
if(k==2)
{
if(ch3[i][j]==1)
{
putpixel(j+300,i+230,RED);
}
}
}
delay(200);
}
}
getch();
closegraph();
}
Flooding
#include <stdio.h>
#include<dos.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int n, i, j, k, gd = DETECT, gm, dy, dx;
int x, y, temp;
int a[20][2], xi[20];
float slope[20];
clrscr();
initgraph(&gd, &gm, "..\\bgi");
printf("\n\n\tEnter the no. of edges of polygon :");
scanf("%d", &n);
printf("\n\n\tEnter the cordinates of polygon:\n\n\n");
for(i=0;i<n;i++) {
printf("\tX%dY%d : ",i,i);
scanf("%d%d", &a[i][0], &a[i][1]);
}
a[n][0] = a[0][0];
a[n][1] = a[0][1];
for (i = 0; i < n; i++)
{
line(a[i][0], a[i][1], a[i + 1][0], a[i + 1][1]);
}
for (i = 0; i < n; i++)
{
dy = a[i + 1][1]- a[i][1];
dx = a[i + 1][0]- a[i][0];
if (dy == 0){
slope[i] = 1.0;}
if (dx == 0){
slope[i] = 0.0;}
if((dy!=0)&&(dx!=0))
{
slope[i] = (float)dx/dy;
}
}
for(y=0; y<480; y++){
k = 0;
for (i = 0; i < n; i++)
{
if (((a[i][1] <= y) && (a[i + 1][1] > y)) || ((a[i][1] > y) && (a[i
+ 1][1] <= y)))
{
}
}
xi[k] = (int)(a[i][0] + slope[i] * (y- a[i][1]));
k++;
for (j = 0; j < k- 1; j++){
for (i = 0; i < k- 1; i++)
{
if (xi[i] > xi[i + 1])
{
temp = xi[i];
xi[i] = xi[i + 1];
xi[i + 1] = temp;
}
}
setcolor(35);
}
for (i = 0; i < k; i += 2)
{
line(xi[i], y, xi[i + 1] + 1, y);
delay(20);
}
}
getch();
closegraph();
}
//Midpoint circle generation Algorithm
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int xc,yc,x,y,r,gd=DETECT,gm;
float p0;
clrscr();
initgraph(&gd,&gm,"..//BGI");
printf("Enter the value of radius and coordinates of circle:\n");
scanf("%d%d%d",&r,&xc,&yc);
x=0;
y=r;
p0=1.25-r;
while(x<y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,4);
putpixel(yc+y,xc+x,5);
putpixel(yc+y,xc-x,6);
putpixel(yc-y,xc-x,7);
putpixel(yc-y,xc+x,8);
delay(100);
if(p0<0)
{
x=x+1;
y=y;
p0=p0+2*x+1;
}
else
{
x=x+1;
y=y-1;
p0=p0+2*(x-y)+1;
}
}
closegraph();
getch();
}

You might also like