You are on page 1of 12

Lab Report: 03

Course title: Computer Graphics Laboratory


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

Date of Submission: 11/06/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: Scan conversion of a line object from (0,0) to (100,50)

I. Rotate it by 30%
Introduction: Scan conversion, also known as rasterization, is a process used in computer
graphics to convert geometric shapes into pixel-based representations on a display screen. In
this case, we'll focus on the scan conversion of a line object from the starting point (0,0) to the
endpoint (100,50) and subsequently rotating it by 30 degrees.

Source code:

#include <iostream>

#include <graphics.h>

#include <cmath>

void rotatePoint(int& x, int& y, float angle) {

float radian = angle * 3.14159 / 180.0;

float newX = x * cos(radian) - y * sin(radian);

float newY = x * sin(radian) + y * cos(radian);

x = (int)newX;

y = (int)newY;

void rotateLine(int x1, int y1, int x2, int y2, float angle) {

rotatePoint(x1, y1, angle);

rotatePoint(x2, y2, angle);

line(x1, y1, x2, y2);

int main() {

int gd = DETECT, gm;

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
initgraph(&gd, &gm, "");

int x1 = 0, y1 = 0, x2 = 100, y2 = 50;

line(x1, y1, x2, y2);

rotateLine(x1, y1, x2, y2, 30);

getch();

closegraph();

return 0;

Output:

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Discussion: The code attempts to draw a line using the graphics.h library and then rotate that
line by 30 degrees using the rotateLine function. However, without additional context and
information about the specific environment in which the code is executed, it is difficult to
determine the exact output or behavior of the code.

II. Scale it to 50%


Introduction: Scan conversion is a technique used to convert a geometric object into a pixel
representation for display on a computer screen. In this case, the code is attempting to scan convert a
line object from the coordinates (0,0) to (100,50) using the Bresenham's line algorithm or a similar
method. Additionally, the code aims to scale the line by 50%, which means reducing its size by half.
However, without the actual implementation of the scan conversion and scaling algorithms in the
provided code, it is not possible to determine the specific output or visual representation of the line.

Source code:

#include <iostream>

#include <graphics.h>

void scaleLine(int x1, int y1, int x2, int y2, float scaleFactor) {

int newX1 = x1 * scaleFactor;

int newY1 = y1 * scaleFactor;

int newX2 = x2 * scaleFactor;

int newY2 = y2 * scaleFactor;

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
line(newX1, newY1, newX2, newY2);

int main() {

int gd = DETECT, gm;

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

int x1 = 0, y1 = 0, x2 = 100, y2 = 50;

float scaleFactor = 0.5;

scaleLine(x1, y1, x2, y2, scaleFactor);

getch();

closegraph();

return 0;

Output:

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Discussion: The code attempts to scale a line object from the coordinates (0,0) to (100,50) by a factor
of 0.5 using the scaleLine function. The line is drawn at half its original size on the screen.

III. Translate it on x-axis by 75%


Introduction: Scan conversion is a technique used to convert a geometric object into a pixel
representation for display on a computer screen. In this case, the code aims to scan convert a
line object from the coordinates (0,0) to (100,50) using the Bresenham's line algorithm or a
similar method. Additionally, the code intends to translate the line along the x-axis by 75% of its
original length.

Source code:

#include <iostream>

#include <graphics.h>

void translateLine(int x1, int y1, int x2, int y2, float translateX) {

int newX1 = x1 + translateX;

int newY1 = y1;

int newX2 = x2 + translateX;

int newY2 = y2;

line(newX1, newY1, newX2, newY2);

int main() {

int gd = DETECT, gm;

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

int x1 = 0, y1 = 0, x2 = 100, y2 = 50;

line(x1, y1, x2, y2);

float translateX = 0.75 * x2; // 75% of x2

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
translateLine(x1, y1, x2, y2, translateX);

getch();

closegraph();

return 0;

Output:

Discussion: The code attempts to translate a line object from the coordinates (0,0) to (100,50) along
the x-axis by 75% of the length of the line (75% of x2). The line is expected to be drawn at a new position

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
on the screen, reflecting the translation.The code initializes a graphics environment, draws the original
line, calculates the translation distance, and then calls the translateLine function to perform the
translation. However, without the complete graphics library and initialization, it is not possible to
determine the exact output or visual representation of the translated line.Overall, the code
demonstrates a basic implementation of translating a line object on the screen using a percentage-
based translation factor.

Experiment no-02: Drawing a kite using Bresenham line algorithm


Introduction: The Bresenham line algorithm is a rasterization algorithm used to draw lines on a
computer screen. The algorithm calculates the coordinates of individual pixels along a line path, taking
into account the slope and direction of the line.To draw a kite shape using the Bresenham line
algorithm, we typically need to define the four sides of the kite as separate line segments and use the
algorithm to draw each segment.The specific implementation details, such as the coordinates of the
kite's vertices and the order in which the line segments are drawn, would depend on the desired shape
and design of the kite.

Source code:

#include <iostream>

#include <graphics.h>

void drawLine(int x1, int y1, int x2, int y2) {

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int x = x1, y = y1;

int xInc = (x2 >= x1) ? 1 : -1;

int yInc = (y2 >= y1) ? 1 : -1;

if (dx >= dy) {

int p = 2 * dy - dx;

while (x != x2) {

putpixel(x, y, WHITE);

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
if (p >= 0) {

y += yInc;

p -= 2 * dx;

x += xInc;

p += 2 * dy;

} else {

int p = 2 * dx - dy;

while (y != y2) {

putpixel(x, y, WHITE);

if (p >= 0) {

x += xInc;

p -= 2 * dy;

y += yInc;

p += 2 * dx;

int main() {

int gd = DETECT, gm;

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

int topX = 250, topY = 100;

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
int leftX = 150, leftY = 250;

int rightX = 350, rightY = 250;

int bottomX = 250, bottomY = 400;

drawLine(topX, topY, leftX, leftY);

drawLine(leftX, leftY, bottomX, bottomY);

drawLine(bottomX, bottomY, rightX, rightY);

drawLine(rightX, rightY, topX, topY);

getch();

closegraph();

return 0;

Output:

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Discussion: The code uses the Bresenham line algorithm to draw a kite shape on the screen. It utilizes
the graphics.h library to initialize a graphics environment, draw individual line segments, and display the
result.The kite shape is defined by four line segments connecting the top, left, bottom, and right points.
The code calls the `drawLine` function four times, passing the appropriate coordinates for each line
segment. The line segments are drawn in the order of top to left, left to bottom, bottom to right, and
right to top.When the code is executed, it opens a graphics window and draws the kite shape using the
Bresenham line algorithm. Each line segment is displayed as a series of pixels, creating the kite shape on
the screen.

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