Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

47
LINES

< > BotCompany Repo | #1006676 // randomRect - randomly move rectangle around in larger rectangle

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5143L/29K).

1  
// random rect inside outer with any size (0*0 to full width/height)
2  
static Rect randomRect(Rect outer) {
3  
  var w = random_incl(outer.w);
4  
  var h = random_incl(outer.h);
5  
  ret randomRect(w, h, outer);
6  
}
7  
8  
static Rect randomRect(int w, int h, Rect outer) {
9  
  ret translateRect(outer.x, outer.y, randomRect(outer.w, outer.h, 0, w, h));
10  
}
11  
12  
static Rect randomRect(Rect outer, int w, int h) {
13  
  ret randomRect(w, h, outer);
14  
}
15  
16  
static Rect randomRect(int w, int h, int border default 0, int rw, int rh) {
17  
  if (rw > w-border*2 || rh > h-border*2) null;
18  
  int ww = random(border, w-border-rw);
19  
  int hh = random(border, h-border-rh);
20  
  ret new Rect(ww, hh, rw, rh);
21  
}
22  
23  
static Rect randomRect(int w, int h) {
24  
  // making variables to ensure functions are called in the same order every time
25  
  int rw = random(w);
26  
  int rh = random(h);
27  
  ret randomRect(w, h, 0, rw, rh);
28  
}
29  
30  
ifclass RGBImage
31  
  static Rect randomRect(RGBImage img, int rw, int rh) {
32  
    ret randomRect(img.w(), img.h(), 0, rw, rh);
33  
  }
34  
  
35  
  static Rect randomRect(RGBImage img) {
36  
    ret randomRect(img.w(), img.h());
37  
  }
38  
endif
39  
40  
// on currentImage()
41  
static Rect randomRect() {
42  
  ret randomRect(currentImage().getWidth(), currentImage().getHeight());
43  
}
44  
45  
static Rect randomRect(BufferedImage img, int rw, int rh) {
46  
  ret randomRect(img.getWidth(), img.getHeight(), 0, rw, rh);
47  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006676
Snippet name: randomRect - randomly move rectangle around in larger rectangle
Eternal ID of this version: #1006676/13
Text MD5: d7de667e4c3ffea61881304193213a2e
Transpilation MD5: fd7bcc44a5e423647f3a12e33872a00a
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-27 14:03:29
Source code size: 1379 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 706 / 535
Version history: 12 change(s)
Referenced in: [show references]