CGR All IMP Algorithms Easy to Learn | Computer Graphics IMP Algorithms | Mumbai University
Computer Graphics Quick Level IMP Algorithms Engineering 3rd Semester | CGR All Important Algorithms Easy Level | Mumbai University
1. DDA Line Drawing Algorithm
Step 1: Input the two endpoints of the line.
Let the endpoints be and .
Step 2: Calculate the difference.
Step 3: Determine the number of steps.
Step 4: Calculate the increment for each step.
Step 5: Start drawing the line.
Set initial points:
Step 6: Repeat for the number of steps.
For each step:
- Plot the point:
- Update and :
Step 7: Stop when all points are plotted.
Algorithm in Short Form
- Input and .
- Compute , .
- Find .
- Compute increments:
- Initialize , .
- For to :
- Plot .
- Update .
- End.
Key Points for Full Marks
- Include a small diagram illustrating the line and increments.
- Write clearly in steps with mathematical notation.
- Draw a flowchart if time permits for bonus clarity.
2. Bresenham Line Drawing Algorithm
Step 1: Input the two endpoints of the line.
Let the endpoints be and .
Step 2: Calculate differences.
Step 3: Initialize decision parameter.
Step 4: Initialize starting point.
Set:
Step 5: Plot the first point.
Plot .
Step 6: Repeat for steps.
For each step:
- If :
- Next point is .
- Update .
- If :
- Next point is .
- Update .
- Plot the new point.
Step 7: Stop when the endpoint is reached.
Algorithm in Short Form
- Input and .
- Compute:
- Initialize:
- Plot .
- For each step until :
- If :
- If :
- Plot .
- End.
Key Points for Full Marks
- Mention that this algorithm works best for lines with a slope between and (for other slopes, modify the algorithm).
- Include a small diagram to show how the decision parameter works.
- Write the steps clearly and concisely. Add a flowchart if possible for better clarity.
3. Midpoint Circle Algorithm
Step 1: Input the circle's radius and center coordinates.
Let the radius be and the center be .
Step 2: Initialize starting point.
Set:
Step 3: Calculate the initial decision parameter.
Step 4: Plot the first point.
Plot the points for all 8 symmetric parts:
Step 5: Repeat until .
For each step:
- If :
- .
- If :
- .
- Decrease .
- Increment .
- Plot the new symmetric points.
Step 6: Stop when .
Algorithm in Short Form
- Input radius and center .
- Initialize:
- Plot the initial 8 symmetric points.
- While :
- If :
- If :
- Increment .
- Plot the new 8 symmetric points.
- End.
Key Points for Full Marks
- Explain symmetry: The algorithm reduces computations by plotting 8 points at once.
- Draw a diagram of a circle with symmetric points labeled.
- Write the steps clearly and include a table of calculations (optional).
- Mention that this algorithm avoids floating-point arithmetic, making it efficient.
4. Flood Fill Algorithm
Flood fill is used to fill connected regions of a 2D screen with a given color.
Steps for the Algorithm
- Input:
- Starting pixel coordinates .
- The color to fill,
newColor
. - The color of the region to be replaced,
oldColor
.
- Check the base condition:
If the pixel at is not theoldColor
or it is alreadynewColor
, return.
- Replace the color:
Change the color of the pixel at tonewColor
.
- Recursive filling:
Call the algorithm recursively for all 4-connected pixels:- Left:
- Right:
- Top:
- Bottom:
- Repeat until all connected pixels are filled.
Algorithm in Short Form
- Input ,
oldColor
, andnewColor
. - If the pixel at is not
oldColor
or is alreadynewColor
, return. - Replace the color of the pixel at with
newColor
. - Recursively call the algorithm for:
- End when no more connected pixels of
oldColor
remain.
Pseudo Code
FloodFill(x, y, oldColor, newColor):
if Screen[x][y] ≠ oldColor OR Screen[x][y] = newColor:
return
Screen[x][y] = newColor
FloodFill(x-1, y, oldColor, newColor) // Left
FloodFill(x+1, y, oldColor, newColor) // Right
FloodFill(x, y-1, oldColor, newColor) // Top
FloodFill(x, y+1, oldColor, newColor) // Bottom
Key Points for Full Marks
- Mention that Flood Fill works for 4-connected pixels or 8-connected pixels (if extended).
- Add a small diagram showing an example of how the algorithm fills the region.
- Clearly explain the recursive nature of the algorithm.
- Optionally, mention that iterative implementations can avoid stack overflow for large regions.
5. Boundary Fill Algorithm
Boundary Fill is used to fill a closed region with a specified color, stopping at a defined boundary.
Steps for the Algorithm
- Input:
- Starting pixel coordinates .
- Fill color (
fillColor
). - Boundary color (
boundaryColor
).
- Check the base condition:
If the pixel at is alreadyboundaryColor
orfillColor
, return.
- Replace the color:
Change the color of the pixel at tofillColor
.
- Recursive filling:
Call the algorithm recursively for all 4-connected or 8-connected neighbors:- For 4-connected:
- Left:
- Right:
- Top:
- Bottom:
- For 8-connected, include diagonals:
- Top-left:
- Top-right:
- Bottom-left:
- Bottom-right:
- For 4-connected:
- Repeat until all pixels are filled.
Algorithm in Short Form
- Input ,
fillColor
, andboundaryColor
. - If the pixel at is
boundaryColor
orfillColor
, return. - Replace the pixel at with
fillColor
. - Recursively call the algorithm for neighbors (4-connected or 8-connected):
- , , , (for 4-connected).
- Add diagonal neighbors for 8-connected.
- End when all reachable pixels within the boundary are filled.
Pseudo Code
BoundaryFill(x, y, fillColor, boundaryColor):
if Screen[x][y] = boundaryColor OR Screen[x][y] = fillColor:
return
Screen[x][y] = fillColor
// 4-connected neighbors
BoundaryFill(x-1, y, fillColor, boundaryColor) // Left
BoundaryFill(x+1, y, fillColor, boundaryColor) // Right
BoundaryFill(x, y-1, fillColor, boundaryColor) // Top
BoundaryFill(x, y+1, fillColor, boundaryColor) // Bottom
// Uncomment below for 8-connected neighbors
// BoundaryFill(x-1, y-1, fillColor, boundaryColor) // Top-left
// BoundaryFill(x+1, y-1, fillColor, boundaryColor) // Top-right
// BoundaryFill(x-1, y+1, fillColor, boundaryColor) // Bottom-left
// BoundaryFill(x+1, y+1, fillColor, boundaryColor) // Bottom-right
Key Points for Full Marks
- Explain that Boundary Fill stops when it encounters the
boundaryColor
. - Mention 4-connected vs. 8-connected filling, depending on requirements.
- Draw a small diagram illustrating a region with a boundary and how it is filled step-by-step.
- Note that this algorithm is recursive and may face stack overflow for large areas. Iterative solutions can be used in such cases.
6. Cohen-Sutherland Line Clipping Algorithm
This algorithm clips a line segment to a rectangular clipping window using region codes.
Steps of the Algorithm
Step 1: Define the rectangular clipping window.
Let the boundaries of the window be:
- Left:
- Right:
- Bottom:
- Top:
Step 2: Assign region codes.
Each endpoint of the line segment is assigned a 4-bit region code:
- Bit positions:
- Top: if , otherwise .
- Bottom: if , otherwise .
- Right: if , otherwise .
- Left: if , otherwise .
Step 3: Perform the following tests:
-
Trivial Acceptance:
If both endpoints have a region code of 0000, the line is completely inside the clipping window. Accept the line. -
Trivial Rejection:
If the logical AND of the region codes for both endpoints is not 0000 (non-zero), the line is completely outside the clipping window. Reject the line. -
Partial Clipping:
If the line cannot be trivially accepted or rejected, proceed with clipping:- Select an endpoint outside the window.
- Find the intersection point of the line with one of the window boundaries based on the region code:
- Top boundary:
- Bottom boundary:
- Right boundary:
- Left boundary:
- Replace the endpoint outside the window with the calculated intersection point.
- Recalculate the region codes and repeat the tests.
Algorithm in Short Form
- Input the clipping window and the line endpoints , .
- Compute the region codes for both endpoints.
- If both region codes are 0000, accept the line.
- If the logical AND of the region codes is non-zero, reject the line.
- Otherwise:
- Select an endpoint outside the window.
- Compute the intersection with the appropriate window boundary.
- Update the endpoint and repeat.
- Stop when the line is fully accepted or rejected.
Pseudo Code
CohenSutherlandClip(x1, y1, x2, y2):
Compute region codes for (x1, y1) and (x2, y2)
while True:
if both codes are 0000:
Accept the line and stop
elif logical AND of both codes ≠ 0000:
Reject the line and stop
else:
Choose an endpoint outside the window
Find intersection with window boundary
Replace the endpoint with the intersection point
Recompute region code
Key Points for Full Marks
- Explain the use of region codes (e.g., binary representation for each side).
- Draw a small diagram of the clipping window with region codes labeled.
- Clearly describe the intersection calculations for each boundary.
- Highlight the efficiency of trivial acceptance/rejection.
7. Sutherland-Hodgman Polygon Clipping Algorithm
The algorithm clips a polygon against a rectangular clipping window. It processes the polygon's vertices edge by edge and creates a new clipped polygon.
Steps of the Algorithm
Step 1: Define the clipping window
The clipping window is defined by its boundaries:
- Left:
- Right:
- Bottom:
- Top:
Step 2: Start with the original polygon
- List all vertices of the polygon as a sequence.
Step 3: Clip the polygon edge by edge
For each edge of the clipping window (left, right, bottom, top):
- Take each vertex pair (current vertex and the next vertex) of the polygon.
- Apply the following rules:
- Case 1: Both vertices are inside the clipping boundary
- Add the second vertex to the output list.
- Case 2: First vertex is inside, second vertex is outside
- Add the intersection point with the boundary to the output list.
- Case 3: First vertex is outside, second vertex is inside
- Add the intersection point with the boundary, followed by the second vertex, to the output list.
- Case 4: Both vertices are outside the boundary
- Do not add anything to the output list.
- Case 1: Both vertices are inside the clipping boundary
- Repeat for all edges of the clipping window.
Step 4: Repeat until all boundaries are processed
After processing all edges of the clipping window, the remaining vertices form the clipped polygon.
Algorithm in Short Form
- Input the clipping window boundaries and the polygon vertices.
- For each edge of the clipping window:
- For each vertex pair of the polygon:
- Apply the four cases based on the positions of the vertices relative to the clipping edge.
- Generate the new list of vertices for the clipped polygon.
- For each vertex pair of the polygon:
- After all edges are processed, the final list of vertices forms the clipped polygon.
Pseudo Code
SutherlandHodgmanClip(polygon, clipEdge):
outputList = []
for each vertex pair (currentVertex, nextVertex) in polygon:
if currentVertex is inside AND nextVertex is inside:
Add nextVertex to outputList
elif currentVertex is inside AND nextVertex is outside:
Add intersection point to outputList
elif currentVertex is outside AND nextVertex is inside:
Add intersection point and nextVertex to outputList
else:
Do nothing
return outputList
Repeat this process for each boundary (Left, Right, Bottom, Top).
Key Points for Full Marks
- Clipping Boundaries: Describe how each polygon vertex is tested against the boundaries.
- Intersection Calculation:
- For vertical boundary (e.g., left or right):
- For horizontal boundary (e.g., top or bottom):
- Draw a diagram showing a polygon being clipped step by step against a window.
- Mention that this algorithm preserves the shape of the polygon as much as possible.
- Highlight its use in computer graphics for viewport clipping.
With this guide and Campusify’s resources, you can confidently approach your CGR exam and achieve excellent results. Don’t forget to join our Telegram Channel for Latest Updates.
Happy studying, and good luck!
Campusify – Your Ultimate Study Resource for MSBTE Diploma And Engineering Students!
Keywords:
CGR IMP Algorithms | Mumbai University Most Asked Algorithms | Engineering Important Algorithms | Mumbai University Most Repeated Algorithms | Semester 3 Engineering Algorithm Bank | Engineering CGR Important Algorithms Mumbai University PDF | Computer Engineering Mumbai University Repeated Algorithms | Civil Engineering Mumbai University Repeated Algorithms | Information Technology Mumbai University Repeated Algorithms | Mechanical Engineering Mumbai University IMP Algorithms | AIML Mumbai University IMP Algorithms | 3rd Semester CGR Computer Engineering Mumbai University Repeated Algorithms | DDA line algorithm | Bresanham line | Midpoint circle generation algorithm | Flood fill algorithm | Boundary fil algorithm | Cohen sudherland line clipping algorithm | Sutherland hodgeman polygon clipping algorithm
Join the conversation