Assignment No.
01 Total Marks: 20
Semester: Spring 2023
Due Date: 23nd May
CS602: Computer Graphics 2023
Prepared By: https://virtualkitab.com/
https://www.youtube.com/@virtualkitaab
YouTube:
TASK Marks=20
Suppose you are working on a project where you need to draw an ellipse of a given
center point (xc,yc), major axis length (a), and minor axis length (b) using the
Midpoint Ellipse Drawing Algorithm in C++. Write a program to implement the
algorithm and display the resulting ellipse. Then fill the Ellipse with any color and
design using the graphics libraries.
Note: You should display your student ID before displaying the Ellipse.
Solution:
#include <graphics.h>
int main()
{
//initialize graphics window
initwindow(640, 480, "Ellipse Drawing Algorithm");
//set center coordinates and axis lengths
int xc = 320;
int yc = 240;
int a = 100;
int b = 50;
//calculate initial values
int x = 0;
int y = b;
int d = (b * b) - (a * a * b) + (0.25 * a * a);
//display student ID
outtextxy(10, 10, "Student ID: [insert ID here]");
//draw the first quadrant
while ((a * a * (y - 0.5)) > (b * b * (x + 1)))
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc - x, yc + y, WHITE);
if (d < 0)
d += (b * b * (2 * x + 3));
else
{
d += ((b * b * (2 * x + 3)) + (a * a * (-2 * y + 2)));
y--;
}
x++;
}
//draw the second quadrant
d = (b * b * (x + 0.5) * (x + 0.5)) + (a * a * (y - 1) * (y - 1)) - (a * a * b * b);
while (y >= 0)
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc - x, yc + y, WHITE);
if (d > 0)
d += (a * a * (-2 * y + 3));
else
{
d += ((b * b * (2 * x + 2)) + (a * a * (-2 * y + 3)));
x++;
}
y--;
}
//fill the ellipse with color
setfillstyle(SOLID_FILL, RED);
fillellipse(xc, yc, a, b);
//wait for user to close the window
getch();
//close the graphics window
closegraph();
return 0;
}