You are on page 1of 26

1. Program to implement DDA Line Drawing Algorithm.

DDA stands for Digital Differential Analyzer. It is an incremental method of scan


conversion of line. In this method, calculation is performed at each step but by using results
of previous steps.
Algorithm:
Step1: Start Algorithm
Step2: Declare x1, y1, x2, y2, dx, dy, x, y as integer variables.
Step3: Enter value of x1, y1, x2, y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm

Program:
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <math.h>
int main(){
int gd=DETECT,gm;
float x,y,dx,dy,steps,i;
int x0, x1, y0,y1;
initgraph(&gd, &gm,(char*)"");
x0 = 100, y0 = 200, x1= 500, y1 = 300;
dx = (float)(x1-x0);
dy = (float)(y1-y0);
if(dx>=dy){
steps = dx;
}
else{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x= x0;

1
y= y0;
i = 1;
while(i<=steps){
putpixel(x,y,WHITE);
x+=dx;
y+=dy;
i=i+1;
}
getch();
closegraph();
}

Screenshot:

2
2. Program to implement Bresenham line algorithm.
This algorithm is used for scan converting a line. It was developed by Bresenham. It is an
efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated
quickly.
In this method, next pixel selected is that one who has the least distance from true line.
Algorithm:
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
Step12: End of Algorithm

Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void draw(int x0, int y0, int x1, int y1){

3
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;
}
}
int main(){
int gd=DETECT,gm;
initgraph(&gd, &gm,(char*)" ");
int error,x0,y0,x1,y1;
printf("Enter co-ordinates if first point: ");
scanf("%d %d", &x0,&y0);
printf("Enter co-ordinates if second point: ");
scanf("%d %d", &x1,&y1);
draw(x0,y0,x1,y1);
getch();
closegraph();
}

Screenshot:

4
3. Program to implement Mid-Point Circle algorithm.
It is based on the following function for testing the spatial relationship between the arbitrary
point (x, y) and a circle of radius r centered at the origin:
Algorithm:
Step1: Put x =0, y =r in equation 2
We have p=1-r
Step2: Repeat steps while x ≤ y
Plot (x, y)
If (p<0)
Then set p = p + 2x + 3
Else
p = p + 2(x-y)+5
y =y - 1 (end if)
x =x+1 (end loop)
Step3: End

