Write a C Program to Draw Ellipse using graphics

By | 02.12.2016

Draw Ellipse using graphics


Write a C Program to Draw Ellipse using graphics. Here’s simple Program to Draw Ellipse using graphics in C Programming Language.


Below is the source code for C Program to Draw Ellipse using graphics which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Draw Ellipse using graphics  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void plotpoints(int cx, int cy, int x, int y) {
        putpixel(cx + x, cy + y, 4);
        putpixel(cx - x, cy + y, 4);
        putpixel(cx + x, cy - y, 4);
        putpixel(cx - x, cy - y, 4);
}
void main() {
        int cx, cy, rx, ry;
        printf("Enter the center ");
        scanf("%d%d", &cx, &cy);
        printf("x radius : ");
        scanf("%d", &rx);
        printf("y radius : ");
        scanf("%d", &ry);
        long rx2 = (long) rx * rx;
        long ry2 = (long) ry * ry;
        long trx2 = 2 * rx2;
        long try2 = 2 * ry2;
        long p, x = 0, y = ry;
        long px = 0;
        long py = trx2 * y;
        p = (long) ((ry2 - (rx2 * ry) + (0.25 * rx2)) + 0.5);
        int gd = DETECT, gm = DETECT;
        initgraph(&gd, &gm, "");
        cleardevice();
        putpixel(cx, cy, 15);
        while (px < py) {
                plotpoints(cx, cy, x, y);
                x++;
                px += try2;
                if (p < 0)
                            p += ry2 + px; else {
                        y--;
                        py -= trx2;
                        p += ry2 + px - py;
                }
        }
        py = trx2 * y;
        px = try2 * x;
        p = (long) ((ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2) + 0.5);
        while (y >= 0) {
                plotpoints(cx, cy, x, y);
                y--;
                py -= trx2;
                if (p > 0)
                            p += rx2 - py; else {
                        x++;
                        px += try2;
                        p += rx2 - py + px;
                }
        }
        getch();
}

Above is the source code for C Program to Draw Ellipse using graphics which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.5 2 votes
Article Rating
Category: C Programming Graphic Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments