Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PointArray.transform by analogy to Point.transform #945

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spec/spec/pointarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe('PointArray', function() {
const squareString = '0,0 1,0 1,1 0,1';
const square = new SVG.PointArray(squareString)

describe('toString()', function() {
it('round trips with string', () => {
expect(square.toString()).toEqual(squareString)
})
})

describe('transform()', function() {
it('translates correctly', () => {
const translation = new SVG.Matrix().translate(2,1)
const newSquare = square.transform(translation)
expect(newSquare.toString()).toEqual('2,1 3,1 3,2 2,2')
})

it('transforms like Point', () => {
const matrix = new SVG.Matrix(1, 2, 3, 4, 5, 6)
const newSquare = square.transform(matrix)
for (let i = 0; i < square.length; i++) {
const squarePoint = new SVG.Point(square[i])
const newSquarePoint = new SVG.Point(newSquare[i])
expect(squarePoint.transform(matrix)).toEqual(newSquarePoint)
}
})
})
})
17 changes: 17 additions & 0 deletions src/types/PointArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ extend(PointArray, {
return points
},

// transform points with matrix (similar to Point.transform)
transform (m) {
const points = []

for (let i = 0; i < this.length; i++) {
const point = this[i]
// Perform the matrix multiplication
points.push([
m.a * point[0] + m.c * point[1] + m.e,
m.b * point[0] + m.d * point[1] + m.f
])
}

// Return the required point
return new PointArray(points)
},

// Move point string
move (x, y) {
var box = this.bbox()
Expand Down