@jscad/modeling
Version:
Constructive Solid Geometry (CSG) Library for JSCAD
149 lines (133 loc) • 4.93 kB
JavaScript
const test = require('ava')
const { star } = require('./index')
const geom2 = require('../geometries/geom2')
const comparePoints = require('../../test/helpers/comparePoints')
test('star (defaults)', (t) => {
const geometry = star()
const pts = geom2.toPoints(geometry)
const exp = [
[],
[],
[],
[-0.11803398874989482, 0.36327126400268045],
[-0.8090169943749473, 0.5877852522924732],
[-0.38196601125010515, 4.6777345306052316e-17],
[-0.8090169943749475, -0.587785252292473],
[-0.1180339887498949, -0.36327126400268045],
[],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 10)
t.true(comparePoints(pts, exp))
})
test('star (options)', (t) => {
// test center
let geometry = star({ outerRadius: 5, center: [5, 5] })
let pts = geom2.toPoints(geometry)
let exp = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 10)
t.true(comparePoints(pts, exp))
// test vertices
geometry = star({ outerRadius: 5, vertices: 8 })
pts = geom2.toPoints(geometry)
exp = [
[],
[],
[],
[],
[],
[-1.4644660940672622, 3.5355339059327378],
[-3.5355339059327373, 3.5355339059327378],
[-3.5355339059327378, 1.464466094067263],
[-5, 6.123233995736766e-16],
[-3.535533905932738, -1.464466094067262],
[-3.5355339059327386, -3.5355339059327373],
[-1.4644660940672647, -3.535533905932737],
[-9.184850993605148e-16, -5],
[],
[],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 16)
t.true(comparePoints(pts, exp))
// test density
geometry = star({ outerRadius: 5, vertices: 8, density: 3 })
pts = geom2.toPoints(geometry)
exp = [
[],
[],
[],
[],
[],
[-1.0355339059327375, 2.5],
[-3.5355339059327373, 3.5355339059327378],
[-2.5, 1.035533905932738],
[-5, 6.123233995736766e-16],
[-2.5000000000000004, -1.0355339059327373],
[-3.5355339059327386, -3.5355339059327373],
[-1.035533905932739, -2.4999999999999996],
[-9.184850993605148e-16, -5],
[],
[],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 16)
t.true(comparePoints(pts, exp))
// test innerRadius
geometry = star({ outerRadius: 5, vertices: 8, innerRadius: 1 })
pts = geom2.toPoints(geometry)
exp = [
[],
[],
[],
[],
[],
[-0.3826834323650897, 0.9238795325112867],
[-3.5355339059327373, 3.5355339059327378],
[-0.9238795325112867, 0.3826834323650899],
[-5, 6.123233995736766e-16],
[-0.9238795325112868, -0.38268343236508967],
[-3.5355339059327386, -3.5355339059327373],
[-0.38268343236509034, -0.9238795325112865],
[-9.184850993605148e-16, -5],
[],
[],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 16)
t.true(comparePoints(pts, exp))
// test start angle
geometry = star({ outerRadius: 5, startAngle: (360 - 45) * 0.017453292519943295 })
pts = geom2.toPoints(geometry)
exp = [
[],
[],
[],
[],
[-0.7821723252011483, 4.938441702975689],
[-1.3504537836886306, 1.350453783688634],
[-4.9384417029756875, 0.7821723252011604],
[-1.701671040210257, -0.8670447016547808],
[-2.26995249869774, -4.455032620941836],
[]
]
t.notThrows(() => geom2.validate(geometry))
t.deepEqual(pts.length, 10)
t.true(comparePoints(pts, exp))
})