c语言课程设计"小球从空中落下,弹起,再落下,弹起幅度越来越小,直至停下"的动画
1个回答
展开全部
// 环境:Microsoft Visual C++ 6.0 + EasyX
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#define WIDTH 100
#define HEIGHT 400
#define RADIUS 10
#define TIMEDIV 0.001
void f(double height, double air_k)
{
initgraph(WIDTH, HEIGHT, SHOWCONSOLE);
setaspectratio(1, -1);
setorigin(0, HEIGHT - RADIUS);
int y_pos = HEIGHT - RADIUS * 2;
bool is_down = true;
double v = 0;
BeginBatchDraw();
double x_frac = 0, x_int;
for (;;)
{
Sleep((unsigned long)(TIMEDIV * 1000));
double a = ((is_down ? 1 : -1) - air_k) * 9.8;
double x =(is_down ? -1 : 1)
* ( v * TIMEDIV + 0.5 * a * TIMEDIV * TIMEDIV)
* ((HEIGHT - 2 * RADIUS) / height);
x_frac = modf(x + x_frac, &x_int);
y_pos += (int)x_int;
if (is_down && y_pos <= 0)
{
is_down = false;
}
v += a * TIMEDIV;
if (!is_down && v <= 0)
{
if (y_pos > 0) is_down = true;
else break;
}
cleardevice();
solidcircle(WIDTH / 2, y_pos, RADIUS);
FlushBatchDraw();
if (kbhit() && getch() == ' ') break;
}EndBatchDraw();
MessageBox(NULL, "小球已停止", "balldown", 0);
closegraph();
}
int main()
{
do {
double height, air_k;
printf("请输入下落高度(单位:米)(小数,大于0):");
scanf("%lf", &height);
printf("请输入空气阻力系数(即空气阻力与小球重力的比值)(小数,0 ~ 1):");
scanf("%lf", &air_k);
printf("按空格可重新开始");
f(height, air_k);
Sleep(1);
system("cls");
} while (1);
getch();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询