Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //randomness macro
- #define frandom (float)arc4random()/UINT64_C(0x100000000)
- #define frandom_range(low,high) ((high-low)*frandom)+low
- //this will pick a random point on the rect edge
- - (CGPoint)pickPointOnRectEdge:(CGRect)edge {
- CGPoint pick = CGPointMake(edge.origin.x, edge.origin.y);
- CGFloat a = edge.size.height;
- CGFloat b = edge.size.width;
- CGFloat edgeLength = 2*a + 2*b;
- float randomEdgeLength = frandom_range(0.0f, (float)edgeLength);
- //going from bottom left counter-clockwise
- if (randomEdgeLength<a) {
- //left side a1
- pick = CGPointMake(edge.origin.x, edge.origin.y + a);
- } else if (randomEdgeLength < a+b) {
- //top side b1
- pick = CGPointMake(edge.origin.x + randomEdgeLength - a, edge.origin.y + edge.size.height );
- } else if (randomEdgeLength < (a + b) + a) {
- //right side a2
- pick = CGPointMake(edge.origin.x + edge.size.width, edge.origin.y + randomEdgeLength - (a+b));
- } else {
- //bottom side b2
- pick = CGPointMake(edge.origin.x + randomEdgeLength - (a + b + a), edge.origin.y);
- }
- return pick;
- }
- static Random random = new Random();
- /** returns a point (x,y) uniformly distributed
- * in the border of the rectangle 0<=x<=a, 0<=y<=b
- */
- public static Point2D.Double randomRect(double a, double b) {
- double x = random.nextDouble() * (2 * a + 2 * b);
- if (x < a)
- return new Point2D.Double(x, 0);
- x -= a;
- if (x < b)
- return new Point2D.Double(a, x);
- x -= b;
- if (x < a)
- return new Point2D.Double(x, b);
- else
- return new Point2D.Double(0, x-a);
- }
- void randomPointsOnPerimeter(int x1, int y1, int x2, int y2) {
- int width = abs(x2 - x1);
- int height = abs(y2 - y1);
- int perimeter = (width * 2) + (height * 2);
- // number of points proportional to perimeter
- int n = (int)(perimeter / 8.0f);
- for (int i = 0; i < n; i++) {
- int x, y;
- int dist = rand() % perimeter;
- if (dist <= width) {
- x = (rand() % width) + x1;
- y = y1;
- } else if (dist <= width + height) {
- x = x2;
- y = (rand() % height) + y1;
- } else if (dist <= (width * 2) + height) {
- x = (rand() % width) + x1;
- y = y2;
- } else {
- x = x1;
- y = (rand() % height) + y1;
- }
- // do something with (x, y)...
- }
- }
- function pickPointOnRectEdge(width,height){
- var randomPoint = Math.random() * (width * 2 + height * 2);
- if (randomPoint > 0 && randomPoint < height){
- return {
- x: 0,
- y: height - randomPoint
- }
- }
- else if (randomPoint > height && randomPoint < (height + width)){
- return {
- x: randomPoint - height,
- y: 0
- }
- }
- else if (randomPoint > (height + width) && randomPoint < (height * 2 + width)){
- return {
- x: width,
- y: randomPoint - (width + height)
- }
- }
- else {
- return {
- x: width - (randomPoint - (height * 2 + width)),
- y: height
- }
- }
- }
- function randomOnRect() {
- let r = Math.random();
- return [Math.min(1, Math.max(0, Math.abs((r * 4 - .5) % 4 - 2) - .5)),
- Math.min(1, Math.max(0, Math.abs((r * 4 + .5) % 4 - 2) - .5))]
- }
Add Comment
Please, Sign In to add comment