Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3891549
  • 博文数量: 146
  • 博客积分: 3918
  • 博客等级: 少校
  • 技术积分: 8585
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-17 13:52
个人简介

个人微薄: weibo.com/manuscola

文章分类

全部博文(146)

文章存档

2016年(3)

2015年(2)

2014年(5)

2013年(42)

2012年(31)

2011年(58)

2010年(5)

分类: C/C++

2011-12-23 23:41:08

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

    先展示下画出来的图像的情况,因为不支持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. }
阅读(41491) | 评论(12) | 转发(5) |
给主人留下些什么吧!~~

jieisme2017-05-04 09:56:09

图形是可以,但这并不是原始天象图

gjf05_052012-06-04 15:46:49

Bean_lee: 大哥,纸上得来终觉浅,你画一遍不就得了,把代码拷下来,调试下不就明白了。
代码面前,没啥秘密。.....
哦,我知道了,求平方根的时候,llimit,rlimit都是可以取到0的。只是不能取负数而已。
又对它了解了一点。

Bean_lee2012-06-04 13:07:05

gjf05_05: 画图函数共有6个循环:
1.-r<x<r,-r<y<r(画个大圆<白色>);
2.0<x<r,-r<y<0(在第一象限填充黑色(0);
3.-1/2r<x<0,-r<y&l.....
大哥,纸上得来终觉浅,你画一遍不就得了,把代码拷下来,调试下不就明白了。
代码面前,没啥秘密。

gjf05_052012-06-03 19:43:36

画图函数共有6个循环:
1.-r<x<r,-r<y<r(画个大圆<白色>);
2.0<x<r,-r<y<0(在第一象限填充黑色(0);
3.-1/2r<x<0,-r<y<0(在圆心上面画一个中圆<半圆>,黑色);
4.1/2r<x<r,0<y<r(大圆心下面的黑色部分);
5.-1/6r<x<1/6r,1/3r<y<2/3r(大圆心下面的小圆<黑色>)
6.-1/6r<x<1/6r,-1/3r<y<-2/3r(大圆心上面的小圆<白色>)
**
3中的1/2r:x^2+(y+1/2r)^2=(1/2r)^2
6中的1/6r:x^2+y^2+ry+2*r^2/9=0;x^2+(y+1/2r)^2=(1/6r)^2;
**
问题:第4中,x不是不能取到

gjf05_052012-06-02 17:18:42

GFree_Wind: 真圆。。。.....
我怎么gcc baguatu.c -o baguatu -lm/*当找不到sqrt()时加的-lm*/
通过之后,左上角不是圆的,而是尖的呢?
这是不是你笑的原因?
错了,是我把第三个for 循环的-y*y掉了。