0% found this document useful (0 votes)
2 views12 pages

CG file

The document outlines lab exercises for a Computer Graphics course, focusing on color filling algorithms and 2D transformations. It includes code examples for filling shapes using flood fill and boundary fill algorithms, as well as performing various transformations like translation, rotation, scaling, shearing, and reflection. Additionally, it covers composite transformations involving sequential translations and scaling operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

CG file

The document outlines lab exercises for a Computer Graphics course, focusing on color filling algorithms and 2D transformations. It includes code examples for filling shapes using flood fill and boundary fill algorithms, as well as performing various transformations like translation, rotation, scaling, shearing, and reflection. Additionally, it covers composite transformations involving sequential translations and scaling operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Computer Science & Technology

Computer Graphics (CSH310B-P)

Course: B. Tech Semester: VI


Session: 2024-25 Subject: Computer Graphics and Multimedia CSH310-P

Lab: 06 Color Filling Algorithms


Objective: To change the design and colour of the circle.
Course Outcome CO1: Statement of CO1 from course plan
Blooms Taxonomy Level: BT3, BT5

1. Draw a square and fill the color in it using flood fill


algorithm. Input:

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

void flood(int x, int y, int new_col, int old_col)


{
if (getpixel(x, y) == old_col) {
putpixel(x, y, new_col);
flood(x + 1, y, new_col, old_col);
flood(x - 1, y, new_col, old_col);
flood(x, y + 1, new_col,
old_col); flood(x, y - 1, new_col,
old_col);
}
}

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

// initialize graph
initgraph(&gd, &gm, "");

// rectangle coordinate
int top, left, bottom, right;

top = left = 50;


bottom = right = 300;

// rectangle for print rectangle


rectangle(left, top, right, bottom);

// filling start coordinate


int x = 51;
int y = 51;

// new color to fill


int newcolor = 12;

// new color which you want to fill


int oldcolor = 0;

// call for fill rectangle


flood(x, y, newcolor, oldcolor);
getch();

return 0;
}

Output:

2. Draw a circle and fill the color in it using boundary fill algorithm.

Input:
#include <graphics.h>
#include <iostream>

using namespace std;

void boundaryFill4(int x, int y, int fill_color, int boundary_color)


