Add public wxDegToRad() and wxRadToDeg() functions.

Define these functions just once in wx/math.h instead of duplicating them in a
dozen of places.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76555 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-05-17 12:29:15 +00:00
parent 4d8ff19a1a
commit a380c1e46f
13 changed files with 62 additions and 67 deletions

View File

@@ -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 */

View File

@@ -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().
*/

View File

@@ -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);

View File

@@ -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

View File

@@ -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...

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 )

View File

@@ -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()
{

View File

@@ -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<CGFloat>(tan(DegToRad(11))), 1, 0, 0 };
static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, static_cast<CGFloat>(tan(wxDegToRad(11))), 1, 0, 0 };
WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
{

View File

@@ -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.
//-----------------------------------------------------------------------------