Skip to content

Commit

Permalink
feat: applyToPoint()
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryGogonis committed Jul 12, 2017
1 parent ed13778 commit 58d3c2c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
15 changes: 15 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ These matrices can be used for matrix calcuations on SVG CTMs (current transform
* [.isInvertible()](#module_immutable-transform-matrix..Matrix+isInvertible) ⇒ <code>boolean</code>
* [.inverse()](#module_immutable-transform-matrix..Matrix+inverse) ⇒ <code>Matrix</code>
* [.divide(m)](#module_immutable-transform-matrix..Matrix+divide) ⇒ <code>Matrix</code>
* [.applyToPoint(x, y)](#module_immutable-transform-matrix..Matrix+applyToPoint) ⇒ <code>Object</code>
* _static_
* [.Matrix](#module_immutable-transform-matrix..Matrix.Matrix)
* [new Matrix(a, b, c, d, e, f)](#new_module_immutable-transform-matrix..Matrix.Matrix_new)
Expand All @@ -44,6 +45,7 @@ These matrices can be used for matrix calcuations on SVG CTMs (current transform
* [.isInvertible()](#module_immutable-transform-matrix..Matrix+isInvertible) ⇒ <code>boolean</code>
* [.inverse()](#module_immutable-transform-matrix..Matrix+inverse) ⇒ <code>Matrix</code>
* [.divide(m)](#module_immutable-transform-matrix..Matrix+divide) ⇒ <code>Matrix</code>
* [.applyToPoint(x, y)](#module_immutable-transform-matrix..Matrix+applyToPoint) ⇒ <code>Object</code>
* _static_
* [.Matrix](#module_immutable-transform-matrix..Matrix.Matrix)
* [new Matrix(a, b, c, d, e, f)](#new_module_immutable-transform-matrix..Matrix.Matrix_new)
Expand Down Expand Up @@ -163,6 +165,19 @@ Multiplies current matrix with an other matrix.
| --- | --- | --- |
| m | <code>Matrix</code> | matrix divisor |

<a name="module_immutable-transform-matrix..Matrix+applyToPoint"></a>

#### matrix.applyToPoint(x, y) ⇒ <code>Object</code>
Apply current matrix to x and y point.

**Kind**: instance method of [<code>Matrix</code>](#module_immutable-transform-matrix..Matrix)
**Returns**: <code>Object</code> - A new transformed point object

| Param | Type | Description |
| --- | --- | --- |
| x | <code>number</code> | value for x |
| y | <code>number</code> | value for y |

<a name="module_immutable-transform-matrix..Matrix.Matrix"></a>

#### Matrix.Matrix
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lint": "standard src/*.js",
"lint:fix": "npm run lint -- --fix",
"test": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"test:dev": "npm run test -- --watch",
"test:dev": "jest --watch",
"prepublish": "npm run clean & npm run build",
"docs": "jsdoc2md src/index.js > docs/README.md",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ class Matrix extends Map {
inverse.get('f')
)
}

/**
* Apply current matrix to x and y point.
*
* @param {number} x - value for x
* @param {number} y - value for y
* @returns {{x: number, y: number}} A new transformed point object
*/
applyToPoint (x, y) {
return {
x: (x * this.get('a')) + (y * this.get('c')) + this.get('e'),
y: (x * this.get('b')) + (y * this.get('d')) + this.get('f')
}
}
}

export default Matrix
8 changes: 8 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,11 @@ describe('divide', () => {
}).toThrow()
})
})

describe('#applyToPoint', () => {
it('should compute the dot product and return a plain object', () => {
const x = 5
const y = 6
expect(new Matrix().applyToPoint(x, y)).toEqual({x, y})
})
})

0 comments on commit 58d3c2c

Please sign in to comment.