Inverse Tangent

This is a discussion on Inverse Tangent within the C++ Programming forums, part of the General Programming Boards category; I'm creating a program (console), that converts rectangular coordinates to polar coordinates (to help me on my math work). Now, ...

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Inverse Tangent

    I'm creating a program (console), that converts rectangular coordinates to polar coordinates (to help me on my math work). Now, the problem is that the answers for my problems, and the answers my program returns, are two different things. My answers are correct (according to my book, and a TI-83+ calculator).

    Here's all my code I use to do the solving:
    Code:
    double FindX(double r, double theta){
    	cout << "x = r * cos(theta)\n";
    	cout << "x = " << r << " * cos(" << theta << ")\n";
    
    	x = cos(theta);
    	x *= r;
    
    	cout << "x = " << x << "\n\n";
    
    	return x;
    }
    
    double FindY(double r, double theta){
    	cout << "y = r * sin(theta)\n";
    	cout << "y = " << r << " * sin(" << theta << ")\n";
    
    	y = sin(theta);
    	y *= r;
    
    	cout << "y = " << y << "\n\n";
    
    	return y;
    }
    
    double FindR(double x, double y){
    	double x2, y2;
    
    	x2 = x * x;
    	y2 = y * y;
    
    	r = sqrt(x2);
    	r += sqrt(y2);
    
    	return r;
    }
    
    double FindTheta(double y, double x){
    	if((x > 0) || (x < 0)){
    		theta = atan((y / x));
    	}
    
    	else if((x == 0)){
    		DisplayError("Division By Zero!");
    	}
    
    	return theta;
    }
    
    double RT(double x, double y){
    	cout << "NOTICE: sqrt(...) means square root\n";
    	cout << "        \"^-1\" means inverse\n\n";
    
    	cout << "r = sqrt(x^2 + y^2)\n";
    	cout << "r = sqrt(" << (x * x) << " + " << (y * y) << ")\n";
    
    	r = FindR(x, y);
    
    	cout << "r = " << r << "\n\n";
    
    	cout << "theta = tan^-1(y/x)\n";
    	cout << "theta = tan^-1(" << y << "/" << x << ")\n";
    
    	theta = FindTheta(y, x);
    
    	cout << "theta = " << theta << "\n\n";
    
    	cout << "( " << r << " , " << theta << " )\n\n";
    
    	return r;
    	return theta;
    }
    I don't understand why the answers are different. Is it because MSVC++'s tan returns different values, or maybe sin and cosin do also?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    From what I have experienced with the math functions is that they use radians instead of degrees.

    If you dont know what radians is this is a short explanation:

    Consider a circle with radius of one. Now to get to know how many degrees 1 radian is you check where the arc of the circle is as long as the radius, in this case 1. There you have 1 radian. A circle with radius 1 has a total arc of 2pi. So then 2pi radians equals 360 degrees. so 1 degree equals 2pi/360, or pi/180, and then x degrees equals x*pi/180 radians. Opposite goes for radians: 1radian = 360/2pi = 180/pi degrees.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Shakti
    From what I have experienced with the math functions is that they use radians instead of degrees.

    If you dont know what radians is this is a short explanation:

    Consider a circle with radius of one. Now to get to know how many degrees 1 radian is you check where the arc of the circle is as long as the radius, in this case 1. There you have 1 radian. A circle with radius 1 has a total arc of 2pi. So then 2pi radians equals 360 degrees. so 1 degree equals 2pi/360, or pi/180, and then x degrees equals x*pi/180 radians. Opposite goes for radians: 1radian = 360/2pi = 180/pi degrees.
    So, if I got polar coordinates of (7, 60), and I want to change them into rectangular coordinates of (x, y), I'd have to do this:

    x = 7 * x(pi/60)?
    y = 7 * y(pi/60)?

    I thought it was:

    x = radius * cos(theta)
    y = radius * sin(theta)

    my method is what causes me trouble on converting polar to rectangular.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    I dont know anything about polar coordinates, all I know was that the math functions uses radians lol. I dunno I might be totally way out lol.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Shakti
    I dont know anything about polar coordinates, all I know was that the math functions uses radians lol. I dunno I might be totally way out lol.
    lol. Thanks though for the information though. It'll prob. come in handy later on.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you would do is use:
    x = radius * cos(theta)
    y = radius * sin(theta)

    and then you would convert that value into a degree answer by:
    x = x * (180/pi)
    y = y * (180/pi)

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Thantos
    What you would do is use:
    x = radius * cos(theta)
    y = radius * sin(theta)

    and then you would convert that value into a degree answer by:
    x = x * (180/pi)
    y = y * (180/pi)
    Ok, I've done that, here's what I've done now:
    Code:
    //...
    
    const double pi = 3.141592654;
    
    // ...
    
    double FindX(double r, double theta){
    	cout << "x = r * cos(theta)\n";
    	cout << "x = " << r << " * cos(" << theta << ")\n";
    
    	x = cos(theta);
    	x *= r;
    
    	cout << "x = " << x << "\n\n";
    
    	return x;
    }
    
    double FindY(double r, double theta){
    	cout << "y = r * sin(theta)\n";
    	cout << "y = " << r << " * sin(" << theta << ")\n";
    
    	y = sin(theta);
    	y *= r;
    
    	cout << "y = " << y << "\n\n";
    
    	return y;
    }
    
    double FindR(double x, double y){
    	double x2, y2;
    
    	x2 = x * x;
    	y2 = y * y;
    
    	r = sqrt((x2 + y2));
    
    	return r;
    }
    
    double FindTheta(double y, double x){
    	double theta2 = 0;
    
    	if((x > 0) || (x < 0)){
    
    		y = 180/pi;
    		x = 180/pi;
    
    		theta = atan(y / x);
    		
    		theta2 = pow(theta, -1);
    	}
    
    	else if((x == 0)){
    		DisplayError("Division By Zero!");
    	}
    
    	return theta2;
    }
    
    //...
    I can get the right value of "R" now, but I'm still not getting theta, x, or y correct.

    theta = [inverse_tangent](y/x)

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    opps my mistake on one part

    It should be:
    x = cos(theta) * (180/pi)
    y = sin(theta) * (180/pi)

    x = x * r
    y = y * r

    you need to convert the sin and cos to from radians to degress first. sorry about that

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    That seems to be working good, thanks

    Onto another question though, how do I do the inverse tangent?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    NAME
    atan - arc tangent function

    SYNOPSIS
    #include <math.h>

    double atan(double x);

    DESCRIPTION
    The atan() function calculates the arc tangent of x; that is the value whose tangent is x.

    RETURN VALUE
    The atan() function returns the arc tangent in radians and the value is mathematically defined to be between -PI/2 and PI/2 (inclusive).
    I highlighted the two really important parts. This is important because arc tangent limits results to quadrents I and IV.

    Here is another function using 2 varables. I've never used it so I'm not sure on how well it works. Also notice the difference in the range of values it will return.
    NAME
    atan2 - arc tangent function of two variables

    SYNOPSIS
    #include <math.h>

    double atan2(double y, double x);

    DESCRIPTION
    The atan2() function calculates the arc tangent of the two variables x and y. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result.

    RETURN VALUE
    The atan2() function returns the result in radians, which is between -PI and PI (inclusive).

  11. #11
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Tantos, thank you so much!!

    That works perfectly for what I need on converting rectangular to polar.

    I am still having trouble though trying to change polar into rectangular. Do you have any ideas on howto fix it?

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'll let you know next week. We just started talking about it in my Precalc class (didn't have time to cover it in Trig)

    But basically you would just reverse the process. Given an x and y pair you calculate the distance, find theta, convert to radians, and call the apprioate functions.

  13. #13
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Thantos
    I'll let you know next week. We just started talking about it in my Precalc class (didn't have time to cover it in Trig)

    But basically you would just reverse the process. Given an x and y pair you calculate the distance, find theta, convert to radians, and call the apprioate functions.
    Thank you. Wouldn't the pow(...) function work though?

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well you could use pow to find distance

    pow ( pow(x, 2) + pow(y, 2), .5);

    but then you have to locate the quadrient, calculate the reference angel, to find the value of theta.

  15. #15
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Thantos
    well you could use pow to find distance

    pow ( pow(x, 2) + pow(y, 2), .5);

    but then you have to locate the quadrient, calculate the reference angel, to find the value of theta.
    There's a problem though, there's no need to find the distance

    Thanks though. I'll work something out. If I find out before you tell me, I'll post it here.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to inverse a trig?
    By Diablo02 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2007, 09:11 PM
  2. Multiplying tangent and bitangent by scale matrix
    By psychopath in forum Game Programming
    Replies: 6
    Last Post: 02-08-2006, 07:28 AM
  3. ROW OPERATIONS (finding Inverse) (im going crazy)
    By alexpos in forum C Programming
    Replies: 1
    Last Post: 11-20-2005, 09:07 AM
  4. inverse matrix
    By Yumin in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2005, 11:06 PM
  5. Calculating inverse tangent
    By bludstayne in forum Tech Board
    Replies: 12
    Last Post: 02-27-2004, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21