X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/14f355c2b5c71fc7c3d680aea366582d2ac60f7b..b404a8f3b072129c107c6d9a5e0f6f53cd34807b:/src/common/matrix.cpp diff --git a/src/common/matrix.cpp b/src/common/matrix.cpp index d0d16755a3..f1f8c319f9 100644 --- a/src/common/matrix.cpp +++ b/src/common/matrix.cpp @@ -1,4 +1,5 @@ -// Name: matrix.cpp +/////////////////////////////////////////////////////////////////////////////// +// Name: src/common/matrix.cpp // Purpose: wxTransformMatrix class // Author: Chris Breeze, Julian Smart // Modified by: Klaas Holwerda @@ -6,11 +7,7 @@ // RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence -///////////////////////////////////////////////////////////////////////////// - -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "matrix.h" -#endif +/////////////////////////////////////////////////////////////////////////////// // Note: this is intended to be used in wxDC at some point to replace // the current system of scaling/translation. It is not yet used. @@ -19,21 +16,20 @@ #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif +#include "wx/matrix.h" + #ifndef WX_PRECOMP -#include "wx/defs.h" + #include "wx/math.h" #endif -#include "wx/matrix.h" -#include - -static const double pi = 3.1415926535; +static const double pi = M_PI; wxTransformMatrix::wxTransformMatrix(void) { - m_isIdentity = FALSE; + m_isIdentity = false; Identity(); } @@ -74,24 +70,24 @@ void wxTransformMatrix::operator = (const wxTransformMatrix& mat) m_isIdentity = mat.m_isIdentity; } -bool wxTransformMatrix::operator == (const wxTransformMatrix& mat) +bool wxTransformMatrix::operator == (const wxTransformMatrix& mat) const { - if (m_isIdentity==TRUE && mat.m_isIdentity==TRUE) - return TRUE; + if (m_isIdentity && mat.m_isIdentity) + return true; int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { - if (m_matrix[i][j] != mat.m_matrix[i][j]) - return FALSE; + if ( !wxIsSameDouble(m_matrix[i][j], mat.m_matrix[i][j]) ) + return false; } } - return TRUE; + return true; } -bool wxTransformMatrix::operator != (const wxTransformMatrix& mat) +bool wxTransformMatrix::operator != (const wxTransformMatrix& mat) const { return (! ((*this) == mat)); } @@ -132,27 +128,22 @@ bool wxTransformMatrix::Invert(void) // now divide by the determinant double det = m_matrix[0][0] * inverseMatrix[0][0] + m_matrix[0][1] * inverseMatrix[1][0] + m_matrix[0][2] * inverseMatrix[2][0]; - if (det != 0.0) - { - inverseMatrix[0][0] /= det; inverseMatrix[1][0] /= det; inverseMatrix[2][0] /= det; - inverseMatrix[0][1] /= det; inverseMatrix[1][1] /= det; inverseMatrix[2][1] /= det; - inverseMatrix[0][2] /= det; inverseMatrix[1][2] /= det; inverseMatrix[2][2] /= det; + if ( wxIsNullDouble(det) ) + return false; + + inverseMatrix[0][0] /= det; inverseMatrix[1][0] /= det; inverseMatrix[2][0] /= det; + inverseMatrix[0][1] /= det; inverseMatrix[1][1] /= det; inverseMatrix[2][1] /= det; + inverseMatrix[0][2] /= det; inverseMatrix[1][2] /= det; inverseMatrix[2][2] /= det; - int i, j; - for (i = 0; i < 3; i++) + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) { - for (j = 0; j < 3; j++) - { - m_matrix[i][j] = inverseMatrix[i][j]; - } + m_matrix[i][j] = inverseMatrix[i][j]; } - m_isIdentity = IsIdentity1(); - return TRUE; - } - else - { - return FALSE; } + m_isIdentity = IsIdentity1(); + return true; } // Make into identity matrix @@ -160,9 +151,9 @@ bool wxTransformMatrix::Identity(void) { m_matrix[0][0] = m_matrix[1][1] = m_matrix[2][2] = 1.0; m_matrix[1][0] = m_matrix[2][0] = m_matrix[0][1] = m_matrix[2][1] = m_matrix[0][2] = m_matrix[1][2] = 0.0; - m_isIdentity = TRUE; + m_isIdentity = true; - return TRUE; + return true; } // Scale by scale (isotropic scaling i.e. the same in x and y): @@ -182,7 +173,7 @@ bool wxTransformMatrix::Scale(double scale) } m_isIdentity = IsIdentity1(); - return TRUE; + return true; } @@ -198,8 +189,8 @@ wxTransformMatrix& wxTransformMatrix::Scale(const double &xs, const double &ys, if (m_isIdentity) { - double tx =xc*(1-xs); - double ty =yc*(1-ys); + double tx = xc*(1-xs); + double ty = yc*(1-ys); r00 = xs; r10 = 0; r20 = tx; @@ -207,10 +198,10 @@ wxTransformMatrix& wxTransformMatrix::Scale(const double &xs, const double &ys, r11 = ys; r21 = ty; } - else if (xc!=0 || yc!=0) + else if ( !wxIsNullDouble(xc) || !wxIsNullDouble(yc) ) { - double tx =xc*(1-xs); - double ty =yc*(1-ys); + double tx = xc*(1-xs); + double ty = yc*(1-ys); r00 = xs * m_matrix[0][0]; r10 = xs * m_matrix[1][0]; r20 = xs * m_matrix[2][0] + tx; @@ -268,12 +259,12 @@ wxTransformMatrix& wxTransformMatrix::Mirror(bool x, bool y) if (x) { temp.m_matrix[1][1] = -1; - temp.m_isIdentity=FALSE; + temp.m_isIdentity=false; } if (y) { temp.m_matrix[0][0] = -1; - temp.m_isIdentity=FALSE; + temp.m_isIdentity=false; } *this = temp * (*this); @@ -296,7 +287,7 @@ bool wxTransformMatrix::Translate(double dx, double dy) m_isIdentity = IsIdentity1(); - return TRUE; + return true; } // Rotate clockwise by the given number of degrees: @@ -306,7 +297,7 @@ bool wxTransformMatrix::Translate(double dx, double dy) bool wxTransformMatrix::Rotate(double degrees) { Rotate(-degrees,0,0); - return TRUE; + return true; } // counter clockwise rotate around a point @@ -323,8 +314,8 @@ wxTransformMatrix& wxTransformMatrix::Rotate(const double °rees, const doubl if (m_isIdentity) { - double tx = x*(1-c)+y*s; - double ty = y*(1-c)-x*s; + double tx = x*(1-c)+y*s; + double ty = y*(1-c)-x*s; r00 = c ; r10 = -s; r20 = tx; @@ -332,10 +323,10 @@ wxTransformMatrix& wxTransformMatrix::Rotate(const double °rees, const doubl r11 = c; r21 = ty; } - else if (x!=0 || y!=0) + else if ( !wxIsNullDouble(x) || !wxIsNullDouble(y) ) { - double tx = x*(1-c)+y*s; - double ty = y*(1-c)-x*s; + double tx = x*(1-c)+y*s; + double ty = y*(1-c)-x*s; r00 = c * m_matrix[0][0] - s * m_matrix[0][1] + tx * m_matrix[0][2]; r10 = c * m_matrix[1][0] - s * m_matrix[1][1] + tx * m_matrix[1][2]; r20 = c * m_matrix[2][0] - s * m_matrix[2][1] + tx;// * m_matrix[2][2]; @@ -384,13 +375,13 @@ bool wxTransformMatrix::TransformPoint(double x, double y, double& tx, double& t { if (IsIdentity()) { - tx = x; ty = y; return TRUE; + tx = x; ty = y; return true; } tx = x * m_matrix[0][0] + y * m_matrix[1][0] + m_matrix[2][0]; ty = x * m_matrix[0][1] + y * m_matrix[1][1] + m_matrix[2][1]; - return TRUE; + return true; } // Transform a point from device to logical coordinates. @@ -407,18 +398,18 @@ bool wxTransformMatrix::InverseTransformPoint(double x, double y, double& tx, do { if (IsIdentity()) { - tx = x; ty = y; return TRUE; + tx = x; + ty = y; + return true; } - double z = (1.0 - m_matrix[0][2] * x - m_matrix[1][2] * y) / m_matrix[2][2]; - if (z == 0.0) - { -// z = 0.0000001; - return FALSE; - } + const double z = (1.0 - m_matrix[0][2] * x - m_matrix[1][2] * y) / m_matrix[2][2]; + if ( wxIsNullDouble(z) ) + return false; + tx = x * m_matrix[0][0] + y * m_matrix[1][0] + z * m_matrix[2][0]; ty = x * m_matrix[0][1] + y * m_matrix[1][1] + z * m_matrix[2][1]; - return TRUE; + return true; } wxTransformMatrix& wxTransformMatrix::operator*=(const double& t) @@ -559,7 +550,7 @@ double wxTransformMatrix::Get_scaleX() { double scale_factor; double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi); - if (rot_angle != 90 && rot_angle != -90) + if ( !wxIsSameDouble(rot_angle, 90) && !wxIsSameDouble(rot_angle, -90) ) scale_factor = m_matrix[0][0]/cos((rot_angle/180)*pi); else scale_factor = m_matrix[0][0]/sin((rot_angle/180)*pi); // er kan nl. niet door 0 gedeeld worden ! @@ -575,7 +566,7 @@ double wxTransformMatrix::Get_scaleY() { double scale_factor; double rot_angle = CheckInt(atan2(m_matrix[1][0],m_matrix[0][0])*180/pi); - if (rot_angle != 90 && rot_angle != -90) + if ( !wxIsSameDouble(rot_angle, 90) && !wxIsSameDouble(rot_angle, -90) ) scale_factor = m_matrix[1][1]/cos((rot_angle/180)*pi); else scale_factor = m_matrix[1][1]/sin((rot_angle/180)*pi); // er kan nl. niet door 0 gedeeld worden ! @@ -608,4 +599,3 @@ void wxTransformMatrix::SetRotation(double rotation) Rotate(-GetRotation(), x, y); Rotate(rotation, x, y); } -