File size: 1,644 Bytes
f23825d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
import { IPoint } from "../geometry"

export interface IRect extends IPoint {
  width: number
  height: number
}

export function containsPoint(rect: IRect, point: IPoint) {
  return (
    point.x >= rect.x &&
    point.x <= rect.x + rect.width &&
    point.y >= rect.y &&
    point.y <= rect.y + rect.height
  )
}

export function right(rect: IRect) {
  return rect.x + rect.width
}

export function bottom(rect: IRect) {
  return rect.y + rect.height
}

export function intersects(rectA: IRect, rectB: IRect) {
  return (
    right(rectA) > rectB.x &&
    right(rectB) > rectA.x &&
    bottom(rectA) > rectB.y &&
    bottom(rectB) > rectA.y
  )
}

export function containsRect(rectA: IRect, rectB: IRect) {
  return containsPoint(rectA, rectB) && containsPoint(rectA, br(rectB))
}

export function br(rect: IRect): IPoint {
  return {
    x: right(rect),
    y: bottom(rect),
  }
}

export function fromPoints(pointA: IPoint, pointB: IPoint): IRect {
  const x1 = Math.min(pointA.x, pointB.x)
  const x2 = Math.max(pointA.x, pointB.x)
  const y1 = Math.min(pointA.y, pointB.y)
  const y2 = Math.max(pointA.y, pointB.y)

  return {
    x: x1,
    y: y1,
    width: x2 - x1,
    height: y2 - y1,
  }
}

export function scale(rect: IRect, scaleX: number, scaleY: number): IRect {
  return {
    x: rect.x * scaleX,
    y: rect.y * scaleY,
    width: rect.width * scaleX,
    height: rect.height * scaleY,
  }
}

export const zeroRect: IRect = { x: 0, y: 0, width: 0, height: 0 }

export function moveRect(rect: IRect, p: IPoint): IRect {
  return {
    x: rect.x + p.x,
    y: rect.y + p.y,
    width: rect.width,
    height: rect.height,
  }
}