cos, sin degree and radian issue!!
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
Code:
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
Re: cos, sin degree and radian issue!!
Why not simply use QTransform?
Re: cos, sin degree and radian issue!!
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
Re: cos, sin degree and radian issue!!
Quote:
Originally Posted by
jesse_mark
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.
Re: cos, sin degree and radian issue!!
Code:
// 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 ?
Re: cos, sin degree and radian issue!!
Quote:
Originally Posted by
jesse_mark
but now how can i apply this transformation on my point ?
QTransform::map()
Re: cos, sin degree and radian issue!!
Quote:
Originally Posted by
jesse_mark
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.
Re: cos, sin degree and radian issue!!
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++ ??
Re: cos, sin degree and radian issue!!
that question is surely answered in numerous places already. here is the first I found.
http://stackoverflow.com/questions/6...-certain-point
Re: cos, sin degree and radian issue!!
Quote:
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/Floatin...uracy_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.
Re: cos, sin degree and radian issue!!
@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.
Re: cos, sin degree and radian issue!!
Quote:
Originally Posted by
jesse_mark
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++ ??
Quote:
Originally Posted by
amleto
Quote:
Originally Posted by
jesse_mark
@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.