-
Notifications
You must be signed in to change notification settings - Fork 1
/
selectionRectangle.pde
76 lines (66 loc) · 1.94 KB
/
selectionRectangle.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* SelectionRectangle for Processing 3
* by Debbie Carne.
*
* Model a rectangle that can be drawn corner to corner with the mouse.
*
* requires a function called from draw() that will paint the rectangle. e.g.
*
* void renderSelection() {
* if ( selectionRect.hasRectangle() ){
* // get rect corners and render the rect.
* PVector topLeft = selectionRect.getTopLeft();
* PVector bottomRight = selectionRect.getBottomRight();
*
* rectMode(CORNERS);
* fill(255, 255, 255, 50);
* //strokeWeight(2);
* stroke(255, 255, 255, 80);
* rect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
* }
* }
*/
class SelectionRectangle {
PVector mTopLeft = new PVector();
PVector mBottomRight = new PVector();
boolean mHasRectangle = false; // we start empty
boolean mIsInDrawingState = false;
float WHRatio = 1;
void startDrawingAt(float xx, float yy) {
mTopLeft.set(xx, yy);
mBottomRight.set(xx,yy);
mIsInDrawingState = true;
mHasRectangle = true;
}
void updateBottomRight(float xx, float yy) {
if ( ! isInDrawingState() ) {
throw new RuntimeException("You can only call updateBottomRight() after calling startDrawingRectAt(). Model currently is not in drawing state");
}
mBottomRight.set(xx, yy);
}
boolean isInDrawingState() {
return mIsInDrawingState;
}
void finishDrawing() {
mIsInDrawingState = false;
mHasRectangle = false;
}
PVector getTopLeft(){
return mTopLeft;
}
PVector getBottomRight() {
return mBottomRight;
}
boolean hasRectangle() {
return mHasRectangle;
}
float width() {
// dist(x1, y1, x2, y2)
// TL mTopLeft.x, mTopLeft.y TR mBottomRight.x, mTopLeft.y
// BL mTopLeft.x, mBottomRight.y BR mBottomRight.x, mBottomRight.y
return dist(mTopLeft.x, mTopLeft.y, mBottomRight.x, mTopLeft.y);
}
float height() {
return dist(mTopLeft.x, mTopLeft.y, mTopLeft.x, mBottomRight.y);
}
}