• API
• FAQ
• Tools
• Archive
daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Jan 19th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. //randomness macro
2. #define frandom (float)arc4random()/UINT64_C(0x100000000)
3. #define frandom_range(low,high) ((high-low)*frandom)+low
4.
5. //this will pick a random point on the rect edge
6. - (CGPoint)pickPointOnRectEdge:(CGRect)edge {
7.   CGPoint pick = CGPointMake(edge.origin.x, edge.origin.y);
8.   CGFloat a = edge.size.height;
9.   CGFloat b = edge.size.width;
10.   CGFloat edgeLength = 2*a + 2*b;
11.
12.   float randomEdgeLength = frandom_range(0.0f, (float)edgeLength);
13.
14.   //going from bottom left counter-clockwise
15.   if (randomEdgeLength<a) {
16.     //left side a1
17.     pick = CGPointMake(edge.origin.x, edge.origin.y + a);
18.   } else if (randomEdgeLength < a+b) {
19.     //top side b1
20.     pick = CGPointMake(edge.origin.x + randomEdgeLength - a, edge.origin.y + edge.size.height );
21.   } else if (randomEdgeLength < (a + b) + a) {
22.     //right side a2
23.     pick = CGPointMake(edge.origin.x + edge.size.width, edge.origin.y + randomEdgeLength - (a+b));
24.   } else {
25.     //bottom side b2
26.     pick = CGPointMake(edge.origin.x + randomEdgeLength - (a + b + a), edge.origin.y);
27.   }
28.   return pick;
29. }
30.
31. static Random random = new Random();
32.
33.  /** returns a point (x,y) uniformly distributed
34.   * in the border of the rectangle 0<=x<=a, 0<=y<=b
35.   */
36.  public static Point2D.Double randomRect(double a, double b) {
37.     double x = random.nextDouble() * (2 * a + 2 * b);
38.     if (x < a)
39.         return new Point2D.Double(x, 0);
40.     x -= a;
41.     if (x < b)
42.         return new Point2D.Double(a, x);
43.     x -= b;
44.     if (x < a)
45.         return new Point2D.Double(x, b);
46.     else
47.         return new Point2D.Double(0, x-a);
48.  }
49.
50. void randomPointsOnPerimeter(int x1, int y1, int x2, int y2) {
51.     int width = abs(x2 - x1);
52.     int height = abs(y2 - y1);
53.     int perimeter = (width * 2) + (height * 2);
54.
55.     //  number of points proportional to perimeter
56.     int n = (int)(perimeter / 8.0f);
57.
58.     for (int i = 0; i < n; i++) {
59.         int x, y;
60.         int dist = rand() % perimeter;
61.
62.         if (dist <= width) {
63.             x = (rand() % width) + x1;
64.             y = y1;
65.         } else if (dist <= width + height) {
66.             x = x2;
67.             y = (rand() % height) + y1;
68.         } else if (dist <= (width * 2) + height) {
69.             x = (rand() % width) + x1;
70.             y = y2;
71.         } else {
72.             x = x1;
73.             y = (rand() % height) + y1;
74.         }
75.
76.         //  do something with (x, y)...
77.
78.     }
79. }
80.
81. function pickPointOnRectEdge(width,height){
82.             var randomPoint = Math.random() * (width * 2 + height * 2);
83.             if (randomPoint > 0 && randomPoint < height){
84.                 return {
85.                     x: 0,
86.                     y: height - randomPoint
87.                 }
88.             }
89.             else if (randomPoint > height && randomPoint < (height + width)){
90.                 return {
91.                     x: randomPoint - height,
92.                     y: 0
93.                 }
94.             }
95.             else if (randomPoint > (height + width) && randomPoint < (height * 2 + width)){
96.                 return {
97.                     x: width,
98.                     y: randomPoint - (width + height)
99.                 }
100.             }
101.             else {
102.                 return {
103.                     x: width - (randomPoint - (height * 2 + width)),
104.                     y: height
105.                 }
106.             }
107.         }
108.
109. function randomOnRect() {
110.     let r = Math.random();
111.     return [Math.min(1, Math.max(0, Math.abs((r * 4 - .5) % 4 - 2) - .5)),
112.             Math.min(1, Math.max(0, Math.abs((r * 4 + .5) % 4 - 2) - .5))]
113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top