PDA

View Full Version : cos, sin degree and radian issue!!



jesse_mark
3rd April 2013, 15:36
Im trying to do a simple rotation to a point, but i dont know why im not getting it accurate.

as i know the cos and sin expect an angle in radian.

f.e. I want to rotate a point A(x1,y1) around a Point (xo,yo) with 270 degree.
lets assume



qreal xo,yo,x1,y1,radianAngle,angle;
qreal newX,newY;


angle = 270;
radianAngel = angle * M_PI/180;

xo = 0;
yo = 0;
x1 = 0;
y1 = 1000;
qreal c = cos(radianAngel);
qreal s = sin(radianAngel);

newX= (x1-xo) * c - (y1-yo) * s+yo;
newY= (x1-xo) * s + (y1-yo) * c+yo;

qDebug()<< QString("rotatin x=%1 , y=%2 with %3 = newx=%4 newy=%5")
.arg(x1)
.arg(y1)
.arg(angel)
.arg(newX)
.arg(newY);

// this is what i get !!
// "rotating x=0 , y=1000 with 270 = newx=1000 newy=-1.83691e-13"



I don't know why for the newY im not getting "0" instead i'm getting "-1.83691e-13".
so the issue is when i transfer to radian and get the cos and sin im not getting the exact correct value.

any suggestion, or what mistake im doing.

Thanks

wysota
3rd April 2013, 15:58
Why not simply use QTransform?

jesse_mark
3rd April 2013, 16:21
honestly, i do not know how to use it to rotate one point around another point.

looking at QTransform & QTransform::rotate ( qreal angle, Qt::Axis axis = Qt::ZAxis ), i dot know how to give it the point i want to rotate it and the point i want to rotate around it .

I would really appreciate if give me an example of how can i use it in my case

Thanks

wysota
3rd April 2013, 16:26
honestly, i do not know how to use it to rotate one point around another point.
You translate the origin of the transform to the pivot point and then rotate.

jesse_mark
3rd April 2013, 16:57
// xo,yo >> point i want to rotate around it.
// x1,y1 >> point i want to rotate.
// angle >> rotation angle

QTransform transform;
transform.translate(xo, yo);
transform.rotate(angle);
// QPointF newpos(x,y);



but now how can i apply this transformation on my point ?

wysota
3rd April 2013, 16:59
but now how can i apply this transformation on my point ?
QTransform::map()

Lesiok
3rd April 2013, 17:12
I don't know why for the newY im not getting "0" instead i'm getting "-1.83691e-13".


Because it is normal for floating point arithmetic. -1.83691e-13 = -0.000000000000083691.
270*M_PI/180 has no exact binary representation. So You compute cos and sin for in example 270.00000001 or 269.99999998 not for 270 degrees.

jesse_mark
3rd April 2013, 17:25
Thank you so much for bearing with me, and for your guid

Added after 9 minutes:

Yeah that is what i noticed, and i was trying to find a way to correct that, so i get the exact correct result.

The QTransform did what i want though.

but i wonder if i don't have the QTransform class, how would I get the correct result if im using C or C++ ??

amleto
3rd April 2013, 21:04
that question is surely answered in numerous places already. here is the first I found.

http://stackoverflow.com/questions/620745/c-rotating-a-vector-around-a-certain-point

ChrisW67
4th April 2013, 07:09
I don't know why for the newY im not getting "0" instead i'm getting "-1.83691e-13".
so the issue is when i transfer to radian and get the cos and sin im not getting the exact correct value.

You are assuming that a fixed length floating point representation of an arbitrary real number is precise, that the transcendental number PI is precisely definable at any precision, that trig functions using such numbers are precise, and that combining a such numbers in a string of mathematical operations is also precise. These assumptions are not true. See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems for some more.

If you are going to continually rotate the same point over time the errors will accumulate. If this is important then you are better off tracking the total rotation in degrees and, starting from the original un-rotated point, do a single rotation.

jesse_mark
4th April 2013, 16:11
@amleto
Thanks, but you probable miss understood my question, it was not about how to rotate about other point, it was how to get accurate result.

@ChrisW67
thanks for the explanation and the link was great, it explains a lot to me.

amleto
5th April 2013, 21:59
Thank you so much for bearing with me, and for your guid

Added after 9 minutes:

Yeah that is what i noticed, and i was trying to find a way to correct that, so i get the exact correct result.

The QTransform did what i want though.

but i wonder if i don't have the QTransform class, how would I get the correct result if im using C or C++ ??


that question is surely answered in numerous places already. here is the first I found.

http://stackoverflow.com/questions/620745/c-rotating-a-vector-around-a-certain-point


@amleto
Thanks, but you probable miss understood my question, it was not about how to rotate about other point, it was how to get accurate result.

I think you misunderstood, er, yourself.