Program:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
void mid_point_circle(int x0, int y0, int radius) {
int x = 0;
int y = radius;
int p = 1 - radius;
while (x <= y) {
putpixel(x + x0, y + y0, WHITE);
putpixel(y + x0, x + y0, WHITE);
putpixel(-x + x0, y + y0, WHITE);
putpixel(-y + x0, x + y0, WHITE);
putpixel(-x + x0, -y + y0, WHITE);
putpixel(-y + x0, -x + y0, WHITE);
putpixel(x + x0, -y + y0, WHITE);
putpixel(y + x0, -x + y0, WHITE);
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}
int main() {
int gd=DETECT,gm;

5
initgraph(&gd, &gm,(char*)"");
int x0 = 250, y0 = 250, radius = 100;
mid_point_circle(x0, y0, radius);
delay(5000);
closegraph();
return 0;
}

Screenshot:

6
4. Program to implement Mid-Point Ellipse Generating Algorithm.
This is an incremental method for scan converting an ellipse that is centered at the origin
in standard position i.e., with the major and minor axis parallel to coordinate system axis.
It is very similar to the midpoint circle algorithm. Because of the four-way symmetry
property we need to consider the entire elliptical curve in the first quadrant.
Algorithm:
Step 1: Start
Step 2: Declare rx , ry , x , y , m , dx , dy , P , P2.
Step 3: Initialize initial point of region1 as
x=0 , y = ry
Step 4: Calculate P= ry2 + rx2 / 4 - ry rx2
dx = 2 ry2 x
dy = 2 rx2 y
Step 5: Update values of dx and dy after each iteration.
Step 6: Repeat steps while (dx < dy):
Plot (x,y)
if(P < 0)
Update x = x+1 ;
P += ry2 [2x + 3 ]
Else
Update x = x + 1
y= y - 1
Step 7: When dx ≥ dy, plot region 2:
Step 8: Calculate P2 = ry2 ( x+1 / 2)2 + rx2 (y -1)2- rx2ry2
Step 9: Repeat till (y > 0)
If (P2 > 0)
Update y = y-1 (x will remain same)
P2 = P2 -2 y rx2 + rx2
else
x = x+1
y = y-1
P2= P2+ 2 ry2 [2x] -2 y rx2 + rx2
Step 10: End

Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void disp();
float x,y;
int xc,yc;
int main(){
int gd = DETECT,gm;
int rx,ry;
float p1,p2;
7
initgraph(&gd, &gm,(char*)"");
printf("Enter the center point:");
scanf("%d %d",&xc,&yc);
printf("Enter the value for Rx and Ry");
scanf("%d%d",&rx,&ry);
x=0;
y=ry;
disp();
p1=(ry*ry)-(rx*rx*rx)+(rx*rx)/4;
while((2.0*ry*ry*x)<(2.0*rx*rx*y)){
x++;
if(p1<=0){
p1=p1+(2.0*ry*ry*x)+(ry*ry);
}
else{
y--;
p1=p1+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(ry*ry);
}
disp();
x=-x;
disp();
x=-x;
}
x=rx;
y=0;
disp();
p2=(rx*rx)+2.0*(ry*ry*rx)+(ry*ry)/4;
while((2.0*ry*ry*x)>(2.0*rx*rx*y)){
y++;
if(p2>0){
p2=p2+(rx*rx)-(2.0*rx*rx*y);
}
else{
x--;
p2=p2+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(rx*rx);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp(){
putpixel(xc+x,yc+y,7);

8
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
}

Screenshot:

9
5. Program to implement 2D transformation-translation Algorithm.
A translation moves an object to a different position on the screen. You can translate a point
in 2D by adding translation coordinate (tx, ty) to the original coordinate X,Y to get the new
coordinate X′,Y′.

Program:
#include <stdio.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
rectangle(100, 100, 200, 200);
int tx = 200, ty = 0;
int points[] = { 100, 100, 200, 200 };
for (int i = 0; i < 4; i += 2) {
points[i] += tx;
points[i + 1] += ty;
}
setcolor(WHITE);
rectangle(points[0], points[1], points[2], points[3]);
getch();
closegraph();
return 0;
}

Screenshot:

10
6. Program to implement 2D Rotation Algorithm.
In rotation, we rotate the object at particular angle θ theta from its origin. From the
following figure, we can see that the point P X,Y is located at angle φ from the horizontal
X coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you
will get a new point P’ X′,Y′.

Program:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#define PI 3.14159265
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
rectangle(100, 100, 200, 200);
int angle = 45;
double radians = (double)angle * PI / 180.0;
int centerX = 150, centerY = 150;
int points[] = { 100, 100, 200, 100, 200, 200, 100, 200 };
for (int i = 0; i < 8; i += 2) {
int x = points[i], y = points[i + 1];
points[i] = centerX + (x - centerX) * cos(radians) - (y - centerY) * sin(radians);
points[i + 1] = centerY + (x - centerX) * sin(radians) + (y - centerY) *
cos(radians);
}
setcolor(WHITE);
line(points[0], points[1], points[2], points[3]);
line(points[2], points[3], points[4], points[5]);
line(points[4], points[5], points[6], points[7]);
line(points[6], points[7], points[0],
points[1]);
getch();
closegraph();
return 0;
}

Screenshot:

11
7. Program to implement 2D Scaling Algorithm.
To change the size of an object, scaling transformation is used. In the scaling process, you
either expand or compress the dimensions of the object. Scaling can be achieved by
multiplying the original coordinates of the object with the scaling factor to get the desired
result.
Let us assume that the original coordinates are X, Y, the scaling factors are (SX, SY), and
the produced coordinates are X′,Y′. This can be mathematically represented as shown
below −
X' = X . SX and Y' = Y . SY

Program:
#include <stdio.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,(char*)"");
rectangle(100, 100, 200, 200);
float sx = 1.5, sy = 2.0;
int centerX = 150, centerY = 150;
int points[] = { 100, 100, 200, 100, 200, 200, 100, 200 };
for (int i = 0; i < 8; i += 2) {
int x = points[i], y = points[i + 1];
points[i] = centerX + (x - centerX) * sx;
points[i + 1] = centerY + (y - centerY) * sy;
}
setcolor(RED);
line(points[0], points[1], points[2], points[3]);
line(points[2], points[3], points[4],
points[5]);
line(points[4], points[5], points[6],
points[7]);
line(points[6], points[7], points[0],
points[1]);
getch();
closegraph();
return 0;
}

Screenshot:

12
8. Program to implement 2D Reflection Algorithm.
Reflection is the mirror image of original object. In other words, we can say that it is a
rotation operation with 180°. In reflection transformation, the size of the object does not
change.

Program:
#include <stdio.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int points[] = { 100, 200, 150, 100, 200, 200 };
setcolor(YELLOW);
fillpoly(3, points);
int axis = 0;
for (int i = 0; i < 6; i += 2) {
if (axis == 0) {
points[i + 1] = 2 * 200 - points[i + 1];
} else {
points[i] = 2 * 150 - points[i];
}
}
setcolor(RED);
fillpoly(3, points);
getch();
closegraph();
return 0;
}

Screenshot:

