Algoritmos
kathialc19 de Octubre de 2013
839 Palabras (4 Páginas)374 Visitas
Círculo
Algoritmo basado en la ecuación del círculo
void PlotPoint(Graphics g, int xc, int yc, int x, int y)
{
g.drawLine(,xc + x,yc + y);
g.drawLine(,xc - x,yc + y);
g.drawLine(,xc + x,yc - y);
g.drawLine(,xc - x,yc - y);
g.drawLine(,xc + x,yc + x);
g.drawLine(,xc - y,yc + x);
g.drawLine(,xc + y,yc - x);
g.drawLine(,xc - y,yc - x);
}
void CircleSimple(Graphics g, int xc, int yc, int r){
int x,y;
double yr;
x = 0;
y = r;
yr = r;
PlotPoint(x,y);
/* se cicla hasta trazar todo un octante */
while (x < yr){
x = x + 1;
yr = Math.sqrt(r*r-x*x);
y = (int)Math.round(yr);
PlotPoint(x,y);
}
}
Elipse
Algoritmo de punto medio para elipse
public void Elipse(Graphics g, int xc, int yc, int rx, int ry){
int x, y, p, px, py;
int rx2, ry2, tworx2, twory2;
ry2 = ry*ry;
rx2 = rx*rx;
twory2 = 2 * ry2;
tworx2 = 2 * rx2;
/* región 1 */
x = 0;
y = ry;
PlotPoint(x,y);
p = (int)Math.round(ry2 - rx2*ry + 0.25*rx2);
px = 0;
py = tworx2*y;
while (px < py) { /* se cicla hasta trazar la región 1 */
x = x + 1;
px = px + twory2;
if (p < 0)
p = p + ry2 + px;
else {
y = y - 1;
py = py - tworx2;
p = p + ry2 + px - py;
}
PlotPoint(x,y);
}
/* región 2 */
p = (int)Math.round(ry2*(x+0.5)*(x+0.5) + rx2*(y-1)*(y-1) - rx2*ry2);
px = 0;
py = tworx2*y;
while (y > 0) { /* se cicla hasta trazar la región 2 */
y = y - 1;
py = py - tworx2;
if (p > 0)
p = p + rx2 - py;
else {
x = x + 1;
px = px + twory2;
p = p + rx2 + py + px;
}
PlotPoint(x,y);
}
}
Parábola
Algoritmo de punto medio para parábolas
public void parabola(Graphics g,int xc, int yc, int p, int bound){
int x,y,d;
int p2, p4;
p2 = 2*p;
p4 = 2*p2;
x = 0;
y = 0;
d = 1 - p;
//region 1
while(y < p && x<=bound){
g.drawLine(xc+x,yc+y,xc+x,yc+y);
g.drawLine(xc+x,yc-y,xc+x,yc-y);
if(d >= 0){
x++;
d = d - p2;
}
y++;
d = d + 2*y +1;
}
if(d == 1) d = 1 - p4;
else d = 1 - p2;
//region 2
while(x<=bound){
g.drawLine(xc+x,yc+y,xc+x,yc+y);
g.drawLine(xc+x,yc-y,xc+x,yc-y);
if(d <= 0){
y++;
d = d + 4*y;
}
x++;
d = d - p4;
}
}
Hipérbola
Algoritmo de punto medio para hipérbola
public void hiperbola(Graphics g,int xc, int yc, long a,long b, int bound){
long a_sqr,b_sqr,a22,b22,a42,b42,x_slope,y_slope;
long d,mida,midb;
long x,y;
x = a;
y = 0;
a_sqr = a*a;
b_sqr = b*b;
a22 = 2*a_sqr;
b22 = 2*b_sqr;
a42 = 2*a22;
b42 = 2*b22;
x_slope = b42*(x+1);//x_slope = 4*b^2*(x+1)
y_slope = a42; //y_slope = 4*a^2*(y-1)
mida = a_sqr/2;
midb = b_sqr/2;
d = a22-b_sqr*(1+2*a)+midb;
...