forked from andreaferretti/paths-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon.js
60 lines (54 loc) · 1.64 KB
/
polygon.js
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
import Polygon from '../dist/node/polygon.js'
import expect from 'expect.js'
describe('polygon function', () => {
it('should start by moving to the first point of the polygon', () => {
let polygon = Polygon({
points: [[1, 6], [3, 1], [7, 5], [7, 2], [3, -1]],
closed: true
})
let path = polygon.path.print()
expect(path.substring(0, 5)).to.be('M 1 6')
})
it('should give the same points that are fed as input', () => {
let points = [[1, 6], [3, 1], [7, 5], [7, 2], [3, -1]]
let polygon = Polygon({
points: points,
closed: true
})
expect(polygon.path.points()).to.eql(points)
})
it('should correctly handle closed and open polygons', () => {
let points = [[-1, 3], [2, 17], [3, 5], [4, 6]]
let polygon1 = Polygon({
points: points,
closed: true
})
let polygon2 = Polygon({
points: points,
closed: false
})
expect(polygon1.path.print()).to.match(/Z/)
expect(polygon2.path.print()).not.to.match(/Z/)
})
it('should compute the expected centroid of a square', () => {
let square = Polygon({
points: [[-1, -1], [-1, 1], [1, 1], [1, -1]],
closed: true
})
expect(square.centroid).to.eql([0, 0])
})
it('should compute the expected centroid of a rotated square', () => {
let square = Polygon({
points: [[-1, 1], [0, 0], [1, 1], [0, 2]],
closed: true
})
expect(square.centroid).to.eql([0, 1])
})
it('should compute the expected centroid of a triangle', () => {
let triangle = Polygon({
points: [[0, 3], [0, 0], [-3, 0]],
closed: true
})
expect(triangle.centroid).to.eql([-1, 1])
})
})