如何绘制一个实心圆? [英] How to draw a filled circle?

查看:298
本文介绍了如何绘制一个实心圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据我的C code中的规范创建位图/ BMP文件,我想提请对我的位图简单的原语。下面code显示了如何画一个矩形,在我的位图:

 如果(curline->类型== 1)//绘制一个矩形
{
    INT XSTART = curline-> X;
    INT ystart = curline-> Y;
    INT宽度= curline->宽度+ XSTART;
    INT高度= curline->身高+ ystart;    INT X = 0;
    INT Y = 0;    对于(Y = ystart; Y<高度; Y ++)
    {
      为(X = XSTART; X<宽度; X ++)
      {
        ARR [X] [Y]的.blue = curline->蓝色;
        ARR [X] [Y]。绿色= curline->绿色;
        ARR [X] [Y] .red = curline->红色;
      }
    }    的printf(绘制矩形\\ n);
}...
保存位图();

示例输出:

因此​​,基本上我设置红色,绿色和蓝色值对于给定的x和y场之内的所有像素。

现在我想通过了解它的中点和半径填充圆。但我怎么知道哪些像素是这个圈子里,哪些像素不?任何帮助将是AP preciated,感谢阅读。


解决方案

一个点位于如果从点的圆的中心的距离小于所述圆的半径的圆的范围内。

相比于用中心(X2,Y2)和半径r的圆考虑一个点(X1,Y1):

  INT DX = X2  -  X1; //水平偏移
INT DY = Y2 - Y1; //垂直偏移
如果((DX * DX + DY * DY)LT =(R * R))
{
    //设置像素的颜色
}

I'm creating bitmap/bmp files according to the specifications with my C code and I would like to draw simple primitives on my bitmap. The following code shows how I draw a rectangle on my bitmap:

if(curline->type == 1) // draw a rectangle
{
    int xstart = curline->x;
    int ystart = curline->y;
    int width = curline->width + xstart;
    int height = curline->height + ystart;

    int x = 0;
    int y = 0;

    for(y = ystart; y < height; y++)
    {
      for(x = xstart; x < width; x++)
      {
        arr[x][y].blue = curline->blue;
        arr[x][y].green = curline->green;
        arr[x][y].red = curline->red;
      }
    }

    printf("rect drawn.\n");
}

...
save_bitmap();

Example output:

So basically I'm setting the red, green and blue values for all pixels within the given x and y field.

Now I'd like to fill a circle by knowing its midpoint and radius. But how do I know which pixels are inside this circle and which pixels ain't? Any help would be appreciated, thanks for reading.

解决方案

A point lies within the bounds of a circle if the distance from the point to the center of the circle is less than the radius of the circle.

Consider a point (x1,y1) compared to a circle with center (x2,y2) and radius r:

int dx = x2 - x1; // horizontal offset
int dy = y2 - y1; // vertical offset
if ( (dx*dx + dy*dy) <= (r*r) )
{
    // set pixel color
}

这篇关于如何绘制一个实心圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