0% found this document useful (0 votes)
42 views8 pages

Cad Program PDF

The document discusses algorithms for drawing lines, circles, and ellipses in computer graphics. It provides C code implementations of the DDA line algorithm, Bresenham's line algorithm, Bresenham's circle algorithm, midpoint circle algorithm, and midpoint ellipse algorithm. Each algorithm uses basic graphics functions like putpixel() to plot points and draw shapes pixel-by-pixel on a 2D graphics plane.

Uploaded by

Darshan Patel
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)
42 views8 pages

Cad Program PDF

The document discusses algorithms for drawing lines, circles, and ellipses in computer graphics. It provides C code implementations of the DDA line algorithm, Bresenham's line algorithm, Bresenham's circle algorithm, midpoint circle algorithm, and midpoint ellipse algorithm. Each algorithm uses basic graphics functions like putpixel() to plot points and draw shapes pixel-by-pixel on a 2D graphics plane.

Uploaded by

Darshan Patel
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/ 8

DDA LINE ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#define round(a)((int)(a+0.5))
void main()
{
clrscr();
int gm=DETECT,gd;
initgraph(&gm,&gd,"C:\\Turboc3\\BGI");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
float x1,y1,x2,y2,x,y,m,dx,dy;
int steps,i;
printf("enter co-ordinate first point x1,y1\n");
scanf("%f\n%f",&x1,&y1);
printf("enter co-ordinate last point x2,y2\n");
scanf("%f\n%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(abs(dx)>=abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
x=x1;
y=y1;
putpixel(getmaxx()/2+x,getmaxy()/2-y,RED);
for(i=0;i<steps;i++)
{
if(abs(dx)>=abs(dy))
{
if (x1<x2)
{
x=x+1;
y=y+m;
}
else
{
x=x-1;
y=y-m;
}
}
else
{
if(y1<y2)
{
y=y+1;
PREPARED BY: U. J. PATEL

Page 1

DDA LINE ALGORITHM


x=x+1/m;
}
else
{
y=y-1;
x=x-1/m;
}
}
putpixel(getmaxx()/2+round(x),getmaxy()/2-round(y),YELLOW);
delay(500);
}
getch();
closegraph();
}

PREPARED BY: U. J. PATEL

Page 2

BRESENHAMS LINE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gm=DETECT,gd;
initgraph(&gm,&gd,"C:\\Turboc3\\BGI");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
float x1,x2,y1,y2,x,y,m,dx,dy,p;
int steps,i;
printf("enter co-ordinate first point x1,y1\n");
scanf("%f\n%f",&x1,&y1);
printf("enter co-ordinate last point x2,y2\n");
scanf("%f\n%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=abs(dy)/abs(dx);
if(abs(dx)>=abs(dy))
{
steps=abs(dx);
p=2*dy - dx;
}
else
{
steps=abs(dy);
p=2*dx - dy;
}
x=x1;
y=y1;
putpixel(getmaxx()/2+x,getmaxy()/2-y,RED);
for(i=0;i<steps;i++)
{
if(abs(dx)>=abs(dy))
{
if (x1<x2)
{
x=x+1;
if(p<0)
{
y=y;
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
}
PREPARED BY: U. J. PATEL

Page 3

BRESENHAMS LINE ALGORITHM


else
{
x=x-1;
if(p<0)
{
y=y;
p=p-2*dy;
}
else
{
y=y-1;
p=p-2*(dy-dx);
}
}
}
else
{
if(y1<y2)
{
y=y+1;
if(p<0)
{
x=x;
p=p+2*dx;
}
else
{
x=x+1;
p=p+2*(dx-dy);
}
}
else
{
y=y-1;
if(p<0)
{
x=x;
p=p-2*dx;
}
else
{
x=x-1;
p=p-2*(dx-dy);
}
}
}
putpixel(getmaxx()/2+x,getmaxy()/2-y,YELLOW);
delay(500);
}
getch();
closegraph();
}

PREPARED BY: U. J. PATEL

Page 4

BRESENHAMS CIRCLE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gm=DETECT,gd;
initgraph(&gm,&gd,"C:\\Turboc3\\BGI");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
int x,y,dx,dy,r,p,xc,yc;
printf("Enter center co-ordinate of the circle xc,yc\n");
scanf("%d\n%d",&xc,&yc);
printf("Enter the value of radius of circle\n")
Enter the radius of the circle");
scanf("%d\n",&r);
x=0;
y=r;
p=3-2r;
putpixel(getmaxx()/2+xc,getmaxy()/2+yc,RED);
whlie(x<y)
{
x++;
if(p<0)
{
y=y;
p=p+4x+2;
}
else
{
y=y-1;
p=p+4(x-y)+2;
}
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc-y,YELLOW);
putpixel(getmaxx()/2+xc+y,getmaxy()/2-yc-x,GREEN);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc-y,BLUE);
putpixel(getmaxx()/2+xc-y,getmaxy()/2-yc-x,RED);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc+y,WHITE);
putpixel(getmaxx()/2+xc-y,getmaxy()/2-yc+x,YELLOW);
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc+y,GREEN);
putpixel(getmaxx()/2+xc+y,getmaxy()/2-yc+x,BLUE);
delay(500);
}
getch();
closegraph();
}

