Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18941
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-23 13:27
文章分类
文章存档

2018年(3)

2016年(8)

2015年(1)

我的朋友

分类: C/C++

2018-01-10 19:58:05

原文地址:C语言画图之 画个太极图 作者:Bean_lee

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

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


    今天,二话不说上代码。同时再次致谢Banu前辈。
    
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>

  5. typedef struct {
  6.                 size_t width;
  7.                 size_t height;
  8.                 unsigned char *data;
  9. } Image;

  10. static Image *
  11. image_new (size_t width,
  12.                                 size_t height)
  13. {
  14.                 Image *image;

  15.                 image = malloc (sizeof *image);
  16.                 image->width = width;
  17.                 image->height = height;
  18.                 image->data = malloc (width * height);

  19.                 return image;
  20. }

  21. static void
  22. image_free (Image *image)
  23. {
  24.                 free (image->data);
  25.                 free (image);
  26. }

  27.                 static void
  28. image_fill (Image *image,
  29.                         unsigned char value)
  30. {
  31.                 memset (image->data, value, image->width * image->height);
  32. }

  33. /**
  34.  * image_set_pixel:
  35.  *
  36.  * Sets a pixel passed in signed (x, y) coordinates, where (0,0) is at
  37.  * the center of the image.
  38.  **/
  39. static void
  40. image_set_pixel (Image *image,
  41.                                 ssize_t x,
  42.                                 ssize_t y,
  43.                                 unsigned char value)
  44. {
  45.                 size_t tx, ty;
  46.                 unsigned char *p;

  47.                 tx = (image->width / 2) + x;
  48.                 ty = (image->height / 2) + y;

  49.                 p = image->data + (ty * image->width) + tx;

  50.                 *p = value;
  51. }

  52. static void
  53. image_save (const Image *image,
  54.                         const char *filename)
  55. {
  56.                 FILE *out;

  57.                 out = fopen (filename, "wb");
  58.                 if (!out)
  59.                                 return;

  60.                 fprintf (out, "P5\n");
  61.                 fprintf (out, "%zu %zu\n", image->width, image->height);
  62.                 fprintf (out, "255\n");

  63.                 fwrite (image->data, 1, image->width * image->height, out);

  64.                 fclose (out);
  65. }

  66. static void draw_Taijitu(Image *image,int radius,int value)
  67. {
  68.                 int x,y;
  69.                 int rlimit ,llimit;

  70.                 int radius_2 = radius*radius;
  71.                 for(y = -radius;y<radius;y++)
  72.                     for(x= -radius;x<radius;x++)
  73.                        if(x*x+y*y <= radius_2)
  74.                           image_set_pixel(image,x,y,0xff);

  75.                 for(y = -radius;y<0;y++)
  76.                     for(x = 0;x<radius;x++)
  77.                         if((x*x)+(y*y) <= radius_2)
  78.                           image_set_pixel(image,x,y,value);

  79.                 for(y = -radius;y<0;y++)
  80.                     for(x = -(int)sqrt((double)(-radius*y-y*y));x<0;x++)
  81.                         image_set_pixel(image,x,y,value);


  82.                 for(y = 0;y<radius;y++)
  83.                 {
  84.                                 llimit = (int)sqrt((double)(radius*y - y*y));
  85.                                 rlimit = (int)sqrt((double)(radius_2 - y*y));
  86.                                 for(x = llimit;x<rlimit;x++)
  87.                                                 image_set_pixel(image,x,y,value);
  88.                 }

  89.                 for(y = 2*radius/6;y<4*radius/6;y++)
  90.                 {
  91.                                 rlimit =(int) sqrt((double)(radius*y-y*y-2*radius_2/9));
  92.                                 llimit = -rlimit;

  93.                                 for(x = llimit;x<rlimit;x++)
  94.                                                 image_set_pixel(image,x,y,value);
  95.                 }

  96.                 for(y = -4*radius/6;y<-2*radius/6;y++)
  97.                 {
  98.                                 rlimit = sqrt(-radius*y-y*y-2*radius_2/9);
  99.                                 llimit = -rlimit;
  100.                                 for(x = llimit;x<rlimit;x++)
  101.                                                 image_set_pixel(image,x,y,0xff);
  102.                 }

  103.                 return ;

  104. }
  105.                 int
  106. main (int argc, char *argv[])
  107. {
  108.                 Image *image;

  109.                 image = image_new (800, 800);

  110.                 image_fill (image, 0xaa);
  111.                 draw_Taijitu (image, 300, 0);
  112.                 image_save (image, "taiji_6.pgm");

  113.                 image_free (image);

  114.                 return 0;
  115. }
阅读(697) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~