CGM File - Sharonkaur - It2 - 104

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

2nd Year

Computer
graphics and
multimedia
practical
file
Sharon
Sahil Kaur Katyal
Vashisht

Branch IT – 32
Roll no - 10413203119
04676803120
INDEX

S.NO. Experiment Name Date

1. Introduction to computer graphics.

2. Implement diagonal, horizontal, perpendicular, line


using pixel illumination.

3. Design a program to rasterize a line using DDA


algorithm.

4. Design a program to implement Bresenham’s line


algorithm.

5. Design a program to rasterize a circle using


Bresenham's circle algorithm.

6. Design a program to draw a circle using mid-point


circle algorithm.

7. Draw a smiley using in built functions.

8. Draw a rainbow using in built functions.

9. Draw a set of concentric circles using in built


functions,

10. Design a program to show translation of an object


using matrix multiplication.

11. Design a program to rotate a line as a 2D


transformation using matrix
multiplication.
12. Design a program to do scaling of a rectangle as a 2D
transformation.

13. Design a program to perform shearing of an object.

14. Design a program to perform reflection of a triangle.

15. Design a program to implement Bezier curve

16. Design a program to implement a program using


Cohen Sutherland's algorithm for line clipping.

17. Design a program to perform reflection of a


rectangle about either of x=0, y=0 or x=y axis.
Experiment-1
Aim. Introduction to computer graphics.

Graphics and Multimedia-now a day probably the most talked


about technology in the field of computer. This technology is
nowadays largely adopted by most computer-based applications
to bridge the gap between a human user & the computer. By
this, multiple media are implemented and used in computer-
based application to enhance their understanding ability before
a common man. These multiple media include, text, sound, video,
graphics animation etc.
Experiment-2
Aim. Implement diagonal, horizontal, perpendicular, line
using pixel illumination.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void main(){

int gd = DETECT, gm; //gd=graphic drivers


//gm=graphics mode: used to provide mode with the highest resolution clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); //initilize the graphic
putpixel(20, 100, YELLOW);
putpixel(40, 300, BLUE);
putpixel(60, 400, RED);
printf("Maximum value of X-Axis %d \n", getmaxx());
printf("Maximum value of Y-Axis %d \n", getmaxy());

setcolor(WHITE);
line(100, 100, 400, 400); //line(x1,y1,x2,y2)
setcolor(RED);
line(0, 0, getmaxx(), getmaxy());
setcolor(BLUE);
line(getmaxx(), 0, 0, getmaxy());
setcolor(YELLOW);
line(getmaxx() / 2, 0, getmaxx() / 2, getmaxy());

getch();
closegraph();
}
Output:
Experiment-3
Aim. Design a program to rasterize a line using DDA
algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>

void main()
{
int i;
float x, y, x1, y1, x2, y2, dx, dy, step; int gd =
DETECT, gm;

clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
//for m>=1 x1
= 100;
y1 = 100;
x2 = 200;
y2 = 400;
/*//for m<1
x1=100; y1=100;
x2=400; y2=200;
*/
dx = abs(x2 - x1); dy
= abs(y2 - y1);

if (dx >= dy){


step = dx;
}
else{
step = dy;
}
dx = dx / step; dy
= dy / step; x = x1;
y = y1; i
= 1;
while (i <= step)
{
putpixel(x, y, BLUE); x =
x + dx;
y = y + dy; i =
i + 1;
}
getch();
closegraph();
}
Output:
For
m>=1:

