Commit 1ac7883f authored by Hugo Carneiro's avatar Hugo Carneiro Committed by Chris Rizzitello

QtMath now provides M_PI constant, operations with degrees, and c++ provides round()

Summary:
Since QtMath library has a proper M_PI the define is not necessary anymore. Furthermore, C++'s round() function behaves in similar fashion to the ROUND2INT macro (rounding away
from zero). If this style of rounding is not specifically required, I suggest another revision to change it to rint(), which rounds towards even numbers.

Reviewers: tcanabrava, rizzitello

Reviewed By: tcanabrava, rizzitello

Subscribers: kde-edu

Tags: #kde_edu

Differential Revision: https://phabricator.kde.org/D16463
parent cd94d3d1
......@@ -23,18 +23,12 @@
#include <QResizeEvent>
#include <QWheelEvent>
#include <QtMath>
#include <KLocalizedString>
// this function is used in executer and canvas:
#define ROUND2INT(x) ( (x) >= 0 ? (int)( (x) + .5 ) : (int)( (x) - .5 ) )
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288419717
#endif
const double DegToRad = M_PI / 180.0;
int kTurtleZValue = 1;
int kCanvasFrameZValue = -10000;
int kCanvasMargin = 20;
......@@ -143,16 +137,16 @@ void Canvas::slotClear()
void Canvas::slotForward(double x)
{
double x2 = turtle->pos().x() + (x * std::sin(turtle->angle() * DegToRad));
double y2 = turtle->pos().y() - (x * std::cos(turtle->angle() * DegToRad));
double x2 = turtle->pos().x() + (x * std::sin(qDegreesToRadians(turtle->angle())));
double y2 = turtle->pos().y() - (x * std::cos(qDegreesToRadians(turtle->angle())));
drawLine(turtle->pos().x(), turtle->pos().y(), x2, y2);
slotGo(x2, y2);
}
void Canvas::slotBackward(double x)
{
double x2 = turtle->pos().x() - ( x * std::sin(turtle->angle() * DegToRad) );
double y2 = turtle->pos().y() + ( x * std::cos(turtle->angle() * DegToRad) );
double x2 = turtle->pos().x() - ( x * std::sin(qDegreesToRadians(turtle->angle())));
double y2 = turtle->pos().y() + ( x * std::cos(qDegreesToRadians(turtle->angle())));
drawLine(turtle->pos().x(), turtle->pos().y(), x2, y2);
slotGo(x2, y2);
}
......@@ -164,7 +158,7 @@ void Canvas::slotCenter()
void Canvas::slotPenWidth(double width)
{
int w = qMax(ROUND2INT(width), 0);
int w = qMax(static_cast<int>(round(width)), 0);
if (w == 0) {
penWidthIsZero = true;
return;
......
......@@ -22,10 +22,6 @@
#include <cmath>
using std::atan;
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288419717
#endif
#include <QApplication>
#include <QBoxLayout>
#include <QClipboard>
......@@ -39,12 +35,12 @@ using std::atan;
#include <QPaintEvent>
#include <QPushButton>
#include <QSpinBox>
#include <QtMath>
#include <KGuiItem>
#include <KLocalizedString>
#include <KStandardGuiItem>
#define ROUND2INT(x) ( (x) >= 0 ? (int)( (x) + .5 ) : (int)( (x) - .5 ) )
//BEGIN DirectionCanvas widget
......@@ -376,7 +372,7 @@ void DirectionDialog::updateDegrees(double deg)
{
// The canvas has changed, update the spinbox and command-LineEdit
skipValueChangedEvent = true;
directionSpin->setValue(ROUND2INT(deg));
directionSpin->setValue(static_cast<int>(round(deg)));
updateCommandBox();
}
......@@ -384,7 +380,7 @@ void DirectionDialog::updatePreviousDegrees(double deg)
{
// The canvas has changed, update the spinbox and commandBox
skipValueChangedEvent = true;
previousDirectionSpin->setValue(ROUND2INT(deg));
previousDirectionSpin->setValue(static_cast<int>(round(deg)));
updateCommandBox();
}
......
......@@ -31,14 +31,9 @@
#include <krandom.h>
#include <KLocalizedString>
#include <QtMath>
// this function is used in executer and canvas:
#define ROUND2INT(x) ( (x) >= 0 ? (int)( (x) + .5 ) : (int)( (x) - .5 ) )
#define DEG2RAD(deg) ( deg * (M_PI / 180) )
#define RAD2DEG(rad) ( rad * (180 / M_PI) )
void Executer::initialize(TreeNode* tree, ErrorList* _errorList)
{
rootNode = tree;
......@@ -490,7 +485,7 @@ void Executer::executeRepeat(TreeNode* node) {
// the iteration state is stored on the variable table
if (currentVariableTable()->contains(id)) {
int currentCount = ROUND2INT((*currentVariableTable())[id].number());
int currentCount = static_cast<int>(round((*currentVariableTable())[id].number()));
if (currentCount > 0) {
(*currentVariableTable())[id].setNumber(currentCount - 1);
} else {
......@@ -498,10 +493,10 @@ void Executer::executeRepeat(TreeNode* node) {
return;
}
} else {
if(ROUND2INT(node->child(0)->value()->number())<=0) // handle 'repeat 0'
if(static_cast<int>(round(node->child(0)->value()->number()))<=0) // handle 'repeat 0'
return;
currentVariableTable()->insert(id, Value((double)(ROUND2INT(node->child(0)->value()->number()) - 1)));
currentVariableTable()->insert(id, Value(round(node->child(0)->value()->number()) - 1.0));
}
newScope = node->child(1);
}
......@@ -1013,42 +1008,42 @@ void Executer::executeTan(TreeNode* node) {
if (!checkParameterQuantity(node, 1, 20000+Token::Tan*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(tan(DEG2RAD(deg)));
node->value()->setNumber(tan(qDegreesToRadians(deg)));
}
void Executer::executeSin(TreeNode* node) {
// //qDebug() << "called";
if (!checkParameterQuantity(node, 1, 20000+Token::Sin*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(sin(DEG2RAD(deg)));
node->value()->setNumber(sin(qDegreesToRadians(deg)));
}
void Executer::executeCos(TreeNode* node) {
// //qDebug() << "called";
if (!checkParameterQuantity(node, 1, 20000+Token::Cos*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(cos(DEG2RAD(deg)));
node->value()->setNumber(cos(qDegreesToRadians(deg)));
}
void Executer::executeArcTan(TreeNode* node) {
// //qDebug() << "called";
if (!checkParameterQuantity(node, 1, 20000+Token::ArcTan*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(RAD2DEG(atan(deg)));
node->value()->setNumber(qRadiansToDegrees(atan(deg)));
}
void Executer::executeArcSin(TreeNode* node) {
// //qDebug() << "called";
if (!checkParameterQuantity(node, 1, 20000+Token::ArcSin*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(RAD2DEG(asin(deg)));
node->value()->setNumber(qRadiansToDegrees(asin(deg)));
}
void Executer::executeArcCos(TreeNode* node) {
// //qDebug() << "called";
if (!checkParameterQuantity(node, 1, 20000+Token::ArcCos*100+90)) return;
double deg = node->child(0)->value()->number();
node->value()->setNumber(RAD2DEG(acos(deg)));
node->value()->setNumber(qRadiansToDegrees(acos(deg)));
}
void Executer::executeSqrt(TreeNode* node) {
// //qDebug() << "called";
......@@ -1067,7 +1062,7 @@ void Executer::executeRound(TreeNode* node) {
if (!checkParameterQuantity(node, 1, 20000+Token::Round*100+90)) return;
double val = node->child(0)->value()->number();
node->value()->setNumber((double)ROUND2INT(val));
node->value()->setNumber(round(val));
}
void Executer::executeGetDirection(TreeNode* node) {
// //qDebug() << "called";
......@@ -1085,7 +1080,7 @@ void Executer::executeMod(TreeNode* node) {
if (!checkParameterType(node, Value::Number, 20000+Token::Mod*100+91)) return;
double x = nodeX->value()->number();
double y = nodeY->value()->number();
double m = (double)(ROUND2INT(x) % ROUND2INT(y));
double m = (double)(static_cast<int>(round(x)) % static_cast<int>(round(y)));
node->value()->setNumber(m);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment