有哪些适合绘制反锯齿圆形的好算法?(填充和未填充)
Xiaolin Wu将直线算法改编为反走样算法,并且对圆算法也做了同样的处理。
http://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
您可以通过以下搜索找到圆形算法:
http://www.google.com/search?q=Xiaolin%20Wu%20circle
-亚当
anti_alised_matrix[x][y] = point[x][y] / 2 + point[x+1][y]/8 + point[x-1][y]/8 + point[x][y-1]/8 + point[x][y+1]/8;
当然,这适用于灰度图像,但你也可以在RGB中轻松实现相同效果。
这真的非常简单,你还可以添加对角线,即[x+1][y+1],并将其分成16或32份。
为所有需要的人提供帮助。我刚刚为我的应用程序编写了一个圆形绘制器函数。
不幸的是,它只能绘制奇数直径的圆,但在 CPU 上绘制非常快。
此外,它可以轻松地移植到任何其他语言,因为没有使用特殊的语法/结构。
其主要优点是可以与混合一起工作,避免像素层叠(这会导致圆上出现黑斑)。
/*
* void drawPixel(int32_t x, int32_t y, uint32_t color)
*
* The algorithm's been written assuming this function to work with alpha-blending
* and packed RGBA colors, but you can change the color system easily.
*
* AA - anti-aliasing
*/
static inline void draw8Symmetry(int32_t cX, int32_t cY, int32_t x, int32_t y, int32_t color) {
drawPixel(cX + x, cY + y, color);
drawPixel(cX + x, cY - y, color);
if (x != 0) { // No repeating on top/bottom
drawPixel(cX - x, cY + y, color);
drawPixel(cX - x, cY - y, color);
}
if (x != y) { // No repeating on corners (45 deg)
drawPixel(cX + y, cY + x, color);
drawPixel(cX - y, cY + x, color);
if (x != 0) { // No repeating on left/right sides
drawPixel(cX + y, cY - x, color);
drawPixel(cX - y, cY - x, color);
}
}
}
void drawCircle(int32_t cX, int32_t cY, int32_t r, uint32_t color) {
int32_t i = 0;
int32_t j = r + 1;
int32_t rr = r * r;
double lastFadeAmount = 0;
double fadeAmount = 0;
int32_t fadeAmountI;
const int32_t maxOpaque = color >> 24;
const int32_t noAlphaColor = color & 0x00FFFFFF;
while (i < j) {
double height = sqrt(rr - i * i);
fadeAmount = (double)maxOpaque * (1.0 - (ceil(height) - height));
// If fade amount is dropping, then, obviously, it's a new step
if (fadeAmount > lastFadeAmount)
j--;
lastFadeAmount = fadeAmount;
// Draw the semi-transparent circle around the filling
draw8Symmetry(cX, cY, i, j, noAlphaColor | ((int32_t)fadeAmount << 24));
// Draw central filling
if (i != 0)
for (int32_t x = -j + 1; x < j; x++) {
drawPixel(cX + x, cY + i, color);
drawPixel(cX + x, cY - i, color);
}
else
for (int32_t x = -j + 1; x < j; x++)
drawPixel(cX + x, cY + i, color);
i++;
}
// Draw top and bottom parts
while (i < r) {
int32_t lineLength = ceil(sqrt(rr - i * i));
for (int32_t x = -lineLength + 1; x < lineLength; x++) {
drawPixel(cX + x, cY + i, color);
drawPixel(cX + x, cY - i, color);
}
i++;
}
}
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;