For m<1:
Experiment-4
Aim. Design a program to implement Bresenham’s line
algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void main()
{
int dx, dy, x, y, x1, y1, x2, y2; long int
dv;
int gd = DETECT, gm;
char str[10]; clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

x1 = 100; y1 = 100;
x2 = 500; y2 = 400;
dx = (x2 - x1);
dy = (y2 - y1);
dv = (long)(2 * (dy) - (dx)); x = x1;
y = y1;

itoa(x1, x, 10);
itoa(y1, y, 10);

sprintf(str, "(%d,%d)", x, y);

outtextxy(0, 0, "x-min=0 y-min=0");


outtextxy(getmaxx() - 40, 0, "x-max");
outtextxy(0, getmaxy() - 10, "y-max");
outtextxy(x, y, str);

while (x <= x2)


{
putpixel(x, y, YELLOW); if
(dv < 0)
{
x = x + 1; y
= y;
dv = dv + (2 * (dy));
}
else
{
x = x + 1; y
= y + 1;
dv = dv + (2 * (dy - dx));
}
}
itoa(x2, x, 10);
itoa(y2, y, 10);
sprintf(str, "(%d,%d)", x-1, y-1); outtextxy(x, y, str);

getch();
closegraph();
}

Output:
Experiment-5
Aim. Design a program to rasterize a circle using
Bresenham's circle algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void PLOT_CIRCLE(int xc, int yc, int x, int y)
{
putpixel(xc + x, yc + y, BLUE); //2 quad
putpixel(xc - x, yc + y, WHITE); //3 quad
putpixel(xc + x, yc - y, YELLOW) //1st quad
;
putpixel(xc - x, yc - y, RED); //4 quad
putpixel(xc + y, yc + x, BLUE); // second quad
putpixel(xc - y, yc + x, WHITE); // 3 quad
putpixel(xc + y, yc - x, YELLOW) //1st quad
;
putpixel(xc - y, yc - x, RED); //4 quad
}
void B_CIRCLE_A(int xc, int yc, int r)
{
int x, y;
int dv;
dv = 3 - 2 * r; x =
0;
y = r;
while (x <= y)
{
PLOT_CIRCLE(xc, yc, x, y); if
(dv >= 0)
{
x = x + 1; y
= y - 1;
dv = dv + 4 * (x - y) + 10;
}
else
{
x = x + 1; y
= y;
dv = dv + 4 * x + 6;
}
}
}

void main()
{
int gd = DETECT, gm, x, y, r, x3, y3, x1, y1; char
str[10];
clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
//printf("ENTER center x,y : ");
//scanf("%d%d",&x,&y);
//printf("ENTER radius ");
//scanf("%d",&r); x3
= getmaxx() / 2; x1 =
x3;
y3 = getmaxy() / 2; y1
= y3;
r = 100;
outtextxy(getmaxx() / 2, getmaxy() / 2 - 15, "radius = 100");

putpixel(x, y, WHITE); x
= x1;
y = y1;
itoa(x1, x, 10);
itoa(y1, y, 10);

sprintf(str, "(%d,%d)", x, y);


outtextxy(x - 50, y + 10, str);

line(getmaxx() / 2, getmaxy() / 2, getmaxx() / 2 + r, getmaxy() / 2);

B_CIRCLE_A(x3, y3, r);

getch();
closegraph();
}

Output:
Experiment-6
Aim. Design a program to draw a circle using mid-
point circle algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

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


{
putpixel(xc + x, yc + y, BLUE);
putpixel(xc - x, yc + y, BLUE);
putpixel(xc + x, yc - y, BLUE);
putpixel(xc - x, yc - y, BLUE);
putpixel(xc + y, yc + x, BLUE);
putpixel(xc - y, yc + x, BLUE);
putpixel(xc + y, yc - x, BLUE);
putpixel(xc - y, yc - x, BLUE);
}

void MID_POINT_CRICLE(int xc, int yc, int r)


{
int x, y; int p;
x = 0;
y = r;
p = 1 - r; while
(x <= y)
{
PLOT_CIRLCE(xc, yc, x, y);

if (p >= 0)
{
x = x + 1; y
= y - 1;
p = p + 2 * (x - y) + 1;
}
else
{

x = x + 1;
p = p + 2 * (x) + 1;
}
}
}

void main()
{
int gd = DETECT, gm, x, y, r; char
c[25], rd[10]; clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
x = getmaxx() / 2; y =
getmaxy() / 2; r =
100;
MID_POINT_CRICLE(x, y, r);
line(x, y, x + r, y); sprintf(c,
"(%d,%d)", x, y); sprintf(rd,
"Radius=%d", r);

outtextxy(x - 20, y + 10, c);


outtextxy(x + 10, y - 10, rd);

putpixel(x, y, WHITE);
getch();
closegraph();
}

