C语言画图之画个太极图

呵呵昨天花了一个圆,今天想画个太极图,我知道没啥技术含量,但是挺有意思的,希望各位看官不要鄙视我不务正业,画完此图,不再做这些事情。

先展示下画出来的图像的情况,因为不支持pgm格式的图像,所以我用的 QQ截图:

今天,二话不说上代码。

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <math.h>
 
typedefstruct{
                size_t width;
                size_t height;
                unsignedchar*data;
}Image;
 
staticImage *
image_new(size_t width,
                                size_t height)
{
                Image *image;
 
                image=malloc(sizeof *image);
                image->width=width;
                image->height=height;
                image->data=malloc(width *height);
 
                returnimage;
}
 
staticvoid
image_free(Image *image)
{
                free(image->data);
                free(image);
}
 
                staticvoid
image_fill(Image *image,
                        unsignedcharvalue)
{
                memset(image->data,value,image->width *image->height);
}
 
/**
* image_set_pixel:
*
* Sets a pixel passed in signed (x, y) coordinates, where (0,0) is at
* the center of the image.
**/
staticvoid
image_set_pixel(Image *image,
                                ssize_tx,
                                ssize_ty,
                                unsignedcharvalue)
{
                size_t tx,ty;
                unsignedchar*p;
 
                tx=(image->width/2)+x;
                ty=(image->height/2)+y;
 
                p=image->data+(ty *image->width)+tx;
 
                *p=value;
}
 
staticvoid
image_save(constImage *image,
                        constchar*filename)
{
                FILE *out;
 
                out=fopen(filename,"wb");
                if(!out)
                                return;
 
                fprintf(out,"P5\n");
                fprintf(out,"%zu %zu\n",image->width,image->height);
                fprintf(out,"255\n");
 
                fwrite(image->data,1,image->width *image->height,out);
 
                fclose(out);
}
 
staticvoiddraw_Taijitu(Image *image,intradius,intvalue)
{
                intx,y;
                intrlimit,llimit;
 
                intradius_2=radius*radius;
                for(y=-radius;y<radius;y++)
                    for(x=-radius;x<radius;x++)
                       if(x*x+y*y<=radius_2)
                          image_set_pixel(image,x,y,0xff);
 
                for(y=-radius;y<0;y++)
                    for(x=0;x<radius;x++)
                        if((x*x)+(y*y)<=radius_2)
                          image_set_pixel(image,x,y,value);
 
                for(y=-radius;y<0;y++)
                    for(x=-(int)sqrt((double)(-radius*y-y*y));x<0;x++)
                        image_set_pixel(image,x,y,value);
 
 
                for(y=0;y<radius;y++)
                {
                                llimit=(int)sqrt((double)(radius*y-y*y));
                                rlimit=(int)sqrt((double)(radius_2-y*y));
                                for(x=llimit;x<rlimit;x++)
                                                image_set_pixel(image,x,y,value);
                }
 
                for(y=2*radius/6;y<4*radius/6;y++)
                {
                                rlimit=(int)sqrt((double)(radius*y-y*y-2*radius_2/9));
                                llimit=-rlimit;
 
                                for(x=llimit;x<rlimit;x++)
                                                image_set_pixel(image,x,y,value);
                }
 
                for(y=-4*radius/6;y<-2*radius/6;y++)
                {
                                rlimit=sqrt(-radius*y-y*y-2*radius_2/9);
                                llimit=-rlimit;
                                for(x=llimit;x<rlimit;x++)
                                                image_set_pixel(image,x,y,0xff);
                }
 
                return;
 
}
                int
main(intargc,char*argv[])
{
                Image *image;
 
                image=image_new(800,800);
 
                image_fill(image,0xaa);
                draw_Taijitu(image,300,0);
                image_save(image,"taiji_6.pgm");
 
                image_free(image);
 
                return0;
}
永不止步步 发表于03-02 11:55 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67417个|学分:363791个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号