我在使用 C++ 时遇到了一个奇怪的问题。在 OpenGL 代码中,我尝试以这种方式计算一个简单的表达式
void mouse(int button, int state, int x, int y){
double posx = (x-300)/300;
double posy = -1*(y-300)/300;
switch(button){
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN){
if(count < max){
particles[count].x = posx;
particles[count].y = posy;
particles[count].active = true;
count++;
}
glutPostRedisplay();
}
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,每个粒子都出现在屏幕中央。奇怪的是,当我替换double posx = (x-300)/300为
double posx;
posx = x-300;
posx = posx/300;
Run Code Online (Sandbox Code Playgroud)
(与 y 相同)代码有效......有人知道为什么会这样吗?也许这是一件非常简单的事情,我做错了。自从我使用 C++ 已经有很长时间了,如果这是我的一个简单错误,那么抱歉。
c++ ×1