geometry-2d
Advanced tools
Comparing version 0.0.20 to 0.1.0
138
lib/point.js
@@ -35,2 +35,11 @@ 'use strict'; | ||
/* The coordinate system assumed by this package is the Cartesian coordinate system as used in Math. | ||
For those methods that imply some preconceived notion of 'up' and 'down' note that we use | ||
the standard Math convention of greater ys being higher, and not the inverted convention | ||
typically used for screen coordinates (greater ys being lower). This is because this library | ||
lives in Math space, not in "computer screen" space. It is trivial to translate accordingly | ||
just prior to display. | ||
*/ | ||
var sq = function sq(x) { | ||
@@ -75,2 +84,7 @@ return Math.pow(x, 2); | ||
}, { | ||
key: 'equalsWithin', | ||
value: function equalsWithin(p2, tolerance) { | ||
return Math.abs(this.x - p2.x) <= tolerance && Math.abs(this.y - p2.y) <= tolerance; | ||
} | ||
}, { | ||
key: 'toString', | ||
@@ -161,9 +175,7 @@ value: function toString() { | ||
value: function fromString(s) { | ||
var _s$split = s.split(Point.SEP); | ||
var _s$split = s.split(Point.SEP), | ||
_s$split2 = _slicedToArray(_s$split, 2), | ||
x = _s$split2[0], | ||
y = _s$split2[1]; | ||
var _s$split2 = _slicedToArray(_s$split, 2); | ||
var x = _s$split2[0]; | ||
var y = _s$split2[1]; | ||
return new Point(parseFloat(x), parseFloat(y)); | ||
@@ -176,2 +188,5 @@ } | ||
// These are bound, not free, vectors | ||
Point.SEP = '~'; | ||
@@ -193,5 +208,10 @@ | ||
}, { | ||
key: 'equalsWithin', | ||
value: function equalsWithin(v2, tolerance) { | ||
return this.from.equalsWithin(v2.from, tolerance) && this.to.equalsWithin(v2.to, tolerance); | ||
} | ||
}, { | ||
key: 'toString', | ||
value: function toString() { | ||
return '(' + this.from + ')' + Vector.ARROW + '(' + this.to + ')'; | ||
return '(' + this.from.toString() + ')' + Vector.ARROW + '(' + this.to.toString() + ')'; | ||
} | ||
@@ -228,11 +248,18 @@ }, { | ||
} | ||
}, { | ||
key: 'scalarMul', | ||
value: function scalarMul(n) { | ||
var delta = new Point(n * this.xDelta(), n * this.yDelta()); | ||
var rv = new Vector(this.from, this.from.add(delta)); | ||
(0, _assert2.default)(this.from.equals(rv.from), 'scalar multiplication should not change the origin of bound vector'); | ||
return rv; | ||
} | ||
}], [{ | ||
key: 'fromString', | ||
value: function fromString(s) { | ||
var _s$split3 = s.split(Vector.ARROW); | ||
var _s$split3 = s.split(Vector.ARROW), | ||
_s$split4 = _slicedToArray(_s$split3, 2), | ||
fromS = _s$split4[0], | ||
toS = _s$split4[1]; | ||
var _s$split4 = _slicedToArray(_s$split3, 2); | ||
var fromS = _s$split4[0]; | ||
var toS = _s$split4[1]; | ||
var _arr2 = [fromS, toS]; | ||
@@ -254,92 +281,7 @@ | ||
Vector.ARROW = '=>'; // there is no way to associate custom checks with Flow types so this is for documentation mostly | ||
Vector.ARROW = '=>'; | ||
var Rectangle = function () { | ||
function Rectangle(topLeft, bottomRight) { | ||
_classCallCheck(this, Rectangle); | ||
this.topLeft = topLeft; | ||
this.bottomRight = bottomRight; | ||
(0, _assert2.default)(topLeft.toTheLeftOf(bottomRight, false));; | ||
(0, _assert2.default)(topLeft.aboveOf(bottomRight, false)); | ||
} | ||
_createClass(Rectangle, [{ | ||
key: 'equal', | ||
value: function equal(o) { | ||
return this.topLeft.equals(o.topLeft) && this.bottomRight.equals(o.bottomRight); | ||
} | ||
}, { | ||
key: 'fourCorners', | ||
value: function fourCorners() { | ||
return { | ||
topLeft: this.topLeft, | ||
topRight: new Point(this.bottomRight.x, this.topLeft.y), | ||
bottomRight: this.bottomRight, | ||
bottomLeft: new Point(this.topLeft.x, this.bottomRight.y) | ||
}; | ||
} | ||
}, { | ||
key: '_pointLiesInside', | ||
value: function _pointLiesInside(p, includeEdges) { | ||
return inRange(p.x, this.topLeft.x, this.bottomRight.x, includeEdges, includeEdges) && inRange(p.y, this.bottomRight.y, this.topLeft.y, includeEdges, includeEdges); | ||
} | ||
}, { | ||
key: 'pointLiesInside', | ||
value: function pointLiesInside(p) { | ||
return this._pointLiesInside(p, true); | ||
} | ||
}, { | ||
key: 'pointLiesSafelyInside', | ||
value: function pointLiesSafelyInside(p) { | ||
return this._pointLiesInside(p, false); | ||
} | ||
}, { | ||
key: 'pointLiesOutside', | ||
value: function pointLiesOutside(p) { | ||
return !this.pointLiesSafelyInside(p); | ||
} | ||
}, { | ||
key: 'pointLiesSafelyOutside', | ||
value: function pointLiesSafelyOutside(p) { | ||
return !this.pointLiesInside(p); | ||
} | ||
}, { | ||
key: 'pointLiesOnEdge', | ||
value: function pointLiesOnEdge(p) { | ||
return this.pointLiesInside(p) && !this.pointLiesSafelyInside(p); | ||
} | ||
}, { | ||
key: 'containsRectangle', | ||
value: function containsRectangle(r) { | ||
var _this = this; | ||
var mayTouchEdge = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
var fc = r.fourCorners(); | ||
var tl = fc.topLeft; | ||
var tr = fc.topRight; | ||
var br = fc.bottomRight; | ||
var bl = fc.bottomLeft; | ||
return _lodash2.default.every([tl, tr, br, bl], function (p) { | ||
return _this._pointLiesInside(p, mayTouchEdge); | ||
}); | ||
} | ||
}], [{ | ||
key: 'topLeftWidthHeight', | ||
value: function topLeftWidthHeight(topLeft, width, height) { | ||
(0, _assert2.default)(width > 0) && (0, _assert2.default)(height > 0); | ||
return new Rectangle(topLeft, topLeft.addX(width).addY(-height)); | ||
} | ||
}]); | ||
return Rectangle; | ||
}(); | ||
exports.inRange = inRange; | ||
exports.Point = Point; | ||
exports.Vector = Vector; | ||
exports.Rectangle = Rectangle; | ||
//# sourceMappingURL=point.js.map |
{ | ||
"name": "geometry-2d", | ||
"version": "0.0.20", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "lib/point.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
@@ -13,4 +13,4 @@ "clean": "rm -fr ./lib/*.js && rm -fr ./lib/*.js.flow && rm -fr ./lib/*.js.map", | ||
"build-watch": "babel src --out-dir lib --source-maps --watch", | ||
"start": "npm run build && node lib/point.js", | ||
"preinstall-globally": "npm run clean && flow check && npm run test && npm run build && cp src/point.js lib/point.js.flow", | ||
"start": "npm run build && node lib/index.js", | ||
"preinstall-globally": "npm run clean && flow check && npm run test && npm run build && for f in $(find src/ -iname *.js | cut -c5-) ; do cp src/$f lib/$f.flow; done", | ||
"install-globally": "npm install -g .", | ||
@@ -17,0 +17,0 @@ "prepublish": "./prepublish.sh" |
@@ -6,3 +6,3 @@ require('source-map-support').install(); | ||
import {inRange, Point, Vector, Rectangle} from '../lib/point.js'; | ||
import {inRange, Point} from '../lib/index.js'; | ||
@@ -177,125 +177,1 @@ describe('inRange', function () { | ||
describe('Vector', function() { | ||
describe('toString', function() { | ||
it('should work', function() { | ||
const v = new Vector(new Point(0,1), new Point(2,3)); | ||
assert.equal(v.toString(), `(0~1)${Vector.ARROW}(2~3)`); | ||
}); | ||
}); | ||
describe('equals', function() { | ||
it('should work', function() { | ||
const v1 = new Vector(new Point(0,1), new Point(2,3)); | ||
const v2 = new Vector(new Point(0,1), new Point(2,3)); | ||
assert(v1!==v2); | ||
assert(v1.equals(v2)); | ||
assert(v2.equals(v1)); | ||
}); | ||
}); | ||
describe('fromString', function() { | ||
it('should work', function() { | ||
const v1 = new Vector(new Point(0,1), new Point(2,3)); | ||
const v2 = Vector.fromString(`(0~1)${Vector.ARROW}(2~3)`); | ||
assert(v1.equals(v2)); | ||
assert(v2.equals(v1)); | ||
}); | ||
it('should work #2', function() { | ||
const values=[[`(0~1)${Vector.ARROW}(2~-3)` , new Vector(new Point(0,1) , new Point( 2,-3))], | ||
[`(0~-1)${Vector.ARROW}(-2~-3)`, new Vector(new Point(0,-1), new Point(-2,-3))]]; | ||
for (let [s, v2] of values) { | ||
const v1 = Vector.fromString(s, false); | ||
assert(v1.equals(v2)); | ||
assert(v2.equals(v1)); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('Rectangle', function() { | ||
let R1, R2; | ||
describe('constructor, factory and equals', function() { | ||
it('should work', function() { | ||
R1 = new Rectangle(new Point(0,2), new Point(3, 0)); | ||
const R2 = Rectangle.topLeftWidthHeight(new Point(0, 2), 3, 2); | ||
assert(R1.equal(R2)); | ||
}); | ||
}); | ||
const OUTLINE = [new Point(0,0), new Point(0, 1), new Point (0,2), new Point (1, 2), new Point (2,2), new Point (3,2), new Point(3, 1), new Point (3, 0), new Point (2, 0), new Point (1,0)]; | ||
const INSIDE = [new Point(0.1,0.1), new Point(0.1, 1), new Point (0.1,1.9), new Point (1, 1.9), new Point (2,1.9), new Point (2.9,1.9), new Point(2.9, 1), new Point (2.9, 0.1), new Point (2, 0.1), new Point (1,0.1)]; | ||
const OUTSIDE = [new Point(-0.1,-0.1), new Point(-0.1, 1), new Point (-0.1,2.1), new Point (1, 2.1), new Point (2,2.1), new Point (3.1,2.1), new Point(3.1, 1.1), new Point (3.1, -0.1), new Point (2, -0.1), new Point (1,-0.1)]; | ||
describe('lying functions', function() { | ||
it('should work', function() { | ||
OUTLINE.forEach( (p)=> { | ||
assert(R1.pointLiesInside(p)); | ||
assert(!R1.pointLiesSafelyInside(p)); | ||
assert( R1.pointLiesOutside(p)); | ||
assert(!R1.pointLiesSafelyOutside(p)); | ||
assert( R1.pointLiesOnEdge(p)); | ||
}); | ||
INSIDE.forEach( (p)=> { | ||
assert(R1.pointLiesInside(p)); | ||
assert(R1.pointLiesSafelyInside(p)); | ||
assert(!R1.pointLiesOutside(p)); | ||
assert(!R1.pointLiesSafelyOutside(p)); | ||
assert(!R1.pointLiesOnEdge(p)); | ||
}); | ||
OUTSIDE.forEach( (p)=> { | ||
assert(!R1.pointLiesInside(p)); | ||
assert(!R1.pointLiesSafelyInside(p)); | ||
assert( R1.pointLiesOutside(p)); | ||
assert( R1.pointLiesSafelyOutside(p)); | ||
assert(!R1.pointLiesOnEdge(p)); | ||
}); | ||
}); | ||
}); | ||
describe('four corners', function() { | ||
it('should work', function() { | ||
const fc = R1.fourCorners(); | ||
const fcExpected = {topLeft: new Point(0,2), | ||
topRight: new Point(3,2), | ||
bottomRight: new Point(3,0), | ||
bottomLeft: new Point(0,0)}; | ||
assert.equal(JSON.stringify(fc), JSON.stringify(fcExpected)); | ||
}); | ||
}); | ||
describe('containsRectangle', function() { | ||
const R1_OUT = new Rectangle(new Point(-0.1,2.1), new Point(3.1, -0.1)); | ||
const R1_IN = [new Rectangle(new Point(0.1,2), new Point(3, 0)) | ||
, new Rectangle(new Point(0,1.9), new Point(3, 0)) | ||
, new Rectangle(new Point(0,2), new Point(2.9, 0)) | ||
, new Rectangle(new Point(0,2), new Point(3, 0.1))]; | ||
const R1_ALLOUT = [new Rectangle(new Point(-0.1,2.1), new Point(3.1, -0.1))]; | ||
it('should work #1', function() { | ||
[true, false].forEach( (b)=> { | ||
assert( R1_OUT .containsRectangle(R1, b)); | ||
assert(!R1 .containsRectangle(R1_OUT, b)); | ||
}); | ||
}); | ||
it('should work #2', function() { | ||
[true, false].forEach( (b)=> { | ||
R1_IN.forEach( (r)=> { | ||
assert(!r .containsRectangle(R1, b)); | ||
assert(!R1 .containsRectangle(r, false)); | ||
assert( R1 .containsRectangle(r, true)); | ||
}); | ||
}); | ||
}); | ||
it('should work #3', function() { | ||
[true, false].forEach( (b)=> { | ||
R1_ALLOUT.forEach( (r)=> { | ||
assert(r .containsRectangle (R1 , b)); | ||
assert(!R1 .containsRectangle(r, b)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70689
17
841