PDA

View Full Version : Dynamic values for qSin and qCos giving error



sreejithsjt
9th May 2016, 15:14
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 :


QPainter Painter(this);
Painter.setRenderHint(QPainter::Antialiasing,true) ;

Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawEllipse((circleX-(circleRadius)),(circleY-(circleRadius)),circleRadius*2,circleRadius*2); // Drawing the ellipse (giving same value for height and with)

Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawLine(circleX,circleY,circleX+circleRad ius,circleY); // Drawing the first line, starting from origin to its right side.
Painter.drawLine(circleX,circleY,circleX+(circleRa dius*(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"


11933

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.

d_stranz
10th May 2016, 17:10
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.:



QPainter Painter(this);
Painter.setRenderHint(QPainter::Antialiasing,true) ;

Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawEllipse((circleX-(circleRadius)),(circleY-(circleRadius)),circleRadius*2,circleRadius*2);

Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawLine(circleX,circleY,circleX+circleRad ius,circleY);

double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
Painter.drawLine(circleX,circleY, endX, endY);


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?

sreejithsjt
13th May 2016, 15:21
Sorry for the delay..

I tried to run your code



Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawLine(circleX,circleY,circleX+circleRad ius,circleY);

double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
qDebug()<<endX;
double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
qDebug()<<endY;
Painter.drawLine(circleX,circleY, endX, endY);


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.



Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawLine(circleX,circleY,circleX+circleRad ius,circleY);

double qcos = qCos(angle*3.14/180.0);
qDebug()<< qcos;
double qsin = qSin(angle*3.14/180.0);
qDebug()<< qsin;


double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
qDebug()<<endX;
double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
qDebug()<<endY;
Painter.drawLine(circleX,circleY, endX, endY);


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. :(

anda_skoa
13th May 2016, 16:15
Add an output for angle.

Cheers,
_

d_stranz
13th May 2016, 16:51
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:



class MyClass
{
// ...

private:
double angle;
};

MyClass::MyClass()
{
double angle = 123.4;
// ...
}

sreejithsjt
14th May 2016, 11:54
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.


Painter.setPen(QPen(QColor(Qt::black)));
Painter.drawLine(circleX,circleY,circleX+circleRad ius,circleY);

qDebug()<< angle;
double qcos = qCos(angle*3.14/180.0);
qDebug()<< qcos;
double qsin = qSin(angle*3.14/180.0);
qDebug()<< qsin;


double endX = circleX+(circleRadius*(qCos(angle*3.14/180.0)));
qDebug()<<endX;
double endY = circleY-(circleRadius*(qSin(angle*3.14/180.0)));
qDebug()<<endY;
Painter.drawLine(circleX,circleY, endX, endY);
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.

d_stranz
14th May 2016, 16:51
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:



double radians = angle * 3.14 / 180.0;
qDebug() << qCos( radians );


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.

sreejithsjt
18th May 2016, 08:26
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



#include <QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
dialog.setMinimumHeight(725);
dialog.setMinimumWidth(1000);

dialog.showMaximized();
dialog.show();

return app.exec();
}


dialog.cpp



#include <QtDebug>

#include "dialog.h"
Dialog::Dialog()
{
drawCircle =0;
main_widget = new QWidget;
menu_layout = new QVBoxLayout;

circleRadiusText =new QTextEdit("");
circleRadiusText->setFixedHeight(30);
circleRadiusText->setFixedWidth(130);

QPushButton *submitButton = new QPushButton("OK");
submitButton->setFixedWidth(300);

menu_layout->addWidget(circleRadiusText);
menu_layout->addWidget(submitButton);
setLayout(menu_layout);

QObject::connect(submitButton, SIGNAL(clicked()),this, SLOT(draw_Circle()));

}
void Dialog::draw_Circle()
{

angle = circleRadiusText->toPlainText().toInt();

drawCircle =1;
update();
}


dialog.h



#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <stdio.h>

QT_BEGIN_NAMESPACE
class QAction;
class QDialogButtonBox;
class QGroupBox;
class QLineEdit;
class QMenuBar;
class QPushButton;
class QTextEdit;
#include <QMainWindow>
#include <QTextEdit>
#include <QString>
#include <QtGui>
#include <QPushButton>
#include <QWidget>
#include <QBoxLayout>
#include <QPainter>
#include <QPaintEvent>
#include <QKeyEvent>
#include <QGraphicsView>
#include <qmath.h>

QT_END_NAMESPACE

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog();

private slots:
void draw_Circle();
void keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Q)
{
qApp->exit();
}
}
void paintEvent(QPaintEvent *)
{
QPainter Painter(this);
Painter.setRenderHint(QPainter::Antialiasing,true) ;
if (drawCircle ==1)
{
int x1,y1,x2,y2;
x1 =100;
y1 = 100;
x2 = 400;
y2 = 400;
int midx = 300;
int midy = 300;
int radius = 200;
angle1 = 100;

qDebug()<< angle;
qDebug()<< angle1;
Painter.setBrush(QColor(Qt::white));
Painter.drawEllipse(x1,y1,x2,y2);
Painter.setPen(QPen(QColor(Qt::black)));

Painter.drawLine(midx, midy, midx+radius, midy);
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.
Painter.drawLine(midx, midy, midx+(radius*qCos(angle*3.14/180)),midy+(radius*qSin(angle*3.14/180))); // Only working in PC
}
}
public:
QWidget *main_widget;
QTextEdit *circleRadiusText;
QVBoxLayout *menu_layout;
int angle, angle1;
int drawCircle;

};

#endif // DIALOG_H


By checking this program or by trying this program you can help me I guess.
Waiting for your reply...