[common] rect intersection
This commit is contained in:
@@ -7,6 +7,10 @@ class Rectangle {
|
||||
public var height(default, default):Float;
|
||||
public var center(get, set):Point;
|
||||
public var direction(default, set):Direction;
|
||||
public var left(get, null):Float;
|
||||
public var right(get, null):Float;
|
||||
public var top(get, null):Float;
|
||||
public var bottom(get, null):Float;
|
||||
|
||||
public function new(x:Float, y:Float, width:Float, height:Float) {
|
||||
this.x = x;
|
||||
@@ -61,7 +65,39 @@ class Rectangle {
|
||||
return point.x >= x && point.y >= y && point.x <= width && point.y <= height;
|
||||
}
|
||||
|
||||
public function intersection(rect:Rectangle):Bool {
|
||||
return !(rect.left > right ||
|
||||
rect.right < left ||
|
||||
rect.top > bottom ||
|
||||
rect.bottom < top);
|
||||
}
|
||||
|
||||
public function lean(rect:Rectangle):Void {
|
||||
var side = rect.getSide(direction);
|
||||
if (direction.x != 0) {
|
||||
x = side.point1.x - width * (direction.x + 1) / 2 - direction.x;
|
||||
} else if (direction.y != 0) {
|
||||
y = side.point1.y - height * (direction.y + 1) / 2 - direction.y;
|
||||
}
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return 'Rectangle{x=$x,y=$y,width=$width,height=$height}';
|
||||
}
|
||||
|
||||
function get_left():Float {
|
||||
return x;
|
||||
}
|
||||
|
||||
function get_right():Float {
|
||||
return x + width;
|
||||
}
|
||||
|
||||
function get_top():Float {
|
||||
return y;
|
||||
}
|
||||
|
||||
function get_bottom():Float {
|
||||
return y + height;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user