{
if (getpixel(x, y) != boundary_color && getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 250, y = 200, radius = 50;

circle(x, y, radius);
boundaryFill4(x, y, 6, 15);

delay(10000);

getch();
closegraph();

return 0;
}

Output:

3. Draw 2 shapes and fill the color using 4-connected region and 8-connected region respectively.

Input:
#include <opencv2/opencv.hpp>

using namespace cv;

void floodFill4Connected(Mat &image, Point seed, Scalar color, Scalar newColor) {


Mat mask;
copyMakeBorder(image, mask, 1, 1, 1, 1, BORDER_CONSTANT, Scalar(0));
floodFill(image, mask, seed, newColor, 0, color, color, 4);
}

void floodFill8Connected(Mat &image, Point seed, Scalar color, Scalar newColor) {


Mat mask;
copyMakeBorder(image, mask, 1, 1, 1, 1, BORDER_CONSTANT, Scalar(0));
floodFill(image, mask, seed, newColor, 0, color, color, 8);
}

int main() {
Mat image = Mat::zeros(400, 600, CV_8UC3);

rectangle(image, Point(50, 50), Point(200, 200), Scalar(255, 255, 255), FILLED);


circle(image, Point(400, 150), 80, Scalar(255, 255, 255), FILLED);
floodFill4Connected(image, Point(100, 100), Scalar(255, 255, 255), Scalar(0, 0, 255));
floodFill8Connected(image, Point(400, 150), Scalar(255, 255, 255), Scalar(0, 255, 0));
imshow("Flood Fill Example", image);
waitKey(0);
return 0;
}
Output:
Department of Computer Science & Technology

Course: B. Tech Semester: VI


Session: 2024-25 Subject: Computer Graphics and Multimedia CSH310-P
Lab: 07 2D Transformation
Objective: To perform 2D transformations (translation, rotation, scaling, shearing, reflection).
Course Outcome CO1: Statement of CO2 from course plan
Blooms Taxonomy Level: BT3
1. Perform various Transformations on different shapes.
a. Translation with Tx=100, Ty=200
b. Rotation anticlockwise with an angle of 90o
c. Scaling with Sx=2 and Sy=3
d. Shearing with SHx=2
e. Perform reflection along
i. X-axis
ii. Y-axis
iii. Z-axis

Input:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int gd = DETECT, gm;
float tx = 100.0, ty = 200.0;
float angle = 90.0;
float radians;
float sx = 2.0, sy = 3.0;
float shx = 2.0;
int i = 0, n = 3;
/* Function prototypes */
void translate(float x[], float y[], float tx, float ty);
void rotate2D(float x[], float y[]);
void scale2D(float x[], float y[]);
void shearX(float x[], float y[]);
void reflectX(float x[], float y[]);
void reflectY(float x[], float y[]);
void reflectOrigin(float x[], float y[]);
void translate(float x[], float y[], float tx, float ty) {
for (i = 0; i < n; i++) {
x[i] = x[i] + tx;
y[i] = y[i] + ty;
}
}
void rotate2D(float x[], float y[]) {
for (i = 0; i < n; i++) {
float tempX = x[i] * cos(radians) - y[i] * sin(radians);
float tempY = x[i] * sin(radians) + y[i] * cos(radians);
x[i] = tempX;
y[i] = tempY;
}
}
void scale2D(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = x[i] * sx;
y[i] = y[i] * sy;
}
}
void shearX(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = x[i] + shx * y[i];
}
}
void reflectX(float x[], float y[]) {
for (i = 0; i < n; i++) {
y[i] = -y[i];
}
}
void reflectY(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = -x[i];
}
}
void reflectOrigin(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = -x[i];
y[i] = -y[i];
}
}
int main() {
float x[10], y[10];
float tx_x[10], tx_y[10];
float rot_x[10], rot_y[10];
float sc_x[10], sc_y[10];
float sh_x[10], sh_y[10];
float refx_x[10], refx_y[10];
float refy_x[10], refy_y[10];
float refO_x[10], refO_y[10];
/* Define a triangle */
x[0] = 100; y[0] = 100;
x[1] = 200; y[1] = 100;
x[2] = 150; y[2] = 200;
/* Convert angle to radians */
radians = angle * M_PI / 180.0;
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw original triangle */
setcolor(WHITE);
for (i = 0; i < n; i++)
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
/* Translation */
for (i = 0; i < n; i++) {
tx_x[i] = x[i];
tx_y[i] = y[i];
}
translate(tx_x, tx_y, tx, ty);
setcolor(RED);
for (i = 0; i < n; i++)
line(tx_x[i], tx_y[i], tx_x[(i + 1) % n], tx_y[(i + 1) % n]);
/* Rotation */
for (i = 0; i < n; i++) {
rot_x[i] = x[i];
rot_y[i] = y[i];
}
rotate2D(rot_x, rot_y);
setcolor(GREEN);
for (i = 0; i < n; i++)
line(rot_x[i], rot_y[i], rot_x[(i + 1) % n], rot_y[(i + 1) % n]);
/* Scaling */
for (i = 0; i < n; i++) {
sc_x[i] = x[i];
sc_y[i] = y[i];
}
scale2D(sc_x, sc_y);
setcolor(BLUE);
for (i = 0; i < n; i++)
line(sc_x[i], sc_y[i], sc_x[(i + 1) % n], sc_y[(i + 1) % n]);
/* Shearing */
for (i = 0; i < n; i++) {
sh_x[i] = x[i];
sh_y[i] = y[i];
}
shearX(sh_x, sh_y);
setcolor(YELLOW);
for (i = 0; i < n; i++)
line(sh_x[i], sh_y[i], sh_x[(i + 1) % n], sh_y[(i + 1) % n]);
/* Reflection about X-axis */
for (i = 0; i < n; i++) {
refx_x[i] = x[i];
refx_y[i] = y[i];
}
reflectX(refx_x, refx_y);
setcolor(MAGENTA);
for (i = 0; i < n; i++)
line(refx_x[i], refx_y[i], refx_x[(i + 1) % n], refx_y[(i + 1) % n]);
/* Reflection about Y-axis */
for (i = 0; i < n; i++) {
refy_x[i] = x[i];
refy_y[i] = y[i];
}
reflectY(refy_x, refy_y);
setcolor(CYAN);
for (i = 0; i < n; i++)
line(refy_x[i], refy_y[i], refy_x[(i + 1) % n], refy_y[(i + 1) % n]);
/* Reflection about Origin (Z-axis equivalent) */
for (i = 0; i < n; i++) {
refO_x[i] = x[i];
refO_y[i] = y[i];
}
reflectOrigin(refO_x, refO_y);
setcolor(LIGHTGREEN);
for (i = 0; i < n; i++)
line(refO_x[i], refO_y[i], refO_x[(i + 1) % n], refO_y[(i + 1) % n]);
getch();
closegraph();
return 0;
}

Output:
Department of Computer Science & Technology
Course: B. Tech Semester: VI
Session: 2024-25 Subject: Computer Graphics and Multimedia CSH310-P

Lab: 08 Composite Transformation


Objective: To implement Composite transformations
Course Outcome CO1: Statement of CO2 from course plan
Blooms Taxonomy Level: BT3
1. Perform Translation followed by Translation with Tx1 & Ty1=50 and Tx2 & Ty2=100.
2. Perform Scaling Sx=Sy=2, followed by Translation with Tx1 & Ty1=50 and Tx2 & Ty2=100.
3. Perform Translation with Tx1 & Ty1=25 and Tx2 & Ty2=55, followed by Scaling with Sx1=2 and Sy2=3.