Output:
Experiment-7
Aim. Draw a smiley using in built functions.

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

void main(){
int gr = DETECT, gm;

initgraph(&gr, &gm, "C:\\TURBOC3\\BGI");

setcolor(YELLOW);

circle(300, 100, 40);


setfillstyle(SOLID_FILL, YELLOW);
floodfill(300, 100, YELLOW);

setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);

fillellipse(310, 85, 2, 6);


fillellipse(290, 85, 2, 6);

ellipse(300, 100, 205, 335, 20, 9);


ellipse(300, 100, 205, 335, 20, 10);
ellipse(300, 100, 205, 335, 20, 11);

getch();
closegraph();
}
Output:
Experiment-8
Aim. Draw a rainbow using in built functions.

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

void main()
{
int gd = DETECT, gm;
int x, y, i;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); x

= getmaxx() / 2;
y = getmaxy() / 2;
for (i = 30; i < 200; i++)
{

delay(50);

setcolor(i / 10);
arc(x, y, 0, 180, i - 10);
}
getch();
}
Output:
Experiment-9
Aim. Draw a set of concentric circles using inbuilt functions.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void main()
{
int gd = DETECT, gm;

clrscr();

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

setcolor(RED);
circle(getmaxx() / 2, getmaxy() / 2, 100); //cirxle(x1,y1,radius);

setcolor(RED);
circle(getmaxx() / 2, getmaxy() / 2, 25);

setcolor(GREEN);
circle(getmaxx() / 2, getmaxy() / 2, 50);

setcolor(YELLOW);
circle(getmaxx() / 2, getmaxy() / 2, 75);

setcolor(BLUE);
circle(getmaxx() / 2, getmaxy() / 2, 100);

setcolor(CYAN);
circle(getmaxx() / 2, getmaxy() / 2, 125);

getch();
closegraph();
}
Output:
Experiment-10
Aim. Design a program to show translation of an object
using matrix multiplication.
#include <stdio.h>
#include <graphics.h>
#include <conio.h>

void DISPLAY_MATRIX(int m[3][4], int r, int c)


{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", m[i][j]);
}
printf("\n");
}
}

void main()
{
int x1, x2, y1, y2, tx, ty, gd = DETECT, gm, i, j, k; int pt[3][4],
tr[3][3], res[3][4];
int x1n, x2n, y1n, y2n;
clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

// printf("Enter min coordinates for reactangle: ");


// scanf("%d%d",&x1,&y1);
// printf("Enter max coordinates for reactangle: ");
// scanf("%d%d",&x2,&y2);
// printf("Enter translating factor: ");
// scanf("%d%d",&tx,&ty);

x1 = 200; y1 = 200;
x2 = 500; y2 = 300;
tx = 50; ty = 20;

pt[0][0] = x1; pt[0][1] = x2; pt[0][2] = x2; pt[0][3] = x1;


pt[1][0] = y1; pt[1][1] = y1; pt[1][2] = y2; pt[1][3] = y2;
pt[2][0] = 1; pt[2][1] = 1; pt[2][2] = 1; pt[2][3] = 1;

tr[0][0] = 1; tr[0][1] = 0; tr[0][2] = tx;


tr[1][0] = 0; tr[1][1] = 1; tr[1][2] = ty;
tr[2][0] = 0; tr[2][1] = 0; tr[2][2] = 1;

printf("DISPLAYING ORIGINAL POINT MATRIX


\n"); DISPLAY_MATRIX(pt, 3, 4);
printf("\nDISPLAYING TRANSLATION MATRIX \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", tr[i][j]);
}
printf("\n");
}

for (i = 0; i < 3; i++)


{
for (j = 0; j < 4; j++)
{

res[i][j] = 0;
for (k = 0; k < 3; k++)
{
res[i][j] = res[i][j] + (tr[i][k] * pt[k][j]);
}
}
}

