Gnuplot画个心形

#用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.

下载地址:http://pan.baidu.com/s/1kTYDsDh

转载于:https://www.cnblogs.com/qydw000/p/5065181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值