C语言画图之画个太极图
作者:谭思 发布时间:[ 2016/12/21 16:06:13 ] 推荐标签:.NET C语言
呵呵昨天花了一个圆,想画个太极图,我知道没啥技术含量,但是挺有意思的,希望各位看官不要鄙视我不务正业,画完此图,不再做这些事情。
先展示下画出来的图像的情况,因为不支持pgm格式的图像,所以我用的 QQ截图:
,二话不说上代码。同时再次致谢Banu前辈。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct {
size_t width;
size_t height;
unsigned char *data;
} Image;
static Image *
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);
return image;
}
static void
image_free (Image *image)
{
free (image->data);
free (image);
}
static void
image_fill (Image *image,
unsigned char value)
{
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.
**/
static void
image_set_pixel (Image *image,
ssize_t x,
ssize_t y,
unsigned char value)
{
size_t tx, ty;
unsigned char *p;
tx = (image->width / 2) + x;
ty = (image->height / 2) + y;
p = image->data + (ty * image->width) + tx;
*p = value;
}
static void
image_save (const Image *image,
const char *filename)
{
FILE *out;
out = fopen (filename, "wb");
if (!out)
return;
fprintf (out, "P5
");
fprintf (out, "%zu %zu
", image->width, image->height);
fprintf (out, "255
");
fwrite (image->data, 1, image->width * image->height, out);
fclose (out);
}
static void draw_Taijitu(Image *image,int radius,int value)
{
int x,y;
int rlimit ,llimit;
int radius_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 (int argc, 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);
return 0;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11