我想建立一个刺激射弹运动行为的程序。我检查了正确的坐标(x,y),但是编译器没有在正确的坐标处打印,导致倒置的抛射物运动(像运动一样摆动) 文件“myconsole.h”工作正常我已单独检查。 这是我的代码:
#include <iostream>
#include <cmath>
#include <windows.h>
#include "myconsole.h"
using namespace std;
int coordinate(int theta, float v, float g, float initialheight, float x) //function to calculate y coordinate. it accepts x and produces y
{
float y = ((initialheight + (x * tan(theta))) - (( g * (x * x))/(2 * (powf((v * cos(theta)), 2)))));//formula
return y;
}
int main()
{
float initialheight = 0;
int y = 0;
int x = 0;
float v = 0;
int theta =0;
float g = 9.81;
cout<<"Enter the following Information"<<endl;
cout<<"Angle (theta) = ";
cin>>theta;
cout<<"Initial Velocity (V) = ";
cin>>v;
cout<<"Initial height = ";
cin>>initialheight;
float sq = 2*g*initialheight;//just for simplification
float sqroot = powf(v * (sin(theta)), 2);//just for simplification
float d = ((v * (cos(theta))/g)*((v * (sin(theta))+ sqrt(sqroot + sq))));
/*equation to calculate total distance covered
by the projectile. I started x from 0 and add 1 to it till it reaches d.for every value of x we get the
corisponding value of y from function coordinate.*/
ClearScreen();
while(x <= d)//loop to increment x
{
y = coordinate(theta, v, g, initialheight, x);
PlaceCursor(x, y); //using function from console.h and placing the cursor at (x, y) position
cout<<"*";
/*although the coordinates are stimulating the correct behavior of projectile but "* " are printing
an inverted projectile*/
x++;
}
system("pause");
return 0 ;
}
答案 0 :(得分:0)
我相信控制台窗口的坐标系从左上角的0,0开始,y增加 down 窗口。你的代码是正确的,图表本身是反转的,因为这是协调的工作方式。
我改变了你的代码,以获得理想的结果:
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo (hStdout, &csbiInfo);
int winheight = csbiInfo.dwSize.Y ;
system("cls");
while(x <= d)//loop to increment x
{
y = coordinate(theta, v, g, initialheight, x);
COORD pos;
pos.X = x;
pos.Y = winheight -y;
SetConsoleCursorPosition(hStdout, pos);
cout<<"*";
答案 1 :(得分:0)
如果没有编码,就应该修复它,将重力值更改为等式中的负值,因为它向你的射弹方向拉动。