CGMA
CGMA
CGMA
APPLICATIONS
Project No. 1
Student’ Kashish
s Name
Gupta
Enrollme ASB/BCA/
nt 22/27
Number
MINI PROJECT OF COMPUTER GRAPHICS
AND MULTIMEDIA APPLICATION
Abstract
The Midpoint Subdivision Algorithm is a fundamental technique used in
computer graphics for generating smooth curves and surfaces. This
algorithm divides a line or a curve into smaller segments by iteratively
finding the midpoint of each segment. It is widely used in various
applications such as rendering Bezier curves, constructing fractals, and
generating terrain in computer games. This assignment aims to provide
a comprehensive explanation of the Midpoint Subdivision Algorithm,
along with a practical implementation of the algorithm in code.
Introduction
The Midpoint Subdivision Algorithm is a technique used to divide a line
or curve into smaller segments. It is particularly useful in computer
graphics for generating smooth curves and surfaces. The algorithm
operates by recursively subdividing a line segment into smaller
segments until a desired level of detail is achieved. The key idea behind
the algorithm is to find the midpoint of each segment and then
recursively apply the same process to the two resulting sub-segments.
Detailed Concept
The Midpoint Subdivision Algorithm can be understood through the
following steps:
Begin with a line segment defined by two endpoints, A and B.
Compute the midpoint M of the line segment using the formula: M(x) =
(A(x) + B(x)) / 2 M(y) = (A(y) + B(y)) / 2
Divide the line segment into two smaller segments: AM and MB.
Recursively apply steps 2 and 3 to each of the smaller segments until
the desired level of detail is achieved.
This process generates a set of points that approximate the original
curve or line segment. By increasing the number of iterations, a
smoother approximation can be obtained.
Diagrams
Figure 1: Diagram illustrating the Midpoint Subdivision Algorithm.
In this diagram:
A and B represent the endpoints of the original line segment.
M1 and M2 represent the midpoints of the line segment
obtained after the first iteration.
The horizontal lines represent the line segments at each iteration.
The vertical lines represent the midpoints computed at each
iteration.
References
Foley, James D., et al. "Computer Graphics: Principles and Practice."
Addison-Wesley Professional, 2014.
Hughes, John F. "Computer Graphics: Principles and Practice." Pearson
Education, 2013.
Hearn, Donald, and Pauline Baker. "Computer Graphics with OpenGL."
Pearson Education, 2017.
Program
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
vector<pair<double, double>>
midpoint_subdivision(pair<double, double> A, pair<double,
double> B, int iterations) {
vector<pair<double, double>> points;
if (iterations == 0) {
points.push_back(A);
points.push_back(B);
return points;
}
pair<double, double> M = {(A.first + B.first) / 2, (A.second +
B.second) / 2}; // Compute midpoint
points.push_back(A);
points.push_back(M);
points.push_back(B);
vector<pair<double, double>> left_points =
midpoint_subdivision(A, M, iterations - 1);
vector<pair<double, double>> right_points =
midpoint_subdivision(M, B, iterations - 1);
for (size_t i = 1; i < left_points.size(); ++i)
points.push_back(left_points[i]);
for (size_t i = 1; i < right_points.size(); ++i)
points.push_back(right_points[i]);
return points;
}
int main() {
pair<double, double> A = {0, 0};
pair<double, double> B = {10, 10};
int iterations = 3;
vector<pair<double, double>> result_points =
midpoint_subdivision(A, B, iterations);
cout << "Resulting points:" << endl;
for (const auto& point : result_points) {
cout << "(" << point.first << ", " << point.second << ")" << endl;
}
return 0;
}
OUTPUT: