Skip to content
This repository has been archived by the owner on Jul 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #8 from mcous/fix-float-tests
Browse files Browse the repository at this point in the history
Fix floating point tests
  • Loading branch information
kasbah committed Apr 7, 2018
2 parents 86bbf20 + 236444e commit 3c49c4e
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 579 deletions.
32 changes: 0 additions & 32 deletions .eslintrc

This file was deleted.

5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "4"
- "6"
- "8"
- node
matrix:
include:
- node_js: node
env: TEST_BROWSERS=true
env: TEST_BROWSERS=false
script: npm run-script ci
deploy:
provider: npm
Expand Down
12 changes: 6 additions & 6 deletions lib/_box.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
'use strict'

// returns a new bounding box that is infinitely small and centered on nothing
var newBox = function() {
var newBox = function () {
return [Infinity, Infinity, -Infinity, -Infinity]
}

// adds the two bounding boxes and returns a new one
var add = function(box, target) {
var add = function (box, target) {
return [
Math.min(box[0], target[0]),
Math.min(box[1], target[1]),
Expand All @@ -18,7 +18,7 @@ var add = function(box, target) {
}

// adds a point to a bounding box
var addPoint = function(box, point) {
var addPoint = function (box, point) {
return [
Math.min(box[0], point[0]),
Math.min(box[1], point[1]),
Expand All @@ -28,7 +28,7 @@ var addPoint = function(box, point) {
}

// add a circle at (cx, cy) with radius r to box
var addCircle = function(box, r, cx, cy) {
var addCircle = function (box, r, cx, cy) {
return [
Math.min(box[0], cx - r),
Math.min(box[1], cy - r),
Expand All @@ -38,7 +38,7 @@ var addCircle = function(box, r, cx, cy) {
}

// translate a box by a delta [x, y]
var translate = function(box, delta) {
var translate = function (box, delta) {
var dx = delta[0]
var dy = delta[1]

Expand All @@ -51,7 +51,7 @@ var translate = function(box, delta) {
}

// get the overall box if box is repeated at [x, y]
var repeat = function(box, repeat) {
var repeat = function (box, repeat) {
return add(box, translate(box, repeat))
}

Expand Down
82 changes: 31 additions & 51 deletions lib/_operate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var THREE_HALF_PI = 3 * Math.PI / 2

// flash operation
// returns a bounding box for the operation
var flash = function(coord, tool, region, plotter) {
var flash = function (coord, tool, region, plotter) {
// no flashing allowed in region mode
if (region) {
plotter._warn('flash in region ignored')
Expand All @@ -35,7 +35,7 @@ var flash = function(coord, tool, region, plotter) {

// given a start, end, direction, arc quadrant mode, and list of potential centers, find the
// angles of the start and end points, the sweep angle, and the center
var findCenterAndAngles = function(start, end, mode, arc, centers) {
var findCenterAndAngles = function (start, end, mode, arc, centers) {
var thetaStart
var thetaEnd
var sweep
Expand All @@ -47,25 +47,22 @@ var findCenterAndAngles = function(start, end, mode, arc, centers) {
thetaEnd = Math.atan2(end[1] - candidate[1], end[0] - candidate[0])

// in clockwise mode, ensure the start is greater than the end and check the sweep
// do the opposite for counter-clockwise
if (mode === 'cw') {
thetaStart = (thetaStart >= thetaEnd) ? thetaStart : (thetaStart + TWO_PI)
}
// do the opposite for counter-clockwise
else {
} else {
thetaEnd = (thetaEnd >= thetaStart) ? thetaEnd : (thetaEnd + TWO_PI)
}

sweep = Math.abs(thetaStart - thetaEnd)

// in single quadrant mode, the center is only valid if the sweep is less than 90 degrees
// in multiquandrant mode there's only one candidate; we're within spec to assume it's good
if (arc === 's') {
if (sweep <= HALF_PI) {
center = candidate
}
}

// in multiquandrant mode there's only one candidate; we're within spec to assume it's good
else {
} else {
center = candidate
}
}
Expand All @@ -88,7 +85,7 @@ var findCenterAndAngles = function(start, end, mode, arc, centers) {
}
}

var arcBox = function(cenAndAngles, r, region, tool, dir) {
var arcBox = function (cenAndAngles, r, region, tool, dir) {
var startPoint = cenAndAngles.start
var endPoint = cenAndAngles.end
var center = cenAndAngles.center
Expand All @@ -101,8 +98,7 @@ var arcBox = function(cenAndAngles, r, region, tool, dir) {
if (dir === 'cw') {
start = endPoint[2]
end = startPoint[2]
}
else {
} else {
start = startPoint[2]
end = endPoint[2]
}
Expand Down Expand Up @@ -136,7 +132,7 @@ var arcBox = function(cenAndAngles, r, region, tool, dir) {
points.push([center[0], center[1] - r])
}

return points.reduce(function(result, m) {
return points.reduce(function (result, m) {
if (!region) {
var mBox = boundingBox.translate(tool.box, m)
return boundingBox.add(result, mBox)
Expand All @@ -146,14 +142,14 @@ var arcBox = function(cenAndAngles, r, region, tool, dir) {
}, boundingBox.new())
}

var roundToZero = function(number, epsilon) {
var roundToZero = function (number, epsilon) {
return (number >= epsilon) ? number : 0
}

// find the center of an arc given its endpoints and its radius
// assume the arc is <= 180 degress
// thank you this guy: http://math.stackexchange.com/a/87912
var arcCenterFromRadius = function(start, end, mode, epsilon, radius) {
var arcCenterFromRadius = function (start, end, mode, epsilon, radius) {
var sign = (mode === 'ccw') ? 1 : -1
var xAve = (start[0] + end[0]) / 2
var yAve = (start[1] + end[1]) / 2
Expand All @@ -171,7 +167,7 @@ var arcCenterFromRadius = function(start, end, mode, epsilon, radius) {
]]
}

var drawArc = function(
var drawArc = function (
start,
end,
offset,
Expand All @@ -182,7 +178,6 @@ var drawArc = function(
epsilon,
pathGraph,
plotter) {

// get the radius of the arc from the offsets
var r = offset[2] || Math.sqrt(Math.pow(offset[0], 2) + Math.pow(offset[1], 2))

Expand All @@ -194,15 +189,13 @@ var drawArc = function(

if (offset[0] && (arc === 's')) {
xCandidates.push(start[0] + offset[0], start[0] - offset[0])
}
else {
} else {
xCandidates.push(start[0] + offset[0])
}

if (offset[1] && (arc === 's')) {
yCandidates.push(start[1] + offset[1], start[1] - offset[1])
}
else {
} else {
yCandidates.push(start[1] + offset[1])
}

Expand All @@ -217,16 +210,14 @@ var drawArc = function(
if (offset[2]) {
arc = 'm'
validCenters = arcCenterFromRadius(start, end, mode, epsilon, offset[2])
}
else if (arc === 's') {
validCenters = candidates.filter(function(c) {
} else if (arc === 's') {
validCenters = candidates.filter(function (c) {
var startDist = Math.sqrt(Math.pow(c[0] - start[0], 2) + Math.pow(c[1] - start[1], 2))
var endDist = Math.sqrt(Math.pow(c[0] - end[0], 2) + Math.pow(c[1] - end[1], 2))

return ((Math.abs(startDist - r) <= epsilon) && (Math.abs(endDist - r) <= epsilon))
})
}
else {
} else {
validCenters = candidates
}

Expand All @@ -250,15 +241,14 @@ var drawArc = function(
})

box = arcBox(cenAndAngles, r, region, tool, mode)
}
else {
} else {
plotter._warn('skipping impossible arc')
}

return box
}

var drawLine = function(start, end, tool, region, pathGraph) {
var drawLine = function (start, end, tool, region, pathGraph) {
pathGraph.add({type: 'line', start: start, end: end})

if (!region) {
Expand All @@ -274,7 +264,7 @@ var drawLine = function(start, end, tool, region, pathGraph) {
}

// interpolate a rectangle and emit the fill immdeiately
var interpolateRect = function(start, end, tool, pathGraph, plotter) {
var interpolateRect = function (start, end, tool, pathGraph, plotter) {
var hWidth = tool.trace[0] / 2
var hHeight = tool.trace[1] / 2
var theta = Math.atan2(end[1] - start[1], end[0] - start[0])
Expand All @@ -293,43 +283,35 @@ var interpolateRect = function(start, end, tool, pathGraph, plotter) {
// no movement
if (start[0] === end[0] && start[1] === end[1]) {
points.push([sXMin, sYMin], [sXMax, sYMin], [sXMax, sYMax], [sXMin, sYMax])
}

// check for first quadrant move
else if ((theta >= 0 && theta < HALF_PI)) {
} else if ((theta >= 0 && theta < HALF_PI)) {
// first quadrant move
points.push(
[sXMin, sYMin],
[sXMax, sYMin],
[eXMax, eYMin],
[eXMax, eYMax],
[eXMin, eYMax],
[sXMin, sYMax])
}

// check for second quadrant move
else if ((theta >= HALF_PI && theta <= PI)) {
} else if ((theta >= HALF_PI && theta <= PI)) {
// second quadrant move
points.push(
[sXMax, sYMin],
[sXMax, sYMax],
[eXMax, eYMax],
[eXMin, eYMax],
[eXMin, eYMin],
[sXMin, sYMin])
}

// third quadrant move
else if ((theta >= -PI && theta < -HALF_PI)) {
} else if ((theta >= -PI && theta < -HALF_PI)) {
// third quadrant move
points.push(
[sXMax, sYMax],
[sXMin, sYMax],
[eXMin, eYMax],
[eXMin, eYMin],
[eXMax, eYMin],
[sXMax, sYMin])
}

// fourth quadrant move
else {
} else {
// fourth quadrant move
points.push(
[sXMin, sYMax],
[sXMin, sYMin],
Expand All @@ -339,7 +321,7 @@ var interpolateRect = function(start, end, tool, pathGraph, plotter) {
[sXMax, sYMax])
}

points.forEach(function(p, i) {
points.forEach(function (p, i) {
var j = (i < (points.length - 1)) ? i + 1 : 0
pathGraph.add({type: 'line', start: p, end: points[j]})
})
Expand All @@ -352,9 +334,8 @@ var interpolateRect = function(start, end, tool, pathGraph, plotter) {

// interpolate operation
// returns a bounding box for the operation
var interpolate = function(
var interpolate = function (
start, end, offset, tool, mode, arc, region, epsilon, pathGraph, plotter) {

if (!region && (tool.trace.length === 0)) {
plotter._warn('tool ' + tool.code + ' is not strokable; ignoring interpolate')
return boundingBox.new()
Expand All @@ -381,9 +362,8 @@ var interpolate = function(

// takes the start point, the op type, the op coords, the tool, and the push function
// returns the new plotter position
var operate = function(
var operate = function (
type, coord, start, tool, mode, arc, region, pathGraph, epsilon, plotter) {

var end = [
((coord.x != null) ? coord.x : start[0]),
((coord.y != null) ? coord.y : start[1])
Expand Down
Loading

0 comments on commit 3c49c4e

Please sign in to comment.