平面物理引擎(我们的星星)3.0 -- 测试程序示例

(我们的星星)3.0 – 测试程序示例

前面已经有了源码,这边就放一个测试程序

#include<iostream>//输入输出流
#include<time.h>//时间
#include<vector>//vector容器
#include<starx.h>//我们的星星类头文件

#include<fstream>//文件读写流


using namespace std;//引用命名空间std

void Write2file(ofstream& output, const double &data)
{
    output<<data<<endl;
}


//void cd(const starx& g){
//    g.speed(x);
//}

int main()
{
	/*准备三个文件流用以存放坐标数据*/
    ofstream output1x("D:\\Lists\\Matlab\\application\\movedatax.txt",ofstream::out|ofstream::trunc);
    ofstream output1y("D:\\Lists\\Matlab\\application\\movedatay.txt",ofstream::out|ofstream::trunc);

    ofstream output2x("D:\\Lists\\Matlab\\application\\movedata2x.txt",ofstream::out|ofstream::trunc);
    ofstream output2y("D:\\Lists\\Matlab\\application\\movedata2y.txt",ofstream::out|ofstream::trunc);

    ofstream output3x("D:\\Lists\\Matlab\\application\\movedata3x.txt",ofstream::out|ofstream::trunc);
    ofstream output3y("D:\\Lists\\Matlab\\application\\movedata3y.txt",ofstream::out|ofstream::trunc);

	/* 定义三颗星星 */
    starx xx2(2,3,0,-18,1);//(0,1.3)位置 初始速度(0,-1)质量1
    starx xx(0,0,0,-18,1);//(0,1)位置 初始速度(0,-1)质量1
    starx t0(1,0,0,0,50);
   
	/*设置计时器(作为时间基准,参考头文件<time.h>)*/
    clock_t now;
    double dtime=0;
    now = clock();//zero = now;


    int tt=0;
    while(tt<70000)//计算星星们的坐标70000次
    {
		/* 当时间变化可见时 (避免循环间隔过短造成计算次数不足)*/
        if(now != clock())
        {
        		/* 指明受力集合 */
        		vector<starx> stx1={t0};//xx受力来源
                vector<starx> stx2={t0,xx};
                vector<starx> stx3={xx,xx2};

                dtime = ((double)(clock()-now))/CLK_TCK;
                /* 计算上一个时间间隔的位移 */
                xx.kinesis(dtime);
                xx2.kinesis(dtime);
                //t0.kinesis(dtime);
                /*计算受力(实际上是合力)*/
                xx.gravitation(stx1);
                xx2.gravitation(stx2);
                //t0.gravitation(stx3);
                /*加速度*/
                xx.accelerate();
                xx2.accelerate();
                //t0.accelerate();
                /* 速度 */
                xx.velocity(dtime);
                xx2.velocity(dtime);
                //t0.velocity(dtime);
                now = clock();
                tt++;

                cout<<"location:"<<xx.location(x)<<","<<xx.location(y)
                <<"\tvelocity:"<<xx.speed(y)
                <<"\tforce:"<<xx.force(x)<<","<<xx.force(y)<<endl;

                //记录x、y坐标到对应文件中
                Write2file(output1x,xx.location(x));
                Write2file(output1y,xx.location(y));

                Write2file(output2x,xx2.location(x));
                Write2file(output2y,xx2.location(y));

                Write2file(output3x,t0.location(x));
                Write2file(output3y,t0.location(y));

        }
    }
    now = clock();

    //cout<<"run time:"<<(double)(now-zero)/CLK_TCK<<endl;
    return 0;
}


然后使用matlab绘制轨迹曲线

  • 代码如下:
%分析平面物理模型的数据

%载入数据
load movedatax.txt;
load movedatay.txt;
load movedata2x.txt;
load movedata2y.txt;
load movedata3x.txt;
load movedata3y.txt;
data1x = movedatax;
data1y = movedatay;
data2x = movedata2x;
data2y = movedata2y;
data3x = movedata3x;
data3y = movedata3y;
%构建x轴离散点   --与data等长
%xis = 1:length(data);
%绘制散点图
%plot(xis,data,'x');
%hold on;
%subplot(1,2,2);
%format;
hold on;
plot(data1x,data1y);
plot(data2x,data2y);
plot(data3x,data3y);%平滑曲线方便观察数据特征
title("质点运动轨迹(70000点数据)");
xlabel("x坐标");
ylabel("y坐标");
% hold on;
plot(2,3,'bx');
plot(0,0,'rx');
plot(1,0,'gx');

看看结果
在这里插入图片描述

放大看看

在这里插入图片描述

ok

准备工作(类的设计思路&头文件):https://blog.csdn.net/qq_33904382/article/details/102757667

类源文件(.cpp):
https://blog.csdn.net/qq_33904382/article/details/102789576

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值