0% found this document useful (0 votes)
31 views11 pages

Graphics Practical

Uploaded by

swarajmandhare17
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)
31 views11 pages

Graphics Practical

Uploaded by

swarajmandhare17
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
You are on page 1/ 11

PROGRAM TO DRAW A LINE USING DDA LINE

DRAWING ALGORITHM

C Source Code
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void LineDDA(float X1,float Y1,float X2,float Y2);
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi");

int x1,y1,x2,y2;
printf("Maximum X -%d Maximum Y -%d\n",getmaxx(),getmaxy());
printf("Enter first point:-");
scanf("%d%d",&x1,&y1);
printf(Enter second point:-");
scanf("%d%d",&x2,&y2);
LineDDA(x1,y1,x2,y2);
getch();
closegraph();
}

void LineDDA(float X1,float Y1,float X2,float Y2)


{
float step,XInc,YInc,dX,dY;

cleardevice();
dX=X2-X1;
dY=Y2-Y1;

if(abs(dX)>abs(dY))
step=abs(dX);
else
step=abs(dY);

XInc=dX/step;
YInc=dY/step;

for(int i=0;i!=step;i++)
{
putpixel(X1,Y1,4);
X1=X1+XInc;
Y1=Y1+YInc;
}
}

1
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------

Maximum X - 639 Maximum Y – 479

Enter first point : 60 30

Enter second point : 400 300

2
PROGRAM TO DRAW A LINE USING BRESENHAM’S
LINE DRAWING ALGORITHM.

C Source Code

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

void lineBresenham(int X1,int Y1,int X2,int Y2);


void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;

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

printf("Enter Starting Point:");


scanf("%d%d",&x1,&y1);
printf("Enter End Point:");
scanf("%d%d",&x2,&y2);

lineBresenham(x1,y1,x2,y2);
getch();
closegraph();
}
void lineBresenham(int X1,int Y1,int X2,int Y2)
{
int dX,dY,temp,swap,s1,s2,P,n;
cleardevice();
dX=abs(X2-X1);
dY=abs(Y2-Y1);
s1=(X2-X1)/dX;
s2=(Y2-Y1)/dY;

if(dY>dX)
{
temp=dX;
dX=dY;
dY=temp;
swap=1;
}
else
swap=0;

3
n=1;
P=2*dY-dX;
do
{
putpixel(X1,Y1,4);

if(P>=0)
{
X1=X1+s1;
Y1=Y1+s2;
P=P+2*(dY-dX);
}
else
{
If(swap==1)
Y1=Y1+s2;
else
X1=X1+s1;
P=P+2*dY;
}
n=n+1;
}while(n<=dX);
}

--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------

Enter starting point : 60 30

Enter end point : 400 300

4
PROGRAM TO DRAW A MIRROR IMAGE OF A GIVEN
LINE WITH RESPECT TO Y-AXIS

C Source Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void mir(int,int,int,int);
void main()
{
int gdriver=DETECT,gmode,x1,x2,y1,y2;
clrscr();
printf("\n \t Enter starting co-ordinate:");
scanf("%d%d",&x1,&y1);
printf("\n \tEnter ending co-ordinate:");
scanf("%d%d",&x2,&y2);
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
mir(x1,x2,y1,y2);
getch();
}
void mir(int x1,int x2,int y1,int y2)
{
int mX,mY;
mX=getmaxx();
mY=getmaxy();
x1=x1%(mX/2);
x2=x2%(mX/2);
cleardevice();
setcolor(1);

line((mX/2),0,mX/2,mY) ;

setcolor(11);

line((mX/2)+x1,y1,(mX/2)+x2,y2);
line((mX/2)-x1,y1,(mX/2)-x2,y2);
}

5
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------

Enter starting co-ordinate :- 100 30

Enter ending co-ordinate :- 300 400

6
PROGRAM TO DRAW A CIRCLE USING
BRESENHAM’S CIRCLE DRAWING ALGORITHM

C source code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void BresCircle(int,int,int);
void PutPixel(int,int,int,int);

void main()
{
int gd=DETECT,gm;
int Xc,Yc,R;
initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("\n Enter the center of the circle:-");


scanf("%d%d",&Xc,&Yc);

printf("\n \t Enter the radius of the circle:-");


scanf("%d",&R);

BresCircle(Xc,Yc,R);

getch();
closegraph();
}

void BresCircle(int Xc,int Yc,int R)


{
int P,X,Y,col;

P=3-2*R;
X=0;
Y=R;
do
{
PutPixel(X,Y,Xc,Yc);

7
X=X+1;
if(P<0)
{
P=P+4*X+6;
}
else
{
Y=Y-1;
P=P+4*(X-Y)+10;
}
}while(X<R/sqrt(2));
}

void PutPixel(int X,int Y,int Xc,int Yc)


{
putpixel(Xc+X,Yc+Y,3);
putpixel(Xc+X,Yc-Y,3);
putpixel(Xc-X,Yc+Y,3);
putpixel(Xc-X,Yc-Y,3);
putpixel(Xc+Y,Yc+X,3);
putpixel(Xc+Y,Yc-X,3);
putpixel(Xc-Y,Yc+X,3);
putpixel(Xc-Y,Yc-X,3);
}

--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------

Enter the co-ordinate of the centre : 200 150

Enter the radius of the circle : 100

8
PROGRAM TO DRAW A CIRCLE USING MIDPOINT
CIRCLE DRAWING ALGORITHM

C source code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void circleMidPoint(int,int,int);

void main()
{
int x,y,radius,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the coordinate of the centre:-");
scanf("%d%d",&x,&y);
printf("Enter the radius of the circle:-");
scanf("%d",&radius);
circleMidPoint(x,y,radius);
getch();
closegraph();
}
void circleMidGraph(int Xc,int Yc,int R)
{
float X=0,Y=R;
float P;

do
{
putpixel(Xc+X,Yc+Y,3);
putpixel(Xc-X,Yc+Y,3);
putpixel(Xc+X,Yc-Y,3);
putpixel(Xc-X,Yc-Y,3);
putpixel(Xc+Y,Yc+X,3);
putpixel(Xc-Y,Yc+X,3);

9
putpixel(Xc+Y,Yc-X,3);
putpixel(Xc-Y,Yc-X,3);

++X;
P=pow(X,2)+pow((Y-0.5),2)-pow(R,2);
if(P>=0)
--Y;
}while(X<Y);
}

--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------

Enter the co-ordinate of the centre : 200 150

Enter the radius of the circle : 100

10
11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy