@jscad/modeling
Version:
Constructive Solid Geometry (CSG) Library for JSCAD
89 lines (73 loc) • 2.56 kB
JavaScript
const test = require('ava')
const { TAU } = require('../../maths/constants')
const { appendArc, fromPoints, toPoints } = require('./index')
const { comparePoints } = require('../../../test/helpers/')
test('appendArc: appending to an empty path produces an error', (t) => {
const p1 = fromPoints({}, [])
t.throws(() => appendArc({ endpoint: [12, 12] }, p1),
{ message: 'the given path must contain one or more points (as the starting point for the arc)' })
})
test('appendArc: appending to a path produces a new path', (t) => {
const p1 = fromPoints({}, [[1, 1], [2, 2]])
let obs = appendArc({ endpoint: [-2, 2] }, p1)
let pts = toPoints(obs)
t.is(pts.length, 3)
// test radius
const p2 = fromPoints({}, [[27, -22], [27, -3]])
obs = appendArc({ endpoint: [12, -22], radius: [15, -20] }, p2)
pts = toPoints(obs)
t.is(pts.length, 7)
// test segments
obs = appendArc({ endpoint: [12, -22], radius: [15, -20], segments: 64 }, p2)
pts = toPoints(obs)
t.is(pts.length, 19)
// test clockwise
obs = appendArc({ endpoint: [12, -22], radius: [15, -20], clockwise: true }, p2)
pts = toPoints(obs)
let exp = [
[],
[],
[],
[],
[],
[],
[]
]
t.is(pts.length, 7)
t.true(comparePoints(pts, exp))
// test large
obs = appendArc({ endpoint: [12, -22], radius: [15, -20], large: true }, p2)
pts = toPoints(obs)
t.is(pts.length, 16)
// test xaxisrotation
obs = appendArc({ endpoint: [12, -22], radius: [15, -20], xaxisrotation: TAU / 4 }, p2)
pts = toPoints(obs)
exp = [
[],
[],
[],
[],
[],
[],
[],
[]
]
t.is(pts.length, 8)
t.true(comparePoints(pts, exp))
// test small arc between far points
obs = appendArc({ endpoint: [120, -220], radius: [5, -5] }, p2)
pts = toPoints(obs)
t.is(pts.length, 2)
})
test('appendArc: appending to a path produces exact endpoint', (t) => {
let p1 = fromPoints({}, [[18, 1.8], [1, 3]])
const endpoint = [1, -3]
p1 = appendArc({
endpoint,
radius: [4, 4],
segments: 36,
large: true
}, p1)
const pts = toPoints(p1)
t.deepEqual(pts[pts.length - 1], endpoint)
})