0% found this document useful (0 votes)
13 views7 pages

Clipping

The document outlines a lab report on drawing ellipses using the Midpoint Algorithm as part of a Computer Graphics and Visualization course at Tribhuvan University. It details the theoretical background, algorithm steps, and provides a C++ code implementation for drawing ellipses and handling clipping. The conclusion highlights the successful demonstration of positioning small ellipses around a larger ellipse, showcasing the integration of mathematical principles with graphical programming.

Uploaded by

luciferparajulee
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)
13 views7 pages

Clipping

The document outlines a lab report on drawing ellipses using the Midpoint Algorithm as part of a Computer Graphics and Visualization course at Tribhuvan University. It details the theoretical background, algorithm steps, and provides a C++ code implementation for drawing ellipses and handling clipping. The conclusion highlights the successful demonstration of positioning small ellipses around a larger ellipse, showcasing the integration of mathematical principles with graphical programming.

Uploaded by

luciferparajulee
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/ 7

Tribhuvan University

Institute of Engineering
Thapathali Campus, Thapathali

Subject: Computer Graphics and Visualization


Lab # 7

Submitted by:
Name: Chitra Raj Joshi
Roll No.: THA080BEI018

Submitted to:
Department of Electronics and Computer Engineering
Submission Date: February 7, 2025
Title: Ellipse Using Midpoint Algorithm

Objective: To draw ellipse using Midpoint Algorithm

Theory:

The Midpoint Algorithm for drawing an ellipse is an efficient method that uses
integer-based calculations to determine the points on the ellipse's boundary. It is an
extension of the Midpoint Circle Drawing Algorithm, designed to handle the elliptical
shape by considering both the major and minor axes.

An ellipse is a geometric shape defined by two axes: the major axis (longer axis) and
the minor axis (shorter axis). The general equation for an ellipse with its center at the
origin is:

(x / a)2 + (y / b)2 = 1

Where:

 a is the length of the semi-major axis (horizontal radius).

 b is the length of the semi-minor axis (vertical radius).

The midpoint algorithm for ellipse drawing works by dividing the ellipse into two
regions based on the slope of the tangent. These regions are referred to as Region 1
and Region 2.

 Region 1: When the slope of the tangent is less than 1, we primarily increase
the x-coordinate while adjusting the y-coordinate based on the ellipse
equation. This region is characterized by a predominantly horizontal stretch of
the ellipse.

 Region 2: When the slope of the tangent is greater than 1, we primarily


increase the y-coordinate while adjusting the x-coordinate. This region
represents the vertical stretch of the ellipse.

2
Midpoint Ellipse Algorithm:

1. Input the length of the semi-major axis and semi-minor axis and obtain center
of ellipse.

2. Initially, we assume ellipse to be centered at origin and the first point as :

(x, y)= (0, ry).

3. Obtain the initial decision parameter for region 1 as: P0 = ry2 + 1/4rx2 - rx2 ry

4. For every (xk, yk) position in region 1:

Pk < 0 Pk >= 0

xk = xk + 1 xk = xk + 1

yk = yk yk = yk + 1

Pk = Pk + 2 ry2 xk + ry2 Pk = Pk + 2 ry2 xk – 2 rx2 yk + ry2

5. Repeat the step4 for region 1 until 2 ry2 x <= 2 rx2 y

6. Obtain the initial value in region 2 using the last point (x, y) of region 1 as:
P0 = ry2 (x + 1/2)2 + rx2 (y - 1)2 - rx2 ry2

7. For every (xk, yk) position in region 2:

Pk <= 0 Pk > 0

xk = xk + 1 xk = xk

yk = yk - 1 yk = yk - 1

Pk = Pk + 2 ry2 xk – 2 rx2 yk + rx2 Pk = Pk – 2 rx2 yk + rx2

8. Repeat the step 7 for region 2 until (x, y) != (rx, 0)

