diff --git a/include/wx/math.h b/include/wx/math.h index f8f23ed40e..407c8e6936 100644 --- a/include/wx/math.h +++ b/include/wx/math.h @@ -133,6 +133,10 @@ inline int wxRound(double x) #endif } +// Convert between degrees and radians. +inline double wxDegToRad(double deg) { return (deg * M_PI) / 180.0; } +inline double wxRadToDeg(double rad) { return (rad * 180.0) / M_PI; } + #endif /* __cplusplus */ diff --git a/interface/wx/math.h b/interface/wx/math.h index 1431f452ea..42114483d2 100644 --- a/interface/wx/math.h +++ b/interface/wx/math.h @@ -42,6 +42,30 @@ wxFloat64 wxConvertFromIeeeExtended(const wxInt8 *bytes); */ void wxConvertToIeeeExtended(wxFloat64 num, wxInt8 *bytes); +/** + Convert degrees to radians. + + This function simply returns its argument multiplied by @c M_PI/180 but is + more readable than writing this expression directly. + + @see wxRadToDeg() + + @since 3.1.0 + */ +double wxDegToRad(double deg); + +/** + Convert radians to degrees. + + This function simply returns its argument multiplied by @c 180/M_PI but is + more readable than writing this expression directly. + + @see wxDegToRad() + + @since 3.1.0 + */ +double wxRadToDeg(double rad); + /** Small wrapper around round(). */ diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index efc3ee6331..b37d04f8c2 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -980,8 +980,6 @@ const int BASE = 80.0; const int BASE2 = BASE/2; const int BASE4 = BASE/4; -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } - // modeled along Robin Dunn's GraphicsContext.py sample @@ -1075,11 +1073,11 @@ void MyCanvas::DrawGraphics(wxGraphicsContext* gc) gc->SetPen(wxPen(wxColour(val.red, val.green, val.blue, 128))); // use translate to artfully reposition each drawn path - gc->Translate(1.5 * BASE2 * cos(DegToRad(angle)), - 1.5 * BASE2 * sin(DegToRad(angle))); + gc->Translate(1.5 * BASE2 * cos(wxDegToRad(angle)), + 1.5 * BASE2 * sin(wxDegToRad(angle))); // use Rotate to rotate the path - gc->Rotate(DegToRad(angle)); + gc->Rotate(wxDegToRad(angle)); // now draw it gc->DrawPath(path); diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index f28eab7b77..2d75114ed5 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -24,6 +24,7 @@ #include "wx/icon.h" #include "wx/dcclient.h" #include "wx/dcmemory.h" + #include "wx/math.h" #endif //----------------------------------------------------------------------------- @@ -36,11 +37,6 @@ static const double RAD2DEG = 180.0 / M_PI; // Local functions //----------------------------------------------------------------------------- -static inline double DegToRad(double deg) -{ - return (deg * M_PI) / 180.0; -} - static wxCompositionMode TranslateRasterOp(wxRasterOperationMode function) { switch ( function ) @@ -613,7 +609,7 @@ void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1, path.MoveToPoint( xc, yc ); // since these angles (ea,sa) are measured counter-clockwise, we invert them to // get clockwise angles - path.AddArc( xc, yc , rad , DegToRad(-sa) , DegToRad(-ea), false ); + path.AddArc( xc, yc , rad, wxDegToRad(-sa), wxDegToRad(-ea), false ); if ( fill && ((x1!=x2)||(y1!=y2)) ) path.AddLineToPoint( xc, yc ); m_graphicContext->DrawPath(path); @@ -638,18 +634,18 @@ void wxGCDCImpl::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h, { wxGraphicsPath path = m_graphicContext->CreatePath(); path.MoveToPoint( 0, 0 ); - path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); + path.AddArc( 0, 0, h/2.0, wxDegToRad(-sa), wxDegToRad(-ea), sa > ea ); path.AddLineToPoint( 0, 0 ); m_graphicContext->FillPath( path ); path = m_graphicContext->CreatePath(); - path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); + path.AddArc( 0, 0, h/2.0, wxDegToRad(-sa), wxDegToRad(-ea), sa > ea ); m_graphicContext->StrokePath( path ); } else { wxGraphicsPath path = m_graphicContext->CreatePath(); - path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); + path.AddArc( 0, 0, h/2.0, wxDegToRad(-sa), wxDegToRad(-ea), sa > ea ); m_graphicContext->DrawPath( path ); } @@ -993,7 +989,7 @@ void wxGCDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, GetOwner()->GetMultiLineTextExtent(text, &w, &h, &heightLine); // Compute the shift for the origin of the next line. - const double rad = DegToRad(angle); + const double rad = wxDegToRad(angle); const double dx = heightLine * sin(rad); const double dy = heightLine * cos(rad); @@ -1004,9 +1000,9 @@ void wxGCDCImpl::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, // Calculate origin for each line to avoid accumulation of // rounding errors. if ( m_backgroundMode == wxTRANSPARENT ) - m_graphicContext->DrawText( lines[lineNum], x + wxRound(lineNum*dx), y + wxRound(lineNum*dy), DegToRad(angle )); + m_graphicContext->DrawText( lines[lineNum], x + wxRound(lineNum*dx), y + wxRound(lineNum*dy), wxDegToRad(angle )); else - m_graphicContext->DrawText( lines[lineNum], x + wxRound(lineNum*dx), y + wxRound(lineNum*dy), DegToRad(angle ), m_graphicContext->CreateBrush(m_textBackgroundColour) ); + m_graphicContext->DrawText( lines[lineNum], x + wxRound(lineNum*dx), y + wxRound(lineNum*dy), wxDegToRad(angle ), m_graphicContext->CreateBrush(m_textBackgroundColour) ); } // call the bounding box by adding all four vertices of the rectangle diff --git a/src/common/dcsvg.cpp b/src/common/dcsvg.cpp index d8f864c3af..2c5075669d 100644 --- a/src/common/dcsvg.cpp +++ b/src/common/dcsvg.cpp @@ -21,6 +21,7 @@ #include "wx/dcscreen.h" #include "wx/icon.h" #include "wx/image.h" + #include "wx/math.h" #endif #include "wx/base64.h" @@ -38,8 +39,6 @@ namespace { -inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } - // This function returns a string representation of a floating point number in // C locale (i.e. always using "." for the decimal separator) and with the // fixed precision (which is 2 for some unknown reason but this is what it was @@ -352,7 +351,7 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor wxCoord w, h, desc; DoGetTextExtent(sText, &w, &h, &desc); - double rad = DegToRad(angle); + double rad = wxDegToRad(angle); // wxT("upper left") and wxT("upper right") CalcBoundingBox(x, y); @@ -548,10 +547,10 @@ void wxSVGFileDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h, double yc = y + ry; double xs, ys, xe, ye; - xs = xc + rx * cos (DegToRad(sa)); - xe = xc + rx * cos (DegToRad(ea)); - ys = yc - ry * sin (DegToRad(sa)); - ye = yc - ry * sin (DegToRad(ea)); + xs = xc + rx * cos (wxDegToRad(sa)); + xe = xc + rx * cos (wxDegToRad(ea)); + ys = yc - ry * sin (wxDegToRad(sa)); + ye = yc - ry * sin (wxDegToRad(ea)); ///now same as circle arc... diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 8d314fb2fb..c6d431f0a1 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -23,21 +23,13 @@ #include "wx/icon.h" #include "wx/bitmap.h" #include "wx/dcmemory.h" + #include "wx/math.h" #include "wx/region.h" #include "wx/log.h" #endif #include "wx/private/graphics.h" -//----------------------------------------------------------------------------- -// Local functions -//----------------------------------------------------------------------------- - -static inline double DegToRad(double deg) -{ - return (deg * M_PI) / 180.0; -} - //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -470,7 +462,7 @@ void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, alpha = 360 + alpha; // TODO obtuse angles - alpha = DegToRad(alpha); + alpha = wxDegToRad(alpha); wxDouble dist = r / sin(alpha/2) * cos(alpha/2); // calculate tangential points @@ -484,7 +476,7 @@ void wxGraphicsPathData::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble a2 = v2.GetVectorAngle()-90; AddLineToPoint(t1.m_x,t1.m_y); - AddArc(c.m_x,c.m_y,r,DegToRad(a1),DegToRad(a2),true); + AddArc(c.m_x,c.m_y,r,wxDegToRad(a1),wxDegToRad(a2),true); AddLineToPoint(p2.m_x,p2.m_y); } diff --git a/src/generic/dcpsg.cpp b/src/generic/dcpsg.cpp index 143d22de75..1c3de08114 100644 --- a/src/generic/dcpsg.cpp +++ b/src/generic/dcpsg.cpp @@ -247,9 +247,6 @@ wxPostScriptDC::wxPostScriptDC(const wxPrintData& printData) { } -// conversion -static const double RAD2DEG = 180.0 / M_PI; - // we don't want to use only 72 dpi from PS print static const int DPI = 600; static const double PS2DEV = 600.0 / 72.0; @@ -468,10 +465,10 @@ void wxPostScriptDCImpl::DoDrawArc (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord { alpha1 = (x1 - xc == 0) ? (y1 - yc < 0) ? 90.0 : -90.0 : - -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG; + wxRadToDeg(-atan2(double(y1-yc), double(x1-xc))); alpha2 = (x2 - xc == 0) ? (y2 - yc < 0) ? 90.0 : -90.0 : - -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG; + wxRadToDeg(-atan2(double(y2-yc), double(x2-xc))); } while (alpha1 <= 0) alpha1 += 360; while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between diff --git a/src/gtk1/dcclient.cpp b/src/gtk1/dcclient.cpp index 4c5ead883f..f76205feab 100644 --- a/src/gtk1/dcclient.cpp +++ b/src/gtk1/dcclient.cpp @@ -71,8 +71,6 @@ const double RAD2DEG = 180.0 / M_PI; static inline double dmax(double a, double b) { return a > b ? a : b; } static inline double dmin(double a, double b) { return a < b ? a : b; } -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } - //----------------------------------------------------------------------------- // temporary implementation of the missing GDK function //----------------------------------------------------------------------------- @@ -1477,7 +1475,7 @@ void wxWindowDCImpl::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord dc.SelectObject(wxNullBitmap); // Calculate the size of the rotated bounding box. - double rad = DegToRad(angle); + double rad = wxDegToRad(angle); double dx = cos(rad), dy = sin(rad); diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 19361f34c6..c9be187664 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -118,9 +118,6 @@ static const int VIEWPORT_EXTENT = 134217727; // private functions // --------------------------------------------------------------------------- -// convert degrees to radians -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } - // call AlphaBlend() to blit contents of hdcSrc to dcDst using alpha // // NB: bmpSrc is the bitmap selected in hdcSrc, it is not really needed @@ -1224,8 +1221,8 @@ void wxMSWDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,doub int rx2 = rx1; int ry2 = ry1; - sa = DegToRad(sa); - ea = DegToRad(ea); + sa = wxDegToRad(sa); + ea = wxDegToRad(ea); rx1 += (int)(100.0 * abs(w) * cos(sa)); ry1 -= (int)(100.0 * abs(h) * m_signY * sin(sa)); @@ -1527,7 +1524,7 @@ void wxMSWDCImpl::DoDrawRotatedText(const wxString& text, wxBkModeChanger bkMode(GetHdc(), m_backgroundMode); // Compute the shift for the origin of the next line. - const double rad = DegToRad(angle); + const double rad = wxDegToRad(angle); const double dx = heightLine * sin(rad); const double dy = heightLine * cos(rad); diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index b13ff2ac2b..35237953b8 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -28,6 +28,7 @@ #include "wx/bitmap.h" #include "wx/log.h" #include "wx/icon.h" + #include "wx/math.h" #include "wx/module.h" // include all dc types that are used as a param #include "wx/dc.h" @@ -55,12 +56,6 @@ namespace { -//----------------------------------------------------------------------------- -// constants -//----------------------------------------------------------------------------- - -const double RAD2DEG = 180.0 / M_PI; - //----------------------------------------------------------------------------- // Local functions //----------------------------------------------------------------------------- @@ -68,9 +63,6 @@ const double RAD2DEG = 180.0 / M_PI; inline double dmin(double a, double b) { return a < b ? a : b; } inline double dmax(double a, double b) { return a > b ? a : b; } -inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } -inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; } - // translate a wxColour to a Color inline Color wxColourToColor(const wxColour& col) { @@ -1195,7 +1187,7 @@ void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double start } } - m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle)); + m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),wxRadToDeg(startAngle),wxRadToDeg(sweepAngle)); } void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) @@ -1319,7 +1311,7 @@ void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale ) // add the rotation to this matrix (radians) void wxGDIPlusMatrixData::Rotate( wxDouble angle ) { - m_matrix->Rotate( RadToDeg(angle) ); + m_matrix->Rotate( wxRadToDeg(angle) ); } // @@ -1664,7 +1656,7 @@ void wxGDIPlusContext::EndLayer() void wxGDIPlusContext::Rotate( wxDouble angle ) { - m_context->RotateTransform( RadToDeg(angle) ); + m_context->RotateTransform( wxRadToDeg(angle) ); } void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy ) diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index d9e6d3915c..0ad8ac0678 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -18,6 +18,7 @@ #include "wx/intl.h" #include "wx/gdicmn.h" #include "wx/log.h" + #include "wx/math.h" #endif #include "wx/fontutil.h" @@ -442,8 +443,7 @@ void wxFontRefData::CreateATSUFont() } #endif -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } -static const CGAffineTransform kSlantTransform = CGAffineTransformMake( 1, 0, tan(DegToRad(11)), 1, 0, 0 ); +static const CGAffineTransform kSlantTransform = CGAffineTransformMake( 1, 0, tan(wxDegToRad(11)), 1, 0, 0 ); void wxFontRefData::MacFindFont() { diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 85944769c6..5f84a90e25 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -12,6 +12,7 @@ #ifndef WX_PRECOMP #include "wx/object.h" +#include "wx/math.h" #endif #if wxOSX_USE_COCOA_OR_CARBON @@ -191,8 +192,7 @@ WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info) return nsfont; } -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } -static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, static_cast(tan(DegToRad(11))), 1, 0, 0 }; +static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, static_cast(tan(wxDegToRad(11))), 1, 0, 0 }; WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info) { diff --git a/src/x11/dcclient.cpp b/src/x11/dcclient.cpp index 3883422711..9de4aa580f 100644 --- a/src/x11/dcclient.cpp +++ b/src/x11/dcclient.cpp @@ -73,8 +73,6 @@ const double RAD2DEG = 180.0 / M_PI; static inline double dmax(double a, double b) { return a > b ? a : b; } static inline double dmin(double a, double b) { return a < b ? a : b; } -static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } - //----------------------------------------------------------------------------- // Implement Pool of Graphic contexts. Creating them takes too much time. //-----------------------------------------------------------------------------