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

40
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 (2404L/15K).

// random rect inside outer with any size (0*0 to full width/height)
static Rect randomRect(Rect outer) {
  ret randomRect(random_incl(outer.w), random_incl(outer.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, int rw, int rh) {
  if (rw > w-border*2 || rh > h-border*2) null;
  ret new Rect(random(border, w-border-rw), random(border, h-border-rh), rw, rh);
}

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

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   

Travelled to 9 computer(s): aoiabmzegqzx, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006676
Snippet name: randomRect - randomly move rectangle around in larger rectangle
Eternal ID of this version: #1006676/10
Text MD5: 023cdff9749ca65a077327ba80029d13
Transpilation MD5: b78507b861e9931948f7ca36cc639785
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-03 02:26:37
Source code size: 1194 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 413 / 156
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1006676 & http://1006676.tinybrain.de