PDA

View Full Version : rotating a point by x-axis is the formula valid fo

AeroSujan
09-29-2011, 02:04 PM
Dear Friends
I am trying to sweep a set of points by some degree theta. So I am using this formula
newx = x;
newy = y * cos(theta) - z * sin(theta);
newz = y * sin(theta) + z * cos(theta);

Is this formula valid for any degree theta.
Because when theta=360 degree, then sin(theta) = 1.47e--05

Is this okay. Please give me some idea.

AleaIactaEst
09-29-2011, 03:57 PM
Yeah, that formula is valid for any degree theta.

The result of your calculations and trigonometric functions you use depend on the precision of your variables of course, i.e. if you're using float / double / etc. .

1.47e-5 is close to 0, so the result should be fine. However if you want better results (move closer to 0) you need to use higher precision.

overlay
09-30-2011, 10:04 AM
This is the output of the following test program (Linux x86_64 and gcc):

passing 360.0 by mistake: 0.958916
passing 360.0f by mistake: 0.958916
360 degrees: -2.44929e-16
360.0f degrees: 1.74846e-07

You should print out your value of theta. You are probably not exactly at 360 degrees because even in single precision, I obtain a value two order of magnitude smaller (e-07) than your result (e-05).

Do you compute your theta incrementally (theta+=step) or directly (theta=i*step)? If you compute it incrementally, you will have an accumulated error in theta.

// debug:
// g++ -O0 -ggdb -g3 -Wall -Wextra -Woverloaded-virtual -o mysin sin.cc
// mysin
//
// release:
// g++ -DNDEBUG -O3 -g0 -Wall -Wextra -Woverloaded-virtual -o mysin2 sin.cc
// mysin2

#include <cmath>
#include <iostream>

int main()
{
const double Pid=acos(-1.0);
const float Pif=acosf(-1.0f);

std::cout << Pid*2.0 << " radians: " << sin(Pid*2.0) << std::endl;
std::cout << Pif*2.0f << " radians: " << sinf(Pif*2.0f) << std::endl;

std::cout << "passing 360.0 by mistake: " << sin(360.0) << std::endl;
std::cout << "passing 360.0f by mistake: " << sinf(360.0f) << std::endl;

double x=360.0;
float y=360.0f;

std::cout << "360 degrees: " << sin(x*Pid/180.0) << std::endl;
std::cout << "360.0f degrees: " << sinf(y*Pif/180.0f) << std::endl;

return 0;
}