函数图像绘制器

· · 科技·工程

用法见代码末尾。

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
#define ld long double
const int N=1e3+10;
void Print(string OutputString,int ColorID=7){
    HANDLE Handle=::GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO Info;
    GetConsoleScreenBufferInfo(Handle,&Info);
    WORD ColorBak=Info.wAttributes;
    SetConsoleTextAttribute(Handle,ColorID);
    cout<<OutputString;
    SetConsoleTextAttribute(Handle,ColorBak);
    return;
}
int a[N][N],sz=50,las[N][N],mxsz=50;
void SetPosition(int x,int y){
    if(x==-1&&y==-1){
        HANDLE Handle=GetStdHandle(STD_OUTPUT_HANDLE);
        COORD Coord={0,0};
        SetConsoleCursorPosition(Handle,Coord);
    }else{
        HANDLE Handle=GetStdHandle(STD_OUTPUT_HANDLE);
        COORD Coord={2*short(sz+y),short(sz-x)};
        SetConsoleCursorPosition(Handle,Coord);     
    }
    return;
}
ld val1,val2,val3,val4,val5,val6,val7,val8,val9,val0;
ld f(ld x);
bool speed;
bool c(ld x,ld y);
void init();
void drawing();
bool mode,repeat;
void update(){
    memset(a,0,sizeof(a));
    if(mode){
        for(ld x=-10;x<=+10;x+=0.01){
            ld y=f(x);
            int i=x*5,j=y*5;
            if(j>mxsz||j<-mxsz)
                continue;
            a[i+mxsz][j+mxsz]=1;
        }       
    }else{
        for(ld x=-10;x<=+10;x+=0.01){
            for(ld y=-10;y<=+10;y+=0.01)if(c(x,y)){
                int i=x*5,j=y*5;
                a[i+mxsz][j+mxsz]=1;
            }   
        }
    }
    SetPosition(-1,-1);
    for(int y=+sz;y>=-sz;y--){
        for(int x=-sz;x<=sz;x++){
            if(y==sz&&x==2)
                SetPosition(y,x),
                Print("-Y",0x17);
            if(y==-2&&x==sz)
                SetPosition(y,x),
                Print("-X",0x17);
            else if(x==0||y==0){
                string opt="  ";
                if(x&&!(x%5)){
                    if(x>0)opt="+";
                    else opt="-";
                    opt+=char(abs(x)/5+'0');
                }else if(y&&!(y%5)){
                    if(y>0)opt="+";
                    else opt="-";
                    opt+=char(abs(y)/5+'0');                    
                }
                SetPosition(y,x);
                if(!x&&!y)
                    Print("  ",0xAA);
                else if(opt!="  ")
                    Print(opt,(a[x+mxsz][y+mxsz]?0x70:0xE1));
                else if(x==0)
                    Print("||",(a[x+mxsz][y+mxsz]?0x77:0xEE));
                else
                    Print("--",(a[x+mxsz][y+mxsz]?0x77:0xEE));
            }
            else if((y==sz-1&&abs(x)==1)||(y==sz-2&&abs(x)==2))
                SetPosition(y,x),
                Print("  ",0xEE);
            else if((x==sz-1&&abs(y)==1)||(x==sz-2&&abs(y)==2))
                SetPosition(y,x),
                Print("  ",0xEE);
            else if(a[x+mxsz][y+mxsz]!=las[x+mxsz][y+mxsz]){
                SetPosition(y,x);
                if(a[x+mxsz][y+mxsz])
                    Print("XX",0x77);
                else
                    Print("  ",0x07);
            }
        }
        cout<<"\n";
    }
    for(ld x=-10;x<=+10;x+=0.01){
        for(ld y=-10;y<=+10;y+=0.01){
            int i=x*5,j=y*5;
            if(speed)las[i+mxsz][j+mxsz]=a[i+mxsz][j+mxsz];
        }   
    }
    return;
}
signed main(){
    cout<<"Now you can scan your screen";
    srand(time(0));
    Sleep(5000);
    init();
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
    memset(las,-1,sizeof(las));
    if(repeat){
        drawing();
    }else{
        update();
    }
    return 0;
}

// ======= 用户自设部分 =========
// 为保证最佳使用效果,请缩小窗口界面再放大窗口大小,谢谢
ld f(ld x){ // 形如 y=f(x) 的函数,请在这里修改 f(x)
    // Eg: 使用函数 y=cos(ax)*sin(bx)
    return cos(x*val2)*sin(x*val1);
}
bool c(ld x,ld y){ // 通用 x, y 表达式
    // Eg: 画满足 4.9<sqrt(x*x+y*y)<5 的图像(空心圆)
    return (4.9<sqrt(x*x+y*y)&&sqrt(x*x+y*y)<5);
}
void init(){
    sz=50;            // 画布范围 x,y in [-sz,+sz] sz<=50
    mode=1;           // 使用表达式 (0) 还是函数 (1)
    repeat=1;         // 自定义常数变动过程
    speed=1;          // 渲染速度优化
}
void drawing(){ // 在自定义常数的情况下的变动情况,可以为定值
    // Eg. 修改 val1, val2 变量
    // Ex. 你可以使用 val0,val1,……,val9 这十个变量
    //     如果你想要更多,请使用以 val 开头的变量,定义在 29 行
    while(1){
        for(val1=-2;val1<=+2;val1+=0.1) // Eg. 变动 val1 [-2,+2,0.1]
        for(val2=-2;val2<=+2;val2+=0.1) // Eg. 变动 val2 [-2,+2,0.1]
            update(); // 变动后记得调用
    }   
}