aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/Point.js
blob: 76fd98517f63f4394b3fce2d786654aac2941cce (plain)
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
import Matrix from './Matrix.js'

export default class Point {
  // Initialize
  constructor (...args) {
    this.init(...args)
  }

  // Clone point
  clone () {
    return new Point(this)
  }

  init (x, y) {
    const base = { x: 0, y: 0 }

    // ensure source as object
    const source = Array.isArray(x)
      ? { x: x[0], y: x[1] }
      : typeof x === 'object'
        ? { x: x.x, y: x.y }
        : { x: x, y: y }

    // merge source
    this.x = source.x == null ? base.x : source.x
    this.y = source.y == null ? base.y : source.y

    return this
  }

  toArray () {
    return [ this.x, this.y ]
  }

  transform (m) {
    return this.clone().transformO(m)
  }

  // Transform point with matrix
  transformO (m) {
    if (!Matrix.isMatrixLike(m)) {
      m = new Matrix(m)
    }

    const { x, y } = this

    // Perform the matrix multiplication
    this.x = m.a * x + m.c * y + m.e
    this.y = m.b * x + m.d * y + m.f

    return this
  }

}

export function point (x, y) {
  return new Point(x, y).transformO(this.screenCTM().inverseO())
}