用Rand()获取随机坐标的代码不起作用 [英] Code to get a random coordinate with Rand() does not work

查看:102
本文介绍了用Rand()获取随机坐标的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在演示.我有一堂课,叫做明星".

(在标头中声明了x,y和z浮点数)

Src:

Hi I am working a a demo. I have a class, called "Star".

(Float x, y, and z are declared in the header)

Src:

#include "Star.h"
#include <cstdlib>
#include <windows.h>
#include "GL/glew.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include <iostream>
#include "GLWrapper.h"
#include <time.h>

Star::Star(void)
{
	srand ( time(NULL) );
	x = (float)(((rand() / (RAND_MAX + 1) * 30) - (30 / 2)) + 0);
	y = (float)(((rand() / (RAND_MAX + 1) * 30) - (30 / 2)) + 0);
	z = (float)(((rand() / (RAND_MAX + 1) * 30) - (30 / 2)) + 0);
}

Star::Star(float xpos, float ypos, float zpos)
{
	x = xpos;
	y = ypos;
	z = zpos;
}
Star::Star(float xcenter, float ycenter, float zcenter, float xw, float yw, float zw)
{
	srand ( time(NULL) );
	x = (float)(((rand() / (RAND_MAX + 1) * xw) - (xw / 2)) + xcenter);
	y = (float)(((rand() / (RAND_MAX + 1) * yw) - (yw / 2)) + ycenter);
	z = (float)(((rand() / (RAND_MAX + 1) * zw) - (zw / 2)) + zcenter);
}
Star::~Star(void)
{
}
void Star::Draw()
{
	glColor3f(1, 0, 0);
	glBegin(GL_QUADS);
        glVertex3f(-0.001908 + x, -0.999998 + y, -1.0 + z);
        glVertex3f(0.001907 + x, 0.999998 + y, -1.0 + z);
        glVertex3f(0.001908 + x, 0.999998 + y, 1.0 + z);
        glVertex3f(-0.001907 + x, -0.999998 + y, 1.0 + z);
    glEnd();
    glBegin(GL_QUADS);
        glVertex3f(0.708423 + x, -0.999998 + y, -0.705791 + z);
        glVertex3f(0.71111 + x, 0.999998 + y, -0.703083 + z);
        glVertex3f(-0.708423 + x, 0.999998 + y, 0.705791 + z);
        glVertex3f(-0.71111 + x, -0.999998 + y, 0.703083 + z);
    glEnd();
    glBegin(GL_QUADS);
        glVertex3f(0.999982 + x, -0.999998 + y, 0.006327 + z);
        glVertex3f(0.99995 + x, 0.999998 + y, 0.010142 + z);
        glVertex3f(-0.999982 + x, 0.999998 + y, -0.006327 + z);
        glVertex3f(-0.99995 + x, -0.999998 + y, -0.010142 + z);
    glEnd();
    glBegin(GL_QUADS);
        glVertex3f(0.703302 + x, -0.999998 + y, 0.710894 + z);
        glVertex3f(0.700584 + x, 0.999998 + y, 0.713572 + z);
        glVertex3f(-0.703302 + x, 0.999998 + y, -0.710894 + z);
        glVertex3f(-0.700584 + x, -0.999998 + y, -0.713572 + z);
    glEnd();
}



当我初始化时,x y和z始终为0.为什么?我该如何解决呢?



When i init, x y and z are allways 0. Why? And how can i fix it?

推荐答案

创建Star时,您没有显示代码.如果使用第二个构造函数,则可以将这些坐标初始化为零.

与其他构造函数一样,原因是分裂.您应该将操作数强制转换为除法之前而不是之后的浮点数.
rand() / (MAX_RAND + 1)的除法结果始终为零,因为两个操作数都是整数,并且分母始终大于分子.

尝试使用(float)(rand()) / (MAX_RAND + 1)((float)30)/2.

另外,代码质量也是不可接受的,因为您在各处都硬编码了即时常量(例如30,-0.999998等).您将如何支持并简单地修复此类代码?至少使用显式声明的常量.最终,它们都可以转换为变量.永远不要假设任何魔术值".

另请参见:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [
You did not show the code were you create Star. If you use second constructor, you can initialize those coordinate to zeros.

With other constructors, the reason is division. You should cast operands to float before the division, not after that.
The result of division rand() / (MAX_RAND + 1) is always zero because both operands are integers and because denominator is always greater then numerator.

Try (float)(rand()) / (MAX_RAND + 1) and ((float)30)/2 instead.

Also, the quality of code is unacceptable because you hard-code immediate constants like 30, -0.999998, etc. — everywhere. How are you going to support and simply fix such code? At least use explicitly declared constants. Eventually, they all could be converted to variables. Never assume any "magic values".

See also:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]
In programming, this is one of the first principles.

In all cases, use the debugger in case of a smallest doubt on your run-time behavior, and make sure you do it before asking questions like that; try to provide comprehensive information.

—SA


这篇关于用Rand()获取随机坐标的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