Thread: Is the point over my ray? Confusing...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Is the point over my ray? Confusing...

    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 )

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    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.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well, comparing the x coordinate to cos ( rayrot ) makes no sense to me.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    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.

  6. #6

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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 )

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    147
    I'm not sure I understand your query.

    Are you asking if the point is on the line representing the array?

    Like a collision?

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Did anyone say 'array' ? No. We did mention a ray though.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Cool

    Quote Originally Posted by Queatrix View Post
    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 )
    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:
    Code:
    (x * cos(rayrot) == y * sin(rayrot))
    However since floating point math isn't exact you'll need to use something like this:
    Code:
    if (fabs(x * cos(rayrot) - y * sin(rayrot) < EPSILON)
    Where EPSILON is a small fraction.
    Last edited by iMalc; 05-31-2007 at 01: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"

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thank you iMalc, that works.
    I was able to see if it was over the ray just by converting == to > or <.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  3. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 10:49 AM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM