How do I plot random points inside a circle? I have the following code that plots random points but I can't seem to figure out how to plot them inside a circle! I've been using the distance formula to generate random points with no luck. I expected to generate points within a circle but instead i just got a blank screen. Not sure what I am doing wrong.
Here is my code:
#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;
}
Your angle is int
in radians
so it got truncated only to angles {0,1,2,3,4,5,6} [rad]
so you cannot cover the inside of circle just those 7
lines.
you are mixing int
and double
while computation
without proper casting it could got truncated (depends on the compiler). If you realize sin,cos
are in range <-1,+1>
after truncation you got just {-1,0,+1}
which will generate just 9
possible angles. (with combination with #1 even less so you render just few points and most likely did not recognize them in view).
I do not use your rand()
so I am not sure what it returns.
My bet that it returns integer in range up to some RAND_MAX
value.
I am used to VCL style Random()
which have two options:
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)
So if your rand()
is similar then you are truncating the result to {0}
render it useless. Consult the documentation of your rand()
to see if it is integer or floating and change accordingly if needed.
You are most likely shifting the center outside view
you are substracting 50
from value in range <-50,+50>
that shift it to <-100,0>
which I bet is outside your screen. I am too lazy to analyse your code but I think your screen is <-50,+50>
so try to not shift at all
When put all together try this instead:
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.