PREPARED BY: U. J. PATEL

Page 5

MID-POINT CIRCLE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gm=DETECT,gd;
initgraph(&gm,&gd,"C:\\Turboc3\\BGI");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
int x,y,dx,dy,r,p,xc,yc;
printf("Enter center co-ordinate of the circle xc,yc\n");
scanf("%d\n%d",&xc,&yc);
printf("Enter the value of radius of circle\n")
Enter the radius of the circle");
scanf("%d\n",&r);
x=0;
y=r;
p=1-r;
putpixel(getmaxx()/2+xc,getmaxy()/2+yc,RED);
whlie(x<y)
{
x++;
if(p<0)
{
y=y;
p=p+2x+1;
}
else
{
y=y-1;
p=p+2(x-y)+1;
}
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc-y,YELLOW);
putpixel(getmaxx()/2+xc+y,getmaxy()/2-yc-x,GREEN);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc-y,BLUE);
putpixel(getmaxx()/2+xc-y,getmaxy()/2-yc-x,RED);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc+y,WHITE);
putpixel(getmaxx()/2+xc-y,getmaxy()/2-yc+x,YELLOW);
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc+y,GREEN);
putpixel(getmaxx()/2+xc+y,getmaxy()/2-yc+x,BLUE);
delay(500);
}
getch();
closegraph();
}

PREPARED BY: U. J. PATEL

Page 6

MID-POINT ELLIPSE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#define round(a)((int)(a+0.5))
void main()
{
clrscr();
int gm=DETECT,gd;
initgraph(&gm,&gd,"C:\\Turboc3\\BGI");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
float xc,rx,yc,ry,x,y,dx,dy,p;
printf("enter co-ordinate of center point xc,yc\n");
scanf("%f\n%f",&xc,&yc);
printf("enter value of rx,ry\n");
scanf("%f\n%f",&rx,&ry);
dx=0;
dy=2*rx*rx*ry;
x=0;
y=ry;
p=round((ry*ry)-(rx*rx*ry)+(0.25*rx*rx));
putpixel(getmaxx()/2+xc,getmaxy()/2-yc,RED);
while(dx<dy)
{
x++;
dx=2*ry*ry*x;
if (p<0)
{
y=y;
dy=2*rx*rx*y;
p=p+2*ry*ry*x+ry*ry;
}
else
{
y=y-1;
dy=2*rx*rx*y;
p=p+2*ry*ry*x-2*rx*rx*y+ry*ry;
}
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc-y,YELLOW);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc+y,YELLOW);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc-y,YELLOW);
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc+y,YELLOW);
delay(50);
}
p=round(ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx-ry*ry);
while (y>0)
{
y--;
if(p>0)
{
x=x;
PREPARED BY: U. J. PATEL

Page 7

MID-POINT ELLIPSE ALGORITHM


p=p-2*rx*rx*y+rx*rx;
}
else
{
x=x+1;
p=p-2*rx*rx*y+2*ry*ry*x+rx*rx;
}
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc-y,BLUE);
putpixel(getmaxx()/2+xc+x,getmaxy()/2-yc+y,BLUE);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc-y,BLUE);
putpixel(getmaxx()/2+xc-x,getmaxy()/2-yc+y,BLUE);
delay(50);
}
getch();
closegraph();
}

PREPARED BY: U. J. PATEL

Page 8

You might also like