Skip to content

Commit

Permalink
feat: growing along different width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Dec 13, 2019
1 parent 86af678 commit 49155ed
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions packages/x6/src/struct/rectangle.ts
Expand Up @@ -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),
)
}

Expand All @@ -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

Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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 &&
Expand Down

0 comments on commit 49155ed

Please sign in to comment.