Code: Select all
1: function proj(vector_one, vector_two) {
2: //try a different formula
3: //dp = vector_one[0]*vector_two[0] + vector_one[1]*vector_two[1];
4: //return [( dp / (vector_two[0]*vector_two[0] + vector_two[1]*vector_two[1]) ) * vector_two[0],( dp / (vector_two[0]*vector_two[0] + vector_two[1]*vector_two[1]) ) * vector_two[1]]
5: //vector_one onto vector_two
6: var a = dot(vector_one, vector_two);
7: var b = dot(vector_two, vector_two);
8: return dot([a[0]/b[0], a[1]/b[1]], vector_two);
9: }
In line 8, you're treating
a and
b as vectors, whereas they are the result of a dot product operation on two vectors, the result of which is a scalar.
(Also, you're giving the dot() function too many arguments.)
Line 8 should be:
Your commented lines 3-4 accomplish exactly as much.
...and you've probably already got this covered, but these are floats you're using, right? Not ints? (I don't know if that's automatically handled in ActionScript.)
Code: Select all
r = Math.sqrt(radx*radx+rady*rady);
To warn you, I don't know ActionScript...
So the radii of your circle are radx in the x-direction and rady in the y-direction, I'm guessing so you have the option of making an oval.
If it's a circle you're using, then the radius in the x-direction is going to be the same as the radius in the y-direction, so why not just use r = radx or r = rady? Because sqrt(radx^2 + rady^2) will give you the length to the corner of a
box that
contains the circle. I don't know if that's what you're looking for, but the derivation I gave sort of relies on
r being the radius of the circle, not the distance from the center to the corner of a box containing the circle.
mattk210 wrote:EDIT AGAIN: where does 60 degrees come from? I think that might be the issue.
Well, you've reminded me that I forgot to give an additional basic definition of a dot product:
If v_one and v_two are vectors, then v_one (dot) v_two = |v_one|*|v_two|*cos(the angle between v_one and v_two)
I use that with the assumption that the angle between them is 60 degrees, and I solve for the individual vectors on the left side because we already know all the magnitudes.
But also... yeah, I do seem to have just pulled 60 degrees out of my ass. I could swear there's some principle in mathematics that says that those angles are always 60 degrees, but now that doesn't seem to make sense to me (like if you extend L to be, say, 5 million, then the 30-60-90 thing totally doesn't hold).
Well, you could still get that angle by doing an inverse cosine of (r / mag(L)). Call it
theta or something. And then you'd have to adjust a few lines, notably:
Code: Select all
# derived quantities:
L = Vector( x_0 - x_c, y_0 - y_c ) # unchanged
theta = inverse_cosine( r / mag(L) )
r_1 = ( r * mag(L) * cosine(theta) ) / L
r_2 = ( r^2 * cosine(2 * theta) ) / r_1
## or the version you'd probably want to put...
## (this assumes that ActionScript has functions Math.cos and Math.cos_inv;
## they're probably named different things than I'm calling them)
# theta = Math.cos_inv(r / mag(L))
# var r_1 = [(r * mag(L) * Math.cos(theta)) / L[0], (r * mag(L) * Math.cos(theta)) / L[1]]
# var r_2 = [(r*r * Math.cos(2 * theta)) / r_1[0], (r*r * Math.cos(2 * theta)) / r_1[1]]
Barring a faulty implementation of ring, circle, or line objects, I don't see why that wouldn't work. Then again, I haven't actually tested it for myself (felt lazy after spending, like, an hour and a half typing that post :p).
If that doesn't work, I might've done my math wrong. :/