Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to calculate polar coordinates in c++

#include <iostream>
#include <cmath>		// For 'atan()' which is the inverse fuction of 'tan' in C++,
						// and for 'M_PI' which is the real number 'pi'.
void radius();		// Calculate the radius.
void theeta();		// Calculate the angle associated to the couple (x,y) in degree.

void main() {

	int x, y;
	cout << "Enter the x coordinate: " << flush;
	cin >> x;
    
	cout << "enter the y coordinate: " << flush;
	cin >> y;
    
    radius();
    theeta();
}

void radius() {
	float r;
	r = sqrt(x*x + y*y);
	cout << "Radius = " << r << endl;
}

void theeta() {
	float theta, angle;
	theta = atan(y/x);		// 'theta' is the angle associated to the couple (x, y) in radian.
	angle = theta*(180/M_PI);		// The conversion of 'theta' from radian to degree.
	cout << "Theta = " << angle << endl;
}
 
PREVIOUS NEXT
Tagged: #calculate #polar #coordinates
ADD COMMENT
Topic
Name
8+2 =