从正方形中画出圆-C语言中的GD库

从正方形中画出圆-C语言中的GD库,c,computer-science,C,Computer Science,我在这个项目中遇到了一些问题,我应该使用GD.h库来构建一个黑色背景的图像,并用正方形画一个圆。 我必须使用这个库来构造图像,并且我不允许使用“gdImageRectangle”以外的任何其他函数来制作正方形的圆。 我有我的所有代码制作和导出的图像,但在我的颜色数组后,我无法找出如何使圆..还有任何提示/教程gd.h将不胜感激。我读了手册,但还是不能完全理解 #include <stdio.h> #include <stdlib.h> #include <strin

我在这个项目中遇到了一些问题,我应该使用GD.h库来构建一个黑色背景的图像,并用正方形画一个圆。 我必须使用这个库来构造图像,并且我不允许使用“gdImageRectangle”以外的任何其他函数来制作正方形的圆。 我有我的所有代码制作和导出的图像,但在我的颜色数组后,我无法找出如何使圆..还有任何提示/教程gd.h将不胜感激。我读了手册,但还是不能完全理解

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>

#define BLACK           0
#define BLUE            1
#define GREEN           2
#define RED             3
#define WHITE           4

int main(int argc, char **argv)
{
    char                    *outfile;                                                                                               //name out of the output file
    FILE                    *out;                                                                                           //output file pointer
    gdImagePtr               img;                                                                                           //GD Image Contruct
    unsigned int             color[5];                                                                                      //color array
    unsigned short int       wide, high;                                                                                    //image attributes

    if (argc == 2)
    {
            outfile = *(argv+6);
            fprintf(stdout, "Using '%s' as output filename\n", outfile);
    }
    else
    {
            outfile = (char *) malloc (sizeof(char) * 64);
            strcpy(outfile, "/home/snicho15/public_html/cos0.png");
    }

    //setting up image width and height for COS0
    wide = 800;
    high = 600;

    //create new image with specified height and width
    img = gdImageCreate(wide, high);

    //GD COLOR
    color[BLACK]             = gdImageColorAllocate(img, 0x00, 0x00, 0x00);
    color[BLUE]              = gdImageColorAllocate(img, 0x00, 0x00, 0xFF);
    color[GREEN]             = gdImageColorAllocate(img, 0x00, 0xFF, 0x00);
    color[RED]               = gdImageColorAllocate(img, 0xFF, 0x00, 0x00);
    color[WHITE]             = gdImageColorAllocate(img, 0xFF, 0xFF, 0xFF);
    //code goes down here

   **where the circle out of squares code goes**

    //open file
    if((out = fopen(outfile, "wb")) == NULL)
    {
             fprintf(stderr, "Error opening '%s'\n", outfile);
             exit(1);
    }

    //send image to file
    gdImagePngEx(img, out, -1);

    //close things up
    fclose(out);
    gdImageDestroy(img);

    return (0);
    }
#包括
#包括
#包括
#包括
#定义黑色0
#定义蓝色1
#定义绿色2
#定义红色3
#定义白色4
int main(int argc,字符**argv)
{
char*outfile;//输出文件的名称
FILE*out;//输出文件指针
gdImagePtr img;//GD图像构造
无符号整数颜色[5];//颜色数组
无符号短整型宽,高;//图像属性
如果(argc==2)
{
输出文件=*(argv+6);
fprintf(标准输出,“使用'%s'作为输出文件名,\n”,输出文件);
}
其他的
{
outfile=(char*)malloc(sizeof(char)*64);
strcpy(outfile,“/home/snicho15/public_html/cos0.png”);
}
//设置COS0的图像宽度和高度
宽=800;
高=600;
//创建具有指定高度和宽度的新图像
img=gdImageCreate(宽、高);
//GD色
颜色[黑色]=gdImageColorAllocate(img,0x00,0x00,0x00);
颜色[蓝色]=gdImageColorAllocate(img,0x00,0x00,0xFF);
颜色[绿色]=gdImageColorAllocate(img,0x00,0xFF,0x00);
颜色[红色]=gdImageColorAllocate(img,0xFF,0x00,0x00);
颜色[白色]=gdImageColorAllocate(img,0xFF,0xFF,0xFF);
//代码在这里
**正方形外圆代码的位置**
//打开文件
if((out=fopen(outfile,“wb”))==NULL)
{
fprintf(stderr,“打开'%s'时出错,\n”,输出文件);
出口(1);
}
//将图像发送到文件
gdImagePngEx(img,out,-1);
//了结
fclose(out);
gdg(img);
返回(0);
}

我唯一需要帮助的就是如何从正方形中画出圆。圆的中心x应为0,并与中心y一起(应为0)。到那时为止,我对一切都很满意。我可以让它编译并创建粘贴在圆上的图像。

作为测试,您是否在圆的中心画了一个正方形?如果是这样,请在X轴和Y轴上的四个正交点中的每一个点的半径处绘制一个正方形。然后,找到一种方法来填充径向点之间的点,或者使用角度,或者使用Bresenham圆算法。@WeatherVane我没有。我还是有点困惑,我如何用GDimaGeCurthCar函数画一个圆……考虑到非常小的矩形是像素。在你想要的像素点绘制每个矩形。或者,如果你要构建一个填充圆,在中心绘制填充矩形,从一个高的薄矩形开始,穿过一个正方形,到一个平的长矩形,计算每个矩形所需的大小;-)