Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 30, 2012
0 parents commit 87be7f6
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
build
components
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "test/mocha"]
path = test/mocha
url = git://github.com/visionmedia/mocha.git
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
support
test
examples
*.sock
Empty file added History.md
Empty file.
11 changes: 11 additions & 0 deletions Makefile
@@ -0,0 +1,11 @@

build: index.js components
@component build --dev

components:
@component install --dev

clean:
rm -fr build components

.PHONY: clean
36 changes: 36 additions & 0 deletions Readme.md
@@ -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.

Copy link
@TooTallNate

TooTallNate Sep 30, 2012

Member

Hahahahaha

This comment has been minimized.

Copy link
@tj

tj Sep 30, 2012

Author Member

:D haha need a dox -> readme markdown executable


## License

MIT
11 changes: 11 additions & 0 deletions component.json
@@ -0,0 +1,11 @@
{
"name": "point",
"version": "0.0.1",
"description": "Point component",
"keywords": ["point", "canvas"],
"scripts": ["index.js"],
"dependencies": {},
"development": {
"component/assert": "*"
}
}
152 changes: 152 additions & 0 deletions index.js
@@ -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 + ')';
};



11 changes: 11 additions & 0 deletions package.json
@@ -0,0 +1,11 @@
{
"name": "point-component",
"version": "0.0.1",
"description": "Point",
"keywords": ["point"],
"component": {
"scripts": {
"point/index.js": "index.js"
}
}
}
22 changes: 22 additions & 0 deletions test/index.html
@@ -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>
1 change: 1 addition & 0 deletions test/mocha
Submodule mocha added at 885fb1
107 changes: 107 additions & 0 deletions test/point.js
@@ -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());
})
})
})

0 comments on commit 87be7f6

Please sign in to comment.