xxxxxxxxxx
137
// creates a vector from a direction and magnitude
function createVector(direction,magnitude){ // OHH YEAH
var x = Math.cos(direction) * magnitude;
var y = Math.sin(direction) * magnitude;
return [x,y];
}
// takes a vector (in format [x,y]) and makes its magnitude zero
function normalize(vector){
var x = vector[0];
var y = vector[1];
var magnitude = Math.sqrt( x*x + y*y);
return [x/magnitude,y/magnitude];
}
// adds two vectors together
function addVectors(vec1,vec2){
return [vec1[0]+vec2[0],vec1[1]+vec2[1]];
}
// subtracts a vector from a vector
function subtractVectors(vec1,vec2){
return [vec1[0]-vec2[0],vec1[1]-vec2[1]];
}
// multiplies a vector by a scalar (number)
function multiplyVector(vector,scalar){
return [ vector[0] * scalar, vector[1] * scalar];
}
// returns the direction of a vector
function getDirection(vector){
return Math.atan(vector[1],vector[0]);
}
// returns the magnitude of a vector
function getMagnitude(vector){
return Math.sqrt(vector[0]*vector[0]+vector[1]*vector[1]);
}
// offsets the position of a vector
function offsetVector(vector, x, y){
return [vector[0] + x, vector[1] + y];
}
// returns a vector with the same magnitude, but new direction
function setDirection(vector, direction){
var magnitude = getMagnitude(vector);
var x = Math.cos(direction) * magnitude;
var y = Math.sin(direction) * magnitude;
return [x,y];
}
// returns a vector with the same direction, but new magnitude
function setMagnitude(vector, magnitude){
var tempVector = normalize(vector);
return [tempVector[0] * magnitude, tempVector[1] * magnitude];
}
// vec#pos is the origin position of the vector, vec# is the component form of the vector
// returns the point where two vectors intersect, returns false if they do not
function getIntersect(vec1pos, vec1, vec2pos, vec2){
// step one: get the X area where the two vectors both exist, return false if there is none
var vec1xMin = vec1pos[0] < vec1[0] + vec1pos[0] ? vec1pos[0] : vec1[0] + vec1pos[0];
var vec1xMax = vec1pos[0] > vec1[0] + vec1pos[0] ? vec1pos[0] : vec1[0] + vec1pos[0];
var vec2xMin = vec2pos[0] < vec2[0] + vec2pos[0] ? vec2pos[0] : vec2[0] + vec2pos[0];
var vec2xMax = vec2pos[0] > vec2[0] + vec2pos[0] ? vec2pos[0] : vec2[0] + vec2pos[0];
if( (vec1xMax > vec2xMin && vec1xMax < vec2xMax) || (vec2xMax > vec1xMin && vec2xMax < vec1xMax)){
// do meth- I mean meth- I mean math
// calculate the slopes of the lines
var vec1slope = (vec1[1])/(vec1[0]);
var vec2slope = (vec2[1])/(vec2[0]);
// the lines are unable to intersect if they are parallel, also prevents division by zero
if (vec1slope == vec2slope) return false;
// calculate the y intercepts of the lines
var vec1y = vec1pos[1] - (vec1pos[0] * vec1slope);
var vec2y = vec2pos[1] - (vec2pos[0] * vec2slope);
// the big one: calculate the x intercept of the two lines
// (I did not copy this formula off of stack overflow, I figured it out myself :D)
var xInt = -1 * (vec1y - vec2y) / (vec1slope - vec2slope);
// check if the xIntercept is within both vectors
if ( (xInt > vec1xMin && xInt < vec1xMax) && ( xInt > vec2xMin && xInt < vec2xMax) ){
// calculate the y value of the x intercept
var yInt = vec1y + ( xInt * vec1slope );
// return the coordinates of the intersection
return [xInt, yInt];
} else {
// return false if the xIntercept is not within both vectors
return false;
}
} else {
// the vectors do not have any x intersection, so they do not intersect
return false;
}
}
// uses a set of points instead of vectors
function pointsIntersection(vec1,vec2,vec3,vec4){
}
// adds a list vectors together, should be formatted as [ [x,y], [x,y], [x,y], ... ]
function addSeveralVectors(vectors){
var outputVector = [0,0];
for(var i = 0; i < vectors.length; i++){
outputVector = addVectors(outputVector, vectors[i]);
}
return outputVector;
}