Results 1 to 8 of 8

Thread: Dynamic values for qSin and qCos giving error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Smile Dynamic values for qSin and qCos giving error

    Hi,

    I was trying to draw a circle, and I need two lines inside the circle starting from the origin.
    The code I used is :

    Qt Code:
    1. QPainter Painter(this);
    2. Painter.setRenderHint(QPainter::Antialiasing,true) ;
    3.  
    4. Painter.setPen(QPen(QColor(Qt::black)));
    5. Painter.drawEllipse((circleX-(circleRadius)),(circleY-(circleRadius)),circleRadius*2,circleRadius*2); // Drawing the ellipse (giving same value for height and with)
    6.  
    7. Painter.setPen(QPen(QColor(Qt::black)));
    8. Painter.drawLine(circleX,circleY,circleX+circleRadius,circleY); // Drawing the first line, starting from origin to its right side.
    9. Painter.drawLine(circleX,circleY,circleX+(circleRadius*(qCos(angle*3.14/180.0))),circleY-(circleRadius*(qSin(angle*3.14/180.0)))); // Drawing second line. Starting from origin, the angle value with respect to first line is "angle"
    To copy to clipboard, switch view to plain text mode 
    Screenshot from 2016-05-09 19:24:40.png

    The output i need to get as shown in figure.
    But while drawing second line I'm getting a run time error...
    Alignment trap: not handling instruction ed840a00 at [<764c58cc>]
    Unhandled fault: alignment exception (0x801) at 0x3fc8f5c3
    If I gave angle value as static, its working fine.

    Eg:
    Painter.drawLine(circleX,circleY,circleX+(circleRa dius*(qCos(60*3.14/180.0))),circleY-(circleRadius*(qSin(60*3.14/180.0))));

    Can anyone tell me what I'm doing wrong and how to overcome this??
    Thanks in advance.
    Last edited by sreejithsjt; 9th May 2016 at 15:21.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic values for qSin and qCos giving error

    Is "angle" initialized with a valid value (in degrees, since you are converting to radians)? What happens if you use temporary variables to hold the computed endpoints? Eg.:

    Qt Code:
    1. QPainter Painter(this);
    2. Painter.setRenderHint(QPainter::Antialiasing,true) ;
    3.  
    4. Painter.setPen(QPen(QColor(Qt::black)));
    5. Painter.drawEllipse((circleX-(circleRadius)),(circleY-(circleRadius)),circleRadius*2,circleRadius*2);
    6.  
    7. Painter.setPen(QPen(QColor(Qt::black)));
    8. Painter.drawLine(circleX,circleY,circleX+circleRadius,circleY);
    9.  
    10. double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
    11. double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
    12. Painter.drawLine(circleX,circleY, endX, endY);
    To copy to clipboard, switch view to plain text mode 

    Do you get the same error? If not, then there is likely something wrong elsewhere in your code, and the error is manifest here.

    What are the values of endX and endY in this case? Are they reasonable?

  3. #3
    Join Date
    May 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Dynamic values for qSin and qCos giving error

    Sorry for the delay..

    I tried to run your code

    Qt Code:
    1. Painter.setPen(QPen(QColor(Qt::black)));
    2. Painter.drawLine(circleX,circleY,circleX+circleRadius,circleY);
    3.  
    4. double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
    5. qDebug()<<endX;
    6. double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
    7. qDebug()<<endY;
    8. Painter.drawLine(circleX,circleY, endX, endY);
    To copy to clipboard, switch view to plain text mode 

    I gave angle value as 30 and the output which I got is wrong.
    output:

    1.76689e-74
    2.9108e-65

    Then I added 4 lines to the code for printing qCos and qSin value.

    Qt Code:
    1. Painter.setPen(QPen(QColor(Qt::black)));
    2. Painter.drawLine(circleX,circleY,circleX+circleRadius,circleY);
    3.  
    4. double qcos = qCos(angle*3.14/180.0);
    5. qDebug()<< qcos;
    6. double qsin = qSin(angle*3.14/180.0);
    7. qDebug()<< qsin;
    8.  
    9.  
    10. double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
    11. qDebug()<<endX;
    12. double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
    13. qDebug()<<endY;
    14. Painter.drawLine(circleX,circleY, endX, endY);
    To copy to clipboard, switch view to plain text mode 

    This time I got the output as

    1.76689e-74
    2.9108e-65
    1.93972e-12
    5.2319e-315
    I don't know how these values are coming as output. Totally confused.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic values for qSin and qCos giving error

    Add an output for angle.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic values for qSin and qCos giving error

    As I asked before:

    Is "angle" initialized with a valid value?
    The symptoms your code displays are entirely consistent with "angle" being uninitialized. If "angle" is a member variable of your class, make sure you didn't accidentally declare a local "angle" variable in your constructor that is hiding the member variable. In other words, make sure you aren't doing this:

    Qt Code:
    1. class MyClass
    2. {
    3. // ...
    4.  
    5. private:
    6. double angle;
    7. };
    8.  
    9. MyClass::MyClass()
    10. {
    11. double angle = 123.4;
    12. // ...
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Dynamic values for qSin and qCos giving error

    The angle variable I declared as integer. And its getting value from a text field.

    int angle;
    angle = circleAngleText->toPlainText().toInt();
    After giving value for angle through text field, i tried to print angle value. Its printing correct value.
    Qt Code:
    1. Painter.setPen(QPen(QColor(Qt::black)));
    2. Painter.drawLine(circleX,circleY,circleX+circleRadius,circleY);
    3.  
    4. qDebug()<< angle;
    5. double qcos = qCos(angle*3.14/180.0);
    6. qDebug()<< qcos;
    7. double qsin = qSin(angle*3.14/180.0);
    8. qDebug()<< qsin;
    9.  
    10.  
    11. double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
    12. qDebug()<<endX;
    13. double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
    14. qDebug()<<endY;
    15. Painter.drawLine(circleX,circleY, endX, endY);
    To copy to clipboard, switch view to plain text mode 
    Output:
    30
    1.76689e-74
    2.9108e-65
    1.93972e-12
    5.2319e-315
    I have doubt on qCos and qSin calculation. As I told earlier, if I give fixed values inside qCos and qSin, its giving proper output.

    This problem is only for embedded QT, and the same code works fine in QT creator(In PC). My Embedded QT version is 4.8.5.
    Last edited by sreejithsjt; 14th May 2016 at 14:29.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic values for qSin and qCos giving error

    So if you put qCos( .523 ) it gives the same answer in both platforms (0.866)? (.523 = 30 * 3.14 / 180)

    And if you remove the calculation to outside the qCos call:

    Qt Code:
    1. double radians = angle * 3.14 / 180.0;
    2. qDebug() << qCos( radians );
    To copy to clipboard, switch view to plain text mode 

    do you also get 0.866, or do you get nonsense?

    We are overlooking something obvious here. If qCos() works on a fixed number, it should work on any input. The compiler doesn't care - it is simply passing qCos() the address of its argument, and qCos() doesn't know where the argument came from.

  8. #8
    Join Date
    May 2016
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Dynamic values for qSin and qCos giving error

    Hi,

    I tried all the way you have said.
    I'm getting correct value only if I gave values directly. (ie If I took value from a text field and assigned to an integer, then its giving wrong values for qCos and qSin).
    But if I print the value which I red from text filed using qDebug(), it'll get correct value.

    The same code I tried in PC. Its working fine.

    I'll add the program below which I hav done.

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include "dialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Dialog dialog;
    8. dialog.setMinimumHeight(725);
    9. dialog.setMinimumWidth(1000);
    10.  
    11. dialog.showMaximized();
    12. dialog.show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp

    Qt Code:
    1. #include <QtDebug>
    2.  
    3. #include "dialog.h"
    4. Dialog::Dialog()
    5. {
    6. drawCircle =0;
    7. main_widget = new QWidget;
    8. menu_layout = new QVBoxLayout;
    9.  
    10. circleRadiusText =new QTextEdit("");
    11. circleRadiusText->setFixedHeight(30);
    12. circleRadiusText->setFixedWidth(130);
    13.  
    14. QPushButton *submitButton = new QPushButton("OK");
    15. submitButton->setFixedWidth(300);
    16.  
    17. menu_layout->addWidget(circleRadiusText);
    18. menu_layout->addWidget(submitButton);
    19. setLayout(menu_layout);
    20.  
    21. QObject::connect(submitButton, SIGNAL(clicked()),this, SLOT(draw_Circle()));
    22.  
    23. }
    24. void Dialog::draw_Circle()
    25. {
    26.  
    27. angle = circleRadiusText->toPlainText().toInt();
    28.  
    29. drawCircle =1;
    30. update();
    31. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <stdlib.h>
    6. #include <math.h>
    7. #include <cmath>
    8. #include <iostream>
    9. #include <stdio.h>
    10.  
    11. QT_BEGIN_NAMESPACE
    12. class QAction;
    13. class QGroupBox;
    14. class QLineEdit;
    15. class QMenuBar;
    16. class QTextEdit;
    17. #include <QMainWindow>
    18. #include <QTextEdit>
    19. #include <QString>
    20. #include <QtGui>
    21. #include <QPushButton>
    22. #include <QWidget>
    23. #include <QBoxLayout>
    24. #include <QPainter>
    25. #include <QPaintEvent>
    26. #include <QKeyEvent>
    27. #include <QGraphicsView>
    28. #include <qmath.h>
    29.  
    30. QT_END_NAMESPACE
    31.  
    32. class Dialog : public QDialog
    33. {
    34. Q_OBJECT
    35.  
    36. public:
    37. Dialog();
    38.  
    39. private slots:
    40. void draw_Circle();
    41. void keyPressEvent(QKeyEvent *event)
    42. {
    43. if(event->key() == Qt::Key_Q)
    44. {
    45. qApp->exit();
    46. }
    47. }
    48. void paintEvent(QPaintEvent *)
    49. {
    50. QPainter Painter(this);
    51. Painter.setRenderHint(QPainter::Antialiasing,true) ;
    52. if (drawCircle ==1)
    53. {
    54. int x1,y1,x2,y2;
    55. x1 =100;
    56. y1 = 100;
    57. x2 = 400;
    58. y2 = 400;
    59. int midx = 300;
    60. int midy = 300;
    61. int radius = 200;
    62. angle1 = 100;
    63.  
    64. qDebug()<< angle;
    65. qDebug()<< angle1;
    66. Painter.setBrush(QColor(Qt::white));
    67. Painter.drawEllipse(x1,y1,x2,y2);
    68. Painter.setPen(QPen(QColor(Qt::black)));
    69.  
    70. Painter.drawLine(midx, midy, midx+radius, midy);
    71. Painter.drawLine(midx, midy, midx+(radius*qCos(angle1*3.14/180)),midy+(radius*qSin(angle1*3.14/180))); // This will work fine in both PC and embedded board.
    72. Painter.drawLine(midx, midy, midx+(radius*qCos(angle*3.14/180)),midy+(radius*qSin(angle*3.14/180))); // Only working in PC
    73. }
    74. }
    75. public:
    76. QWidget *main_widget;
    77. QTextEdit *circleRadiusText;
    78. QVBoxLayout *menu_layout;
    79. int angle, angle1;
    80. int drawCircle;
    81.  
    82. };
    83.  
    84. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    By checking this program or by trying this program you can help me I guess.
    Waiting for your reply...
    Last edited by sreejithsjt; 18th May 2016 at 11:46. Reason: reformatted to look better

Similar Threads

  1. Replies: 1
    Last Post: 26th April 2016, 14:46
  2. Replies: 3
    Last Post: 30th April 2012, 07:39
  3. Replies: 4
    Last Post: 31st January 2012, 08:59
  4. WCHAR to QString giving error in vs2005
    By ucomesdag in forum Qt Programming
    Replies: 2
    Last Post: 1st May 2008, 23:25
  5. "make" is giving an error
    By Sarma in forum Qt Programming
    Replies: 6
    Last Post: 21st August 2006, 14:28

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.