Completed
Pull Request — develop (#75)
by
unknown
01:12
created

math.js ➔ define   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
dl 0
loc 31
rs 8.8571
nop 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A math.js ➔ ... ➔ distancePoint 0 3 1
A math.js ➔ ... ➔ distance 0 3 1
A math.js ➔ ... ➔ distanceLink 0 17 4
1
define(function () {
2
  var self = this;
3
4
  self.distance = function distance(a, b) {
5
    return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
6
  };
7
8
  self.distancePoint = function distancePoint(a, b) {
9
    return Math.sqrt(distance(a, b));
10
  };
11
12
  self.distanceLink = function distanceLink(p, a, b) {
13
    /* http://stackoverflow.com/questions/849211 */
14
    var l2 = distance(a, b);
15
    if (l2 === 0) {
16
      return distance(p, a);
17
    }
18
    var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2;
19
    if (t < 0) {
20
      return distance(p, a);
21
    } else if (t > 1) {
22
      return distance(p, b);
23
    }
24
    return distancePoint(p, {
25
      x: a.x + t * (b.x - a.x),
26
      y: a.y + t * (b.y - a.y)
27
    });
28
  };
29
30
  return self;
31
});
32