printf("\nDISPLAYING RESULT
MATRIX\n"); DISPLAY_MATRIX(res, 3, 4);

outtextxy(x1, y1 - 10, "original reactangle");


setcolor(RED);
rectangle(x1, y1, x2, y2);

x1n = res[0][0]; y1n = res[1][0];


x2n = res[0][2]; y2n = res[1][2];

setcolor(WHITE);
outtextxy(x1n, y1n - 10, "translated reactangle");
setcolor(BLUE);
rectangle(x1n, y1n, x2n, y2n);
getch();
closegraph();
}
Output:
Experiment-11
Aim. Design a program to rotate a line as a 2D
transformation using matrix multiplication.

#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <math.h> void
main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, xend, yend, xshift, yshift; double
angle, q;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

printf("\t\tROTATION OF A LINE (KEEPING ONE END POINT FIXED)\n");


//printf("Enter cordinates of a line: ");
//scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1 = 100; y1 = 100;
x2 = 100; y2 = 200;

printf("ORIGINAL CORDINATES\n");
printf("(%d, %d) (%d, %d)", x1, y1, x2, y2);
setcolor(RED);
line(x1, y1, x2, y2);
//printf("Enter the angle of rotation(anticlockwise) in degrees\n");
//scanf("%d",&angle);
angle = 90;
q = (angle * 3.14) / 180;

xshift = x2 - x1;
yshift = y2 - y1;
//xend=floor(x2*cos(q) + y2*sin(q));
//yend=floor(x2*-sin(q) + y2*cos(q));

xend = floor(x1 + (xshift * cos(q) + yshift * sin(q)));


yend = floor(y1 + (xshift * -1 * sin(q) + yshift * cos(q)));

printf("\nROTATED END CORDINATES\n");


printf("(%d, %d) (%d, %d)", x1, y1, xend, yend);

setcolor(BLUE);
line(x1, y1, xend, yend);
getch();
closegraph();
}
Output:
Experiment-12
Aim. Design a program to do scaling of a rectangle
as a 2D transformation.

#include <conio.h>
#include <stdio.h>
#include <graphics.h>
void main()
{
int x1, x2, y1, y2, sx, sy, gd = DETECT, gm, i, j, k; int pt[3][4],
tr[3][3], res[3][4];
int x1n, x2n, y1n, y2n;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("\t\tSCALING OF A RECTANGLE\n");
//printf("Enter min coordinate of rectangle "); scanf("%d%d",&x1,&y1);
//printf("Enter max coordinate of rectangle "); scanf("%d%d",&x2,&y2);
//printf("Enter scaling factor"); scanf("%d%d",&sx,&sy); x1 =
200; y1 = 150;
x2 = 300; y2 = 200;
sx = 2; sy = 2;

pt[0][0] = x1; pt[0][1] = x2; pt[0][2] = x2; pt[0][3] = x1;


pt[1][0] = y1; pt[1][1] = y1; pt[1][2] = y2; pt[1][3] = y2;
pt[2][0] = 1; pt[2][1] = 1; pt[2][2] = 1; pt[2][3] = 1;

tr[0][0] = sx; tr[0][1] = 0; tr[0][2] = 0;


tr[1][0] = 0; tr[1][1] = sy; tr[1][2] = 0;
tr[2][0] = 0; tr[2][1] = 0; tr[2][2] = 0;

//IMPLEMENTING TRANSFORMATION USING MATRIX MULTIPLICATION

for (i = 0; i < 3; i++)


{
for (j = 0; j < 4; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
res[i][j] += tr[i][k] * pt[k][j];
}
}

printf("DISPLAYING ORIGINAL POINT MATRIX \n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++) printf("%d
", pt[i][j]);
printf("\n");
}
printf("DISPLAYING SCALING MATRIX \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++) printf("%d
", tr[i][j]);
printf("\n");
}

outtextxy(x1, y1 - 10, "ORIGINAL RECTANGLE");


setcolor(RED); rectangle(x1,
y1, x2, y2);

x1n = res[0][0]; x2n = res[0][2];


y1n = res[1][0]; y2n = res[1][2];

setcolor(WHITE);
outtextxy(x1n, y1n - 10, "SCALED RECTANGLE");
setcolor(BLUE);
rectangle(x1n, y1n, x2n, y2n);
getch();
closegraph();
}

