2019-11-29 12:51:58 +08:00
|
|
|
/** 三角函数计算 */
|
|
|
|
function JTriangle(beg, end) {
|
2019-12-06 10:31:19 +08:00
|
|
|
this.init(beg, end);
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
JTriangle.prototype = {
|
2019-12-06 10:31:19 +08:00
|
|
|
constructor: JTriangle,
|
|
|
|
beg: null,
|
|
|
|
end: null,
|
|
|
|
abspowx: 0,
|
|
|
|
abspowy: 0,
|
|
|
|
abspowz: 0,
|
|
|
|
drictx: 0,
|
|
|
|
dricty: 0,
|
|
|
|
drict: 0,
|
2019-11-29 12:51:58 +08:00
|
|
|
|
2019-12-06 10:31:19 +08:00
|
|
|
init (beg, end) {
|
|
|
|
this.beg = beg;
|
|
|
|
this.end = end;
|
|
|
|
this.abspowx = Math.pow(this.end.x - this.beg.x, 2);
|
|
|
|
this.abspowy = Math.pow(this.end.y - this.beg.y, 2);
|
|
|
|
this.abspowz = this.abspowx + this.abspowy;
|
|
|
|
this.absx = Math.abs(this.end.x - this.beg.x);
|
|
|
|
this.absy = Math.abs(this.end.y - this.beg.y);
|
|
|
|
this.absz = Math.sqrt(Math.pow(this.end.x - this.beg.x, 2), Math.pow(this.end.y - this.beg.y, 2));
|
|
|
|
this.drictx = this.end.x > this.beg.x ? 1 : -1;
|
|
|
|
this.dricty = this.end.y > this.beg.y ? 1 : -1;
|
|
|
|
this.drict = this.drictx * this.dricty;
|
|
|
|
this.diff_x = end.x - beg.x;
|
|
|
|
this.diff_y = end.y - beg.y;
|
|
|
|
},
|
|
|
|
getRotation () {
|
|
|
|
return Math.atan(this.diff_y / this.diff_x);
|
|
|
|
},
|
|
|
|
getAngle () {
|
|
|
|
return 360 * Math.atan(this.diff_y / this.diff_x) / (2 * Math.PI);
|
|
|
|
},
|
|
|
|
getCos (n) {
|
|
|
|
return this.drictx * Math.sqrt(Math.pow(n, 2) * this.abspowx / this.abspowz);
|
|
|
|
},
|
|
|
|
getSin (n) {
|
|
|
|
return this.dricty * Math.sqrt(Math.pow(n, 2) * this.abspowy / this.abspowz);
|
|
|
|
},
|
2020-05-12 16:45:45 +08:00
|
|
|
getAbsCos(n) {
|
|
|
|
return Math.sqrt(Math.pow(n, 2) * this.abspowx / this.abspowz);
|
|
|
|
},
|
|
|
|
getAbsSin(n) {
|
|
|
|
return Math.sqrt(Math.pow(n, 2) * this.abspowy / this.abspowz);
|
|
|
|
},
|
2019-12-06 10:31:19 +08:00
|
|
|
getCosRate () {
|
|
|
|
return Math.sqrt(this.abspowx / this.abspowz);
|
|
|
|
},
|
|
|
|
getSinRate () {
|
|
|
|
return Math.sqrt(this.abspowy / this.abspowz);
|
|
|
|
},
|
2020-08-21 14:14:26 +08:00
|
|
|
getLength() {
|
|
|
|
return Math.sqrt(this.abspowz);
|
|
|
|
},
|
2019-12-06 10:31:19 +08:00
|
|
|
getTanRate () {
|
|
|
|
var diff_x = this.end.x - this.beg.x;
|
|
|
|
var diff_y = this.end.y - this.beg.y;
|
|
|
|
return Math.abs(diff_y / diff_x);
|
|
|
|
},
|
|
|
|
getCotRate () {
|
|
|
|
var diff_x = this.end.x - this.beg.x;
|
|
|
|
var diff_y = this.end.y - this.beg.y;
|
|
|
|
return Math.abs(diff_x / diff_y);
|
|
|
|
},
|
|
|
|
middlePoint () {
|
|
|
|
return {
|
|
|
|
x: Math.min(this.end.x, this.beg.x) + Math.abs(this.end.x - this.beg.x) / 2,
|
|
|
|
y: Math.min(this.end.y, this.beg.y) + Math.abs(this.end.y - this.beg.y) / 2
|
|
|
|
};
|
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default JTriangle;
|