Mid Point Ellipse Algorithm
Mid Point Ellipse Algorithm
y
Region I
b
dy/dx = –1
Region II
a x
m = –1
Derivation:
SE
Prediction
(xk +1, yk –½) Region I
x is always incremented in each step, i.e. xk+1 = xk + 1.
yk+1 = yk if E is selected, or yk+1 = yk – 1 if SE is selected.
In order to make decision between S and SE, a prediction (x k+1, yk–½) is set at
the middle between the two candidate pixels. A prediction function P k can be
defined as follows:
Pk = f(xk+1, yk–½)
= b2(xk+1)2 + a2(yk–½)2 – a2b2
= b2(xk2 + 2xk + 1) + a2(yk2 – yk + ¼) – a2b2
If Pk < 0, select E :
If Pk > 0, select SE :
If E is selected,
Pk+1E = b2(2xk + 5)
2PkE = Pk+1E – PkE = 2b2
If SE is selected,
Pk+1E = b2(2xk + 5)
2PkE = Pk+1E – PkE = 2b2
Initial values:
x0 = 0, y0 = b, P0 = b2 + ¼a2(1 – 4b)
P0E = 3b2, P0SE = 3b2 – 2a2(b – 1)
In region II (dy/dx < –1), all calculations are similar to that in region I except
that y is decremented in each step.
(xk, yk)
SE
Prediction
(xk +½, yk –1)
Region II
Pk = f(xk+½, yk–1)
= b2(xk+½)2 + a2(yk–1)2 – a2b2
= b2(xk2 + xk + ¼) + a2(yk2 – 2yk + 1) – a2b2
If Pk > 0, select S :
If Pk < 0, select SE :
If S is selected,
Pk+1S = a2(5 – 2yk)
2PkS = Pk+1S – PkS = 2a2
If SE is selected,
Pk+1S = a2(5 – 2yk)
2PkS = Pk+1S – PkS = 2a2
Pk+1SE = 2b2(2xk + 2) – a2(5 – 2yk)
2PkSE = Pk+1SE – PkSE = 2(a2 + b2)
At region I, dy/dx > –1, x < a2√/(a 2 + b2) and y > b 2√/(a 2 + b 2), therefore
PkSE < b 2(2a2/√(a 2 + b 2) – 2a2(b 2/√(a 2 + b2) – 1) = 2a2 + 3b 2
Code:
#include<iostream>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
}
}while(y>0);
}
void display(int xs,int ys,int x,int y)
{
putpixel(xs+x,ys+y,WHITE); // plot points by using 4 point symmetry
putpixel(xs-x,ys-y,WHITE);
putpixel(xs+x,ys-y,WHITE);
putpixel(xs-x,ys+y,WHITE);
}
int main(void)
{
int xs1,ys1;
float rx1,ry1;
int gd = DETECT,gm; // Initialise the graphics system
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\t\tMidpoint Ellipe Drawing Algorithm\n";
cout<<"Enter the Center Co-ordinates\n";
cout<<"xc = \t";
cin>>xs1;
cout<<"yc = \t";
cin>>ys1;
cout<<"Enter the X Radius\t";
cin>>rx1;
cout<<"Enter the Y Radius\t";
cin>>ry1;
ellips1(xs1,ys1,rx1,ry1);
getch();
closegraph();
}
Output:
Discussion
The algorithm first determines the point for region 1 than proceeds to
calculate for Region 2. The processing time for the algorithm is high.