You are on page 1of 4

Experiment: 4

Student Name: Rohit                     UID: 20BCA1150


Branch: BCA                                                    Section/Group: 6
Semester: 5                                                      Date of Performance:
9/22/2022
Subject Name: COMPUTER GRAPHICS LAB Subject Code: 20CAP-316

Task to be done:
Scan Convert a ellipse with centre (130, 100) and and major axis=50 , minor
axis=45  using Midpoint Ellipse Algorithm
Steps of Experiment

Mid-Point Ellipse Algorithm : 


 Take input radius along x axis and y axis and obtain center of ellipse.
 Initially, we assume ellipse to be centered at origin and the first point as :
(x, y0)= (0, ry).
 Obtain the initial decision parameter for region 1 as: p1 0=ry2+1/4rx2-rx 2ry
 For every xk position in region 1 : 
If p1k<0 then the next point along the is (x k+1 , yk) and
p1k+1=p1k+2ry2xk+1+ry2
Else, the next point is (x k+1, yk-1 ) 
And p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2
 Obtain the initial value in region 2 using the last point (x 0, y0) of region 1
as: p20=ry2(x0+1/2)2+rx2 (y0-1)2-rx2ry2
 At each yk in region 2 starting at k =0 perform the following task. 
If p2k>0 the next point is (x k, yk-1) and p2k+1=p2k-2rx2yk+1+rx2
 Else, the next point is (x k+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2
 Now obtain the symmetric points in the three quadrants and plot the
coordinate value as: x=x+xc, y=y+yc
 Repeat the steps for region 1 until 2r y2x>=2rx2y
Code of Experiment
#include <stdio.h>
void midptellipse(int rx, int ry, int xc, int yc)
{

float dx, dy, d1, d2, x, y;


x = 0;
y = ry;

// Initial decision parameter of region 1


d1 = (ry * ry)
- (rx * rx * ry)
+ (0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;

// For region 1
while (dx < dy) {

// Print points based on 4-way symmetry


printf("(%f, %f)\n", x + xc, y + yc);
printf("(%f, %f)\n", -x + xc, y + yc);
printf("(%f, %f)\n", x + xc, -y + yc);
printf("(%f, %f)\n", -x + xc, -y + yc);

// Checking and updating value of


// decision parameter based on algorithm
if (d1 < 0) {
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else {
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}

// Decision parameter of region 2


d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5)))
+ ((rx * rx) * ((y - 1) * (y - 1)))
- (rx * rx * ry * ry);
// Plotting points of region 2
while (y >= 0) {

// printing points based on 4-way symmetry


printf("(%f, %f)\n", x + xc, y + yc);
printf("(%f, %f)\n", -x + xc, y + yc);
printf("(%f, %f)\n", x + xc, -y + yc);
printf("(%f, %f)\n", -x + xc, -y + yc);

// Checking and updating parameter


// value based on algorithm
if (d2 > 0) {
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else {
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}

// Driver code
int main()
{
// To draw a ellipse of major and
// minor radius 15, 10 centered at (50, 50)
midptellipse(10, 15, 50, 50);

return 0;
}

Evaluation Grid:
Sr. Parameters Marks Obtained Maximum Marks
No.
1. Demonstration and 5
Performance (Pre Lab
Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like