@jscad/modeling
Version:
Constructive Solid Geometry (CSG) Library for JSCAD
156 lines (137 loc) • 4.82 kB
JavaScript
const test = require('ava')
const { TAU } = require('../maths/constants')
const geom2 = require('../geometries/geom2')
const comparePoints = require('../../test/helpers/comparePoints')
const { circle } = require('./index')
test('circle (defaults)', (t) => {
const geometry = circle()
const pts = geom2.toPoints(geometry)
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 32)
})
test('circle (options)', (t) => {
// test center
let geometry = circle({ radius: 3.5, center: [6.5, 6.5] })
let pts = geom2.toPoints(geometry)
let exp = [
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 32)
t.true(comparePoints(pts, exp))
// test radius
geometry = circle({ radius: 3.5, segments: 16 })
pts = geom2.toPoints(geometry)
exp = [
[ ],
[ ],
[ ],
[ ],
[ ],
[-1.339392013277814, 3.2335783637895035],
[-2.474873734152916, 2.4748737341529163],
[-3.2335783637895035, 1.3393920132778145],
[-3.5, 0],
[-3.233578363789504, -1.3393920132778139],
[-2.474873734152917, -2.474873734152916],
[-1.339392013277816, -3.233578363789503],
[ ],
[ ],
[ ],
[ ]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 16)
t.true(comparePoints(pts, exp))
// test startAngle
geometry = circle({ radius: 3.5, startAngle: TAU / 4, segments: 16 })
pts = geom2.toPoints(geometry)
exp = [
[ ],
[-1.339392013277814, 3.2335783637895035],
[-2.474873734152916, 2.4748737341529163],
[-3.2335783637895035, 1.3393920132778145],
[-3.5, 0],
[-3.233578363789504, -1.3393920132778139],
[-2.474873734152917, -2.474873734152916],
[-1.339392013277816, -3.233578363789503],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 14)
t.true(comparePoints(pts, exp))
// test endAngle
geometry = circle({ radius: 3.5, endAngle: TAU / 4, segments: 16 })
pts = geom2.toPoints(geometry)
exp = [
[ ],
[ ],
[ ],
[ ],
[ ],
[ ]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 6)
t.true(comparePoints(pts, exp))
// test full rotation with non-zero startAngle
geometry = circle({ startAngle: 1, endAngle: 1 + TAU })
pts = geom2.toPoints(geometry)
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 32)
// test segments
geometry = circle({ radius: 3.5, segments: 5 })
pts = geom2.toPoints(geometry)
exp = [
[ ],
[ ],
[-2.8315594803123156, 2.0572483830236563],
[-2.831559480312316, -2.0572483830236554],
[ ]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 5)
t.true(comparePoints(pts, exp))
})
test('circle (radius zero)', (t) => {
const geometry = circle({ radius: 0 })
const pts = geom2.toPoints(geometry)
t.notThrows(() => geom2.validate(geometry))
t.is(pts.length, 0)
})