抛体运动
速率10m/s,抛射角60度,运动时间2.3s
同样的速率,平抛,运动时间1.0s
程序代码:
#include <Windows.h>
#include <clocale>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <conio.h>
// 全局常数及变量
const double PI(2 * asin(1.0)); // 圆周率
const WCHAR K_ENTER(0x000D); // Enter键码
const RECT Border = { 10, 10, 620, 350 }; // 图框位置及尺寸
const POINT Origin = { 20, 170 }; // 坐标原点位置
const double Scale(30.0); // 放大系数
HDC hDC(GetDC(GetConsoleWindow())); // 绘图设备
// 函数原型
void DrawGraph(void); // 绘制图像
void Wait(void); // 等待按键
// 程序入口
void wmain(void)
{
setlocale(LC_ALL, "chs");
DrawGraph();
Wait();
ReleaseDC(NULL, hDC);
}
void DrawGraph(void)
{
const int radius(5); // 质点半径
const double angle(PI / 3); // 抛射角
const double v0(10.0); // 初始速率
const double g(9.8); // 重力加速度
const double delta_time(0.03); // 时间间隔
HPEN hPen1(CreatePen(PS_SOLID, 1, RGB(50, 50, 50)));
HBRUSH hBrush1(CreateSolidBrush(RGB(50, 50, 50)));
HPEN hPen2(CreatePen(PS_SOLID, 1, RGB(255, 255, 0)));
HBRUSH hBrush2(CreateSolidBrush(RGB(255, 255, 0)));
double x(0.0), y(0.0), t(0.0);
clock_t Clock(clock());
POINT point;
bool refresh(true);
while (t < 2.3)
{
if (clock() >= Clock + delta_time * CLOCKS_PER_SEC)
{
SelectObject(hDC, hPen1);
SelectObject(hDC, hBrush1);
Ellipse(hDC, point.x - radius, point.y - radius, point.x + radius, point.y + radius);
Clock = clock();
t += delta_time;
refresh = true;
}
if (refresh)
{
x = v0 * t * cos(angle);
y = v0 * t * sin(angle) - 0.5 * g * t * t;
point.x = static_cast<int>(Origin.x + x * Scale);
point.y = static_cast<int>(Origin.y - y * Scale);
SelectObject(hDC, hPen2);
SelectObject(hDC, hBrush2);
Ellipse(hDC, point.x - radius, point.y - radius, point.x + radius, point.y + radius);
refresh = false;
}
}
DeleteObject(hPen1);
DeleteObject(hBrush1);
DeleteObject(hPen2);
DeleteObject(hBrush2);
}
void Wait(void)
{
const WCHAR msg[] = L"按<Enter>键结束程序";
RECT rect = { Border.left, Border.bottom + 20, Border.right, Border.bottom + 50 };
SetTextColor(hDC, RGB(255,255,255));
SetBkColor(hDC, TRANSPARENT);
DrawTextW(hDC, msg, ARRAYSIZE(msg), &rect, DT_CENTER);
HANDLE hConsole(GetStdHandle(STD_OUTPUT_HANDLE)); // 控制台句柄
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(hConsole, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hConsole, &CursorInfo);
while (_getwch() != K_ENTER)
{
;
}
CursorInfo.bVisible = true;
SetConsoleCursorInfo(hConsole, &CursorInfo);
}
[ 本帖最后由 TonyDeng 于 2012-4-17 12:26 编辑 ]







