Computer Graphics and Animation
Practical #3
Name Roll Number
Subject/Course: Computer Graphics & Animation / BSc IT
Practical Basic Shapes & Line Drawing Algorithm (DDA)
1. Consider the 2 end points of a line as (50,50), (100,100). Draw the line using DDA Line drawing algorithm.
Code DDA:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int sign(int n){
if(n < 0){return -1;}
else if(n == 0){return 0;}
else{return 1;}
}
void DDA(int x1, int y1, int x2, int y2){
int x, y, dx, dy, L;
float ax, ay, Dx, Dy;
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) > abs(dy)){L = dx;}
else{L = dx;}
Dx = dx/L;
Dy = dy/L;
ax = x1 + 0.5 * sign(Dx);
ay = y1 + 0.5 * sign(Dy);
int i = 1;
while(i <= L){
x = (int) ax;
y = (int) ay;
putpixel(x,y,WHITE);
ax += Dx;
ay += Dy;
i++;
}
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
DDA(50,50,100,100);
getch();
closegraph();
}
Vidyalankar School of Information Technology
Output:
2. Draw the basic shapes in the center of the screen. i. Circle ii. Rectangle iii. Square iv. Concentric Circles v.
Ellipse vi. Line.
Code:
1)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
circle(x,y,50);
getch();
closegraph();
}
2)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
rectangle(x + 75,y + 50,x - 75,y - 50);
Vidyalankar School of Information Technology
getch();
closegraph();
}
3)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
rectangle(x + 50,y + 50,x - 50,y - 50);
getch();
closegraph();
}
4)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
circle(x,y,25);
circle(x,y,50);
circle(x,y,75);
circle(x,y,100);
getch();
closegraph();
}
5)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
ellipse(x, y, 0, 360, 75, 25);
getch();
closegraph();
Vidyalankar School of Information Technology
}
6)
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://TC//bgi");
int x = getmaxx()/2;
int y = getmaxy()/2;
line(x - 25, y - 75, x + 25, y + 75);
getch();
closegraph();
}
Output:
1)
2)
Vidyalankar School of Information Technology
3)
4)
Vidyalankar School of Information Technology
5)
6)
Vidyalankar School of Information Technology
Vidyalankar School of Information Technology