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

68 lines
2.1 KiB
JavaScript
Raw Normal View History

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);
},
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
};
}
2019-11-29 12:51:58 +08:00
};
export default JTriangle;