Algoritmos
Enviado por kathialc • 19 de Octubre de 2013 • 839 Palabras (4 Páginas) • 347 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 ==
...