Optimize calculations of the angles in DrawArc()

Perform calculations in radians to avoid conversions to/from degrees.
This commit is contained in:
Artur Wieczorek
2020-10-04 21:43:56 +02:00
parent c91ced2412
commit 9e5f333bcc

View File

@@ -27,12 +27,6 @@
#include "wx/geometry.h" #include "wx/geometry.h"
#endif #endif
//-----------------------------------------------------------------------------
// constants
//-----------------------------------------------------------------------------
static const double RAD2DEG = 180.0 / M_PI;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Local functions // Local functions
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -686,11 +680,11 @@ void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1,
double dy = y1 - yc; double dy = y1 - yc;
double radius = sqrt((double)(dx * dx + dy * dy)); double radius = sqrt((double)(dx * dx + dy * dy));
wxCoord rad = (wxCoord)radius; wxCoord rad = (wxCoord)radius;
double sa, ea; double sa, ea; // In radians
if (x1 == x2 && y1 == y2) if (x1 == x2 && y1 == y2)
{ {
sa = 0.0; sa = 0.0;
ea = 360.0; ea = 2.0 * M_PI;
} }
else if (radius == 0.0) else if (radius == 0.0)
{ {
@@ -699,11 +693,11 @@ void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1,
else else
{ {
sa = (x1 - xc == 0) ? sa = (x1 - xc == 0) ?
(y1 - yc < 0) ? 90.0 : -90.0 : (y1 - yc < 0) ? M_PI / 2.0 : -M_PI / 2.0 :
-atan2(double(y1 - yc), double(x1 - xc)) * RAD2DEG; -atan2(double(y1 - yc), double(x1 - xc));
ea = (x2 - xc == 0) ? ea = (x2 - xc == 0) ?
(y2 - yc < 0) ? 90.0 : -90.0 : (y2 - yc < 0) ? M_PI / 2.0 : -M_PI / 2.0 :
-atan2(double(y2 - yc), double(x2 - xc)) * RAD2DEG; -atan2(double(y2 - yc), double(x2 - xc));
} }
bool fill = m_brush.GetStyle() != wxBRUSHSTYLE_TRANSPARENT; bool fill = m_brush.GetStyle() != wxBRUSHSTYLE_TRANSPARENT;
@@ -713,7 +707,7 @@ void wxGCDCImpl::DoDrawArc( wxCoord x1, wxCoord y1,
path.MoveToPoint( xc, yc ); path.MoveToPoint( xc, yc );
// since these angles (ea,sa) are measured counter-clockwise, we invert them to // since these angles (ea,sa) are measured counter-clockwise, we invert them to
// get clockwise angles // get clockwise angles
path.AddArc( xc, yc , rad, wxDegToRad(-sa), wxDegToRad(-ea), false ); path.AddArc( xc, yc , rad, -sa, -ea, false );
if ( fill && ((x1!=x2)||(y1!=y2)) ) if ( fill && ((x1!=x2)||(y1!=y2)) )
path.AddLineToPoint( xc, yc ); path.AddLineToPoint( xc, yc );
m_graphicContext->DrawPath(path); m_graphicContext->DrawPath(path);