getPath function

dynamic getPath(
  1. dynamic points
)

Implementation

getPath(points) {
    var p = new Path();
    if (points == null) {
      return p;
    }

    var contours = getContours(points);

    for (var contourIndex = 0; contourIndex < contours.length; ++contourIndex) {
        var contour = contours[contourIndex];

        var prev = null;
        var curr = contour[contour.length - 1];
        var next = contour[0];

        if (curr["onCurve"]) {
            p.moveTo(curr["x"], curr["y"]);
        } else {
            if (next["onCurve"]) {
                p.moveTo(next["x"], next["y"]);
            } else {
                // If both first and last points are off-curve, start at their middle.
                var start = {"x": (curr["x"] + next["x"]) * 0.5, "y": (curr["y"] + next["y"]) * 0.5};
                p.moveTo(start["x"], start["y"]);
            }
        }

        for (var i = 0; i < contour.length; ++i) {
            prev = curr;
            curr = next;
            next = contour[(i + 1) % contour.length];

            if (curr["onCurve"]) {
                // This is a straight line.
                p.lineTo(curr["x"], curr["y"]);
            } else {
                var prev2 = prev;
                var next2 = next;

                if (!prev["onCurve"]) {
                    prev2 = { "x": (curr["x"] + prev["x"]) * 0.5, "y": (curr["y"] + prev["y"]) * 0.5 };
                }

                if (!next["onCurve"]) {
                    next2 = { "x": (curr["x"] + next["x"]) * 0.5, "y": (curr["y"] + next["y"]) * 0.5 };
                }

                p.quadraticCurveTo(curr["x"], curr["y"], next2["x"], next2["y"]);
            }
        }

        p.closePath();
    }
    return p;
}