# All files / src/lib PathRounder.js

100% Statements 30/30
100% Branches 4/4
100% Functions 3/3
100% Lines 30/30

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71

    24x
    24x

    25x

    26x

    26x
    26x

    26x

    26x
    26x
    26x

    26x

    26x
    27x
    27x
    27x
    27x

    27x

    27x
    27x
    27x

    27x
    1x
    1x

    1x

    26x

    26x

    27x
    27x

    26x
    26x

     import { cloneDeep } from 'lodash';

    export default class Path {

        constructor(radius) {
            this.path='';
            this.radius = radius;
        }

        get svgPath() {
            return this.path;
        }

        setPath(transitionPath) {
            const points = cloneDeep(transitionPath);

            let p1 = points.shift();
            let p2 = points.shift();

            let v01;
            let norm01;
            let q;
            let r;

            let v12 = { x: (p2.x-p1.x),
                y: (p2.y-p1.y),
            };
            let norm12 = Math.sqrt(v12.x * v12.x + v12.y*v12.y);
            v12.x /= norm12;
            v12.y /= norm12;

            let path = `M${p1.x} ${p1.y}`;

            while (points.length > 0) {
                p1 = p2;
                p2 = points.shift();
                v01 = v12;
                norm01 = norm12;

                v12 = { x: (p2.x-p1.x),
                    y: (p2.y-p1.y),
                };
                norm12 = Math.sqrt(v12.x * v12.x + v12.y*v12.y);
                v12.x /= norm12;
                v12.y /= norm12;

                if ((norm01 < 2*this.radius) || (norm12 < 2*this.radius))  {
                    let norm = Math.min(norm01, norm12);
                    q = { x: (p1.x - norm/2 * v01.x),
                        y: (p1.y - norm/2 * v01.y),
                    };
                    r = { x: (p1.x + norm/2 * v12.x),
                        y: (p1.y + norm/2 * v12.y),
                    };
                } else {
                    q = { x: (p1.x - this.radius * v01.x),
                        y: (p1.y - this.radius * v01.y),
                    };
                    r = { x: (p1.x + this.radius * v12.x),
                        y: (p1.y + this.radius * v12.y),
                    };
                }

                path += ` L${q.x} ${q.y}`;
                path += ` Q${p1.x} ${p1.y} ${r.x} ${r.y}`;
            }
            path += ` L${p2.x} ${p2.y}`;
            this.path = path.trim();
        }
    }