Ellipse using Mid-Point Algorithm

On October 7, 2008, in Algorithms, Shapes, by , Views : 4598


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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();
}

Download the code using the below link:

Please 'Like' or '+1' us to get the download link! (If you have already liked us on facebook, then like us either on G+ or twitter)



Hope, it helps! 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *


*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
Car
Car Movement

Final translated Image
2D Transformations: Translations

wheel
Moving Wheel using Mid-Point Circle algorithm and DDA Line algorithm

MinMaxClose