Input:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int gd = DETECT, gm;
int i = 0;
int n = 3; /* Number of vertices (triangle) */
/* Original triangle coordinates */
float x_orig[3] = {100.0, 200.0, 150.0};
float y_orig[3] = {100.0, 100.0, 200.0};
/* Translation parameters */
float Tx1 = 50.0, Ty1 = 50.0;
float Tx2 = 100.0, Ty2 = 100.0;
/* Scaling parameters */
float Sx = 2.0, Sy = 2.0;
/* Temporary arrays for transformations */
float x_temp[3], y_temp[3];
float x_res[3], y_res[3];
/* Function prototypes */
void translate(float x[], float y[], float tx, float ty);
void scale2D(float x[], float y[], float sx, float sy);
void translate(float x[], float y[], float tx, float ty) {
for (i = 0; i < n; i++) {
x[i] = x[i] + tx;
y[i] = y[i] + ty;
}
}
void scale2D(float x[], float y[], float sx, float sy) {
for (i = 0; i < n; i++) {
x[i] = x[i] * sx;
y[i] = y[i] * sy;
}
}
int main() {
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw original triangle */
setcolor(WHITE);
for (i = 0; i < n; i++) {
line((int)x_orig[i], (int)y_orig[i], (int)x_orig[(i+1)%n], (int)y_orig[(i+1)%n]);
}
/* 1. Translation by (Tx1,Ty1) then (Tx2,Ty2) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
setcolor(RED);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
/* 2. Scaling by (Sx,Sy) then Translation by (Tx1,Ty1) and (Tx2,Ty2) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
scale2D(x_temp, y_temp, Sx, Sy);
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
setcolor(GREEN);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
/* 3. Translation by (Tx1,Ty1) and (Tx2,Ty2) then Scaling by (Sx,Sy) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
scale2D(x_temp, y_temp, Sx, Sy);
setcolor(BLUE);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
getch();
closegraph();
return 0;
}

Output:
Department of Computer Science & Technology

Course: B. Tech Semester: VI


Session: 2024-25 Subject: Computer Graphics and Multimedia CSH310-P

Lab: 09 Animation using C


Objective: To perform Animation using C
Course Outcome CO1: Statement of CO2 from course plan
Blooms Taxonomy Level: BT3
1. Design an Analog clock.
Input:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int x_end,y_end;
int gd = DETECT, gm;
int i = 0;
int x_center = 320, y_center = 240; /* Center of clock */
int radius = 200; /* Clock radius */
int sec_len = 180, min_len = 170, hr_len = 120; /* Hand lengths */
float sec_angle, min_angle, hr_angle;
struct time t;
void draw_face() {
setcolor(WHITE);
circle(x_center, y_center, radius);
for (i = 0; i < 12; i++) {
float theta = i * M_PI / 6;
int x1 = x_center + (int)(radius * cos(theta));
int y1 = y_center + (int)(radius * sin(theta));
int x2 = x_center + (int)((radius - 20) * cos(theta));
int y2 = y_center + (int)((radius - 20) * sin(theta));
line(x1, y1, x2, y2);
}
}
void draw_hand(float angle, int length, int color) {
setcolor(color);
x_end = x_center + (int)(length * sin(angle));
y_end = y_center - (int)(length * cos(angle));
line(x_center, y_center, x_end, y_end);
}
int main() {
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
while (!kbhit()) {
gettime(&t);
sec_angle = (t.ti_sec / 60.0) * 2 * M_PI;
min_angle = ((t.ti_min + t.ti_sec/60.0) / 60.0) * 2 * M_PI;
hr_angle = ((t.ti_hour % 12 + (t.ti_min+t.ti_sec/60.0)/60.0) / 12.0) * 2 * M_PI;
cleardevice();
draw_face();
draw_hand(hr_angle, hr_len, WHITE);
draw_hand(min_angle, min_len, GREEN);
draw_hand(sec_angle, sec_len, RED);
delay(1000);
}
closegraph();
return 0;
}
Output:

2. Create an animation of a moving car.

Input:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int gd2 = DETECT, gm2;
int x = 0, y = 350; /* Initial car position */
int i2 = 0;
void draw_car(int x_pos, int y_pos) {
/* Car body */
rectangle(x_pos, y_pos - 20, x_pos + 100, y_pos + 20);
/* Wheels */
circle(x_pos + 20, y_pos + 25, 10);
circle(x_pos + 80, y_pos + 25, 10);
/* Windows */
line(x_pos + 20, y_pos - 20, x_pos + 40, y_pos - 40);
line(x_pos + 60, y_pos - 20, x_pos + 40, y_pos - 40);
line(x_pos + 60, y_pos - 20, x_pos + 80, y_pos - 40);
}
int main() {
initgraph(&gd2, &gm2, "C:\\TURBOC3\\BGI");
for (i2 = 0; i2 <= getmaxx() + 100; i2 += 5) {
cleardevice();
draw_car(i2, y);
delay(100);
}
getch();
closegraph();
return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy