BangleApps_old/apps/mtimer/poly.lib.js

104 lines
2.6 KiB
JavaScript

exports.Poly = function(n) {
this.n = n == undefined ? 128 : n * 2;
this.midScreenX = Bangle.appRect.w / 2; // g.getWidth() / 2;
this.midScreenY = Bangle.appRect.h / 2; // g.getHeight() / 2;
this.points = new Int32Array(this.n);
this._index = 0;
this.minX = undefined;
this.maxX = undefined;
this.minY = undefined;
this.maxY = undefined;
this.width = 0;
this.height = 0;
this.getPoints = function() {
return this.points.subarray(0, this._index+1);
};
this.addPoint = function(x, y) {
this.points[this._index] = x;
this._index++;
this.points[this._index] = y;
this._index++;
if (this.minX === undefined) {
this.minX = x;
this.maxX = x;
this.minY = y;
this.maxY = y;
}
this.minX = Math.min(this.minX, x);
this.maxX = Math.max(this.maxX, x);
this.minY = Math.min(this.minY, y);
this.maxY = Math.max(this.maxY, y);
this.width = this.maxX - this.minX + 1;
this.height = this.maxY - this.minY + 1;
this.midX = Math.round(this.width / 2);
this.midY = Math.round(this.height / 2);
this.centerX = this.minX + this.midX + 1;
this.centerY = this.minY + this.midY + 1;
return this;
};
this.p = this.addPoint;
this.getWidth = function() {
return this.width;
};
this.getHeight = function() {
return this.height;
};
this.getmidY = function() {
return this.midY;
};
this.getmidX = function() {
return this.midX;
};
/**
* Convert an angle and radius into x and y cooridinates
* @param a angle in radians
* @param r radius, defaults to half screen width
* @param x x cooridinate, defaults to center
* @param y y coordinate, defaults to center
*/
this.addRadialPoint = function(a, r, x, y) {
r = r === undefined ? this.midScreenX : r;
x = x === undefined ? this.midScreenX : x;
y = y === undefined ? this.midScreenY : y;
this.addPoint(Math.round((Math.cos(a) * r)) + x, Math.round((Math.sin(a) * r)) + y);
return this;
};
this.makeArc = function(a1, a2, r, x, y) {
const aS = a1; // < a2 ? a1 : a2;
const aE = a2; // > a1 ? a2 + 0.001: a1 + 0.001;
if (a1 < a2) {
for (let a = aS; a <= aE; a+=0.1) {
this.addRadialPoint(a, r, x, y);
}
} else {
for (let a = aS; a >= aE; a-=0.1) {
this.addRadialPoint(a, r, x, y);
}
}
this.addRadialPoint(a2, r, x, y); // make sure we actually have a point at the end angle
// this.addPoint(x, y); // center point
return this;
};
this.makeSeg = function(a1, a2, r1, r2, x, y) {
this.makeArc(a1 , a2, r1, x, y);
this.makeArc(a2, a1, r2, x, y);
return this;
};
}