"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SPath2D = void 0; var index_1 = require("./index"); var lib_1 = require("@saga-web/base/lib"); var SPath2D = (function () { function SPath2D() { this._path = new Path2D(); this.svgBuilder = new lib_1.SStringBuilder(); } SPath2D.prototype.svgPath = function () { return this.svgBuilder.toString(" "); }; SPath2D.prototype.addPath = function (path) { this._path.addPath(path._path); this.svgBuilder.append(path.svgBuilder.toString(" ")); }; SPath2D.prototype.moveTo = function (x, y) { this._path.moveTo(x, y); this.svgBuilder.append("M " + x + " " + y); }; SPath2D.prototype.lineTo = function (x, y) { this._path.lineTo(x, y); this.svgBuilder.append("L " + x + " " + y); }; SPath2D.prototype.rect = function (x, y, width, height) { var r = x + width; var b = y + height; this._path.moveTo(x, y); this._path.lineTo(r, y); this._path.lineTo(r, b); this._path.lineTo(x, b); this._path.closePath(); this.svgBuilder.append("M " + x + " " + y + " L" + r + " " + y + " L" + r + " " + b + " L" + x + " " + b + " Z"); }; SPath2D.prototype.polyline = function (points) { var _this = this; if (points.length < 2) { return; } points.map(function (it, index) { if (index == 0) { _this._path.moveTo(it.x, it.y); _this.svgBuilder.append("M " + it.x + " " + it.y); } else { _this._path.lineTo(it.x, it.y); _this.svgBuilder.append("L " + it.x + " " + it.y); } }); }; SPath2D.prototype.polygon = function (points) { var _this = this; if (points.length < 3) { return; } points.map(function (it, index) { if (index == 0) { _this._path.moveTo(it.x, it.y); _this.svgBuilder.append("M " + it.x + " " + it.y); } else { _this._path.lineTo(it.x, it.y); _this.svgBuilder.append("L " + it.x + " " + it.y); } }); this._path.closePath(); this.svgBuilder.append("Z"); }; SPath2D.arc = function (x, y, width, height, startAngle, endAngle) { var a = width / 2; var b = height / 2; var p1 = SPath2D.arcPoint(a, b, startAngle); var p2 = SPath2D.arcPoint(a, b, endAngle); var large = SPath2D.largeArcFlag(startAngle, endAngle); p1.x = x + a + p1.x; p1.y = y + b - p1.y; p2.x = x + a + p2.x; p2.y = y + b - p2.y; return "M " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y; }; SPath2D.chord = function (x, y, width, height, startAngle, endAngle) { var a = width / 2; var b = height / 2; var p1 = SPath2D.arcPoint(a, b, startAngle); var p2 = SPath2D.arcPoint(a, b, endAngle); var large = SPath2D.largeArcFlag(startAngle, endAngle); p1.x = x + a + p1.x; p1.y = y + b - p1.y; p2.x = x + a + p2.x; p2.y = y + b - p2.y; return "M " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y + " Z"; }; SPath2D.pie = function (x, y, width, height, startAngle, endAngle) { var a = width / 2; var b = height / 2; var p1 = SPath2D.arcPoint(a, b, startAngle); var p2 = SPath2D.arcPoint(a, b, endAngle); var large = SPath2D.largeArcFlag(startAngle, endAngle); p1.x = x + a + p1.x; p1.y = y + b - p1.y; p2.x = x + a + p2.x; p2.y = y + b - p2.y; return "M " + (x + a) + " " + (y + b) + " L " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y + " Z"; }; SPath2D.largeArcFlag = function (startAngle, endAngle) { return ((Math.abs(endAngle - startAngle) * 180) / Math.PI) % 360 > 179.99999 ? 1 : 0; }; SPath2D.arcPoint = function (a, b, angle) { var p = new index_1.SPoint(); if (angle >= 0) { angle = angle - Math.floor(angle / Math.PI / 2) * 2 * Math.PI; } else { angle = angle + Math.ceil(-angle / Math.PI / 2) * 2 * Math.PI; } if (Math.abs(Math.PI / 2 - angle) <= 0.000001) { p.x = 0; p.y = b; } else if (Math.abs((Math.PI * 3) / 2 - angle) <= 0.000001) { p.x = 0; p.y = -b; } else if (Math.PI / 2 < angle && angle < (Math.PI * 3) / 2) { p.x = -(a * b) / Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle)); p.y = -(a * b * Math.tan(angle)) / Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle)); } else { p.x = (a * b) / Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle)); p.y = (a * b * Math.tan(angle)) / Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle)); } return p; }; return SPath2D; }()); exports.SPath2D = SPath2D;