Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // remove fully overlapped rectangles
- // drag mouse to draw a new rectangle
- // Code for Processing 3
- int grid = 10; // snap to grid, 1 to disable
- float newX0, newY0;
- float newX1, newY1;
- int rectNumber = 0;
- boolean drawInternal = false; // 'i' key
- boolean transparency = true; // 't' key
- ArrayList<ExtraRect> extraRectList = new ArrayList();
- class Rect {
- float x0, y0, x1, y1;
- Rect(float _x0, float _y0, float _x1, float _y1) {
- x0 = _x0;
- y0 = _y0;
- x1 = _x1;
- y1 = _y1;
- }
- }
- class ExtraRect {
- Rect origRect;
- ArrayList<Rect> rectList;
- int n;
- float c;
- ExtraRect(Rect _origRect, float _c, int _n) {
- origRect = _origRect;
- rectList = new ArrayList();
- rectList.add(origRect);
- c = _c;
- n = _n;
- }
- }
- void setup(){
- size(600, 600);
- colorMode(HSB, 1);
- stroke(0, 0, 0, 0);
- textSize(20);
- }
- void draw(){
- background(1);
- // draw saved rects
- textAlign(CENTER, CENTER);
- for(int i = 0; i < extraRectList.size(); i ++) {
- ExtraRect er = extraRectList.get(i);
- Rect r = er.origRect;
- // draw filled rect
- if (transparency) {
- fill(er.c, 1, 0.8, 0.3);
- } else {
- fill(er.c, 0.4, 0.9, 1);
- }
- rectangle(r.x0, r.y0, r.x1, r.y1);
- // draw internal boundaries
- if (drawInternal) {
- fill(0, 0, 0, 0);
- stroke(0, 0, 0, 1);
- ArrayList<Rect> rectList = er.rectList;
- for(int j = 0; j < rectList.size(); j ++) {
- Rect ir = rectList.get(j);
- hollowRect(ir.x0, ir.y0, ir.x1, ir.y1);
- }
- stroke(0, 0, 0, 0);
- }
- // draw number
- fill(0, 0, 0, 1);
- text(str(er.n), (r.x0+r.x1)/2, (r.y0+r.y1)/2);
- }
- // draw the new rect
- if (mousePressed) {
- fill(0, 0, 0, 0.3);
- rectangle(newX0, newY0, newX1, newY1);
- }
- // draw texts
- textAlign(LEFT, TOP);
- fill(0, 0, 0, 1);
- text("Rects displayed: "+str(extraRectList.size()), 0, 0);
- // find the rect under the cursor (find it from the back)
- for(int i = extraRectList.size()-1; i >= 0; i --) {
- Rect r = extraRectList.get(i).origRect;
- if (r.x0 <= mouseX && mouseX <= r.x1 && r.y0 <= mouseY && mouseY <= r.y1) {
- text("Cursor: "+str(extraRectList.get(i).n), 0, 20);
- break;
- }
- }
- }
- void removeHiddenRects(ArrayList<ExtraRect> extraRects, Rect newRect) {
- Rect nr = newRect;
- // for each ExtraRect
- for (int i = 0; i < extraRects.size(); i ++) {
- ArrayList<Rect> rectList = extraRects.get(i).rectList;
- // for each Rect areas of an ExtraRect
- for (int j = 0; j < rectList.size(); j ++) {
- Rect r = rectList.get(j);
- // check if it's not touching
- if (r.x1 < nr.x0 || nr.x1 < r.x0 || r.y1 < nr.y0 || nr.y1 < r.y0) {
- continue;
- }
- // split rect into smaller rects - clockwise pattern
- //
- // +-n---+ <-- old rect
- // | | |
- // | +-+-e
- // | | | | <-- new rect
- // w-+-+ |
- // | | |
- // +---s-+
- // find each key points
- boolean n = r.x0 < nr.x0 || r.y0 < nr.y0;
- float xn = max(r.x0, nr.x0);
- float yn = r.y0;
- boolean e = nr.x1 < r.x1 || r.y0 < nr.y0;
- float xe = r.x1;
- float ye = max(r.y0, nr.y0);
- boolean s = nr.x1 < r.x1 || nr.y1 < r.y1;
- float xs = min(r.x1, nr.x1);
- float ys = r.y1;
- boolean w = r.x0 < nr.x0 || nr.y1 < r.y1;
- float xw = r.x0;
- float yw = min(r.y1, nr.y1);
- // remove current rect
- rectList.remove(j);
- j --;
- // add new rects
- if (n && e && yn != ye) {
- j ++;
- rectList.add(j, new Rect(xn, yn, xe, ye-1));
- }
- if (e && s && xe != xs) {
- j ++;
- rectList.add(j, new Rect(xs+1, ye, xe, ys));
- }
- if (s && w && ys != yw) {
- j ++;
- rectList.add(j, new Rect(xw, yw+1, xs, ys));
- }
- if (w && n && xw != xn) {
- j ++;
- rectList.add(j, new Rect(xw, yn, xn-1, yw));
- }
- }
- // ExtraRect fully overlapped - remove it
- if (rectList.size() == 0) {
- extraRects.remove(i);
- i --;
- }
- }
- }
- // 4-point rect (inclusive)
- void rectangle(float x0, float y0, float x1, float y1) {
- rect(x0, y0, x1-x0+1, y1-y0+1);
- }
- // pixel perfect inclusive outline drawing
- void hollowRect(float x0, float y0, float x1, float y1) {
- line(x0, y0, x1, y0);
- line(x1, y0, x1, y1);
- line(x1, y1, x0, y1);
- line(x0, y1, x0, y0);
- }
- // mouse callback functions for updating the coord of the new rectangle
- float step(float n) {
- return round(n/grid)*grid;
- }
- void mousePressed() {
- newX0 = step(mouseX);
- newY0 = step(mouseY);
- newX1 = step(mouseX)-1;
- newY1 = step(mouseY)-1;
- }
- void mouseDragged() {
- newX1 = step(mouseX)-1;
- newY1 = step(mouseY)-1;
- }
- void mouseReleased() {
- // correct the coords
- float t;
- if (newX1 < newX0) {
- t = newX0;
- newX0 = newX1+1;
- newX1 = t-1;
- }
- if (newY1 < newY0) {
- t = newY0;
- newY0 = newY1+1;
- newY1 = t-1;
- }
- // clip to boundaries
- newX0 = newX0 < 0 ? 0 : newX0;
- newY0 = newY0 < 0 ? 0 : newY0;
- newX1 = newX1 > width-1 ? width-1 : newX1;
- newY1 = newY1 > height-1 ? height-1 : newY1;
- // add to the extraRect
- if(newX0-newX1 != 1 && newY0-newY1 != 1){
- Rect newRect = new Rect(newX0, newY0, newX1, newY1);
- // before adding, remove overlapping rect(s)
- removeHiddenRects(extraRectList, newRect);
- // add to the list
- rectNumber ++;
- extraRectList.add(new ExtraRect(newRect, random(1), rectNumber));
- }
- }
- // toggle settings
- void keyPressed() {
- if (key == ' ') {
- extraRectList.clear();
- rectNumber = 0;
- }
- else if (key == 'i') {
- drawInternal = !drawInternal;
- }
- else if (key == 't') {
- transparency = !transparency;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement