Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 87be7f6
Showing
12 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
build | ||
components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "test/mocha"] | ||
path = test/mocha | ||
url = git://github.com/visionmedia/mocha.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
support | ||
test | ||
examples | ||
*.sock |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
build: index.js components | ||
@component build --dev | ||
|
||
components: | ||
@component install --dev | ||
|
||
clean: | ||
rm -fr build components | ||
|
||
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
# point | ||
|
||
`Point` object for canvas etc. | ||
|
||
## Installation | ||
|
||
``` | ||
$ component install component/point | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
var Point = require('point'); | ||
var a = new Point(5, 5); | ||
var b = new Point(10, 10); | ||
a.distance(b); | ||
``` | ||
|
||
or without `new`: | ||
|
||
```js | ||
var p = require('point'); | ||
var a = p(5, 5); | ||
var b = p(10, 10); | ||
a.distance(b); | ||
``` | ||
|
||
## API | ||
|
||
... too lazy | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "point", | ||
"version": "0.0.1", | ||
"description": "Point component", | ||
"keywords": ["point", "canvas"], | ||
"scripts": ["index.js"], | ||
"dependencies": {}, | ||
"development": { | ||
"component/assert": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
|
||
/** | ||
* Expose `Point`. | ||
*/ | ||
|
||
module.exports = Point; | ||
|
||
/** | ||
* Initialize a new `Point` with x / y. | ||
* | ||
* @param {Number} x | ||
* @param {Number} y | ||
* @api public | ||
*/ | ||
|
||
function Point(x, y) { | ||
if (!(this instanceof Point)) return new Point(x, y); | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
/** | ||
* Return a negated point. | ||
* | ||
* @return {Point} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.negate = function(){ | ||
return new Point(-this.x, -this.y); | ||
}; | ||
|
||
/** | ||
* Add x / y. | ||
* | ||
* @param {Point} p | ||
* @return {Point} new point | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.add = function(p){ | ||
return new Point(this.x + p.x, this.y + p.y); | ||
}; | ||
|
||
/** | ||
* Sub x / y. | ||
* | ||
* @param {Point} p | ||
* @return {Point} new point | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.sub = function(p){ | ||
return new Point(this.x - p.x, this.y - p.y); | ||
}; | ||
|
||
/** | ||
* Multiply x / y. | ||
* | ||
* @param {Point} p | ||
* @return {Point} new point | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.mul = function(p){ | ||
return new Point(this.x * p.x, this.y * p.y); | ||
}; | ||
|
||
/** | ||
* Divide x / y. | ||
* | ||
* @param {Point} p | ||
* @return {Point} new point | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.div = function(p){ | ||
return new Point(this.x / p.x, this.y / p.y); | ||
}; | ||
|
||
/** | ||
* Check if these points are the same. | ||
* | ||
* @param {Point} p | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.equals = function(p){ | ||
return this.x == p.x && this.y == p.y; | ||
}; | ||
|
||
/** | ||
* Return a clone of this point. | ||
* | ||
* @return {Point} new point | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.clone = function(){ | ||
return new Point(this.x, this.y); | ||
}; | ||
|
||
/** | ||
* Return angle in radians. | ||
* | ||
* @return {Number} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.angle = function(){ | ||
return Math.atan2(this.x, this.y); | ||
}; | ||
|
||
/** | ||
* Return angle in degrees. | ||
* | ||
* @return {Number} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.degrees = function(){ | ||
return this.angle() * 180 / Math.PI; | ||
}; | ||
|
||
/** | ||
* Return the distance between points. | ||
* | ||
* @param {Point} p | ||
* @return {Number} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.distance = function(p){ | ||
var x = this.x - p.x; | ||
var y = this.y - p.y; | ||
return Math.sqrt(x * x + y * y); | ||
}; | ||
|
||
/** | ||
* Return "(x, y)" string representation. | ||
* | ||
* @return {String} | ||
* @api public | ||
*/ | ||
|
||
Point.prototype.toString = function(){ | ||
return '(' + this.x + ', ' + this.y + ')'; | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "point-component", | ||
"version": "0.0.1", | ||
"description": "Point", | ||
"keywords": ["point"], | ||
"component": { | ||
"scripts": { | ||
"point/index.js": "index.js" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<html> | ||
<head> | ||
<title>point tests</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<link rel="stylesheet" href="mocha/mocha.css" /> | ||
<script src="mocha/mocha.js"></script> | ||
<script>mocha.setup('bdd')</script> | ||
<script> | ||
function assert(expr, msg) { | ||
if (!expr) throw new Error(msg || 'failed'); | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
<script src="../build/build.js"></script> | ||
<script src="point.js"></script> | ||
<script> | ||
mocha.run(); | ||
</script> | ||
</body> | ||
</html> |
Submodule mocha
added at
885fb1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
var Point = require('point') | ||
, assert = require('component-assert'); | ||
|
||
describe('Point(x, y)', function(){ | ||
it('should return a new point', function(){ | ||
var p = Point(5, 10); | ||
assert(p instanceof Point); | ||
assert(5 == p.x); | ||
assert(10 == p.y); | ||
}) | ||
|
||
describe('#negate()', function(){ | ||
it('should return a negated point', function(){ | ||
var p = Point(5, 10).negate(); | ||
assert(-5 == p.x); | ||
assert(-10 == p.y); | ||
}) | ||
}) | ||
|
||
describe('#add(p)', function(){ | ||
it('should add', function(){ | ||
var p = Point(0, 5).add(Point(5, 10)); | ||
assert(5 == p.x); | ||
assert(15 == p.y); | ||
}) | ||
}) | ||
|
||
describe('#sub(p)', function(){ | ||
it('should subtract', function(){ | ||
var p = Point(10, 10).sub(Point(5, 2)); | ||
assert(5 == p.x); | ||
assert(8 == p.y); | ||
}) | ||
}) | ||
|
||
describe('#mul(p)', function(){ | ||
it('should multiply', function(){ | ||
var p = Point(10, 10).mul(Point(5, 5)); | ||
assert(50 == p.x); | ||
assert(50 == p.y); | ||
}) | ||
}) | ||
|
||
describe('#div(p)', function(){ | ||
it('should divide', function(){ | ||
var p = Point(50, 50).div(Point(5, 5)); | ||
assert(10 == p.x); | ||
assert(10 == p.y); | ||
}) | ||
}) | ||
|
||
describe('#angle()', function(){ | ||
it('should return the angle in radians', function(){ | ||
var rad = Point(50, 50).angle(); | ||
assert('0.79' == rad.toFixed(2)); | ||
}) | ||
}) | ||
|
||
describe('#degress()', function(){ | ||
it('should return the angle in degrees', function(){ | ||
var deg = Point(50, 50).degrees(); | ||
assert(45 == deg); | ||
}) | ||
}) | ||
|
||
describe('#clone()', function(){ | ||
it('should return a clone of the point', function(){ | ||
var p = Point(5, 5); | ||
var a = p.clone(); | ||
assert(p !== a); | ||
assert(5 == a.x); | ||
assert(5 == a.y); | ||
}) | ||
}) | ||
|
||
describe('#distance(p)', function(){ | ||
it('should return the distance between points', function(){ | ||
var a = Point(5, 5); | ||
var b = Point(5, 5); | ||
assert(0 == a.distance(b)); | ||
|
||
a = Point(2, 2); | ||
b = Point(10, 10); | ||
assert('11.3' == a.distance(b).toFixed(1)); | ||
}) | ||
}) | ||
|
||
describe('#equals(p)', function(){ | ||
it('should check if the points are equivalent', function(){ | ||
var a = Point(5, 5); | ||
var b = Point(5, 10); | ||
var c = Point(5, 5); | ||
assert(false == a.equals(b)); | ||
assert(true == a.equals(c)); | ||
}) | ||
}) | ||
|
||
describe('#toString()', function(){ | ||
it('should return a string representation', function(){ | ||
var p = new Point(15, 30); | ||
assert('(15, 30)' == p.toString()); | ||
var p = new Point(-5, 0); | ||
assert('(-5, 0)' == p.toString()); | ||
}) | ||
}) | ||
}) |
Hahahahaha