Output:
Experiment-13
Aim. Design a program to perform shearing of an object.

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

void main()
{
int x1, x2, y1, y2, shx, gd = DETECT, gm, i, j, k; int
pt[3][4], tr[3][3], res[3][4];
int x1n, x2n, y1n, y2n, x3n, y3n, x4n, y4n;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("\t\tSHEARING OF A RECTANGLE\n");
//printf("\nEnter min coordinates for rectangle ");
//scanf("%d%d",&x1,&y1);
//printf("\nEnter max coordinates or rectangle");
//scanf("%d %d",&x2,&y2);

x1 = 100; y1 = 250;
x2 = 150; y2 = 300;
shx = 1;

pt[0][0] = x1; pt[0][1] = x2; pt[0][2] = x2; pt[0][3] = x1;


pt[1][0] = y1; pt[1][1] = y1; pt[1][2] = y2; pt[1][3] = y2;
pt[2][0] = 1; pt[2][1] = 1; pt[2][2] = 1; pt[2][3] = 1;

tr[0][0] = 1; tr[0][1] = shx; tr[0][2] = 0;


tr[1][0] = 0; tr[1][1] = 1; tr[1][2] = 0;
tr[2][0] = 0; tr[2][1] = 0; tr[2][2] = 1;

//IMPLEMENTING TRANSFORMATION USING MATRIX MULTIPLICATION

for (i = 0; i < 3; i++)


{
for (j = 0; j < 4; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
{
res[i][j] = res[i][j] + (tr[i][k] * pt[k][j]);
}
}
}
printf("DISPLAYING ORIGINAL POINT MATRIX\n");
for (i = 0; i < 3; i++)
{

for (j = 0; j < 4; j++)


{
printf("%d ", pt[i][j]);
}
printf("\n");
}
printf("DISPLAYING SHEARING MATRIX\n");
for (i = 0; i < 3; i++)
{

for (j = 0; j < 3; j++)


{
printf("%d ", tr[i][j]);
}
printf("\n");
}

printf("DISPLAYING RESULT MATRIX\n");


for (i = 0; i < 3; i++)
{

for (j = 0; j < 4; j++)


{
printf("%d ", res[i][j]);
}
printf("\n");
}

setcolor(WHITE);
outtextxy(x1, y1 - 20, "ORIGINAL RECTANGLE");
setcolor(RED); rectangle(x1,
y1, x2, y2);
x1n = res[0][0]; y1n = res[1][0];
x2n = res[0][1]; y2n = res[1][1];
x3n = res[0][2]; y3n = res[1][2];
x4n = res[0][3]; y4n = res[1][3];

setcolor(WHITE);
outtextxy(x1n, y1n - 20, "SHEARED RECTANGLE");
setcolor(BLUE);
line(x1n, y1n, x2n, y2n);
line(x1n, y1n, x4n, y4n);
line(x2n, y2n, x3n, y3n);
line(x3n, y3n, x4n, y4n);
getch();
closegraph();
}

Output:
Experiment-14
Aim. Design a program to perform shearing of an object.

