#用C++生成心形的x、y坐标,heart.cpp:
#include <iostream> #include <fstream> #include <cmath> #define MIN_VALUE 1e-8 int main() { double x,y,t; double step=0.01; bool exit_or_not=false; std::ofstream outfile; outfile.open("heart.dat",std::ofstream::out); for(x=-1.0; x<=1.0+step; x+=step) { if( fabs(x-0.0)<MIN_VALUE ) { x=0.0; } if( fabs(x-1.0)<MIN_VALUE ) { x=1.0; exit_or_not=true; } t = 1-x*x; if(t<0) { continue; } y = sqrt(t)+cbrt(x*x); outfile << x << " " << y << std::endl; if(exit_or_not) { exit_or_not=false; break; } } for(x=1.0; x>=-1.0-step; x-=step) { if( fabs(x-0.0)<MIN_VALUE ) { x=0.0; } if( fabs(x+1.0)<MIN_VALUE ) { x=-1.0; exit_or_not=true; } t = 1-x*x; if(t<0) { continue; } y = -sqrt(t)+cbrt(x*x); outfile << x << " " << y << std::endl; if(exit_or_not) { exit_or_not=false; break; } } return true; }
$ g++ -o heart heart.cpp $ ./heart
#生成了x、y坐标数据文件 heart.dat
再编写Gnuplot脚本,look:
set ter jpeg set out 'heart.jpg' set title 'Heart...' unset key set xlabel 'X' set ylabel 'Y' set xrange [-1.5:1.5] set yrange [-1.2:2.0] set grid plot 'heart.dat' lt 1 pt 3
然后,生成运行,即可生成图片了。
$ cat heart.plt | gnuplot
Love you so much.