I am trying to detect if my point (x & y) are over my ray (rayrot).
This is the best I could come up with, but it's not working. How should I do this?
Code:if ( x > cos ( rayrot ) * y && y > sin ( rayrot ) * x )
This is a discussion on Is the point over my ray? Confusing... within the C++ Programming forums, part of the General Programming Boards category; I am trying to detect if my point (x & y) are over my ray (rayrot). This is the best ...
I am trying to detect if my point (x & y) are over my ray (rayrot).
This is the best I could come up with, but it's not working. How should I do this?
Code:if ( x > cos ( rayrot ) * y && y > sin ( rayrot ) * x )
Define "not working". Also, I'm not sure what kind of degrees cos() and sin() take in input. Euler or Radians ? Your code sounds about right if we're talking about radians.
Yeah, I'm using radians. "not working" as in, it compiles and runs without errors, but it doesn't accomplish what I want it to.
Well, comparing the x coordinate to cos ( rayrot ) makes no sense to me.
I don't see why you're multiplying the cos and sin values by the same coordinates you wish to compare to. You should multiply those with the radius to get the correct X and Y values.
Also, you will get an inverse result if the coordinates are negative.
The radius thing doesn't work either, here is what I have:
Code:raid = sqrt ( ( x * x ) + ( y * y ) ) ; if ( x > cos ( rayrot ) * raid && y > sin ( rayrot ) * raid )
Just a guess, and it might not be the most efficient way to solve this, but couldn't you check if the inverse tangent of y/x is greater than rayrot? Essentially, if the angle that the position vector (x,y) makes with the x axis is greater than the angle the ray makes with the x axis, then the point must be above the ray.
Of course, you'd have to be careful to make adjustments if the point or ray is not in the first quadrant.
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
I'm not sure I understand your query.
Are you asking if the point is on the line representing the array?
Like a collision?
Did anyone say 'array' ? No. We did mention a ray though.
Ray - what ray?
A Ray consists of a point and a direction, where the direction would normally be a vector. However this 'rayrot' seems to be just a single value, therefore it is just an angle.
The problem would however make sense, if you state that the ray happens to cross through (0, 0) for example (which I'll assume). However since you're using angles instead of vectors, you also need to state which direction an angle of zero points in. (I'll assume that increasing angle turns clockwise, and that zero is straight up, for now).
This would mean that a point (x, y) is on the ray if:However since floating point math isn't exact you'll need to use something like this:Code:(x * cos(rayrot) == y * sin(rayrot))
Where EPSILON is a small fraction.Code:if (fabs(x * cos(rayrot) - y * sin(rayrot) < EPSILON)
Last edited by iMalc; 05-31-2007 at 02:48 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Thank you iMalc, that works.
I was able to see if it was over the ray just by converting == to > or <.
Oh, I took "over" to mean "on" rather than "above".
Yes that'll do it.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"