rt-sim-training-client/src/iscs/utils/JTriangle.js

74 lines
2.3 KiB
JavaScript
Raw Normal View History

/** 三角函数计算 */
function JTriangle(beg, end) {
this.init(beg, end);
}
JTriangle.prototype = {
constructor: JTriangle,
beg: null,
end: null,
abspowx: 0,
abspowy: 0,
abspowz: 0,
drictx: 0,
dricty: 0,
drict: 0,
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);
},
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);
},
getCosRate () {
return Math.sqrt(this.abspowx / this.abspowz);
},
getSinRate () {
return Math.sqrt(this.abspowy / this.abspowz);
},
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
};
}
};
export default JTriangle;