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

lib/forcegraph/math.js   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 36
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 7
c 1
b 0
f 0
nc 1
mnd 1
bc 7
fnc 4
dl 0
loc 36
rs 10
bpm 1.75
cpm 1.75
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B math.js ➔ define 0 34 1
1
'use strict';
2
3
define(function () {
4
  function distance(a, b) {
5
    return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
6
  }
7
8
  function distancePoint(a, b) {
9
    return Math.sqrt(distance(a, b));
10
  }
11
12
  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
    }
22
    if (t > 1) {
23
      return distance(p, b);
24
    }
25
    return distancePoint(p, {
26
      x: a.x + t * (b.x - a.x),
27
      y: a.y + t * (b.y - a.y)
28
    });
29
  }
30
31
  return {
32
    distance: distance,
33
    distancePoint: distancePoint,
34
    distanceLink: distanceLink
35
  };
36
});
37