zoukankan      html  css  js  c++  java
  • C++ 用RGB 三种颜色绘图

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #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 char)(pow((x),1.0/3.0)) // cube root
     
    unsigned char GR(int,int);
    unsigned char BL(int,int);
     
    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;
    }
    void pixel_write(int,int);
    FILE *fp;
    int main(){
        fp = fopen("MathPic.png","wb");
        fprintf(fp, "P6
    %d %d
    255
    ", 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 char color[3];
        color[0] = RD(i,j)&255;
        color[1] = GR(i,j)&255;
        color[2] = BL(i,j)&255;
        fwrite(color, 1, 3, fp);
    }
    # 上边代码保存为 draw.cpp  编译与运行
    
    g++ draw.cpp -o draw  (编译)
    
    ./draw  (运行)
    
    
    然后当前目录就会出现 MathPic.png

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

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

  • 相关阅读:
    BETA 版冲刺前准备
    第十一次作业
    Alpha 冲刺 (10/10)
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    Alpha 冲刺 (5/10)
    Alpha 冲刺 (4/10)
    抽奖系统现场编程
  • 原文地址:https://www.cnblogs.com/lixiuran/p/6213611.html
Copyright © 2011-2022 走看看