delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/03/14:45:57

From: Martin Ambuhl <mambuhl AT earthlink DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: angle
Date: Wed, 03 Mar 1999 13:52:29 -0500
References: <36dd5abb DOT 0 AT news DOT sbbs DOT se>
X-Posted-Path-Was: not-for-mail
X-Accept-Language: en
X-ELN-Date: 3 Mar 1999 18:52:20 GMT
X-ELN-Insert-Date: Wed Mar 3 11:15:33 1999
Organization: Nocturnal Aviation
Lines: 113
Mime-Version: 1.0
NNTP-Posting-Host: 1cust181.tnt11.nyc3.da.uu.net
Message-ID: <36DD84ED.FAF5CF82@earthlink.net>
X-Mailer: Mozilla 4.5 [en] (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Erik Johansson wrote:
> 
> Lets say I have two points on the screen. (x1,y1) and (x2,y2)
> And then I draw a line between the points.
> How can I calculate the angle of that line in degrees?

This is actually elementary math and has very little to do with C or
C++, and has been asked and answered several times in the last week. 
Just try running the following C program for some hints.  This is math
you should have already had.
#include <math.h>
#include <stdio.h>
#if !defined(M_PI)
#define M_PI        3.14159265358979323846  /* pi */
#endif

typedef struct {
    double x, y;
} vector;
typedef struct {
    double r, a;
} polar;

double sqr(double x)
{
    return x * x;
}
double degrees(double x)
{
    return x * 180. / M_PI;
}
double degrees2(double x)
{
    while (x > 2 * M_PI)
        x -= 2 * M_PI;
    while (x < 0)
        x += 2 * M_PI;
    return degrees(x);
}
double radians(double x)
{
    return x * M_PI / 180.;
}

polar to_polar(vector v)
{
    polar p;
    p.r = sqrt(sqr(v.x) + sqr(v.y));
    p.a = atan2(v.y, v.x);
    return p;
}

vector to_vector(polar p)
{
    vector v;
    v.x = p.r * cos(p.a);
    v.y = p.r * sin(p.a);
    return v;
}

double dot_product(vector a, vector b)
{
    return a.x * b.x + a.y * b.y;
}

double cross_product(vector a, vector b)
    /* actually a the magnitude of a vector normal to the xy plane */
{
    return a.x * b.y - a.y * b.x;
}


int main(void)
{
    vector a = {10, 5}, b = {5, 10}
    /*, ivector = {1, 0}, jvector = {0, 1} */;
    polar ra, rb;
    double dp, cp, theta;

    printf("Let's look at a=%gi+%gj and b=%gi+%gj.\n",
           a.x, a.y, b.x, b.y);
    ra = to_polar(a);
    rb = to_polar(b);
    printf("a is (%g,%g = %g degrees) in polar coords\n",
           ra.r, ra.a, degrees(ra.a));
    printf("b is (%g,%g = %g degrees) in polar coords\n",
           rb.r, rb.a, degrees(rb.a));
    printf("The difference between the angles is %g = %g or %g
degrees\n",
           rb.a - ra.a, degrees(rb.a - ra.a), degrees2(rb.a - ra.a));
    printf("Note the interesting facts:\n"
           "  The dotproduct a*b = %g\n"
           "  The crossproduct axb = %gk\n",
           dp = dot_product(a, b), cp = cross_product(a, b));
    theta = acos(dp / (ra.r * rb.r));
    printf("   acos(a*b/(mag(a)*mag(b)) = %g (%g or %g degrees))\n",
           theta, degrees(theta), degrees2(theta));

    theta = asin(cp / (ra.r * rb.r));
    printf("   asin(axb/(mag(a)*mag(b)) = %g (%g or %g degrees))\n",
           theta, degrees(theta), degrees2(theta));

    theta = atan(fabs(cp) / dp);
    printf("   atan(|axb|/(a*b)) = %g (%g or %g degrees))\n",
           theta, degrees(theta), degrees2(theta));
    return 0;
}



-- 
Martin Ambuhl (mambuhl AT earthlink DOT net)
Note: mambuhl AT tiac DOT net will soon be inactive

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019