Wednesday, February 8, 2012

2.14

Two distances are required to specify the location of a point relative to an origin in two dimensional space:

  • The horizontal and vertical distances (x,y) in Cartesian coordinates
  • The radius and angle (r, theta) in radial coordinates

It is relatively straightforward to compute Cartesian coordinates (x,y) on the basis of polar coordinates (r,theta). The reverse process is not so simple. The radius can be computed by the following formula: 
r = sqrt(x^2 + y^2)
If the coordinates lie within the first and fourth coordinates (i.e., x>0), then a simple formula can be used to compute theta:
theta = arctan(y/x)
The difficulty arises for the other cases. The following table summarizes the possibilities:


Write a program to calculate r and theta and test is by filling in this table:

x= [1,1,0,-1,-1,-1,0,1,0];
y= [0,1,1,1,0,-1,-1,-1,0];

theta = atan2(y,x);
theta = theta +(theta<0)*2*pi
r = (sqrt(x.^2+y.^2))



No comments:

Post a Comment