13
9. Program to implement 2D Shear Algorithm.
A transformation that slants the shape of an object is called the shear transformation. There
are two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and
other shifts Y coordinate values. However; in both the cases only one coordinate changes
its coordinates and other preserves its values. Shearing is also termed as Skewing.

Program:
#include <stdio.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
rectangle(100, 100, 200, 200);
float shx = 0.5, shy = 0;
int points[] = { 100, 100, 200, 100, 200, 200, 100, 200 };
for (int i = 0; i < 8; i += 2) {
int x = points[i], y = points[i + 1];
points[i] = x + shx * y;
points[i + 1] = y + shy * x;
}
setcolor(RED);
line(points[0], points[1], points[2], points[3]);
line(points[2], points[3], points[4], points[5]);
line(points[4], points[5], points[6], points[7]);
line(points[6], points[7], points[0], points[1]);
shx = 0, shy = 0.5;
for (int i = 0; i < 8; i += 2) {
int x = points[i], y = points[i + 1];
points[i] = x + shx * y;
points[i + 1] = y + shy * x;
}
setcolor(GREEN);
line(points[0], points[1], points[2], points[3]);
line(points[2], points[3], points[4], points[5]);
line(points[4], points[5], points[6], points[7]);
line(points[6], points[7], points[0], points[1]);
getch();
closegraph();
return 0;
}

14
Screenshot:

