Re-inventing the wheel? Definitely. Discovering that wheel for myself? Absolutely.
The code written here is procedural and we’re dealing only with two dimensions, I’m sure there are plenty of classes and aggregations of classes that handle the same thing. I started looking at some fundamental vector geometry and got to the fun part of using trig to determine directions of vectors. I coded this bit with some decent comments. It should hopefully make sense especially if you are familiar with the vocab. =)
/*define the x and y properties of a two dimensional vector representing it's coordinate displacement. The numbers here represent the coordinate style that flash uses. Or rather the the two dimensional DisplayObject properties: x and y as type Number, publicly accessible on any object that inherits from DisplayObject*/
var xDisplacement = 4;
var yDisplacemnet = 5;
/*"rowVector" is being written as a generic object that I'm treating as a vector. It's written in a format somewhat resembling a row Matrix format, or a "row vector" A vector by the way has two properties, length and direction, and should not be confused with the x and y coordinates of a point. Remember our goal in this code is to determine the vectors direction, more commonly described in degrees. Radians typically describe the direction property of vectors in higher mathematics. */
var rowVector = {x:xDisplacement, y:yDisplacemnet}
//Ultimately return the vectors direction in easy to understand degrees.
var vectorDirectionInDegress = getDegreesFromRowVector(rowVector);
//Output of our result:
trace("vectorDirectionInDegress = "+vectorDirectionInDegress);
//Return the vector's direction in degrees.
function getDegreesFromRowVector(rowVector){
var radians = getTangentInRadians(rowVector.y, rowVector.x);
var degrees = convertRadiansToDegrees(radians);
return degrees;
}
//Use the native flash Math.atan to return our opposite over adjacent ratio
//atan returns radians.
function getTangentInRadians(opposite, adjacent){
//atan returns radians from a tangent, or the ratio of opposite over adjacent
var radians = Math.atan(opposite/adjacent);
return radians;
}
//Here is the formula to converting radians to degrees.
function convertRadiansToDegrees(rad){
var degrees = rad * 180/Math.PI;
return degrees;
}
Determining the other property of a Vector namely it’s distance or length is fairly simple in the native flash environment using the Pythagorean theorem.
Note that degrees in flash look opposite of those represented in Cartesian geometry, You can convert flash degrees to Cartesian degrees by multiplying by -1. In flash Y coordinates look different from Cartesian coordinates in that flash’s Y axis is incrementing positively as you you move downward on the screen.
This makes sense if you were to imagine rotating the Cartesian plane on it’s X axis or “Pitch” 180 degrees so that it apparently directly mirrors the planes graphical direction and scalar value of before you flipped it.
Rotations are mirrored as well. Positive flash degrees rotate from 0 to 360 degrees clockwise from the positive Flash X plane. Positive Cartesian degrees increment from 0 to 360 counter-clockwise from the positive X plane.
One interesting thing that I’m seeing in at least 2d vectors is that if you multiply the same length coordinate, such as y, of a vector by -1 You will get a vector that graphically is identical to that vectors direction in comparison to a coordinate plane whose same property (in this case Y) was flipped by multiplied -1. This is great in considering that the ratios of sin cos and tan seemingly do not have to be applied differently regardless of the difference in visual direction of coordinate planes in flash and traditional Cartesian planes. Think about where 45degrees is on the flash and Cartesian coordinate planes. Multiply a vector’s y distance property by -1 just as how we get the two coordinate systems to look the same.
A vector equal in displacement or length and direction is the same as any other vector with the same length and direction regardless of it’s position One vector is also equal to the sum of any number of vectors that result in the same length and direction. In other words, you might take a round about way to wind up at your destination but regardless there is only one direct path to your final destination. Look into vector addition for the math.
The ratios of a vector’s direction with sin cos and tan are used the same regardless of plane direction. I’m assuming that sin cos and tan have a direct relationship to the relative positive and negative direction of the associated coordinate planes.
If you don’t understand what I’m saying, and would like to; Build up your knowledge in basic vector geometry, and fundamental trig. That’s what I’m doing here. I’m really glad that I’m starting to at least get a good feel for understanding Trigonometric ratios and attaining the benefits of knowing how to apply these concepts programmatically.
Oh yeah, been looking at Papervision 3D 2.0, Great White this is all your fault! =) Thought this would be a good place to do some ground work.
The implications of applying vector geometry to 3 dimensions as opposed to the two dimensional coordinates used in this example is very similar. I’m glad to finally take the due diligence necessary to understand how an object such as a matrix gives us a great way to manage and understand points and vectors their properties and relations to one another.
Afterthought:
Can you represent a vector with 4 dimensions applying a value representing time? Would that mean that the vector would now also contain the property of time as well as it’s length and directions from the previous 2 dimensions? I’m guessing that the addition of vectors with Matrixes having 4 values could represent four dimensions. If you could hypothetically imagine traveling through time and winding up at a point in your life no matter the direction length and time, and winding up at that same point given a different path you are looking at the same 4D vector. The sum of any amount of 4 dimensional vectors’ properties added in either Matrix or Row format (Here being Time, Length, and Direction) represent a single 4D vector. There’s probably a different name for 4d vectors. Let me know if I’m off base here. I’m thinking this is getting into some simple quantum physics and how the numbers might help back up the theory.