如何使用 OpenGL 在圆内绘制随机点
How do I plot random points inside a circle using OpenGL?
如何在圆内绘制随机点?我有以下代码可以绘制随机点,但我似乎无法弄清楚如何在圆圈内绘制它们!我一直在使用距离公式生成随机点,但没有运气。我希望在一个圆圈内生成点,但我只是得到了一个空白屏幕。不知道我做错了什么。
这是我的代码:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <vector>
#include <cstdlib>
#define __gl_h_
#include <cmath>
#include <iostream>
struct Point
{
float x, y;
unsigned char r, g, b, a;
};
std::vector< Point > points;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("Random Points");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// populate points
for( size_t i = 0; i < 1000; ++i )
{
Point pt;
//pt.x = -50 + (rand() % 100);
//pt.y = -50 + (rand() % 100);
int angle = (rand() % 100 + 1) * 3.1416 * 2;
int radius = (rand() % 100 + 1) * 50;
pt.x = ((radius * cos(angle))-50);
pt.y = ((radius * sin(angle))-50);
pt.r = 125;
pt.g = 125;
pt.b = 125;
pt.a = 255;
points.push_back(pt);
}
glutMainLoop();
return 0;
}
-
您的角度以弧度为单位
int
所以它只被截断到角度
{0,1,2,3,4,5,6} [rad]
所以你不能只覆盖那些7
线的圆的内部。 -
您在计算时混合
int
和double
如果没有适当的转换,它可能会被截断(取决于编译器)。如果您意识到截断后
sin,cos
<-1,+1>
范围内,那么您只会得到{-1,0,+1}
这将产生9
可能的角度。(与#1的组合甚至更少,所以你只渲染几个点,很可能没有在视野中识别它们)。 -
我不使用你的
rand()
所以我不确定它会返回什么。我敢打赌,它返回的整数范围高达某个
RAND_MAX
值。我习惯了VCL风格的
Random()
,它有两个选择:double Random(); // return pseudo-random floating number in range <0.0,1.0) int Random(int max); // return pseudo-random integer number in range <0,max)
因此,如果您的
rand()
相似,那么您正在截断结果以{0}
使其无用。请参阅rand()
的文档以查看它是整数还是浮动,并根据需要进行相应更改。 -
您很可能正在将中心移到外部视图
您正在从范围
<-50,+50>
的值中减去50
,然后将其转换为我敢打赌在您的屏幕之外的<-100,0>
。我懒得分析你的代码,但我认为你的屏幕<-50,+50>
所以尽量不要移动
当全部放在一起时,请尝试以下操作:
double angle = double(rand() % 1000) * 6.283185307179586476925286766559;
int radius = rand() % 51;
pt.x = double(double(radius)*cos(angle));
pt.y = double(double(radius)*sin(angle));
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- QuadTree只在窗口的右上角绘制
- 绘制旋转的三角形
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何在QT中的自定义视频小工具t上绘制矩形
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 如何绘制具有随机顶点数量的多边形并旋转它们
- C++:绘制随机布尔值的性能
- 卡片牌:我该如何制作,所以我只会随机绘制一次卡片
- 如何使用 OpenGL 在圆内绘制随机点
- 在c++ 11中优雅地绘制n个随机值