#include <stdio.h>
#include <graphics.h>
#include <conio.h> void
main()
{
int x1, x2, x3, y1, y2, y3, rx, ry, gd = DETECT, gm, i, j, k; int pt[3][4],
tr[3][3], res[3][4];
int x1n, x2n, x3n, y1n, y2n, y3n; initgraph(&gd,
&gm, "C:\\TURBOC3\\BGI");
printf("\t\tREFLECTION OF A TRIANGLE\n");
outtextxy(getmaxx() - 120, getmaxy() / 2 - 10, "MIRROR (X-AXIS)");
setcolor(YELLOW);
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

x1 = 250;
x2 = 200;
x3 = 300;
rx = 1;

printf("\nORIGINAL COORDINATES OF TRIANGLE");


printf("\n(%d, %d) (%d,%d) (%d,%d)", x1, y1, x2, y2, x3, y3);
setcolor(WHITE);
ry = -1;
y1 = 200;
y2 = 150;
y3 = 120;

outtextxy(x1, y1 - 10, "ORIGINAL TRIANGLE");


setcolor(RED); line(x1,
y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

pt[0][0] = x1; pt[0][1] = x2; pt[0][2] = x3;


pt[1][0] = y1; pt[1][1] = y2; pt[1][2] = y3;
pt[2][0] = 1; pt[2][1] = 1; pt[2][2] = 1;
tr[0][0] = rx; tr[0][1] = 0; tr[0][2] = 0;
tr[1][0] = 0; tr[1][1] = ry; tr[1][2] = 0;
tr[2][0] = 0; tr[2][1] = 0; tr[2][2] = 1;
//IMPLEMENTING TRANSFORMATION USING MATRIX MULTIPLICATION
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
{
res[i][j] = res[i][j] + (tr[i][k] * pt[k][j]);
}
}
}

printf("DISPLAYING ORIGINAL POINT MATRIX \n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", pt[i][j]);
}
printf("\n");
}

printf("\nDISPLAYING REFLECTION MATRIX \n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", tr[i][j]);
}
printf("\n");
}

printf("\n DISPLAYING RESULT MATRIX \n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", res[i][j]);
}
printf("\n");
}

x1n = res[0][0]; y1n = res[1][0] + getmaxy();


x2n = res[0][1]; y2n = res[1][1] + getmaxy();
x3n = res[0][2]; y3n = res[1][2] + getmaxy();

setcolor(WHITE);
outtextxy(x1n, y1n, "REFLECTED TRIANGLE");
setcolor(BLUE);
line(x1n, y1n, x2n, y2n);
line(x2n, y2n, x3n, y3n);
line(x3n, y3n, x1n, y1n);
getch();
closegraph();
}

Output:
Experiment-15
Aim. Design a program to implement Bezier curve.

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

void main()
{
int gd = DETECT, gm, x[4], y[4], i; int xu,
yu;
// float xu, yu;
float u;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// printf("\n Enter 4 Control Points");
// for(i=0;i<4;i++)
// scanf("%d%d", &x[i],&y[i]); x[0]
= 50; y[0] = 50;
x[1] = 200; y[1] = 150;
x[2] = 350; y[2] = 300;
x[3] = 300; y[3] = 150;
printf("\n Curve is:");
for (u = 0.0; u < 1.0; u += 0.0005)
{
xu = x[0] * pow(1 - u, 3) + 3 * u * pow(1 - u, 2) * x[1] + 3 * pow(u, 2) * (1 - u) * x[2]
+ pow(u, 3) * x[3];
yu = y[0] * pow(1 - u, 3) + 3 * u * pow(1 - u, 2) * y[1] + 3 * pow(u, 2) * (1 - u) * y[2]
+ pow(u, 3) * y[3];
putpixel((int)xu, (int)yu, WHITE);
}
getch();
closegraph();
}
Output:
Experiment-16
Aim. Design a program to implement a program using
Cohen Sutherland ’s algorithm for line clipping.

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

int a1, a2, a3, a4, b1, b2, b3, b4; int

sign(int a)
{
if (a <= 0)
{
return 0;
}
else
{
return 1;
}
}

void region_code(int x1, int x2, int y1, int y2, int xmax, int xmin, int ymax, int ymin)
{
a1 = sign(y1 - ymax); a2 = sign(ymin - y1); a3 = sign(x1 - xmax); a4 = sign(xmin - x1);
b1 = sign(y2 - ymax); b2 = sign(ymin - y2); b3 = sign(x2 - xmax); b4 = sign(xmin -
x2);
}

