Skip to content

Commit

Permalink
mult and div by point
Browse files Browse the repository at this point in the history
  • Loading branch information
karolselak committed Sep 27, 2016
1 parent f52f37a commit efb6368
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
40 changes: 36 additions & 4 deletions README.md
Expand Up @@ -69,21 +69,53 @@ yielding a new point.

### `mult(k)`

Multiply this point's x & y coordinates with from point,
Multiply this point's x & y coordinates by fractor,
yielding a new point.

### Parameters

| parameter | type | description |
| --------- | ------ | -------------- |
| `k` | Number | fractor |



**Returns** `Point`, output point


### `div(k)`

Divide this point's x & y coordinates by divider,
yielding a new point.

### Parameters

| parameter | type | description |
| --------- | ------ | -------------- |
| `k` | Number | divider |



**Returns** `Point`, output point


### `multByPoint(p)`

Multiply this point's x & y coordinates by point,
yielding a new point.

### Parameters

| parameter | type | description |
| --------- | ----- | --------------- |
| `k` | Point | the other point |
| `p` | Point | the other point |



**Returns** `Point`, output point


### `div(k)`
### `divByPoint(p)`

Divide this point's x & y coordinates with from point,
yielding a new point.
Expand All @@ -92,7 +124,7 @@ yielding a new point.

| parameter | type | description |
| --------- | ----- | --------------- |
| `k` | Point | the other point |
| `p` | Point | the other point |



Expand Down
43 changes: 35 additions & 8 deletions index.js
Expand Up @@ -45,15 +45,31 @@ Point.prototype = {
sub: function(p) { return this.clone()._sub(p); },

/**
* Multiply this point's x & y coordinates with from point,
* Multiply this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
multByPoint: function(p) { return this.clone()._multByPoint(p); },

/**
* Divide this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
divByPoint: function(p) { return this.clone()._divByPoint(p); },

/**
* Multiply this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} k the other point
* @return {Point} output point
*/
mult: function(k) { return this.clone()._mult(k); },

/**
* Divide this point's x & y coordinates with from point,
* Divide this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} k the other point
* @return {Point} output point
Expand Down Expand Up @@ -212,20 +228,31 @@ Point.prototype = {
},

_mult: function(k) {
this.x *= k.x;
this.y *= k.y;
this.x *= k;
this.y *= k;
return this;
},

_div: function(k) {
this.x /= k.x;
this.y /= k.y;
this.x /= k;
this.y /= k;
return this;
},

_multByPoint: function(p) {
this.x *= p.x;
this.y *= p.y;
return this;
},

_divByPoint: function(p) {
this.x /= p.x;
this.y /= p.y;
return this;
},

_unit: function() {
var m = this.mag();
this._div(new Point(m,m));
this._div(this.mag());
return this;
},

Expand Down
24 changes: 18 additions & 6 deletions test.js
Expand Up @@ -37,15 +37,27 @@ test('Point', function(t) {
t.end();
});
t.test('#mult', function(t) {
t.equals((new Point(0, 0).mult(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).mult(new Point(5,5))).equals(new Point(0, 5)), true);
t.equals((new Point(1, 1).mult(new Point(5,5))).equals(new Point(5, 5)), true);
t.equals((new Point(0, 0).mult(5)).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).mult(5)).equals(new Point(0, 5)), true);
t.equals((new Point(1, 1).mult(5)).equals(new Point(5, 5)), true);
t.end();
});
t.test('#div', function(t) {
t.equals((new Point(0, 0).div(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).div(new Point(5,5))).equals(new Point(0, 1/5)), true);
t.equals((new Point(1, 1).div(new Point(5,5))).equals(new Point(1/5, 1/5)), true);
t.equals((new Point(0, 0).div(5)).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).div(5)).equals(new Point(0, 1/5)), true);
t.equals((new Point(1, 1).div(5)).equals(new Point(1/5, 1/5)), true);
t.end();
});
t.test('#multByPoint', function(t) {
t.equals((new Point(0, 0).multByPoint(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).multByPoint(new Point(5,5))).equals(new Point(0, 5)), true);
t.equals((new Point(1, 1).multByPoint(new Point(4,5))).equals(new Point(4, 5)), true);
t.end();
});
t.test('#divByPoint', function(t) {
t.equals((new Point(0, 0).divByPoint(new Point(5,5))).equals(new Point(0, 0)), true);
t.equals((new Point(0, 1).divByPoint(new Point(5,5))).equals(new Point(0, 1/5)), true);
t.equals((new Point(1, 1).divByPoint(new Point(4,5))).equals(new Point(1/4, 1/5)), true);
t.end();
});
t.test('#rotate', function(t) {
Expand Down

0 comments on commit efb6368

Please sign in to comment.