Results 1 to 12 of 12

Thread: cos, sin degree and radian issue!!

  1. #1
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. qreal xo,yo,x1,y1,radianAngle,angle;
    2. qreal newX,newY;
    3.  
    4.  
    5. angle = 270;
    6. radianAngel = angle * M_PI/180;
    7.  
    8. xo = 0;
    9. yo = 0;
    10. x1 = 0;
    11. y1 = 1000;
    12. qreal c = cos(radianAngel);
    13. qreal s = sin(radianAngel);
    14.  
    15. newX= (x1-xo) * c - (y1-yo) * s+yo;
    16. newY= (x1-xo) * s + (y1-yo) * c+yo;
    17.  
    18. qDebug()<< QString("rotatin x=%1 , y=%2 with %3 = newx=%4 newy=%5")
    19. .arg(x1)
    20. .arg(y1)
    21. .arg(angel)
    22. .arg(newX)
    23. .arg(newY);
    24.  
    25. // this is what i get !!
    26. // "rotating x=0 , y=1000 with 270 = newx=1000 newy=-1.83691e-13"
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cos, sin degree and radian issue!!

    Why not simply use QTransform?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cos, sin degree and radian issue!!

    Quote Originally Posted by jesse_mark View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cos, sin degree and radian issue!!

    Qt Code:
    1. // xo,yo >> point i want to rotate around it.
    2. // x1,y1 >> point i want to rotate.
    3. // angle >> rotation angle
    4.  
    5. QTransform transform;
    6. transform.translate(xo, yo);
    7. transform.rotate(angle);
    8. // QPointF newpos(x,y);
    To copy to clipboard, switch view to plain text mode 
    but now how can i apply this transformation on my point ?
    Last edited by jesse_mark; 3rd April 2013 at 17:33.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cos, sin degree and radian issue!!

    Quote Originally Posted by jesse_mark View Post
    but now how can i apply this transformation on my point ?
    QTransform::map()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    jesse_mark (3rd April 2013)

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cos, sin degree and radian issue!!

    Quote Originally Posted by jesse_mark View Post
    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.

  9. #8
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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++ ??
    Last edited by jesse_mark; 3rd April 2013 at 17:25.

  10. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cos, sin degree and radian issue!!

    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.

  12. The following user says thank you to ChrisW67 for this useful post:

    jesse_mark (4th April 2013)

  13. #11
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  14. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: cos, sin degree and radian issue!!

    Quote Originally Posted by jesse_mark View Post
    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 View Post
    that question is surely answered in numerous places already. here is the first I found.

    http://stackoverflow.com/questions/6...-certain-point
    Quote Originally Posted by jesse_mark View Post
    @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.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. How to play video , by rotating at 90 degree?
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 6
    Last Post: 21st April 2011, 12:11
  2. Qt 4.7.1 VNC issue?
    By djstava in forum Newbie
    Replies: 0
    Last Post: 14th March 2011, 03:34
  3. Degree of color
    By NewLegend in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2010, 15:33
  4. Button grid with label rotate 90 degree
    By whitefurrows in forum Qt Programming
    Replies: 14
    Last Post: 24th June 2009, 17:17
  5. qt3 to qt4 - uic issue
    By hvengel in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2007, 02:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.