Implementación de algoritmos/Geometría/Algoritmo del punto medio para circunferencias
Apariencia
Circunferencias
[editar]Java
[editar]void CircleMidPoint(Graphics g, int xc, int yc, int r){
int x, y, p;
x = 0;
y = r;
p = 1 - r;
PlotPoint(g,xc,yc,x,y);
/* se cicla hasta trazar todo un octante */
while (x < y){
x = x + 1;
if (p < 0)
p = p + 2*x + 1;
else {
y = y - 1;
p = p + 2*(x - y) + 1;
}
PlotPoint(g,xc,yc,x,y);
}}
Cónicas
[editar]Elipse (En java)
[editar] 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);
}
}
Elipse (En C)
[editar] #include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y, rx2, ry2,p1,p2;
int xc, yc, gm, gd=DETECT, rx, ry;
printf("ENTER RX AND RY:");
scanf("%d %d",&rx,&ry);
printf("ENTER THE CO-ORDINATES OF THE CENTER:");
scanf("%d %d",&xc,&yc);
initgraph(&gd,&gm," ");
putpixel(xc, yc,15);
x=0;
y=ry;
rx2=pow(rx,2);
ry2=pow(ry,2);
p1=ry2-(rx2*ry)+(0.25*rx2);
while((ry2*x)<(rx2*y))
{
if(p1<0)
{ x++;
p1=p1+(2*ry2*x)+ry2;
}
else
{
x++; y--;
p1=p1+(2*ry2*x)-(2*rx2*y)+ry2;
}
putpixel(xc+x, yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
}
p2=(ry2)*pow((x+0.5),2)+(rx2)*pow((y-1),2)-(rx2*ry2);
while(y>0)
{
if (p2>0)
{
y--;
p2=p2-(2*rx2*y) +rx2;
}
else
{
x++; y--;
p2=p2+ (2*ry2*x)-(2*rx2*y)+rx2;
}
putpixel(xc+x, yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
}
getch();
closegraph();
}