CGM File - Sharonkaur - It2 - 104
CGM File - Sharonkaur - It2 - 104
CGM File - Sharonkaur - It2 - 104
Computer
graphics and
multimedia
practical
file
Sharon
Sahil Kaur Katyal
Vashisht
Branch IT – 32
Roll no - 10413203119
04676803120
INDEX
void main(){
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);
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);
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);
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>
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);
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;
setcolor(YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
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;
= 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();
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 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");
x1 = 200; y1 = 200;
x2 = 500; y2 = 300;
tx = 50; ty = 20;
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);
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("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));
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;
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;
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;
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");
x1 = 110; y1 = 70;
x2 = 150; y2 = 200;
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;
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: