0% found this document useful (0 votes)
36 views13 pages

CG Assignment

Uploaded by

arunabadiger01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views13 pages

CG Assignment

Uploaded by

arunabadiger01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Graphics and Fundamentals of

Image processing Assignment on Haker rank


Questions
Problem 1:
You are given an N×N 2D matrix representing an image. Rotate the
image by 90 degrees clockwise.
Input Format
 The first line contains an integer N, the size of the matrix.
 The next N lines each contain N integers representing the
matrix.
Input: 3 1 2 3 4 5 6 7 8 9
Constraints
 1≤N≤100
 Each element in the matrix is an integer.
Output Format
 Print the rotated matrix.
Output: 7 4 1 8 5 2 9 6 3
Sample Input 0
3
123
456
789
Sample Output 0
741
852
963
Code: def rotate_matrix(matrix):
N = len(matrix)
# Transpose the matrix
for i in range(N):
for j in range(i, N):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reverse each row
for i in range(N):
matrix[i] = matrix[i][::-1]
return matrix

# Reading input
N = int(input().strip())
matrix = []
for _ in range(N):
matrix.append(list(map(int, input().split())))

# Rotating the matrix


rotated_matrix = rotate_matrix(matrix)

# Printing the rotated matrix


for row in rotated_matrix:
print(' '.join(map(str, row)))
problem 2:
You are given a set of points in a 2D plane representing a shape.
Apply a scaling transformation with a given scaling factor to enlarge
the shape.
Input Format
 The first line contains an integer P, the number of points.
 The next P lines each contain two integers x and y representing
the coordinates of each point.
 The last line contains an integer S, the scaling factor.
Input: 3 1 1 2 2 3 3 2
Constraints
 1≤P≤100
 −1000≤x,y≤1000
 1≤S≤10
Output Format
 Print the coordinates of the scaled points, one per line.
Output: 2 2 4 4 6 6
Sample Input 0
3
11
22
33
2
Sample Output 0
22
44
66
Code : def scale_points(points, scaling_factor):
scaled_points = []
for x, y in points:
scaled_points.append((x * scaling_factor, y * scaling_factor))
return scaled_points

# Reading input
P = int(input().strip())
points = []
for _ in range(P):
x, y = map(int, input().split())
points.append((x, y))
S = int(input().strip())

# Scaling the points


scaled_points = scale_points(points, S)

# Printing the scaled points


for x, y in scaled_points:
print(x, y)
problem 3:
You are given two points in a 2D plane. Use Bresenham's line
algorithm to determine the points on the grid that form a line
between these two points.
Input Format
 The first line contains two integers 𝑥1 and 𝑦1, the coordinates
of the start point.
 The second line contains two integers 𝑥2 and 𝑦2, the
coordinates of the end point.
Input: 0 0 3 4
Constraints
 −100≤ x1,y1,x2,y2 ≤100
Output Format
 Print the coordinates of the points on the line, one per line.
Output: 0 0 1 1 2 2 3 3 3 4
Sample Input 0
-1 -1
21
Sample Output 0
-1 -1
00
10
21
Code: def bresenham_line(x1, y1, x2, y2):
points = []
dx = abs(x2 - x1)
dy = abs(y2 - y1)
sx = 1 if x1 < x2 else -1
sy = 1 if y1 < y2 else -1
err = dx - dy

while True:
points.append((x1, y1))
if x1 == x2 and y1 == y2:
break
e2 = 2 * err
if e2 > -dy:
err -= dy
x1 += sx
if e2 < dx:
err += dx
y1 += sy
return points

# Reading input
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())

# Getting the points on the line using Bresenham's algorithm


line_points = bresenham_line(x1, y1, x2, y2)

# Printing the points on the line


for x, y in line_points:
print(x, y)

problem 4:
You are given a grid representing a 2D plane with some cells filled.
Use the flood fill algorithm to color the connected region starting
from a given cell.

Input Format

The first line contains two integers N and M, the dimensions of the
grid.
The next N lines each contain M integers representing the grid.
The next line contains two integers x and y, the starting cell
coordinates.
The last line contains an integer C, the new color.
Input: 4 4 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 2

Constraints
1≤N,M≤100
0≤grid[i][j]≤9
0≤x
0≤y
0≤C≤9
Output Format

Print the modified grid.


Output: 0 0 0 0 0 2 2 0 0 2 2 0 0 0 0 0

Sample Input 0

33
000
010
000
11
2
Sample Output 0

000
020
000
Code: def flood_fill(grid, x, y, new_color):
N = len(grid)
M = len(grid[0])
original_color = grid[x][y]

if original_color == new_color:
return grid

def fill(x, y):


if x < 0 or x >= N or y < 0 or y >= M:
return
if grid[x][y] != original_color:
return

grid[x][y] = new_color

fill(x + 1, y)
fill(x - 1, y)
fill(x, y + 1)
fill(x, y - 1)

fill(x, y)
return grid
# Reading input
N, M = map(int, input().split())
grid = []
for _ in range(N):
grid.append(list(map(int, input().split())))
x, y = map(int, input().split())
new_color = int(input().strip())

# Applying the flood fill algorithm


result_grid = flood_fill(grid, x, y, new_color)

# Printing the modified grid


for row in result_grid:
print(' '.join(map(str, row)))

Problem 5:
Generate and visualize the Mandelbrot set for a given range and
iteration limit.
Input Format
 The first line contains four floats 𝑥min, 𝑥max, 𝑦min and
ymaxrepresenting the range of complex numbers.
 The second line contains an integer I, the number of iterations.
Input: -2.0 2.0 -2.0 2.0 1000
Constraints
 −2.0≤ xmin ,xma ,ymin,ymax ≤2.0
Output Format
Output: A 2D array representing the Mandelbrot set (pseudo-visual
representation)
Sample Input 0
3
22
22
22
22
0.5
Sample Output 0
Bezier Point at t = 0.5: (2.0, 2.0)
Code: #include <stdio.h>

// Function to calculate the point on a Bezier curve at parameter t


void bezier_point(double t, double x[], double y[], int n, double
*x_result, double *y_result) {
double x_temp[n];
double y_temp[n];

for (int i = 0; i < n; i++) {


x_temp[i] = x[i];
y_temp[i] = y[i];
}

for (int j = n - 1; j > 0; j--) {


for (int i = 0; i < j; i++) {
x_temp[i] = (1 - t) * x_temp[i] + t * x_temp[i + 1];
y_temp[i] = (1 - t) * y_temp[i] + t * y_temp[i + 1];
}
}

*x_result = x_temp[0];
*y_result = y_temp[0];
}

int main() {
int n = 3;
double x[] = {2, 2, 2}; // x-coordinates of control points
double y[] = {2, 2, 2}; // y-coordinates of control points
double t = 0.5; // Parameter t

double x_result, y_result;

bezier_point(t, x, y, n, &x_result, &y_result);

printf("Bezier Point at t = %.1f: (%.1f, %.1f)\n", t, x_result,


y_result);
return 0;
}

You might also like