C++ 趣图

一、心形

1、

 http://mathworld.wolfram.com/HeartCurve.html

#include<stdio.h>
using namespace std;
int main()
{
	for (float y=1.5;y>-1.5;y-=0.1) //行 
	{
	    for(float x=-1.5;x<1.5;x+=0.05) //列 
	    {
		    float a=x*x+y*y-1;
	        putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
	    }
	    putchar('\n');
    }
} 

对上面第二个公式的画图。

2、字符下标输出

#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
int main()
{
	for (float f=-1.0;f<=1.0;f+=0.1)
	{
	    cout<<f<<" "<<".:-=+*#%@"[(int)(abs(f)*8)]<<endl;
    }
} 

".:-=+*#%@"这个当成一个字符串,利用下标输出即可
3、
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
	system("mode con cols=500 ");//更改输出界面的宽度
	for (float y=1.5f;y>-1.5f;y-=0.1f)
	{
	    for(float x=-1.5f;x<1.5f;x+=0.05f)
	    {
		    float a=x*x+y*y-1;
		    float f= a*a*a-x*x*y*y*y;
	        putchar(f<=0.0?".:-=+*#%@"[(int)(f*-8)]:' ');
	    }
	    putchar('\n');
    }
} 

利用下标对其输出进行变换

二、文件读写

1、

#include<iostream>
#include<stdio.h>
#include <fstream>
using namespace std;
int main()
{
	ofstream fout("C:/Users/Administrator/Desktop/output.txt");
	int num = 150;
	char name[] = "John Doe";
	fout << "Here is a number " << num << "\n";
	fout << "Now here is a string: " << name << "\n";
	fout << flush; 
	fout.close();
} 

三、

#include <math.h>
#include <stdio.h>
#define T double //双精度 

T f(T x, T y) 
{
    return sqrt(x * x + y * y) - 0.8;//半径0.8 的圆 
}

char outline(T x, T y) 
{
    T delta = 0.001;
    if (fabs(f(x, y)) < 0.05) //0.8圆外又加了0.1宽度外框圆环 
	{
        T dx = f(x + delta, y) - f(x - delta, y);
        T dy = f(x, y + delta) - f(x, y - delta);//求梯度 (斜率)
        return "|/=\\|/=\\|"[(int)((atan2(dy, dx) / 6.2831853072 + 0.5) * 8 + 0.5)];//将斜率与值对应 
    }
    else if (f(x, y) < 0) //圆内输出. 
        return '.';
    else //圆外 不输出 内容 
        return ' ';
}

int main() {
    for (T y = -1; y < 1; y += 0.05)
        for (T x = -1; x < 1; x += 0.025)
            putchar(outline(x, y));
        putchar('\n');
}

 四、

#include<iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define T double

T c(T x,T y,T r){return sqrt(x*x+y*y)-r;} //圆 

T u(T x,T y,T t){return x*cos(t)+y*sin(t);}
T v(T x,T y,T t){return y*cos(t)-x*sin(t);}

T fa(T x,T y){return fmin(c(x,y,0.5),c(x*0.47+0.15,y+0.25,0.3));}//

T no(T x,T y){return c(x*1.2+0.97,y+0.25,0.2);}

T nh(T x,T y){return fmin(c(x+0.9,y+0.25,0.03),c(x+0.75,y+0.25,0.03));}

T ea(T x,T y){return fmin(c(x*1.7+0.3,y+0.7,0.15),c(u(x,y,0.25)*1.7,v(x,y,0.25)+0.65,0.15));}
T ey(T x,T y){return fmin(c(x+0.4,y+0.35,0.1),c(x+0.15,y+0.35,0.1));}
T pu(T x,T y){return fmin(c(x+0.38,y+0.33,0.03),c(x+0.13,y+0.33,0.03));}
T fr(T x,T y){return c(x*1.1-0.3,y+0.1,0.15);}
T mo(T x,T y){return fmax(c(x+0.15,y-0.05,0.2),-c(x+0.15,y,0.25));}
T o(T x,T y,T(*f)(T,T),T i){T r=f(x,y);return fabs(r)<0.02?(atan2(f(x,y+1e-3)-r,f(x+1e-3,y)-r)+0.3)*1.273+6.5:r<0?i:0;}
T s(T x,T y,T(*f)(T,T),T i){return f(x,y)<0?i:0;}

T f(T x,T y){return o(x,y,no,1)?fmax(o(x,y,no,1),s(x,y,nh,12)):fmax(o(x,y,fa,1),fmax(o(x,y,ey,11),fmax(o(x,y,ea,1),fmax(o(x,y,mo,1),fmax(s(x,y,fr,13),s(x,y,pu,12))))));}

int main(int a,char**b)
{
	ofstream fout("C:/Users/Administrator/Desktop/output.txt");
	for(T y=-1,s=a>1?strtod(b[1],0):1;y<0.6;y+=0.05/s)//:1倍数
	{
	    for(T x=-1;x<0.6;x+=0.025/s)
	    {
		    putchar(" .|/=\\|/=\\| @!"[(int)f(u(x,y,0.3),v(x,y,0.3))]);
		    fout<<" .|/=\\|/=\\| @!"[(int)f(u(x,y,0.3),v(x,y,0.3))];
	    }
	    fout<<"\n";//换行
    }
	fout << flush; 
	fout.close();
}

1倍

3倍

4倍

 

 

 

 

 

  

  

 

  

  

  

  

 

posted on 2018-05-31 10:57  箬笠蓑衣  阅读(720)  评论(0编辑  收藏  举报