CG Practicals 009

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

COMPUTER GRAPHICS PRACTICALS

NAME: Shivam Kumar ROLL NO: 00924802019

CONTENT:

S. No. NAME Date:


1 DDA Line drawing Algorithm 06.09.2021
Bresenham’s Line Drawing
2 13.09.2021
Algorithm
Bresenham’s Circle drawing
3 20.09.2021
Algorithm
4 Mid Point Circle Algorithm 11.09.2021
5 Translation in 2D 11.09.2021
1. Write a program using DDA.

#include<stdio.h>
#include<conio.h>
#include<graphics.h> void
main()
{
float x1,y1,x2,y2; float
m,dx,dy,x,y;
int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:\TURBOC3\BGI");

printf("Using Digital Differential Algorithm\n"); printf("Enter the


initial coordinates of x,y:\t"); scanf("%f %f",&x1,&y1);
printf("Enter the final coordinates of x,y:\t"); scanf("%f
%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
printf("dx=%f\n",dx,"dy=%f\n",dy); m=dy/dx;
printf("m=%f\n",m); if(m<=1){
for(x=x1;x<=x2;x++)
{
putpixel((x),(y),WHITE); y=y+m;
}
}
else{
for(y=y1;y<=y2;y++)
{
putpixel((x),(y),WHITE); x=x+(1/m);
}
}
getch();
closegraph();
}
OUTPUT:
2. Write a program using BDLA.

#include<stdio.h>
#include<conio.h>
#include<graphics.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),WHITE); y=y+1;
p=p+2*dy-2*dx;

}
else
{ putpixel((x),(y),RED); p=p+2*dy;

}
x=x+1;
}
}

void main()
{
int gdriver=DETECT, gmode,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();
closegraph();
}
OUTPUT:
3. Write a program using BDCA.

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

void points(int xc,int yc,int x,int y)


{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW); putpixel(-
x+xc,-y+yc,GREEN); putpixel(-
x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14); putpixel(-
y+xc,-x+yc,15); putpixel(-y+xc,x+yc,6);
}
void BresenhamCircle(int xc,int yc,int r)
{
int x=0,y=r,d=3-(2*r);
points(xc,yc,x,y); while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10; y=y-
1;
}
x=x+1; points(xc,yc,x,y);
}
}

int main(void)
{
int xc,yc,r,gdriver = DETECT, gmode; initgraph(&gdriver,
&gmode, "C:\\TURBOC3\\BGI");
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :"); scanf("%d",&r);
BresenhamCircle(xc,yc,r);
getch(); closegraph();
return 0;

OUTPUT:
4. Write a program using MPCA.

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

void main()
{
int gdriver=DETECT, gmode, error, x, y, r, x1, y1,p; initgraph(&gdriver,
&gmode, "c:\\turboc3\\bgi"); printf("Enter radius of circle: \n");
scanf("%d",&r);
printf("Enter co-ordinates of center:\n");
scanf("%d%d",&x,&y);
x1 = 0;
y1 = r; p =
1-r;
while (y1 >= x1){
putpixel(x + x1, y + y1, 9);
putpixel(x + y1, y + x1, 9);
putpixel(x - y1, y + x1, 5);
putpixel(x - x1, y + y1, 5);
putpixel(x - x1, y - y1, 7);
putpixel(x - y1, y - x1, 7);
putpixel(x + y1, y - x1, GREEN);
putpixel(x + x1, y - y1, GREEN);
if (p < 0){
x1 += 1;
p += 2*x1 + 1;
}
else{
y1 -= 1;
x1 += 1;
p = p + 2*(x1 - y1) + 1;
}
}
getch();
}
OUTPUT:
5. Write a program using 2D translation.

#include<stdio.h>
#include<graphics.h>
#include<conio.h> int
gd=DETECT,gm;
int n,xs[100],ys[100],i,ty,tx; void draw();
void translate(); void
main()
{
printf("Enter co-ordinates(x,y):");
for(i=0;i<4;i++) scanf("%d%d",&xs[i],&ys[i]);
printf("Enter units for Translation in X & Y direction respectively:");
scanf("%d%d",&tx,&ty);
initgraph (&gd,&gm,"(:\\TURBOC3?\\BGI\\");
setcolor(WHITE);
draw(); translate ();
setcolor(RED);
draw();
getch();
}
void draw()
{
for(i=0;i<4;i++) line(xs[i],ys[i],xs[(i+1)%4],ys[(i+1)%4]);
}
void translate()
{
for(i=0;i<4;i++)
{
xs[i]+=tx;
ys[i]+=ty;
}
}
OUTPUT:

You might also like