void deciding()
{
//Line is visible,if both region codes are zero
if (a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0 && b1 == 0 && b2 == 0 && b3
== 0 && b4 == 0)
{
printf("\n Line is completely visible");
}
//Line is clipping candidATE, if any region code is non zero also
//bitwise 'AND' operation is zero. else
{
if (((a1 & b1) || (a2 & b2) || (a3 & b3) || (a4 & b4)) == 0) printf("\nLine
is eligible for clipping");
else
{
printf("\n Line is not visible");
}
}
}
void clip(int x1, int y1, int x2, int y2, int xmax, int xmin, int ymax, int ymin)
{
setcolor(RED);
rectangle(xmin, ymin, xmax, ymax);
setcolor(YELLOW);
line(x1, y1, x2, y2);
region_code(x1, x2, y1, y2, xmax, xmin, ymax, ymin);
deciding();
}

void main()
{
int x1, y1, x2, y2, xmax, xmin, ymax, ymin, gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

xmin = 100; ymin = 100;


xmax = 300; ymax = 300;

x1 = 110; y1 = 70;
x2 = 150; y2 = 200;

clip(x1, y1, x2, y2, xmax, xmin, ymax, ymin); getch();


closegraph();
}

Output:
Experiment-17
Aim. Design a program to perform reflection of a
rectangle.
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
void main()
{
int x1, x2, x3, y1, y2, y3, x4, y4, rx, ry, gd = DETECT, gm, i, j, k;
int pt[3][4], tr[3][3], res[3][4];
int x1n, x2n, x3n, x4n, y1n, y2n, y3n, y4n;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("\t\tREFLECTION OF A RECTANGLE\n");
outtextxy(getmaxx() - 120, getmaxy() / 2 - 10, "MIRROR (X-AXIS)");
setcolor(YELLOW);
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
x1 = 400;
x2 = 400;
x3 = 300;
x4 = 300;
rx = 1;

printf("\nORIGINAL COORDINATES OF RECTANGLE");


printf("\n(%d, %d) (%d,%d) (%d,%d) (%d,%d)", x1, y1, x2, y2, x3, y3, x4, y4);
setcolor(WHITE);
ry = -1;
y1 = 400;
y2 = 300;
y3 = 300;
y4 = 400;

outtextxy(x1, y1- 10, "ORIGINAL RECTANGLE");


setcolor(RED);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x4, y4);
line(x4, y4, x1, y1);
pt[0][0] = x1; pt[0][1] = x2; pt[0][2] = x3; pt[0][3] = x4;
pt[1][0] = y1; pt[1][1] = y2; pt[1][2] = y3; pt[1][3] = y4;
pt[2][0] = 1; pt[2][1] = 1; pt[2][2] = 1; pt[2][3] = 1;
tr[0][0] = rx; tr[0][1] = 0; tr[0][2] = 0;
tr[1][0] = 0; tr[1][1] = ry; tr[1][2] = 0;
tr[2][0] = 0; tr[2][1] = 0; tr[2][2] = 1;

//IMPLEMENTING TRANSFORMATION USING MATRIX MULTIPLICATION


for (i = 0; i < 3; i++){
for (j = 0; j < 4; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
{
res[i][j] = res[i][j] + (tr[i][k] * pt[k][j]);
}
}
}
printf("DISPLAYING ORIGINAL POINT MATRIX \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d ", pt[i][j]);
}
printf("\n");
}
printf("\nDISPLAYING REFLECTION MATRIX \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", tr[i][j]);
}
printf("\n");
}
printf("\n DISPLAYING RESULT MATRIX \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d ", res[i][j]);
}
printf("\n");
}
x1n = res[0][0]; y1n = res[1][0] + getmaxy();
x2n = res[0][1]; y2n = res[1][1] + getmaxy();
x3n = res[0][2]; y3n = res[1][2] + getmaxy();
x4n = res[0][3]; y4n = res[1][3] + getmaxy();

setcolor(WHITE);
outtextxy(x1n, y1n, "REFLECTED RECTANGLE");
setcolor(BLUE);
line(x1n, y1n, x2n, y2n);
line(x2n, y2n, x3n, y3n);
line(x3n, y3n, x4n, y4n);
line(x4n, y4n, x1n, y1n);

getch();
closegraph();
}
Output:

You might also like