当前位置: 代码迷 >> 图形/图像 >> I do. 【附:C++用不多于一条推博字符数的代码会作出怎样的图像?】
  详细解决方案

I do. 【附:C++用不多于一条推博字符数的代码会作出怎样的图像?】

热度:542   发布时间:2016-05-13 02:26:25
I do. 【附:C++用不多于一条推博字符数的代码能作出怎样的图像?】

以上为原创主贴

以下为转帖及少量翻译。

很多C++程序员在进行的一场比赛:

用不多于一条微博的字符数的代码能作出怎样的数学图像?

http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art

比赛用的基础代码及其来源:http://pastebin.com/uQkCQGhz

// NOTE: compile with g++ filename.cpp -std=c++11//    also, if this doesn't work for you, try the alternate char-based version#include <iostream>


// NOTE: compile with g++ filename.cpp -std=c++11//    also, if this doesn't work for you, try the alternate char-based version #include <iostream>#include <cmath>#define DIM 1024#define DM1 (DIM-1)#define _sq(x) ((x)*(x))                           // square#define _cb(x) abs((x)*(x)*(x))                    // absolute value of cube#define _cr(x) (unsigned short)(pow((x),1.0/3.0))  // cube root unsigned short GR(int,int);unsigned short BL(int,int); unsigned short RD(int i,int j){    // YOUR CODE HERE}unsigned short GR(int i,int j){    // YOUR CODE HERE}unsigned short BL(int i,int j){    // YOUR CODE HERE} void pixel_write(int,int);FILE *fp;int main(){    fp = fopen("MathPic","wb");    fprintf(fp, "P6\n%d %d\n1023\n", DIM, DIM);    for(int j=0;j<DIM;j++)        for(int i=0;i<DIM;i++)            pixel_write(i,j);    fclose(fp);    return 0;}void pixel_write(int i, int j){    static unsigned short color[3];    color[0] = RD(i,j)&DM1;    color[1] = GR(i,j)&DM1;    color[2] = BL(i,j)&DM1;    fwrite(color, 2, 3, fp);}


悄悄问一声:看到有人画美国国旗,但那个难度似乎高些,画的不够完整;

我国国旗简单明快,谁能把我国的国旗给插上去?即使不参赛过去围观学习也不错

以前用LaTeX-pstricks做过一个,关键点坐标都计算好了:

http://blog.csdn.net/stereohomology/article/details/42736953

它的草图设计是这样的,如果直接从设计图出发也可以:




发现这个帖子的过程是这样的:


1. 先从知乎上看到一个用C++代码例子的问答,代码是转帖,但是结果相当漂亮。

http://www.zhihu.com/question/30262900/answer/48741026


2 然后根据链接找到了顾森的博客:

http://www.matrix67.com/blog/archives/6039


3. 最终发现是stackexchange上的。惊讶

http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art

我比较喜欢的是这种:

<span style="font-size:12px;">unsigned char RD(int i,int j){    float s=3./(j+99);    float y=(j+sin((i*i+_sq(j-700)*5)/100./DIM)*35)*s;    return (int((i+DIM)*s+y)%2+int((DIM*2-i)*s+y)%2)*127;}unsigned char GR(int i,int j){    float s=3./(j+99);    float y=(j+sin((i*i+_sq(j-700)*5)/100./DIM)*35)*s;    return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127;}unsigned char BL(int i,int j){    float s=3./(j+99);    float y=(j+sin((i*i+_sq(j-700)*5)/100./DIM)*35)*s;    return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127;}</span>


unsigned char RD(int i,int j){#define P 6.03float s=3./(j+250),y=(j+sin((i*i+_sq(j-700)*5)/100./DIM+P)*15)*s;return (int((i+DIM)*s+y)%2+int((DIM*2-i)*s+y)%2)*127;}unsigned char GR(int i,int j){float s=3./(j+250);float y=(j+sin((i*i+_sq(j-700)*5)/100./DIM+P)*15)*s;return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127;}unsigned char BL(int i,int j){float s=3./(j+250);float y=(j+sin((i*i+_sq(j-700)*5)/100./DIM+P)*15)*s;return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127;}

以及这种:

unsigned char RD(int i,int j){        #define A float a=0,b,k,r,x    #define B int e,o    #define C(x) x>255?255:x    #define R return    #define D DIM    R BL(i,j)*(D-i)/D;}unsigned char GR(int i,int j){          #define E DM1    #define F static float    #define G for(    #define H r=a*1.6/D+2.4;x=1.0001*b/D    R BL(i,j)*(D-j/2)/D;}unsigned char BL(int i,int j){    F c[D][D];if(i+j<1){A;B;G;a<D;a+=0.1){G b=0;b<D;b++){H;G k=0;k<D;k++){x=r*x*(1-x);if(k>D/2){e=a;o=(E*x);c[e][o]+=0.01;}}}}}R C(c[j][i])*i/D;}


<span style="font-size:12px;">for(double a=0;a<DIM;a+=0.1){       // Incrementing a by 1 will miss points    for(int b=0;b<DIM;b++){         // 1024 here is arbitrary, but convenient        double r = a*(1.6/DIM)+2.4; // This is the r in the logistic bifurcation diagram (x axis)        double x = 1.0001*b/DIM;    // This is x in the logistic bifurcation diagram (y axis). The 1.0001 is because nice fractions can lead to pathological behavior.        for(int k=0;k<DIM;k++){            x = r*x*(1-x);          // Apply the logistic map to x            // We do this DIM/2 times without recording anything, just to get x out of unstable values            if(k>DIM/2){                if(c[(int)a][(int)(DM1*x)]<255){                    c[(int)a][(int)(DM1*x)]+=0.01; // x makes a mark in c[][]                } // In the golfed code, I just always add 0.01 here, and clip c to 255            }        }                }    }</span>


因为绝大多数代码答案并没有特殊的针对某种平台的独特的函数或命令,

初步测试,Visual C++、GCC都可编译。

只不过有库的限制和字符数的约定,build的时候可能会出现很多warning


那些为了节约代码而定义了很多宏的,显然不是经济的风格,相当低效。不过反正只是一种代码填字游戏。


  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!