|
Author |
Topic: Circle Point Picking (Read 3567 times) |
|
rgovostes
|
Circle Point Picking
« on: Mar 5th, 2005, 7:03pm » |
|
This is a Circle class (and a related Point class) which defines a circle centered at (x, y) with a radius of r. Using the "randomPoint()" function you can get a Point object which specifies one coordinate pair on the circumference of the circle. You can also set the stroke and fill colors and have the circle draw itself if needed. Code:class Circle { float r, x, y; color c, d; boolean f, s; Circle(float _x, float _y, float _r) { r = _r; x = _x; y = _y; } Circle(Point _p, float _r) { r = _r; x = _p.x(); y = _p.y(); } Point randomPoint() { float x1 = 2, x2 = 2; while((x1 * x1) + (x2 * x2) >= 1) { x1 = random(-1.0, 1.0); x2 = random(-1.0, 1.0); } float nx = ((x1 * x1) - (x2 * x2)) / ((x1 * x1) + (x2 * x2)); float ny = (2 * x1 * x2) / ((x1 * x1) + (x2 * x2)); nx = (nx * r) + x; ny = (ny * r) + y; return new Point(nx, ny); } float radius() { return r; } boolean drawFill() { return f; } void drawFill(boolean _f) { f = _f; } color fillColor() { return c; } void fillColor(color _c) { c = _c; } boolean drawStroke() { return s; } void drawStroke(boolean _s) { s = _s; } color strokeColor() { return d; } void strokeColor(color _d) { d = _d; } void draw() { f ? fill(c) : noFill(); s ? stroke(d) : noStroke(); ellipseMode(CENTER); ellipse(x, y, 2 * r, 2 * r); } } class Point { float x, y; Point(float _x, float _y) { x = _x; y = _y; } Point(int _x, int _y) { x = (float)_x; y = (float)_y; } float x() { return x; } float y() { return y; } } |
|
|
|
|
|
JohnG
|
Re: Circle Point Picking
« Reply #1 on: Mar 6th, 2005, 1:15pm » |
|
There's an awful lot easier way to create a random point on the circumference than your code: Code: Point randomPoint() { float a=random(TWO_PI); float nx=r*cos(a); float ny=r*sin(a); return new Point(nx,ny); } |
| Should be a fair bit quicker than generating points randomly and hoping they're within the circle, then working out where on the curcumference they project to.
|
|
|
|
Koenie
|
Re: Circle Point Picking
« Reply #2 on: Mar 6th, 2005, 7:45pm » |
|
I think the first method should be a bit faster, because random(), cos() and sin() are pretty slow functions. (Is that the point of this code?) Koenie
|
http://koeniedesign.com
|
|
|
rgovostes
|
Re: Circle Point Picking
« Reply #3 on: Mar 7th, 2005, 1:57pm » |
|
I think the fastest method would be to use Bresenham's algorithm to generate an array of the points on 1/8th of a circle, then randomly pick one and multiply by 1 or -1 for both x and y. However, I'm not sure how much of a speed boost it needs... it seems to run quite quickly as-is.
|
|
|
|
|