15
10. Program to implement Cohen-Sutherland Line Clipping Algorithm.
In this algorithm, we are given 9 regions on the screen. Out of which one region is of the
window and the rest 8 regions are around it given by 4 digit binary. The division of the
regions are based on (x_max, y_max) and (x_min, y_min).
The central part is the viewing region or window, all the lines which lie within this region
are completely visible. A region code is always assigned to the endpoints of the given line.
Algorithm:
Step1: Calculate positions of both endpoints of the line
Step2: Perform OR operation on both of these end-points
Step3: If the OR operation gives 0000
Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.
Step4: If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)
(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window
(b) If bit 2 is "1" line intersect with right boundary
y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window
(c) If bit 3 is "1" line intersects with bottom boundary
X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window
(d) If bit 4 is "1" line intersects with the top boundary
X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window

Program:
#include <stdio.h>
#include <graphics.h>
#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

16
int xmin = 50, ymin = 50, xmax = 200, ymax = 200;
int calculateCode(int x, int y) {
int code = INSIDE;
if (x < xmin) {
code |= LEFT;
} else if (x > xmax) {
code |= RIGHT;
}
if (y < ymin) {
code |= BOTTOM;
} else if (y > ymax) {
code |= TOP;
}
return code;
}
void cohenSutherland(int x1, int y1, int x2, int y2) {
int code1 = calculateCode(x1, y1);
int code2 = calculateCode(x2, y2);
int accept = 0;
while (1) {
if (!(code1 | code2)) {
accept = 1;
break;
} else if (code1 & code2) {
break;
} else {
int x, y;
int code = code1 ? code1 : code2;
if (code & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (code & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (code & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else if (code & LEFT) {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (code == code1) {
x1 = x;
y1 = y;
code1 = calculateCode(x1, y1);
} else {

17
x2 = x;
y2 = y;
code2 = calculateCode(x2, y2);
}
}
}
if (accept) {
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
rectangle(xmin, ymin, xmax, ymax);
cohenSutherland(10, 80, 250, 300);
getch();
closegraph();
return 0;
}

Screenshot:

18
11. Program to implement Flood Fill Algorithm.
In this method, a point or seed which is inside region is selected. This point is called a seed
point. Then four connected approaches or eight connected approaches is used to fill with
specified color.
The flood fill algorithm has many characters similar to boundary fill. But this method is
more suitable for filling multiple colors boundary. When boundary is of many colors and
interior is to be filled with one color we use this algorithm.

Program:
#include <stdio.h>
#include <graphics.h>
void floodFill(int x, int y, int fillColor, int borderColor)
{
int currentColor = getpixel(x, y);
if (currentColor != borderColor && currentColor != fillColor)
{
putpixel(x, y, fillColor);
floodFill(x + 1, y, fillColor, borderColor);
floodFill(x - 1, y, fillColor, borderColor);
floodFill(x, y + 1, fillColor, borderColor);
floodFill(x, y - 1, fillColor, borderColor);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int poly[] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};
drawpoly(5, poly);
floodFill(150, 150, RED, WHITE);
getch();
closegraph();
return 0;
}

Screenshot:

19
12. Program to implement Boundary Fill Algorithm.
This algorithm uses the recursive method. First of all, a starting pixel called as the seed is
considered. The algorithm checks boundary pixel or adjacent pixels are colored or not. If
the adjacent pixel is already filled or colored then leave it, otherwise fill it. The filling is
done using four connected or eight connected approaches.

Program:
#include <stdio.h>
#include <graphics.h>
void boundaryFill(int x, int y, int fill_color, int boundary_color)
{
int current_color = getpixel(x, y);
if (current_color != boundary_color && current_color != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill(x + 1, y, fill_color, boundary_color);
boundaryFill(x - 1, y, fill_color, boundary_color);
boundaryFill(x, y + 1, fill_color, boundary_color);
boundaryFill(x, y - 1, fill_color, boundary_color);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int poly[] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};
drawpoly(5, poly);
boundaryFill(150, 150, RED, WHITE);
getch();
closegraph();
return 0;
}

Screenshot:

20
13. Program to implement 3D- Transformation-Translation.
It is the process of changing the relative location of a 3-D object with respect to the original
position by changing its coordinates.

Program:
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#include <graphics.h>
int x1,x2,z1,y2,mx,my,depth;
void draw();
void trans();
int main(){
int gd = DETECT, gm,c;
initgraph(&gd,&gm,(char*)"");
printf("Enter 1st top value(x1,y1): ");
scanf("%d %d",&x1,&z1);
printf("Enter right bottom value(x2,y2): ");
scanf("%d %d",&x2,&y2);
depth = (x2-x1)/4;
mx = (x1+x2)/2;
my = (z1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
}
void draw(){
bar3d(x1,z1,x2,y2,depth,7);
outtextxy(x1+50,z1-15,"Object");
}
void trans(){
int a1,a2,b1,b2,dep,x,y;
printf("Enter the translation Distances(tx,ty):");
scanf("%d%d",&x,&y);
a1 = x1+x;
a2 = x2+x;
b1 = z1+y;
b2 = y2+y;
dep =(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,7);
outtextxy(a1+50,b1-15,"Image");
setcolor(15);
draw();
21
getch();
}

Screenshot:

22
14. Program to implement 3D- Transformation-Rotation.
Rotation in 3D is more nuanced as compared to the rotation transformation in 2D, as in 3D
rotation we have to deal with 3-axes (x, y, z).

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x1,x2,z1,z2,mx,my,depth;
void draw();
void rotate();
int main(){
int gd = DETECT, gm, c;
initgraph(&gd,&gm,(char*)"");
printf("Enter 1st top value(x1,y1): ");
scanf("%d %d",&x1,&z1);
printf("Enter right bottom value(x2,y2): ");
scanf("%d %d",&x2,&z2);
depth = (x2-x1)/4;
mx = (x1+x2)/2;
my = (z1+z2)/2;
draw();
getch();
cleardevice();
rotate();
getch();
}
void draw(){
bar3d(x1,z1,x2,z2,depth,7);
outtextxy(x1+50,z1-15,"Object");
}
void rotate(){
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate: ");
scanf("%f",&t);
t=t*(3.14/180);
a1 = mx+(x1-mx)*cos(t)-(z1-my)*sin(t);
a2 = mx+(x2-mx)*cos(t)-(z2-my)*sin(t);
b1 = mx+(x1-mx)*sin(t)-(z1-my)*cos(t);
b2 = mx+(x2-mx)*sin(t)-(z2-my)*cos(t);
if(a2>a1){
dep = (a2-a1)/4;
}
else{
23
dep = (a1-a2)/4;
}
bar3d(a1,b1,a2,b2,dep,7);
outtextxy(a1+50,b1-15,"Image");
setcolor(15);
draw();
getch();

Screenshot:

24
15. Program to implement 3D- Transformation-Scaling.
It is performed to resize the 3D-object that is the dimension of the object can be scaled(alter)
in any of the x, y, z direction through Sx, Sy, Sz scaling factors.

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include <process.h>
int x1,x2,z1,z2,mx,my,depth;
void draw();
void scale();
int main(){
int gd = DETECT, gm, c;
initgraph(&gd,&gm,(char*)"");
printf("Enter 1st top value(x1,y1): ");
scanf("%d %d",&x1,&z1);
printf("Enter right bottom value(x2,y2): ");
scanf("%d %d",&x2,&z2);
depth = (x2-x1)/4;
mx = (x1+x2)/2;
my = (z1+z2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw(){
bar3d(x1,z1,x2,z2,depth,7);
outtextxy(x1+50,z1-15,"Object");
}
void scale(){
int x,y,a1,b1,a2,b2,dep;
printf("Enter scaling Factors: ");
scanf("%d %d",&x,&y);
a1 = mx+(x1-mx)*x;
a2 = mx+(x2-mx)*x;
b1 = mx+(z1-mx)*y;
b2 = mx+(z2-mx)*y;
dep = (a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,7);
outtextxy(a1+50,b1-15,"Image");
setcolor(15);
draw();
25
getch();
}

Screenshot:

26

You might also like