您的位置:首页 > 其它

续:一种让数值在指定范围内由小到大再变小的生成方法(固定时间内)

2013-09-20 02:29 411 查看
#include "stdafx.h"
#include <windows.h>
#include<time.h>
#include <iostream>

using namespace std;

const static int TIME_PER_FRAME = 100;
SYSTEMTIME sys;

const static int TOTAL_TIME = 10000;
const static int CHANGING_TIMES = 4;

const static int RANGE = 80;
const static int FRONT = 100;

int GetCurTime()
{
GetLocalTime(&sys);
return sys.wMinute * 60000 + sys.wSecond * 1000 + sys.wMilliseconds;//big enough for this test code
}

int main()
{
int startTime,endTime;
int curPassedTime = 0;
int changedTimes = 0;
int curValue = 0;
int TIME_START =  GetCurTime();
while(curPassedTime < TOTAL_TIME)
{
startTime = GetCurTime();
curPassedTime = startTime - TIME_START;

if(curPassedTime >  (changedTimes + 1) * TOTAL_TIME / CHANGING_TIMES)
changedTimes += 1;

int seedTime = curPassedTime -  changedTimes * TOTAL_TIME / CHANGING_TIMES;

if(seedTime < TOTAL_TIME / (2 * CHANGING_TIMES))
curValue = FRONT - seedTime * 2 * RANGE / (TOTAL_TIME / CHANGING_TIMES);
else
curValue = FRONT - 2 * RANGE + seedTime * 2 * RANGE / (TOTAL_TIME / CHANGING_TIMES);

cout<< curValue << endl;

endTime = GetCurTime();
if(endTime - startTime < TIME_PER_FRAME)
Sleep(TIME_PER_FRAME - (endTime - startTime));//fixed FPS
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: