Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 87be7f6582315b379edcd57137068852df113a16 0 parents
@tj tj authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules
+build
+components
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "test/mocha"]
+ path = test/mocha
+ url = git://github.com/visionmedia/mocha.git
4 .npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
0  History.md
No changes.
11 Makefile
@@ -0,0 +1,11 @@
+
+build: index.js components
+ @component build --dev
+
+components:
+ @component install --dev
+
+clean:
+ rm -fr build components
+
+.PHONY: clean
36 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
@TooTallNate Owner

Hahahahaha

@tj Owner
tj added a note

:D haha need a dox -> readme markdown executable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+## License
+
+ MIT
11 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 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 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 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  test/mocha
@@ -0,0 +1 @@
+Subproject commit 885fb12f6e1c3e510afc9e3b8c7f232ea587492e
107 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());
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.