You are on page 1of 9

Lab Report: 04

Course title: Computer Graphics Laboratory


Course code: CSE-304
3rd Year 1st Semester Examination 2022

Date of Submission: 16-7-2023

Submitted to-
Dr. Mohammad Shorif Uddin
Professor
Department of Computer Science and Engineering
Jahangirnagar University
&
Dr. Morium Akter
Associate Professor
Department of Computer Science and Engineering
Jahangirnagar University
Savar, Dhaka-1342

Class Roll Exam Roll Name

366 Lamia Binta Latif

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Experiment no-01: Boundary Fill Algorithm
Introduction: Boundary Fill Algorithm starts at a pixel inside the polygon to be filled and paints the
interior proceeding outwards towards the boundary. This algorithm works only if the color with which
the region has to be filled and the color of the boundary of the region are different.

Source code:

#include <graphics.h>

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);

//driver code

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
int x = 250, y = 200, radius = 50;

circle(x, y, radius);

boundaryFill4(x, y, 6, 15);

delay(10000);

getch();

closegraph();

return 0;

Output:

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Discussion:

Experiment no-01: Flood Fill Algorithm


Introduction: The traditional flood-fill algorithm takes three parameters: a start node, a target color,
and a replacement color. The algorithm looks for all nodes in the array that are connected to the start
node by a path of the target color and changes them to the replacement color.

Source code:

#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;

initgraph(&gd, &gm, "");

int top, left, bottom, right;

top = left = 50;

bottom = right = 300;

rectangle(left, top, right, bottom);

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
int x = 51;

int y = 51;

int newcolor = 12;

int oldcolor = 0;

flood(x, y, newcolor, oldcolor);

getch();

return 0;

Output:

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Discussion:.

Experiment no-02: Resize to double of a triangle whose vertices are A(0,0),


B(1,1),C(5,2) and keeping the vertex C(5,2) fixed.
Introduction:

Source code:

#include <iostream>

#include <graphics.h>

void resizeTriangle(double scale) {

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

int Ax = 0, Ay = 0;

int Bx = 10, By = 10;

int Cx = 50, Cy = 20;

int newAx = Ax * scale;

int newAy = Ay * scale;

int newBx = Bx * scale;

int newBy = By * scale;

int newCx = Cx;

int newCy = Cy;

line(Ax, Ay, Bx, By);

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
line(Bx, By, Cx, Cy);

line(Cx, Cy, Ax, Ay);

line(newAx, newAy, newBx, newBy);

line(newBx, newBy, newCx, newCy);

line(newCx, newCy, newAx, newAy);

delay(5000);

closegraph();

int main() {

double scale = 2.0;

resizeTriangle(scale);

return 0;

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Output:

Discussion

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Department of Computer Science and Engineering
Jahangirnagar University
Savar, Dhaka, Bangladesh

You might also like