CG 2 Important notes
CG 2 Important notes
CG 2 Important notes
Basic Transformations:
Example:
Say you want to rotate a square around its center and then move it to the top-right
corner. A composite transformation ensures the rotation happens first, followed
by the move.
Composite transformations are a powerful tool in computer graphics, allowing
for efficient and precise manipulation of objects.
Describe what is Homogenous coordinates.
Representation:
o In 2D, a regular point is represented by (x, y).
o With homogeneous coordinates, a point is represented by (X, Y, W),
where W is the extra dimension.
Key Feature:
o The actual location of the point depends on the ratio between X, Y,
and Z, not their absolute values. So, (2X, 2Y, 2W) represents the
same point as (X, Y, W), as long as W isn't zero.
Benefits:
o Representing Points at Infinity: Points infinitely far away in the
traditional sense can have finite homogeneous coordinates by setting
W to zero. This is useful in computer graphics for things like light
direction.
o Simpler Transformations: Many geometric transformations, like
translation, rotation, and scaling, become easier to express and
combine using homogeneous coordinates. They can all be represented
as multiplications by specific 4x4 matrices.
Here's an analogy:
Computer graphics (CG) has a vast range of applications across various fields.
Here's a brief overview of some key areas:
Other Applications:
Applications of Rasterization:
Advantages of Rasterization:
Efficiency: Rasterization is computationally efficient, making it suitable
for real-time rendering of complex scenes.
Hardware Acceleration: Modern graphics processing units (GPUs) are
optimized for rasterization, further enhancing its speed and performance.
Wide Support: Raster images are widely supported by various display
technologies and file formats, making them a versatile output format.
Limitations of Rasterization:
where:
Our goal is to find efficient ways to determine all the points that lie on the circle's
perimeter. We can achieve this by exploiting the symmetry of a circle.
Iterative Approach:
1. Starting Point: We can begin by placing a pixel at (R, 0), which is a point
on the circle in the first octant (positive X and positive Y).
2. Symmetry: Since a circle is symmetrical, any point on the circle in one
octant will have a corresponding mirror point in the other seven octants.
We only need to calculate points in one octant and then replicate them to
other octants.
3. Decision Making: The key idea is to decide efficiently which pixel to
choose next, either to the right (X + 1) or diagonally up and to the left (X +
1, Y - 1). This decision will ensure we trace the circle's path accurately.
Midpoint Approach:
Instead of directly choosing the next pixel, we can consider a midpoint between
the two candidate points (X + 1, Y) and (X + 1, Y - 1). This midpoint has
coordinates:
(X + 0.5, Y - 0.5)
This term, P, acts as an error term. It tells us how far the midpoint is from the
perfect circle path.
P = P + 2
Iterative Algorithm:
1. Initialize: X = 0, Y = R, P = 1 - R
2. Repeat until X > Y:
o If P < 0: Plot (X, Y) X = X + 1 P = P + 2 + 3
o Else: Plot (X, Y) X = X + 1 Y = Y - 1 P = P + 2
3. Reflect points to other octants to complete the circle.
Derive matrix for 2D rotation transformation.
We can represent a point in 2D space using its coordinates (x, y). When we rotate
this point around the origin by an angle θ, its new position becomes (x', y').
Cosine (cos(θ)) represents the ratio of the adjacent side (original x) to the
hypotenuse (distance from the origin) after the rotation.
Sine (sin(θ)) represents the ratio of the opposite side (original y) to the
hypotenuse after the rotation.
Using these trigonometric relationships, we can express the new x' and y'
coordinates in terms of the original x and y, and the rotation angle θ:
x' = cos(θ) * x - sin(θ) * y
y' = sin(θ) * x + cos(θ) * y
Matrix Representation:
To apply the rotation to a point (x, y), we multiply its coordinates by the rotation
matrix:
| x' | | R(θ) | | x |
| y' | = | | * | y |
| | | 1 | (represents translation
by 0 in both x and y)
This matrix multiplication efficiently performs the rotation on the point using the
cosine and sine values for the given angle θ.
Basic Transformations:
Homogeneous Coordinates
Representation:
o A point in 2D is represented as (X, Y, W).
o A point in 3D is represented as (X, Y, Z, W).
o W can be any non-zero value. Typically, W is set to 1 for points in the
drawing space and used as a scaling factor in other cases.
Properties:
o A point remains the same even if all its homogeneous coordinates are
multiplied by a non-zero constant. (cX, cY, cZ, cW) represents the
same point as (X, Y, Z, W) for c ≠ 0.
o This allows for flexibility in scaling the coordinates without affecting
the actual position of the point.
Benefits:
o Homogeneous coordinates simplify the representation of certain
transformations like translation, scaling, and rotation. They allow
these transformations to be expressed as matrix multiplications.
o They can efficiently handle points at infinity, which is useful in
computer graphics for representing vanishing points or camera
perspective.
Here are the homogeneous transformation matrices for translation, scaling, and
rotation:
1. Translation:
A translation matrix (T) allows you to move a point by a specific distance in the
X and Y directions (or X, Y, and Z in 3D).
| 1 0 Tx |
| 0 1 Ty | (where Tx and Ty are the translation
values)
| 0 0 1 |
2. Scaling:
3. Rotation:
A rotation matrix (R) allows you to rotate a point around the origin by an angle θ.
Here, the specific form of the matrix depends on the axis of rotation (X, Y, or Z).
Both flood fill and boundary fill algorithms are used to fill connected regions in a
digital image or array. Here's a breakdown of each:
Concept: Fills all connected pixels of a specific color (seed color) within a
bounded area, replacing them with a new fill color.
Process:
1. User specifies a starting point (seed) within the desired region.
2. The algorithm checks the color of the seed pixel and its surrounding
pixels.
3. If a neighboring pixel has the seed color, it's replaced with the new fill
color, and the algorithm recursively checks its neighbors, repeating
the process until all connected pixels with the seed color are filled.
Example: Imagine a coloring book image with a red bucket. You want to
fill the bucket with blue. You click inside the red area (seed). The flood fill
algorithm replaces all connected red pixels with blue, effectively filling the
bucket.
Concept: Fills all connected pixels within a bounded area, stopping when it
encounters a different color defined as the boundary.
Process:
1. User specifies a starting point (seed) inside the desired region.
2. The algorithm checks the color of the seed pixel and its surrounding
pixels.
3. If a neighboring pixel is not the boundary color and not already filled,
it's replaced with the fill color, and the algorithm recursively checks
its neighbors. This continues until all connected pixels that are not the
boundary color are filled.
Example: Same coloring book image with a red bucket. This time, you
want to fill everything except the red bucket with blue. You click inside a
white area (seed). The boundary fill algorithm replaces all connected white
pixels with blue, stopping when it reaches the red boundary of the bucket.
Flood Fill:
Merits:
o Simpler to implement.
o Efficient for filling regions with a single connected component (no
holes).
Demerits:
o Can be slow for complex images with many connected components.
o Not suitable for filling regions with holes, as it might fill the holes as
well.
Boundary Fill:
Merits:
o More versatile, can handle complex images with holes and multiple
regions.
o Generally faster for complex images.
Demerits:
o Slightly more complex to implement compared to flood fill.
o Requires defining a specific boundary color.
The choice between flood fill and boundary fill depends on the specific image
and desired outcome.
Flood fill is suitable for simpler images with well-defined regions and no
holes.
Boundary fill is preferable for complex images with multiple regions,
holes, or specific boundaries you want to respect.
Explain the z-buffer algorithm for hidden surface removal with a suitable
example.
Concept:
Imagine the scene as if you're looking through a camera. The Z-buffer is a special
memory area that stores the depth (distance from the viewpoint) information for
each pixel on the screen. During rendering, objects are processed one by one.
Process:
Example:
Imagine a scene with a red cube in front of a blue sphere. As the renderer
processes each object:
Benefits:
Line Clipping
Cohen-Sutherland Algorithm
Steps:
1. Region Coding:
o Assign a 4-bit code to each endpoint of the line segment:
Bit 1: Set to 1 if the endpoint is to the left of the viewport (x <
xmin).
Bit 2: Set to 1 if the endpoint is to the right of the viewport (x >
xmax).
Bit 3: Set to 1 if the endpoint is below the viewport (y < ymin).
Bit 4: Set to 1 if the endpoint is above the viewport (y > ymax).
2. Trivial Acceptance/Rejection:
o If both endpoints have a code of 0000 (completely inside the
viewport), the entire line is visible and can be drawn.
o If the bitwise AND operation of the two endpoint codes is non-zero
(both endpoints on the same side of a boundary), the line is
completely outside the viewport and can be discarded.
3. Clipping:
o If neither of the above conditions is met, the algorithm iteratively
clips the line segment against each boundary (left, right, top, bottom)
where an endpoint violates the boundary condition (code bit is 1).
o The clipping process involves calculating the intersection point of the
line segment with the boundary line.
o Only the portion of the line segment that falls within the viewport
boundaries is retained.
Example:
Consider a line segment with endpoints A (10, 20) and B (30, 5). The viewport
boundaries are xmin = 5, xmax = 25, ymin = 0, ymax = 15.
Region Coding:
o A (10, 20): Code = 0100 (left of viewport, above viewport)
o B (30, 5): Code = 1000 (right of viewport, below viewport)
Trivial Check: Neither endpoint code is 0000 (completely inside), and the
bitwise AND (0100 & 1000) is not 0 (endpoints not on opposite sides), so
clipping is necessary.
Clipping Process:
1. Clip against left boundary (x = xmin = 5):
Calculate intersection point (X', Y') based on line equation and
boundary line equation.
Update A to (X', Y'). New code becomes 0000 (now inside).
2. Clip against bottom boundary (y = ymin = 0):
Calculate intersection point (X'', Y'').
Update B to (X'', Y''). New code becomes 0100 (left of
viewport, inside viewport).
Result: The clipped line segment becomes A' (5, 20) to B'' (20, 0), which falls
entirely within the viewport.