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).

// random rect inside outer with any size (0*0 to full width/height)
static Rect randomRect(Rect outer) {
  var w = random_incl(outer.w);
  var h = random_incl(outer.h);
  ret randomRect(w, h, outer);
}

static Rect randomRect(int w, int h, Rect outer) {
  ret translateRect(outer.x, outer.y, randomRect(outer.w, outer.h, 0, w, h));
}

static Rect randomRect(Rect outer, int w, int h) {
  ret randomRect(w, h, outer);
}

static Rect randomRect(int w, int h, int border default 0, int rw, int rh) {
  if (rw > w-border*2 || rh > h-border*2) null;
  int ww = random(border, w-border-rw);
  int hh = random(border, h-border-rh);
  ret new Rect(ww, hh, rw, rh);
}

static Rect randomRect(int w, int h) {
  // making variables to ensure functions are called in the same order every time
  int rw = random(w);
  int rh = random(h);
  ret randomRect(w, h, 0, rw, rh);
}

ifclass RGBImage
  static Rect randomRect(RGBImage img, int rw, int rh) {
    ret randomRect(img.w(), img.h(), 0, rw, rh);
  }
  
  static Rect randomRect(RGBImage img) {
    ret randomRect(img.w(), img.h());
  }
endif

// on currentImage()
static Rect randomRect() {
  ret randomRect(currentImage().getWidth(), currentImage().getHeight());
}

static Rect randomRect(BufferedImage img, int rw, int rh) {
  ret randomRect(img.getWidth(), img.getHeight(), 0, rw, rh);
}

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: 615 / 423
Version history: 12 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1029737 - randomRect_enclosing_in - rect that contains x and is contained in y [dev.]