0% found this document useful (0 votes)
29 views13 pages

Graphics Assignment

Uploaded by

xiwof42578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

Graphics Assignment

Uploaded by

xiwof42578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Write a program to implement DDA line drawing algorithm.

Ans.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

x0 = 80, y0 = 160, x1 = 300, y1 = 200;


dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
Output:
2. Write a program to implement Bresenham’s line drawing algorithm.
Ans.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}

void main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
}

Output:
[Link] a program to implement Midpoint circle drawing algorithm.
Ans.
#include <stdio.h>

#include <graphics.h>

void drawCircle(int x1, int y1, int r);

void main()

{ int gd = DETECT, gm;

int x, y, r;

printf("Enter the Midpoint and Radius: ");

scanf("%d%d%d", &x, &y, &r);

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

drawCircle(x, y, r);

getch();

closegraph();

void drawCircle(int x1, int y1, int r)

int x = 0, y = r;

int p = 1 - r;

void plotPoints(int, int, int, int);

plotPoints(x1, y1, x, y);

while (x < y)

x++;

if (p < 0)

p += 2 * x + 1;

else

y--;

p += 2 * (x - y) + 1; }
plotPoints(x1, y1, x, y);

void plotPoints(int xctr, int yctr, int x, int y)

putpixel(xctr + x, yctr + y, 1);

putpixel(xctr - x, yctr + y, 1);

putpixel(xctr + x, yctr - y, 1);

putpixel(xctr - x, yctr - y, 1);

putpixel(xctr + y, yctr + x, 1);

putpixel(xctr - y, yctr + x, 1);

putpixel(xctr + y, yctr - x, 1);

putpixel(xctr - y, yctr - x, 1);

Output:
[Link] a program to implement Bresenham circle drawing algorithm.
Ans.
# include <stdio.h>

# include <conio.h>

# include <graphics.h>

void main()

int xc,yc,r,p,x,y;

int gd,gm;

detectgraph(&gd,&gm);

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

clrscr();

printf("\nEnter the co-ordinates of center : ");

scanf("%d %d",&xc,&yc);

printf("\nEnter the radius: ");

scanf("%d",&r);

x = 0;

y = r;

p=3-(2*r);

for(x=0;x<=y;x++)

if(p < 0)

p = p + (4 * x)+6;

else

y=y-1;

p = p +4 *(x-y)+10;

putpixel(xc+x,yc-y,WHITE);

putpixel(xc-x,yc-y,WHITE);
putpixel(xc+x,yc+y,WHITE);

putpixel(xc-x,yc+y,WHITE);

putpixel(xc+y,yc-x,WHITE);

putpixel(xc-y,yc-x,WHITE);

putpixel(xc+y,yc+x,WHITE);

putpixel(xc-y,yc+x,WHITE);

getch();

closegraph();

Output:
5. Write a program to clip a line using Cohen and Sutherland line
clipping algorithm.
Ans.
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<graphics.h>

#include<dos.h>

typedef struct coordinate

int x,y;

char code[4];

}PT;

void drawwindow();

void drawline(PT p1,PT p2);

PT setcode(PT p);

int visibility(PT p1,PT p2);

PT resetendpt(PT p1,PT p2);

void main()

int gd=DETECT,v,gm;

PT p1,p2,p3,p4,ptemp;

printf("\nEnter x1 and y1\n");

scanf("%d %d",&p1.x,&p1.y);

printf("\nEnter x2 and y2\n");

scanf("%d %d",&p2.x,&p2.y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

drawwindow();

delay(500);

drawline(p1,p2);

delay(500);

cleardevice();
delay(500);

p1=setcode(p1);

p2=setcode(p2);

v=visibility(p1,p2);

delay(500);

switch(v)

case 0: drawwindow();

delay(500);

drawline(p1,p2);

break;

case 1: drawwindow();

delay(500);

break;

case 2: p3=resetendpt(p1,p2);

p4=resetendpt(p2,p1);

drawwindow();

delay(500);

drawline(p3,p4);

break;

delay(5000);

closegraph();

void drawwindow()

line(150,100,450,100);

line(450,100,450,350);

line(450,350,150,350);

line(150,350,150,100);

void drawline(PT p1,PT p2)


{

line(p1.x,p1.y,p2.x,p2.y);

PT setcode(PT p) //for setting the 4 bit code

PT ptemp;

if(p.y<100)

[Link][0]='1'; //Top

else

[Link][0]='0';

if(p.y>350)

[Link][1]='1'; //Bottom

else

[Link][1]='0';

if(p.x>450)

[Link][2]='1'; //Right

else

[Link][2]='0';

if(p.x<150)

[Link][3]='1'; //Left

else

[Link][3]='0';

ptemp.x=p.x;

ptemp.y=p.y;

return(ptemp);

int visibility(PT p1,PT p2)

{
int i,flag=0;

for(i=0;i<4;i++)

if(([Link][i]!='0') || ([Link][i]!='0'))

flag=1;

if(flag==0)

return(0);

for(i=0;i<4;i++)

if(([Link][i]==[Link][i]) && ([Link][i]=='1'))

flag='0';

if(flag==0)

return(1);

return(2);

PT resetendpt(PT p1,PT p2)

PT temp;

int x,y,i;

float m,k;

if([Link][3]=='1')

x=150;

if([Link][2]=='1')

x=450;
if(([Link][3]=='1') || ([Link][2]=='1'))

m=(float)(p2.y-p1.y)/(p2.x-p1.x);

k=(p1.y+(m*(x-p1.x)));

temp.y=k;

temp.x=x;

for(i=0;i<4;i++)

[Link][i]=[Link][i];

if(temp.y<=350 && temp.y>=100)

return (temp);

if([Link][0]=='1')

y=100;

if([Link][1]=='1')

y=350;

if(([Link][0]=='1') || ([Link][1]=='1'))

m=(float)(p2.y-p1.y)/(p2.x-p1.x);

k=(float)p1.x+(float)(y-p1.y)/m;

temp.x=k;

temp.y=y;

for(i=0;i<4;i++)

[Link][i]=[Link][i];

return(temp);

else
return(p1);

Output:

Before clipping:

After clipping:

You might also like