richtingscoefficient
by Tobias Beuving
HTML
<span id="feedback"></span><br/>
<canvas id="c" width="300" height="300"></canvas>
JavaScript
'use strict';
var Point = Point || {};
Point = function (xIn, yIn) {
this.x = xIn;
this.y = yIn;
}
Point.prototype.mVectorToDegrees = function (pointIn) {
var tmpDeg = (180/Math.PI);
if (pointIn == undefined) {
var x= Math.atan2(this.y, this.x);
tmpDeg = (x > 0 ? x : (2*Math.PI + x)) * 360 / (2*Math.PI);
return tmpDeg;
//return Math.atan(this.mGetRiCo())*tmpDeg;
} else {
//return Math.atan(this.mGetRiCo(pointIn))*tmpDeg;
//return Math.atan2(this.y-pointIn.y, this.x-pointIn.x)*tmpDeg;
var x= Math.atan2(pointIn.y-this.y, pointIn.x-this.x);
tmpDeg = (x > 0 ? x : (2*Math.PI + x)) * 360 / (2*Math.PI);
var theta_deg = Math.atan2(-(pointIn.y-this.y),-(pointIn.x-this.x))/Math.PI*180 +180;
return theta_deg;
}
}
Point.prototype.mRotate = function (radiusIn, degreesIn) {
//x = cos( angle ) * distance;
//y = sin( angle ) * distance;
var tmpAngle = (degreesIn*( Math.PI/180));
var tmpX = this.x+(radiusIn * Math.cos(tmpAngle));
var tmpY = this.y+(radiusIn * Math.sin(tmpAngle));
return new Point(tmpX,tmpY);
}
/**
* Translate n pixels over vector/rico
*/
Point.prototype.mTranslatePoint = function(ricoIn, distanceIn) {
var stepS = Math.sqrt(Math.pow(1,2)+Math.pow(ricoIn,2));
if (ricoIn<0) {
//stepS *=-1;
}
var nSteps = distanceIn/stepS;
var tmpX = nSteps;
if (ricoIn<0) {
// tmpX *=-1;
// tmpY *=-1;
}
var tmpY = tmpX*ricoIn;
if ( tmpY<0) {
//tmpY *=-1;
}
return new Point(this.x + tmpX, this.y + tmpY);
// return new Point(this.x + distanceIn, this.y + (ricoIn*distanceIn));
}
/**
* Returns the point on the middle between this.x,this.y and pointIn.x, pointIn.y
* @param pointIn required origin point from which the length should be calculated
* @returns the point in the middle of the line
* http://www.goeievraag.nl/vraag/wetenschap/wiskunde/middelloodlijn-bepalen-vergelijking-lijn.293839
...