Skip to content

Commit

Permalink
implement Component:createDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
axelpale committed Jan 17, 2025
1 parent c808105 commit 8a8259f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
1 change: 0 additions & 1 deletion lib/components/Component/createDirection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = function (theta, phi) {
// a Direction
//

// TODO rename to createDirection
if (typeof theta !== 'number') {
throw new Error('Invalid theta angle')
}
Expand Down
21 changes: 21 additions & 0 deletions lib/components/Component/createDistance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Distance = require('../../geometry/Distance')

module.exports = function (dist) {
// @Component:createDistance(dist)
//
// Create a Distance relative to the component.
//
// Parameters
// dist
// a number, a distance in the coordinate space of the component.
//
// Return
// a Distance
//

if (typeof dist !== 'number') {
throw new Error('Invalid distance. Must be a number.')
}

return new Distance(this, dist)
}
1 change: 1 addition & 0 deletions lib/components/Component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ proto.bringToFront = require('./bringToFront')
proto.createBasis = require('./createBasis')
proto.createPoint = proto.at
proto.createDirection = require('./createDirection')
proto.createDistance = require('./createDistance')
proto.createOrientation = require('./createOrientation')
proto.createVector = require('./createVector')
// TODO proto.getFirstChild
Expand Down
38 changes: 23 additions & 15 deletions test/components/Component/geometryCreation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,43 @@
<div id="testspace"></div>
</div>
<script>
test.plan(8)
const t = test
t.plan(10)

// Setup
const view = tapspace.createView('#testspace')
const space = tapspace.createSpace()
view.addChild(space)

// Create Basis
// Test createBasis

const b0 = space.getBasis()
const b1 = space.createBasis(space.at(0, 0), 1, 0)
// Test
test.notOk(b0.almostEqual(null), 'should detect nullish')
test.ok(b1.almostEqual(b0), 'should be almost identical with space basis')
test.ok(b0.almostEqual(b1), 'should be almost identical with space basis')
t.notOk(b0.almostEqual(null), 'should detect nullish')
t.ok(b1.almostEqual(b0), 'should be almost identical with space basis')
t.ok(b0.almostEqual(b1), 'should be almost identical with space basis')

// Test createDirection

// Create Direction
const dir1 = space.createDirection(Math.PI, Math.PI / 2)
const dir0 = { basis: space, dir: { x: -1, y: 0, z: 0 } }
// Test
test.notOk(dir1.equal(dir0), 'should have floating point errors')
test.ok(dir1.almostEqual(dir0), 'should be same direction within tolerance')
t.notOk(dir1.equal(dir0), 'should have floating point errors')
t.ok(dir1.almostEqual(dir0), 'should be same direction within tolerance')

// Test createDistance

const dist1 = space.createDistance(2)
const dist0 = view.createDistance(2).changeBasis(space)
t.equal(dist1.dist, 2, 'should have correct number')
t.ok(dist1.equal(dist0), 'should be equal')

// Test createOrientation

// Create Orientation
const orient1 = space.createOrientation(Math.PI)
const orient0 = { basis: space, orient: { a: -1, b: 0 } } // upside down
// Test
test.ok(orient1.equal(orient1), 'should equal self')
test.notOk(orient1.almostEqual(orient0.orient), 'should not take raw')
test.ok(orient1.almostEqual(orient0), 'should have same orientation')
t.ok(orient1.equal(orient1), 'should equal self')
t.notOk(orient1.almostEqual(orient0.orient), 'should not take raw')
t.ok(orient1.almostEqual(orient0), 'should have same orientation')
</script>
</body>
</html>

0 comments on commit 8a8259f

Please sign in to comment.