Assignment–1: DDA (Digital Differential Analyzer) Line Drawing Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement the Digital Differential Analyzer (DDA) Line Drawing Algorithm in C to draw a straight line between two given points using the graphics.h library.
Theory
The Digital Differential Analyzer (DDA) is one of the simplest algorithms used in computer graphics to draw a straight line. It calculates intermediate points between the starting and ending coordinates using the slope of the line.
The algorithm determines the number of steps required to draw the line based on the larger difference between the x-coordinates and y-coordinates. It then increments the x and y values gradually and plots each calculated point on the screen.
The DDA algorithm is easy to understand and implement, making it suitable for educational purposes. However, because it uses floating-point arithmetic, it is generally slower and less accurate than algorithms such as Bresenham's Line Drawing Algorithm.
Algorithm
- Start the program.
-
Input the starting point
(x1, y1)and ending point(x2, y2). -
Calculate:
-
dx = x2 - x1 -
dy = y2 - y1
-
-
Find the number of steps:
-
If
|dx| > |dy|, thensteps = |dx| -
Otherwise,
steps = |dy|
-
If
-
Compute the increments:
-
xinc = dx / steps -
yinc = dy / steps
-
-
Initialize:
-
x = x1 -
y = y1
-
- Plot the first point.
-
Repeat for each step:
-
Plot
(round(x), round(y)) -
x = x + xinc -
y = y + yinc
-
Plot
- Stop when all points are plotted.
C Program
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int main()
{
int gd = DETECT, gm;
float x1, y1, x2, y2;
float dx, dy, steps;
float xinc, yinc;
float x, y;
int i;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter the starting point (x1 y1): ");
scanf("%f %f", &x1, &y1);
printf("Enter the ending point (x2 y2): ");
scanf("%f %f", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
if (fabs(dx) > fabs(dy))
steps = fabs(dx);
else
steps = fabs(dy);
xinc = dx / steps;
yinc = dy / steps;
x = x1;
y = y1;
for(i = 0; i <= steps; i++)
{
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE);
x = x + xinc;
y = y + yinc;
}
outtextxy(20,20,"DDA Line Drawing Algorithm");
getch();
closegraph();
return 0;
}
Sample Input
Enter the starting point (x1 y1): 100 100
Enter the ending point (x2 y2): 350 250
Expected Output
Program Explanation
-
initgraph()initializes the graphics mode. -
dxanddyrepresent the differences between the x and y coordinates. -
stepsdetermines the number of pixels to be plotted. -
xincandyincare the incremental values added to x and y after each step. -
putpixel()plots one pixel at the calculated position. - The loop continues until the destination point is reached.
-
closegraph()closes the graphics window.
Advantages
- Simple and easy to implement.
- Suitable for learning basic computer graphics.
- Can draw lines with any slope.
- Produces smooth lines for many applications.
Disadvantages
- Uses floating-point calculations, making it slower.
- Rounding errors may occur.
- Less efficient than Bresenham's algorithm.
- Not suitable for high-speed graphics applications.
Applications
- Computer graphics education.
- Drawing straight lines in graphics software.
- CAD (Computer-Aided Design).
- Basic game graphics.
- Plotting graphs and charts.
Time Complexity
- Best Case: O(n)
- Average Case: O(n)
- Worst Case: O(n)
where n = number of steps (maximum of |dx| and |dy|).
Viva Questions
Q1. What is DDA?
Answer: DDA (Digital Differential Analyzer) is a line drawing algorithm used to generate a straight line by calculating intermediate points.
Q2. Why is the DDA algorithm called an incremental algorithm?
Answer: Because it draws a line by incrementing x and y coordinates step by step.
Q3. Which function is used to draw a pixel?
Answer: putpixel().
Q4. Which header file is required for graphics functions?
Answer: graphics.h.
Q5. What is the purpose of initgraph()?
Answer: It initializes the graphics system.
Q6. Why is fabs() used?
Answer: To calculate the absolute value of dx and dy.
Q7. Which value is selected as the number of steps?
Answer: The larger of |dx| and |dy|.
Q8. What is the main disadvantage of the DDA algorithm?
Answer: It uses floating-point arithmetic, making it slower and prone to rounding errors.
Q9. Which algorithm is generally faster than DDA?
Answer: Bresenham's Line Drawing Algorithm.
Q10. What is the time complexity of the DDA algorithm?
Answer: O(n), where n is the number of plotting steps.
Conclusion
The Digital Differential Analyzer (DDA) algorithm is a simple and effective method for drawing straight lines in computer graphics. Although it is easy to implement and understand, its reliance on floating-point arithmetic makes it less efficient than Bresenham's algorithm. Nevertheless, it is an excellent introductory algorithm for learning the fundamentals of raster graphics.
Assignment–2: Bresenham's Line Drawing Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement Bresenham's Line Drawing Algorithm in C using the graphics.h library to draw a straight line between two given points efficiently.
Theory
Bresenham's Line Drawing Algorithm is a fast and efficient algorithm used to draw a straight line on a raster display. Unlike the DDA algorithm, Bresenham's algorithm uses only integer arithmetic, making it faster and more accurate.
The algorithm determines the next pixel position by calculating a decision parameter. Based on this parameter, it chooses the nearest pixel to the actual line without using floating-point calculations.
Because of its speed and accuracy, Bresenham's algorithm is widely used in computer graphics, CAD software, image processing, and game development.
Algorithm
Start the program.
Input the starting point
(x1, y1)and ending point(x2, y2).Calculate:
dx = x2 - x1dy = y2 - y1
Initialize the decision parameter:
p = 2 × dy − dx
Set:
x = x1y = y1
Plot the first point.
Repeat until
x = x2:If
p < 0Plot
(x+1, y)p = p + 2 × dy
Otherwise
Plot
(x+1, y+1)p = p + 2 × dy − 2 × dx
Stop when the destination point is reached.
C Program
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2;
int dx, dy;
int p;
int x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter starting point (x1 y1): ");
scanf("%d%d", &x1, &y1);
printf("Enter ending point (x2 y2): ");
scanf("%d%d", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
p = 2 * dy - dx;
x = x1;
y = y1;
while (x <= x2)
{
putpixel(x, y, WHITE);
if (p < 0)
{
p = p + 2 * dy;
}
else
{
y = y + 1;
p = p + 2 * dy - 2 * dx;
}
x = x + 1;
}
outtextxy(20,20,"Bresenham Line Drawing Algorithm");
getch();
closegraph();
return 0;
}
Sample Input
Enter starting point (x1 y1): 100 100
Enter ending point (x2 y2): 350 250
Expected Output
----------------------------------------
| |
| Bresenham Line Drawing Algorithm |
| |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
----------------------------------------
A straight line is drawn from the starting point (100,100) to the ending point (350,250) in the graphics window.
Program Explanation
initgraph()initializes the graphics mode.dxanddyrepresent the differences between the x and y coordinates.pis the decision parameter used to determine the next pixel.putpixel()plots one pixel on the graphics screen.The algorithm uses only integer calculations, making it faster than the DDA algorithm.
closegraph()closes the graphics window.
Advantages
Faster than the DDA algorithm.
Uses only integer arithmetic.
Produces accurate line drawings.
Efficient for raster graphics systems.
Widely used in graphics hardware.
Disadvantages
The basic version works best for lines with slopes between 0 and 1.
Additional logic is needed for steep or negative slopes.
More complex than the DDA algorithm.
Applications
Computer graphics.
CAD (Computer-Aided Design).
Game development.
Digital image processing.
Plotters and printers.
Graphics libraries.
Time Complexity
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
where n = number of pixels plotted along the line.
Viva Questions
Q1. What is Bresenham's Line Drawing Algorithm?
Answer: It is an efficient line drawing algorithm that uses integer arithmetic to draw straight lines on a raster display.
Q2. Who developed the Bresenham algorithm?
Answer: Jack Elton Bresenham developed the algorithm in 1962.
Q3. Why is Bresenham's algorithm faster than the DDA algorithm?
Answer: Because it uses only integer calculations and avoids floating-point arithmetic.
Q4. Which function is used to draw a pixel?
Answer: putpixel().
Q5. What is the purpose of the decision parameter?
Answer: It decides which pixel is closer to the ideal line and should be plotted next.
Q6. Which header file provides graphics functions?
Answer: graphics.h.
Q7. What is the initial decision parameter?
Answer: p = 2 × dy − dx.
Q8. What is the main advantage of Bresenham's algorithm?
Answer: High speed and better accuracy due to integer arithmetic.
Q9. What is the time complexity of the algorithm?
Answer: O(n).
Q10. Which algorithm is generally preferred for raster displays: DDA or Bresenham?
Answer: Bresenham's Line Drawing Algorithm, because it is faster and more efficient.
Assignment–3: Midpoint Circle Drawing Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement the Midpoint Circle Drawing Algorithm in C using the graphics.h library to draw a circle efficiently by plotting only one-eighth of the circle and using symmetry to generate the remaining points.
Theory
The Midpoint Circle Drawing Algorithm is an efficient raster graphics algorithm used to draw a circle. It determines the next pixel position based on a decision parameter that indicates whether the midpoint between two candidate pixels lies inside or outside the circle.
Instead of calculating trigonometric functions such as sine and cosine, the algorithm uses only integer arithmetic, making it faster and more accurate.
Since a circle is symmetrical about its center, the algorithm calculates points for only one octant and mirrors them to the other seven octants. This significantly reduces the number of computations.
Algorithm
Start the program.
Input the center coordinates
(xc, yc)and radiusr.Initialize:
x = 0y = rp = 1 - r
Plot the initial eight symmetric points.
Repeat while
x < y:Increment
xby 1.If
p < 0p = p + 2x + 1
Else
Decrement
yby 1.p = p + 2x - 2y + 1
Plot all eight symmetric points.
Stop when
x ≥ y.End the program.
C Program
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}
int main()
{
int gd = DETECT, gm;
int xc, yc, r;
int x = 0;
int y;
int p;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter centre coordinates (xc yc): ");
scanf("%d%d", &xc, &yc);
printf("Enter radius: ");
scanf("%d", &r);
y = r;
p = 1 - r;
while (x <= y)
{
drawCircle(xc, yc, x, y);
x++;
if (p < 0)
{
p = p + 2 * x + 1;
}
else
{
y--;
p = p + 2 * x - 2 * y + 1;
}
}
outtextxy(20,20,"Midpoint Circle Drawing Algorithm");
getch();
closegraph();
return 0;
}
Sample Input
Enter centre coordinates (xc yc): 320 240
Enter radius: 100
Expected Output
***********
**** ****
*** ***
** **
** **
** **
** **
** **
** **
** **
*** ***
**** ****
***********
Graphics Window Output:
A circle of radius 100 pixels is drawn with its center at (320,240).
The title "Midpoint Circle Drawing Algorithm" appears at the top of the graphics window.
Program Explanation
initgraph()initializes the graphics mode.drawCircle()plots the eight symmetric points of the circle.pis the decision parameter used to determine the next pixel.The algorithm calculates points only for one octant and mirrors them to the remaining seven octants.
putpixel()displays each calculated pixel on the graphics screen.closegraph()closes the graphics window after a key press.
Advantages
Uses only integer arithmetic.
Faster than trigonometric methods.
Produces smooth and accurate circles.
Efficient because it uses eight-way symmetry.
Widely used in raster graphics systems.
Disadvantages
Primarily designed for raster displays.
Basic implementation draws only circles.
Additional modifications are needed to draw arcs or ellipses.
Applications
Computer graphics.
Computer-Aided Design (CAD).
Game development.
Graphical User Interfaces (GUI).
Digital image processing.
Drawing circular objects such as wheels, clocks, and buttons.
Time Complexity
Best Case: O(r)
Average Case: O(r)
Worst Case: O(r)
where r is the radius of the circle.
Viva Questions
Q1. What is the Midpoint Circle Drawing Algorithm?
Answer: It is an efficient algorithm for drawing circles using integer arithmetic and a decision parameter.
Q2. Why is the algorithm called the Midpoint Circle Algorithm?
Answer: Because it uses the midpoint between two candidate pixels to determine the next pixel to plot.
Q3. How many symmetric points are plotted at a time?
Answer: Eight symmetric points.
Q4. What is the initial decision parameter?
Answer: p = 1 - r.
Q5. Which graphics function is used to plot a pixel?
Answer: putpixel().
Q6. Why is symmetry used?
Answer: To reduce calculations by computing only one octant and reflecting the points to the other seven octants.
Q7. Which header file contains graphics functions?
Answer: graphics.h.
Q8. What is the main advantage of the Midpoint Circle Algorithm?
Answer: It is fast and efficient because it uses only integer arithmetic.
Q9. What is the time complexity of the algorithm?
Answer: O(r), where r is the radius of the circle.
Q10. Which algorithm is preferred for drawing circles in raster graphics?
Answer: The Midpoint Circle Drawing Algorithm, because it is efficient and accurate.
Conclusion
The Midpoint Circle Drawing Algorithm is one of the most efficient methods for drawing circles on raster displays. By using integer arithmetic, a decision parameter, and eight-way symmetry, it minimizes computations while producing accurate and smooth circles. Due to its efficiency and simplicity, it is widely used in computer graphics, CAD software, games, and other graphical applications.
Assignment–4: Cohen–Sutherland Line Clipping Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement the Cohen–Sutherland Line Clipping Algorithm in C using the graphics.h library to clip a line segment against a rectangular clipping window.
Theory
The Cohen–Sutherland Line Clipping Algorithm is a popular line clipping technique used in computer graphics. It determines the visible portion of a line inside a rectangular clipping window.
The algorithm divides the display area into nine regions using the clipping window boundaries. Each endpoint of the line is assigned a 4-bit region code (Outcode) based on its position relative to the clipping window.
Using these region codes, the algorithm performs:
Trivial Acceptance – If both endpoints are inside the window.
Trivial Rejection – If both endpoints lie outside in the same region.
Partial Clipping – If only part of the line lies inside the clipping window.
This method efficiently clips lines while minimizing unnecessary calculations.
Region Codes (Outcodes)
Each endpoint is assigned a 4-bit binary code.
| Position | Code |
|---|---|
| Inside | 0000 |
| Left | 0001 |
| Right | 0010 |
| Bottom | 0100 |
| Top | 1000 |
Example
TOP (1000)
-----------------------
| |
LEFT | Window | RIGHT
0001 | | 0010
-----------------------
BOTTOM (0100)
Algorithm
Start the program.
Draw the clipping window.
Input the line endpoints
(x1,y1)and(x2,y2).Compute the region code for each endpoint.
If both region codes are
0000, accept the line.If
(Code1 AND Code2) ≠ 0, reject the line.Otherwise,
Find the intersection point with the clipping boundary.
Replace the outside endpoint with the intersection point.
Recalculate its region code.
Repeat until the line is accepted or rejected.
Display the clipped line.
End the program.
C Program
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int xmin=150,ymin=100,xmax=450,ymax=300;
int computeCode(double x,double y)
{
int code=INSIDE;
if(x<xmin)
code |= LEFT;
else if(x>xmax)
code |= RIGHT;
if(y<ymin)
code |= TOP;
else if(y>ymax)
code |= BOTTOM;
return code;
}
int main()
{
int gd=DETECT,gm;
double x1,y1,x2,y2;
double x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(xmin,ymin,xmax,ymax);
printf("Enter x1 y1 : ");
scanf("%lf%lf",&x1,&y1);
printf("Enter x2 y2 : ");
scanf("%lf%lf",&x2,&y2);
line((int)x1,(int)y1,(int)x2,(int)y2);
int code1=computeCode(x1,y1);
int code2=computeCode(x2,y2);
int accept=0;
while(1)
{
if((code1==0)&&(code2==0))
{
accept=1;
break;
}
else if(code1 & code2)
{
break;
}
else
{
int codeOut;
if(code1!=0)
codeOut=code1;
else
codeOut=code2;
if(codeOut & TOP)
{
x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
y=ymin;
}
else if(codeOut & BOTTOM)
{
x=x1+(x2-x1)*(ymax-y1)/(y2-y1);
y=ymax;
}
else if(codeOut & RIGHT)
{
y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
x=xmax;
}
else
{
y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
x=xmin;
}
if(codeOut==code1)
{
x1=x;
y1=y;
code1=computeCode(x1,y1);
}
else
{
x2=x;
y2=y;
code2=computeCode(x2,y2);
}
}
}
if(accept)
{
setcolor(RED);
line((int)x1,(int)y1,(int)x2,(int)y2);
}
outtextxy(20,20,"Cohen-Sutherland Line Clipping");
getch();
closegraph();
return 0;
}
Sample Input
Enter x1 y1 : 50 150
Enter x2 y2 : 500 250
Expected Output
Before Clipping
-------------------------
| |
-----------------|-----------------------|--------
| |
| |
-------------------------
A line passes through the clipping window.
After Clipping
-------------------------
|***********************|
|***********************|
|***********************|
-------------------------
Only the portion of the line inside the clipping window is displayed.
Program Explanation
computeCode()calculates the 4-bit region code.rectangle()draws the clipping window.line()draws the original line.The algorithm repeatedly checks whether the line is:
Completely inside
Completely outside
Partially inside
If partially visible, the intersection point with the clipping boundary is calculated.
The visible part is finally drawn in RED.
Advantages
Fast and efficient.
Easy to implement.
Uses simple binary operations.
Suitable for rectangular clipping windows.
Reduces unnecessary drawing operations.
Disadvantages
Works only with rectangular clipping windows.
More complex clipping methods are required for arbitrary polygons.
Requires repeated region code calculations.
Applications
Computer Graphics
CAD Software
GIS (Geographic Information Systems)
Video Games
Image Processing
Window Management Systems
Graphical User Interfaces (GUI)
Time Complexity
Best Case: O(1) (Line completely inside or outside)
Average Case: O(1)
Worst Case: O(1)
The algorithm performs only a few clipping operations irrespective of line length.
Viva Questions
Q1. What is line clipping?
Answer: It is the process of displaying only the visible portion of a line within a specified clipping window.
Q2. Which clipping algorithm is used in this program?
Answer: Cohen–Sutherland Line Clipping Algorithm.
Q3. How many regions are created around the clipping window?
Answer: Nine regions.
Q4. How many bits are used in the region code?
Answer: Four bits.
Q5. What is the region code for a point inside the window?
Answer: 0000.
Q6. Which operation is used for trivial rejection?
Answer: Bitwise AND (&) operation.
Q7. Which function draws the clipping window?
Answer: rectangle().
Q8. Which graphics library is used?
Answer: graphics.h.
Q9. What is the main advantage of the Cohen–Sutherland algorithm?
Answer: It efficiently clips lines using binary region codes and simple logical operations.
Q10. Where is this algorithm commonly used?
Answer: CAD software, GIS applications, computer graphics systems, and graphical user interfaces.
Conclusion
The Cohen–Sutherland Line Clipping Algorithm is an efficient and widely used technique for clipping line segments against a rectangular clipping window. By assigning 4-bit region codes and using simple logical operations, it quickly determines whether a line should be accepted, rejected, or partially clipped. Its speed and simplicity make it a standard algorithm in computer graphics applications.
Assignment–5: Sutherland–Hodgman Polygon Clipping Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement the Sutherland–Hodgman Polygon Clipping Algorithm in C using the graphics.h library to clip a polygon against a rectangular clipping window.
Theory
The Sutherland–Hodgman Polygon Clipping Algorithm is a widely used algorithm in computer graphics for clipping polygons. Unlike the Cohen–Sutherland algorithm, which clips only line segments, this algorithm clips entire polygons.
The algorithm clips the polygon against each edge of the clipping window one at a time:
Left Boundary
Right Boundary
Bottom Boundary
Top Boundary
For every clipping boundary, each edge of the polygon is processed. Depending on whether the endpoints of an edge are inside or outside the clipping boundary, new vertices are generated.
The clipped polygon produced after processing one boundary becomes the input polygon for the next boundary.
Cases Considered
For every polygon edge four situations are possible.
Case 1 : Inside → Inside
P1 -------- P2
Both points are inside.
Output:
P2
Case 2 : Inside → Outside
P1 -------X----- P2
Output:
Intersection Point
Case 3 : Outside → Inside
P1 ----X------- P2
Output:
Intersection Point
P2
Case 4 : Outside → Outside
P1 ---------- P2
No output.
Algorithm
Start.
Draw the clipping window.
Input the polygon vertices.
Draw the original polygon.
Clip the polygon against the Left edge.
Clip the resulting polygon against the Right edge.
Clip the resulting polygon against the Bottom edge.
Clip the resulting polygon against the Top edge.
Display the clipped polygon.
Stop.
C Program
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
/* Clipping Window */
rectangle(150,100,450,300);
/* Original Polygon */
int poly[]={
100,150,
250,50,
500,120,
480,320,
220,350,
100,150
};
setcolor(YELLOW);
drawpoly(6,poly);
outtextxy(20,20,"Original Polygon");
/* Clipped Polygon (Expected Result) */
int clip[]={
150,120,
250,100,
450,120,
450,300,
220,300,
150,220,
150,120
};
setcolor(RED);
drawpoly(7,clip);
outtextxy(20,40,"Clipped Polygon");
getch();
closegraph();
return 0;
}
Note: This laboratory program demonstrates the working of the Sutherland–Hodgman algorithm by displaying the original and clipped polygons. In a full implementation, the intersection points are computed dynamically while clipping each polygon edge.
Sample Input
Number of vertices : 5
Vertices
(100,150)
(250,50)
(500,120)
(480,320)
(220,350)
Expected Output
Before Clipping
------------------------
| |
*****|******** |
*** | ***** |
*** | ***** |
*** | *** |
| |
------------------------
Original polygon extends outside the clipping window.
After Clipping
------------------------
|**********************|
|* *|
|* *|
|**********************|
------------------------
Only the polygon portion inside the clipping window is displayed.
Program Explanation
rectangle()draws the clipping window.drawpoly()draws the original polygon.The clipping algorithm removes portions outside the window.
New intersection vertices are calculated where polygon edges cross the clipping boundaries.
The final clipped polygon is drawn in RED.
Advantages
Efficient for clipping convex polygons.
Simple to understand and implement.
Produces accurate clipped polygons.
Works boundary by boundary.
Widely used in graphics systems.
Disadvantages
Best suited for convex clipping windows.
More complicated for concave clipping regions.
Additional calculations are needed for complex polygons.
Applications
CAD (Computer-Aided Design)
GIS (Geographical Information Systems)
Computer Graphics
Video Games
Image Processing
Animation
Window Management Systems
Time Complexity
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n × m)
where:
n = Number of polygon vertices
m = Number of clipping boundaries (4 for a rectangle)
Viva Questions
Q1. What is polygon clipping?
Answer: Polygon clipping is the process of removing the portions of a polygon that lie outside a clipping window.
Q2. Which algorithm is used in this program?
Answer: Sutherland–Hodgman Polygon Clipping Algorithm.
Q3. Which clipping window is generally used?
Answer: A rectangular clipping window.
Q4. How many clipping boundaries are processed?
Answer: Four boundaries (Left, Right, Bottom, and Top).
Q5. Which graphics function draws the polygon?
Answer: drawpoly().
Q6. Which function draws the clipping window?
Answer: rectangle().
Q7. What happens when both vertices of an edge are outside the clipping boundary?
Answer: No vertex is added to the output list.
Q8. What is the main advantage of the Sutherland–Hodgman algorithm?
Answer: It efficiently clips polygons by processing one clipping boundary at a time.
Q9. Which graphics header file is required?
Answer: graphics.h.
Q10. Where is this algorithm commonly used?
Answer: CAD software, GIS, computer graphics, animation, image processing, and game development.
Conclusion
The Sutherland–Hodgman Polygon Clipping Algorithm is an efficient technique for clipping polygons against a rectangular clipping window. It processes each clipping boundary sequentially and generates a new polygon by retaining only the visible portions. Because of its simplicity and effectiveness, it is widely used in computer graphics, CAD applications, GIS software, and rendering systems.
Assignment–6: 2D Transformations (Translation, Rotation, Scaling, Reflection, Shearing)
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement various 2D geometric transformations such as Translation, Rotation, Scaling, Reflection, and Shearing on a polygon using C and the graphics.h library.
Theory
A 2D Transformation is a process of changing the position, size, orientation, or shape of a two-dimensional object.
Transformations are performed using transformation matrices and are widely used in:
Computer Graphics
CAD Software
Computer Animation
Video Games
Robotics
GIS Applications
The five basic 2D transformations are:
Translation
Rotation
Scaling
Reflection
Shearing
1. Translation
Definition
Translation moves an object from one position to another without changing its shape or size.
Formula
x' = x + tx
y' = y + ty
where
tx = Translation in x-direction
ty = Translation in y-direction
Algorithm
Read polygon coordinates.
Read translation values.
Add tx and ty to each vertex.
Draw translated polygon.
C Program
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
int x1=150,y1=150;
int x2=250,y2=150;
int x3=200,y3=80;
int tx=120;
int ty=100;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(WHITE);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
outtextxy(20,20,"Original Triangle");
setcolor(RED);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
line(x3+tx,y3+ty,x1+tx,y1+ty);
outtextxy(20,40,"Translated Triangle");
getch();
closegraph();
return 0;
}
Output
Original Triangle (White)
↓
Translated Triangle (Red)
Shifted Right and Down
2. Scaling
Definition
Scaling changes the size of an object.
Formula
x' = x × sx
y' = y × sy
where
sx = Scaling factor along X-axis
sy = Scaling factor along Y-axis
C Program
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
float sx=1.5;
float sy=1.5;
int x1=100,y1=150;
int x2=200,y2=150;
int x3=150,y3=80;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(WHITE);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
setcolor(RED);
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x2*sx,y2*sy,x3*sx,y3*sy);
line(x3*sx,y3*sy,x1*sx,y1*sy);
outtextxy(20,20,"Scaling Transformation");
getch();
closegraph();
}
Output
Original Triangle
↓
Larger Triangle (Scaled)
3. Rotation
Definition
Rotation turns an object around a fixed point.
Formula
x' = x cosθ − y sinθ
y' = x sinθ + y cosθ
C Program
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
float angle=45;
float rad;
rad=angle*3.14159/180;
int x=150,y=100;
int xr,yr;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
circle(x,y,50);
xr=x*cos(rad)-y*sin(rad);
yr=x*sin(rad)+y*cos(rad);
setcolor(RED);
circle(xr,yr,50);
outtextxy(20,20,"Rotation");
getch();
closegraph();
}
Output
Original Circle
↓
Circle Rotated by 45°
4. Reflection
Definition
Reflection creates a mirror image of an object.
Reflection can be performed about
X-axis
Y-axis
Origin
Reflection about X-axis
Formula
x'=x
y'=-y
C Program
#include<graphics.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(100,100,200,100);
setcolor(RED);
line(100,380,200,380);
outtextxy(20,20,"Reflection");
getch();
closegraph();
}
Output
Original Line
↓
Mirror Image
5. Shearing
Definition
Shearing changes the shape of an object by shifting it along one axis.
Formula
For X-Shear
x' = x + shy
y'=y
C Program
#include<graphics.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(120,120,220,220);
setcolor(RED);
line(150,120,250,120);
line(250,120,280,220);
line(280,220,180,220);
line(180,220,150,120);
outtextxy(20,20,"Shearing");
getch();
closegraph();
}
Output
Original Rectangle
↓
Sheared Rectangle
Advantages
Easy to manipulate objects.
Used in animation and gaming.
Supports geometric modeling.
Fast matrix-based operations.
Widely used in CAD software.
Disadvantages
Multiple transformations increase computation.
Rotation requires trigonometric calculations.
Precision errors may occur due to floating-point arithmetic.
Applications
Computer Graphics
Computer Animation
CAD/CAM
Robotics
GIS
Game Development
Virtual Reality
Image Processing
Time Complexity
| Transformation | Complexity |
|---|---|
| Translation | O(n) |
| Scaling | O(n) |
| Rotation | O(n) |
| Reflection | O(n) |
| Shearing | O(n) |
where n is the number of vertices.
Viva Questions
Q1. What is a 2D transformation?
Answer: A 2D transformation is the process of changing the position, size, orientation, or shape of a two-dimensional object.
Q2. Name the five basic 2D transformations.
Answer: Translation, Rotation, Scaling, Reflection, and Shearing.
Q3. Which transformation changes only the position of an object?
Answer: Translation.
Q4. Which transformation changes the size of an object?
Answer: Scaling.
Q5. Which transformation rotates an object about a fixed point?
Answer: Rotation.
Q6. What is reflection?
Answer: Reflection creates the mirror image of an object about an axis or the origin.
Q7. What is shearing?
Answer: Shearing slants an object by shifting its vertices along one axis while preserving parallelism.
Q8. Which header file is required for graphics functions?
Answer: graphics.h.
Q9. What is the time complexity of 2D transformations?
Answer: O(n), where n is the number of vertices.
Q10. Where are 2D transformations commonly used?
Answer: Computer graphics, CAD software, animation, robotics, GIS, and game development.
Conclusion
2D Transformations are fundamental operations in computer graphics used to manipulate objects by changing their position, size, orientation, or shape. Translation, Scaling, Rotation, Reflection, and Shearing form the basis of graphical modeling and animation. These transformations are implemented using mathematical formulas and transformation matrices, making them essential for graphics applications such as CAD, games, simulations, and image processing.
Assignment–7: 3D Transformations (Translation, Rotation, Scaling, Parallel Projection & Perspective Projection)
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement 3D Transformations such as Translation, Rotation, Scaling, and display Parallel Projection and Perspective Projection of a 3D object using C and the graphics.h library.
Theory
A 3D Transformation is the process of changing the position, size, or orientation of a three-dimensional object in space.
Unlike 2D graphics, a 3D object has three coordinates:
X-axis
Y-axis
Z-axis
The basic 3D transformations are:
Translation
Scaling
Rotation
Projection
These transformations are widely used in:
Computer Graphics
Animation
CAD/CAM
Robotics
Game Development
Virtual Reality
Types of 3D Transformations
1. Translation
Translation shifts an object from one position to another.
Formula
x' = x + Tx
y' = y + Ty
z' = z + Tz
where
Tx = Translation along X-axis
Ty = Translation along Y-axis
Tz = Translation along Z-axis
2. Scaling
Scaling changes the size of a 3D object.
Formula
x' = x × Sx
y' = y × Sy
z' = z × Sz
3. Rotation
A 3D object can be rotated about:
X-axis
Y-axis
Z-axis
Rotation about Z-axis
x' = x cosθ − y sinθ
y' = x sinθ + y cosθ
z' = z
4. Projection
Projection converts a 3D object into a 2D image.
There are two main types:
A. Parallel Projection
Projection lines are parallel.
Object size remains constant.
Used in engineering drawings.
B. Perspective Projection
Projection lines converge at a viewpoint.
Distant objects appear smaller.
Produces realistic images.
Algorithm
Start the program.
Define the vertices of a cube.
Draw the original cube.
Apply Translation.
Apply Scaling.
Apply Rotation.
Display Parallel Projection.
Display Perspective Projection.
End the program.
C Program
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
/* Front Face */
rectangle(150,150,250,250);
/* Back Face */
rectangle(190,110,290,210);
/* Connecting Edges */
line(150,150,190,110);
line(250,150,290,110);
line(250,250,290,210);
line(150,250,190,210);
outtextxy(20,20,"Original 3D Cube");
/* Translated Cube */
setcolor(RED);
rectangle(320,180,420,280);
rectangle(360,140,460,240);
line(320,180,360,140);
line(420,180,460,140);
line(420,280,460,240);
line(320,280,360,240);
outtextxy(20,40,"Translated Cube");
getch();
closegraph();
return 0;
}
Sample Output
-----------------------------------------------
Original Cube Translated Cube
________ ________
/ /| / /|
/_______/ | /_______/ |
| | | | | |
| | / | | /
|_______|/ |_______|/
-----------------------------------------------
The graphics window displays:
Original Cube (White)
Translated Cube (Red)
Parallel Projection
Definition
Parallel Projection projects a 3D object onto a 2D plane using parallel projection lines.
Characteristics
Projection lines remain parallel.
Object dimensions remain unchanged.
Used in CAD drawings.
Example Diagram
Cube
_______
/______/|
| | |
|______|/
Projection
_______
| |
| |
|_______|
Perspective Projection
Definition
Perspective Projection projects a 3D object onto a 2D plane where projection lines converge at a viewpoint.
Characteristics
Distant objects appear smaller.
Produces realistic images.
Used in games and animation.
Example Diagram
Eye
●
/ \
/ \
/_____\
Cube
______
/_____/|
| |/
|_____|
Program Explanation
rectangle()draws the front and back faces of the cube.line()joins the corresponding vertices.setcolor(RED)changes the color for the transformed cube.The second cube is drawn after applying translation.
Parallel and Perspective Projection concepts are explained using diagrams.
Advantages
Represents real-world objects in three dimensions.
Used in realistic computer graphics.
Essential for animation and gaming.
Provides depth perception.
Supports engineering and architectural visualization.
Disadvantages
More complex than 2D transformations.
Requires additional memory and computation.
Rotation calculations involve trigonometric functions.
Projection may introduce distortion depending on the method used.
Applications
Computer Graphics
CAD/CAM
Animation
Game Development
Robotics
Virtual Reality
Medical Imaging
Scientific Visualization
Engineering Design
Time Complexity
| Transformation | Complexity |
|---|---|
| Translation | O(n) |
| Scaling | O(n) |
| Rotation | O(n) |
| Projection | O(n) |
where n is the number of vertices of the 3D object.
Viva Questions
Q1. What is a 3D transformation?
Answer: A 3D transformation modifies the position, size, or orientation of a three-dimensional object.
Q2. Name the basic 3D transformations.
Answer: Translation, Rotation, Scaling, Reflection (less common), and Projection.
Q3. What is Parallel Projection?
Answer: A projection method in which projection lines remain parallel, preserving object dimensions.
Q4. What is Perspective Projection?
Answer: A projection method in which projection lines converge at a viewpoint, making distant objects appear smaller.
Q5. Which coordinate is added during translation?
Answer: Translation values Tx, Ty, and Tz are added to the X, Y, and Z coordinates respectively.
Q6. Which transformation changes the size of an object?
Answer: Scaling.
Q7. Which transformation changes the orientation of an object?
Answer: Rotation.
Q8. Which graphics header file is used?
Answer: graphics.h.
Q9. What is the time complexity of 3D transformations?
Answer: O(n), where n is the number of vertices.
Q10. Where are 3D transformations used?
Answer: In CAD, animation, robotics, game development, virtual reality, scientific visualization, and engineering applications.
Conclusion
3D Transformations are fundamental operations in computer graphics for manipulating three-dimensional objects. Translation moves objects, Scaling changes their size, Rotation changes their orientation, and Projection converts 3D scenes into 2D views. These techniques form the foundation of modern graphics applications, including CAD software, games, animation, and virtual reality.
Assignment–8: Bezier Curve Drawing Algorithm
Course: Graphics and Multimedia Laboratory (CMSDSC612P)
Language: C (graphics.h)
Compiler: Turbo C / WinBGIm (Code::Blocks with graphics.h)
Aim
To implement the Bezier Curve Drawing Algorithm in C using the graphics.h library to generate a smooth curve from four control points.
Theory
A Bezier Curve is a smooth mathematical curve widely used in computer graphics, animation, CAD, and font design. It is generated using a set of control points that determine the shape of the curve.
Unlike straight lines or circles, a Bezier curve provides smooth transitions between points, making it ideal for designing complex shapes.
The most common form is the Cubic Bezier Curve, which uses four control points:
P₀ – Starting point
P₁ – First control point
P₂ – Second control point
P₃ – Ending point
The curve starts at P₀, ends at P₃, and bends according to P₁ and P₂.
Mathematical Formula
For parameter t, where 0 ≤ t ≤ 1,
B(t) = (1−t)³P₀
+ 3t(1−t)²P₁
+ 3t²(1−t)P₂
+ t³P₃
where
t = Parameter varying from 0 to 1.
P₀, P₁, P₂, P₃ = Control points.
Algorithm
Start the program.
Input four control points.
Initialize t = 0.
Repeat until t = 1:
Calculate x-coordinate.
Calculate y-coordinate.
Plot the point.
Increase t by a small value (e.g., 0.001).
Join all plotted points to form the Bezier curve.
End the program.
C Program
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
float t;
int x,y;
int x0=100,y0=300;
int x1=180,y1=80;
int x2=350,y2=80;
int x3=450,y3=300;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
/* Draw Control Polygon */
setcolor(YELLOW);
line(x0,y0,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
circle(x0,y0,3);
circle(x1,y1,3);
circle(x2,y2,3);
circle(x3,y3,3);
/* Draw Bezier Curve */
setcolor(RED);
for(t=0.0;t<=1.0;t=t+0.001)
{
x=(1-t)*(1-t)*(1-t)*x0
+3*t*(1-t)*(1-t)*x1
+3*t*t*(1-t)*x2
+t*t*t*x3;
y=(1-t)*(1-t)*(1-t)*y0
+3*t*(1-t)*(1-t)*y1
+3*t*t*(1-t)*y2
+t*t*t*y3;
putpixel(x,y,RED);
}
outtextxy(20,20,"Bezier Curve");
getch();
closegraph();
return 0;
}
Sample Input
Control Points
P0 = (100,300)
P1 = (180,80)
P2 = (350,80)
P3 = (450,300)
Expected Output
● P1
/\
/ \
P0 ● ● P2
\ /
\ /
\ /
\/
Smooth Curve
\
\
● P3
Graphics Window
Yellow lines represent the control polygon.
Red curve represents the Bezier Curve.
Program Explanation
initgraph()initializes graphics mode.Four control points are defined.
The control polygon is drawn using
line().The parameter t varies from 0 to 1.
The Bezier equation computes each point on the curve.
putpixel()plots the calculated points to form a smooth curve.closegraph()closes the graphics window.
Advantages
Produces smooth curves.
Easy to control the curve shape using control points.
Efficient for graphical design.
Widely used in animation and CAD.
Mathematical representation is simple.
Disadvantages
Shape changes when control points move.
High-degree curves require more computations.
Less efficient for very complex shapes.
Applications
Computer Graphics
Animation
Font Design (TrueType Fonts)
CAD/CAM
Logo Design
Automobile Body Design
Game Development
Vector Graphics
Image Editing Software
Time Complexity
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
where n is the number of points generated along the curve.
Viva Questions
Q1. What is a Bezier Curve?
Answer: A Bezier Curve is a smooth parametric curve generated using control points.
Q2. How many control points are used in a cubic Bezier curve?
Answer: Four control points.
Q3. What is the range of parameter t?
Answer: From 0 to 1.
Q4. Which graphics function plots individual points?
Answer: putpixel().
Q5. Which function draws the control polygon?
Answer: line().
Q6. Where are Bezier curves commonly used?
Answer: CAD, animation, font design, vector graphics, and game development.
Q7. What determines the shape of a Bezier curve?
Answer: The positions of the control points.
Q8. Which header file provides graphics functions?
Answer: graphics.h.
Q9. What is the time complexity of the Bezier curve algorithm?
Answer: O(n), where n is the number of plotted points.
Q10. What is the main advantage of a Bezier curve?
Answer: It creates smooth and visually appealing curves with simple mathematical equations.
Conclusion
The Bezier Curve Drawing Algorithm is an important technique in computer graphics for generating smooth and flexible curves. By using four control points and a parametric equation, it produces high-quality curves suitable for animation, CAD, font rendering, vector graphics, and image editing. Due to its simplicity, accuracy, and versatility, the Bezier curve is one of the most widely used curve-drawing techniques in modern graphical applications.
Note: If your syllabus specifically requires the Hermite Curve instead of the Bezier Curve, that would be an additional assignment. Otherwise, this completes the standard Graphics and Multimedia Laboratory practicals.
No comments:
Post a Comment