10 Replies Latest reply on Jul 13, 2007 8:59 AM by Greg Dove

    comparing angles

    Level 7
      I have a need to compare angles in a project I'm working on.

      For example I have an angle, say 180 degrees. I then want to check if
      another angle is within 45 degress either side of this angle. that's easy
      enough, I can check if my angle is less than 225 and greater than 135. But
      this falls apart when the angle to check against is for example 0 degress. I
      then need to check if my angle is less than 45 and greater than 315, which
      mathematically it can't be.

      Anyone have any ideas how to solve this, sure would appreciate the help.

      Thanks

      Dave


        • 1. Re: comparing angles
          cicnats
          Have you tried getting the difference in angles first, and than checking if it's absolute value is less or greater than the desired angle (45)? A way to get the difference could be just to subtract the angles, and then move that difference to the -180..180 range (by adding or subtracting 360) ..

          I'm not sure if this is entirely correct, i haven't tried it right now. I remember I needed to check angles for a project or two, but am too lazy to go find where that was ;)
          • 2. Re: comparing angles
            Level 7
            I'm trying to throw some numbers at this, but really can't see how it's
            going to work.

            Any other ideas

            Thanks for replying

            "cicnats" <webforumsuser@macromedia.com> wrote in message
            news:f77ilt$j3p$1@forums.macromedia.com...
            > Have you tried getting the difference in angles first, and than checking
            > if
            > it's absolute value is less or greater than the desired angle (45)? A way
            > to
            > get the difference could be just to subtract the angles, and then move
            > that
            > difference to the -180..180 range (by adding or subtracting 360) ..
            >
            > I'm not sure if this is entirely correct, i haven't tried it right now. I
            > remember I needed to check angles for a project or two, but am too lazy to
            > go
            > find where that was ;)
            >


            • 3. Re: comparing angles
              Greg Dove Level 4
              There's probably better ways to do this...but I think this works.
              • 4. Re: comparing angles
                Level 7
                You beauty!!!

                That's works wonderfully and has saved me hours!

                Thanks so much

                Dave

                "GWD" <webforumsuser@macromedia.com> wrote in message
                news:f77qqj$rt7$1@forums.macromedia.com...
                > There's probably better ways to do this...but I think this works.
                >
                > for (var i=-360;i<360;i+=10) {
                > trace('testing :'+i);
                > for (var j=(i-60);j<=(i+60);j+=10){
                > trace(j+" versus "+i+" is "+testAngle(j,i,45))
                > }
                >
                > }
                >
                > function testAngle(angle:Number, versus:Number, difference:Number):Boolean
                > {
                > angle = angle % 360;
                > versus = versus % 360;
                >
                > var diff = (versus-angle) % 360
                > if (diff>180) diff = 360 -diff
                > if (diff<-180) diff = 360 +diff
                > diff = Math.abs(diff)
                >
                > if (diff>difference) return false;
                > return true;
                > }
                >


                • 5. Re: comparing angles
                  cicnats Level 1
                  yeah, well, what was it that I said? ;)

                  try putting this on an empty stage

                  • 6. Re: comparing angles
                    Greg Dove Level 4
                    You're welcome. I just got it working... I didn't try to optimise it.

                    "You beauty!" <== It's been a while since I heard that expression. I usually associate it with Aussies or Kiwis (i.e. people from New Zealand, not the fruit). But perhaps its more global than that.
                    • 7. Re: comparing angles
                      Greg Dove Level 4
                      @cicnats : oops just saw your post. I guess you went and found that code after all. I added in extra checks because I didn't assume it was just for checking _rotation properties (which are always in the -180 to 180 range), but arbitrary angles outside that range. I don't know if that's the case or not.
                      • 8. Re: comparing angles
                        Level 7
                        Thanks for all your input guys, both methods are great, i'm actually working
                        on a gps project, where a wrapper program sends gps data (lat, long, and
                        direction) every second. We then have predefined waypoints and want to check
                        if we match any waypoints and if we are travelling in the right direction
                        (give or take)

                        Everything now working well, thanks a million.

                        And yes "you beauty" is rather australian, but I think it sounds good!

                        Cheers

                        "GWD" <webforumsuser@macromedia.com> wrote in message
                        news:f77ufr$2pa$1@forums.macromedia.com...
                        > @cicnats : oops just saw your post. I guess you went and found that code
                        > after
                        > all. I added in extra checks because I didn't assume it was just for
                        > checking
                        > _rotation properties (which are always in the -180 to 180 range), but
                        > arbitrary
                        > angles outside that range. I don't know if that's the case or not.
                        >


                        • 9. Re: comparing angles
                          cicnats Level 1
                          quote:

                          Originally posted by: GWD
                          @cicnats : oops just saw your post. I guess you went and found that code after all.


                          err.. no, just wrote it on the spot, to see if it works :)

                          but I did remember the last project where I needed that. I had "robots" or "soldiers" walking in formation (wedge), checking each other's position and direction from time to time (their visibility cone was what I needed this for), setting their own waypoints and keeping an eye on their respective sector.. you controlled the pointman's waypoint and the others followed, keeping formation... it was quite cool an experiment.. (it was meant to become a demonstration of how tactical game players should keep formation.. but shortly after that my disk crashed, so now I have only the swfs that I put on a server before it happened... oh well..
                          • 10. Re: comparing angles
                            Greg Dove Level 4
                            quote:

                            err.. no, just wrote it on the spot, to see if it works :)


                            Oh ok... well my comment was only in response to your first post. What I should have said was if I knew you were working on it I would have left it to you, lol. My code didn't have the nice visual element that yours did (I don't think traces count for that!)
                            Sounds like what you did previously was quite cool.... which reminds me I better back up what I'm working on... ;)