9. Now obtain the symmetric points in the three quadrants and plot the
coordinate value as: x=x+xc, y=y+yc

3
4
Code:
#include <graphics.h> int newX1 = X1 + Umax * dx;
#include <iostream> int newY1 = Y1 + Umax * dy;
#include <cmath> int newX2 = X1 + Umin * dx;
#include <algorithm> int newY2 = Y1 + Umin * dy;

using namespace std; setcolor(BLUE);


line(newX1, newY1, newX2, newY2);
void Layout() { }
setbkcolor(WHITE);
cleardevice(); int main() {
settextstyle(DEFAULT_FONT, int gd = DETECT, gm;
HORIZ_DIR, 2); initgraph(&gd, &gm, NULL);
setcolor(BLACK);
outtextxy(10, 10, "Chitra Raj Joshi"); int Xmin, Ymin, Xmax, Ymax, X1, Y1,
} X2, Y2;
cout << "Enter the coordinates of
void drawWindow(int Xmin, int Ymin, int clipping window (Xmin, Ymin, Xmax,
Xmax, int Ymax) { Ymax): ";
setcolor(BLACK); cin >> Xmin >> Ymin >> Xmax >>
rectangle(Xmin, Ymin, Xmax, Ymax); Ymax;
} cout << "Enter the endpoints of the line
((X1, Y1), (X2, Y2)): ";
void drawLine_without_clipping(int X1, int cin >> X1 >> Y1 >> X2 >> Y2;
Y1, int X2, int Y2) {
setcolor(RED); // Before Clipping
line(X1, Y1, X2, Y2); Layout();
} drawWindow(Xmin, Ymin, Xmax,
Ymax);
void drawLine_with_clipping(int Xmin, int drawLine_without_clipping(X1, Y1, X2,
Ymin, int Xmax, int Ymax, int X1, int Y1, Y2);
int X2, int Y2) { setcolor(BLACK);
int dx = X2 - X1; outtextxy(20, 440, "Before Clipping
int dy = Y2 - Y1; (Press any key to continue)");
float Umax = 0, Umin = 1; getch();

int P[4] = {-dx, dx, -dy, dy}; // After Clipping


int Q[4] = {X1 - Xmin, Xmax - X1, Y1 - cleardevice(); // Clear screen for
Ymin, Ymax - Y1}; second image
Layout();
for (int i = 0; i < 4; i++) { drawWindow(Xmin, Ymin, Xmax,
if (P[i] == 0) continue; Ymax);
float t = (float)Q[i] / P[i]; drawLine_with_clipping(Xmin, Ymin,
if (P[i] < 0) { Xmax, Ymax, X1, Y1, X2, Y2);
Umax = max(Umax, t); outtextxy(20, 440, "After Clipping
} else { (Press any key to exit)");
Umin = min(Umin, t);
} getch();
} closegraph();
return 0;
if (Umax > Umin) return; // Reject the }
line

5
Output:

6
Conclusion:

In this experiment, we successfully implemented a program that draws small ellipses


around a large ellipse using C++ and the graphics library. To achieve this, we utilized
the parametric equations of an ellipse to calculate the positions of the small ellipses
along the big ellipse's circumference. The small ellipses were evenly spaced by
dividing the full angle 2π by the number of ellipses, ensuring a uniform distribution.
The sizes of the small ellipses were chosen to be a fraction of the big ellipse's size,
and their positions were calculated using trigonometric functions (cosine and sine) to
determine the appropriate center coordinates.

The program successfully demonstrates the geometric concept of positioning objects


along a curve and ensures that the small ellipses are tangent to each other along the
circumference, fulfilling the desired objective. By adjusting parameters such as the
number and size of the small ellipses, this method can be extended for various
applications involving circular or elliptical patterns.

In conclusion, the implementation effectively showcases how mathematical principles


and graphical programming can be combined to create visually appealing and
geometrically accurate designs.

You might also like