Results 1 to 11 of 11

Thread: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

  1. #1

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Resolved [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    the other is i have hit a roadblock in programming math-wise.
    i can't figure out how to convert two points on a grid into an angle.

    edit: final post has working code, but the code is in xna 4 using vector2. The only difference is you will need to find length yourself.
    Last edited by Lord Orwell; Mar 4th, 2011 at 08:48 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: any good online sites to refresh trigonometry?

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: any good online sites to refresh trigonometry?

    Quote Originally Posted by Lord Orwell View Post
    i can't figure out how to convert two points on a grid into an angle.
    The dot product formula makes the angle between vectors easy to compute. For points (a, b), (c, d), the angle between the vectors from (0, 0) to each point is

    ArcCos((a, b) dot (c, d)/|(a, b)||(c, d)|)
    = ArcCos((ac+bd)/Sqrt((a^2+b^2)*(c^2+d^2)))

    It can be proven with the law of cosines, which is a generalization of the Pythagorean theorem. There are much more intuitive ways to state this rule but this one is computationally simple. Sorry, I don't know any trig review sites.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: any good online sites to refresh trigonometry?

    Quote Originally Posted by jemidiah View Post
    The dot product formula makes the angle between vectors easy to compute. For points (a, b), (c, d), the angle between the vectors from (0, 0) to each point is

    ArcCos((a, b) dot (c, d)/|(a, b)||(c, d)|)
    = ArcCos((ac+bd)/Sqrt((a^2+b^2)*(c^2+d^2)))

    It can be proven with the law of cosines, which is a generalization of the Pythagorean theorem. There are much more intuitive ways to state this rule but this one is computationally simple. Sorry, I don't know any trig review sites.
    so (ac + bd), how would i do just that part if point a,b is on the origin? It looks like ac + bd would then be zero and i would get a division by zero error?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: any good online sites to refresh trigonometry?

    If ac+bd = 0 there is no problem, since you're just multiplying by it. If a=b=0, there is a problem, since a^2+b^2 = 0 so you're dividing by 0. To be clear, the formula I gave is used to find the angle theta (not theta_1 or theta_2) as in this picture. The algebraic "trouble" of dividing by 0 when a=b=0 corresponds to the geometric trouble of the A vector having length 0, in which case the angle isn't defined since you're trying to find the angle between a vector and a point, or, if c=d=0, a point and a point.

    My best guess is that you want to find the reference angle of a single vector. In the above picture, the reference angle for vector A is labeled as theta_2. You can find it using my formula by making the B vector, (c, d), lie on the positive x-axis. For instance, use c=1, d=0. The formula simplifies significantly in this special case.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: any good online sites to refresh trigonometry?

    i finally solved this. There was a code sample on a yahoo answers page that i started with, and after i corrected the three glaring errors in it, i rotated the axis and fixed it to make a 0-359 degrees with vertical top being 0.
    C# Code:
    1. Vector2 diff = new Vector2(mx, my);
    2.            Rotation = Math.Asin(1 * diff.Y / diff.Length());
    3.             Rotation = Math.PI / 2 + Rotation;
    4.            if (diff.X < 0.0)
    5.            {
    6.                Rotation = MathHelper.ToRadians(360) - Rotation;// Rotation * -1;
    7.            }
    Last edited by Lord Orwell; Mar 4th, 2011 at 12:05 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    For things like this, in the future you might prefer the Atan2 function. Most modern language's math libraries include it; there's a Wikipedia page on it.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    i actually tried atan2, but didn't have any luck. If you can improve my code, please do so. I'm happy it works, but it is for a game and processing may become an issue.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    I imagine a library atan2 implementation would be somewhat optimized, though no matter what you'll need to evaluate an inverse trig function. Using atan2 gets rid of some extra calculations like square rooting. More importantly, using it makes for simple, readable code--no muddling through trig identities to figure out what the code does.

    The following two pure VC# routines should be identical. The first is your original without using XNA. The atan2 version would be even simpler except your axes are slightly non-standard and you wanted output to be positive.

    Code:
    public double GetAngle1(double x, double y)
    {
        double rotation = Math.Asin(1 * y / (Math.Sqrt(x*x + y*y)));
        rotation += Math.PI / 2;
        if (x < 0.0)
        {
            rotation = 2 * Math.PI - rotation;
        }
        return rotation;
    }
    public double GetAngle2(double x, double y)
    {
        double rotation = Math.Atan2(x, -y);
        return rotation > 0 ? rotation : rotation + 2*Math.PI;
    }
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10

    Thread Starter
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    ok i am not complete learned on all the c#/c style math operands. What do ? and : do?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] [C#] Getting the angle from origin to a point on x, y axis.

    Oh, that's just the inline if syntax. The C#
    Code:
    c ? t : f
    is equivalent to the VB6 (and I believe .NET)
    Code:
    IIf(c, t, f)
    I suppose I should mention the one difference, which I think has been fixed in newer versions of VB.NET. On older versions of the language, both the t and f "arguments" get evaluated in VB, but they're not both evaluated in C# (which follows the C tradition of using short-circuiting logic by default).

    Perhaps I should have written my translation of your code as
    Code:
    public double GetAngle1(double x, double y)
    {
        double rotation = Math.Asin(1 * y / (Math.Sqrt(x*x + y*y)));
        rotation += Math.PI / 2;
        return rotation < 0.0 ? 2 * Math.PI - rotation : rotation;
    }
    I find ? : more idiomatic than small If blocks, but that's just personal preference.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width