diff --git a/packages/x6/src/struct/rectangle.ts b/packages/x6/src/struct/rectangle.ts index 91c221945b1..dc6a61b1108 100644 --- a/packages/x6/src/struct/rectangle.ts +++ b/packages/x6/src/struct/rectangle.ts @@ -46,7 +46,7 @@ export class Rectangle { Math.round(this.x), Math.round(this.y), Math.round(this.width), - Math.round(this.height) + Math.round(this.height), ) } @@ -56,7 +56,7 @@ export class Rectangle { x: number | Rectangle | Rectangle.RectangleLike, y?: number, w?: number, - h?: number + h?: number, ): void { const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x @@ -66,6 +66,63 @@ export class Rectangle { this.height = b.height } + grow(amount: number): this + grow(width: number, height: number): this + grow(width: number, height?: number) { + const w = width + const h = height != null ? height : width + this.x -= w + this.y -= h + this.width += 2 * w + this.height += 2 * h + + return this + } + + add(rect: Rectangle | Rectangle.RectangleLike) { + if (rect != null) { + const minX = Math.min(this.x, rect.x) + const minY = Math.min(this.y, rect.y) + const maxX = Math.max(this.x + this.width, rect.x + rect.width) + const maxY = Math.max(this.y + this.height, rect.y + rect.height) + + this.x = minX + this.y = minY + this.width = maxX - minX + this.height = maxY - minY + } + + return this + } + + intersect(rect: Rectangle | Rectangle.RectangleLike) { + if (rect != null) { + const r1 = this.x + this.width + const r2 = rect.x + rect.width + + const b1 = this.y + this.height + const b2 = rect.y + rect.height + + this.x = Math.max(this.x, rect.x) + this.y = Math.max(this.y, rect.y) + this.width = Math.min(r1, r2) - this.x + this.height = Math.min(b1, b2) - this.y + } + + return this + } + + rotate90() { + const t = (this.width - this.height) / 2 + this.x += t + this.y -= t + const tmp = this.width + this.width = this.height + this.height = tmp + + return this + } + containsPoint(x: number, y: number): boolean containsPoint(point: Point | Point.PointLike): boolean containsPoint(x: number | Point | Point.PointLike, y?: number): boolean { @@ -85,7 +142,7 @@ export class Rectangle { x: number | Rectangle | Rectangle.RectangleLike, y?: number, w?: number, - h?: number + h?: number, ) { const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x @@ -108,7 +165,7 @@ export class Rectangle { x: number | Rectangle | Rectangle.RectangleLike, y?: number, w?: number, - h?: number + h?: number, ) { const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x @@ -139,59 +196,6 @@ export class Rectangle { ) } - add(rect: Rectangle | Rectangle.RectangleLike) { - if (rect != null) { - const minX = Math.min(this.x, rect.x) - const minY = Math.min(this.y, rect.y) - const maxX = Math.max(this.x + this.width, rect.x + rect.width) - const maxY = Math.max(this.y + this.height, rect.y + rect.height) - - this.x = minX - this.y = minY - this.width = maxX - minX - this.height = maxY - minY - } - - return this - } - - intersect(rect: Rectangle | Rectangle.RectangleLike) { - if (rect != null) { - const r1 = this.x + this.width - const r2 = rect.x + rect.width - - const b1 = this.y + this.height - const b2 = rect.y + rect.height - - this.x = Math.max(this.x, rect.x) - this.y = Math.max(this.y, rect.y) - this.width = Math.min(r1, r2) - this.x - this.height = Math.min(b1, b2) - this.y - } - - return this - } - - grow(amount: number) { - this.x -= amount - this.y -= amount - this.width += 2 * amount - this.height += 2 * amount - - return this - } - - rotate90() { - const t = (this.width - this.height) / 2 - this.x += t - this.y -= t - const tmp = this.width - this.width = this.height - this.height = tmp - - return this - } - equals(rect: Rectangle | Rectangle.RectangleLike) { return ( rect != null &&