Thuật toán vẽ Elip
Trang 1 trong tổng số 1 trang
Thuật toán vẽ Elip
void Put4Pixel(int x, int y, int x0, int y0, Color color)
{
SetPixel(x + x0, y + y0, color);
SetPixel(-x + x0, y + y0, color);
SetPixel(x + x0, -y + y0, color);
SetPixel(-x + x0, -y + y0, color);
}
public void DrawEllipse(MyPoint start, MyPoint end, Color color)
{
if(start.X == end.X )
{
DrawLineDDA(start.X, start.Y * 2, end.X, end.Y, color);
return;
}
if (start.Y == end.Y)
{
DrawLineDDA(start.X * 2, start.Y, end.X, end.Y, color);
return;
}
int A = Math.Abs(end.X - start.X), B = Math.Abs(end.Y - start.Y);
int A2, B2, p, Const1, Const2, Delta1, Delta2, x, y, MaxX, MaxY;
int x0 = start.X, y0 = start.Y;
A2 = A * A;
B2 = B * B;
MaxX = (int)Math.Round(A2 / Math.Sqrt(A2 + B2));
MaxY = (int)Math.Round(B2 / Math.Sqrt(A2 + B2));
p = B2 - A2 * B + A2 / 4;
Const1 = 2 * B2;
Const2 = 2 * A2;
x = 0;
y = B;
Delta1 = B2 * (2 * x + 3);
Delta2 = 2 * A2 * (1 - y) + B2 * (2 * x + 3);
Put4Pixel(x, y,x0, y0, color);
while (x < MaxX)
{
if (p >= 0)
{
p += Delta2;
Delta2 += Const2;
y--;
}
else
p += Delta1;
Delta2 += Const1;
Delta1 += Const1;
x++;
Put4Pixel(x, y, x0, y0, color);
}
p = (A2 - A * B2 + B2 / 4);
Const1 = 2 * A2;
Const2 = 2 * B2;
x = A;
y = 0;
Delta1 = A2 * (2 * y + 3);
Delta2 = 2 * B2 * (1 - x) + A2 * (2 * y + 3);
Put4Pixel(x, y, x0, y0, color);
while (y < MaxY)
{
if (p >= 0)
{
p += Delta2;
Delta2 += Const2;
x--;
}
else
p += Delta1;
Delta2 += Const1;
Delta1 += Const1;
y++;
Put4Pixel(x, y, x0, y0, color);
}
}
Được sửa bởi nth ngày 23/01/10, 11:45 pm; sửa lần 2.
Re: Thuật toán vẽ Elip
// Vẽ Elip bằng thuật toán Bresnham
void CGraphics::Set4Pixel(int xc, int yc, int x, int y)
{ SetPixel(xc + x, yc + y);
SetPixel(xc - x, yc + y);
SetPixel(xc + x, yc - y);
SetPixel(xc - x, yc - y);
}
void CGraphics::DrawElip_Bresenham(int xc, int yc, int a, int b)
{ long x, y, dx, dy, rx, ry, p; // d= delta
rx = a * a; ry = b * b;
x = 0; y = b;
dx = 0; dy = (rx << 1) * y;
Set4Pixel(xc, yc, x, y);
p = Round(ry - (rx * b) + (0.25 * rx));
while (dx < dy)
{ x++;
dx += ry<<1;
if(p < 0) p += dx+ry;
else
{ y--;
dy -= rx<<1;
p += ry + dx - dy;
}
Set4Pixel(xc, yc, x, y);
}
p = Round(ry * (x+0.5)*(x+0.5) + rx*(y-1)*(y-1) - rx*ry);
while (y>0)
{ y--;
dy -= rx<<1;
if(p>0) p += rx - dy;
else
{ x++;
dx += ry<<1;
p += dx + rx - dy;
}
Set4Pixel(xc, yc, x, y);
}
}
// Bài này hog phải của H đâu, H chỉ xem lại thôi, bài bản quyền của bạn H
Được sửa bởi nth ngày 23/01/10, 11:47 pm; sửa lần 1.
Re: Thuật toán vẽ Elip
Vẽ 1 nữa trước, thực tế chỉ vẽ 1/8 thôi, rồi lấy đối xứng
Thuật toán vẽ MidPoint:
void graphics1::DrawEllipse_MidPoint(int xc, int yc, int a, int b)
{ int x, y;
double p, c1, c2;
x = 0; y = b;
p = b*b + a*a*(b-0.5)*(b-0.5) - a*a*b*b;
while (b*b*x <= a*a*y)
{ Set4Pixel(x,y,xc,yc);
c1 = b*b*(2*x+3);
if(p < 0) p += c1;
else
{ c2 = c1 + a*a*(-2*(y-0.5)+1);
p += c2;
y--;
}
x++;
}
}
Similar topics
» thuật toán Midpoint
» Thuật toán vẽ Đường Thẳng
» Demo thuật toán tổng quát
» Thuật toán DDA_Line dạng tổng quát
» Tìm hiểu cây 2 - 3 - 4
» Thuật toán vẽ Đường Thẳng
» Demo thuật toán tổng quát
» Thuật toán DDA_Line dạng tổng quát
» Tìm hiểu cây 2 - 3 - 4
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|