10#include "qwt_bezier.h"
26 inline BezierData(
const QPointF& p1,
const QPointF& cp1,
27 const QPointF& cp2,
const QPointF& p2 ):
39 static inline double minFlatness(
double tolerance )
45 return 16 * ( tolerance * tolerance );
48 inline double flatness()
const
52 const double ux = 3.0 * m_cx1 - 2.0 * m_x1 - m_x2;
53 const double uy = 3.0 * m_cy1 - 2.0 * m_y1 - m_y2;
54 const double vx = 3.0 * m_cx2 - 2.0 * m_x2 - m_x1;
55 const double vy = 3.0 * m_cy2 - 2.0 * m_y2 - m_y1;
57 const double ux2 = ux * ux;
58 const double uy2 = uy * uy;
60 const double vx2 = vx * vx;
61 const double vy2 = vy * vy;
63 return qwtMaxF( ux2, vx2 ) + qwtMaxF( uy2, vy2 );
66 inline BezierData subdivided()
70 const double c1 = midValue( m_cx1, m_cx2 );
72 bz.m_cx1 = midValue( m_x1, m_cx1 );
73 m_cx2 = midValue( m_cx2, m_x2 );
75 bz.m_cx2 = midValue( bz.m_cx1, c1 );
76 m_cx1 = midValue( c1, m_cx2 );
77 bz.m_x2 = m_x1 = midValue( bz.m_cx2, m_cx1 );
79 const double c2 = midValue( m_cy1, m_cy2 );
81 bz.m_cy1 = midValue( m_y1, m_cy1 );
82 m_cy2 = midValue( m_cy2, m_y2 );
84 bz.m_cy2 = midValue( bz.m_cy1, c2 );
85 m_cy1 = midValue( m_cy2, c2 );
86 bz.m_y2 = m_y1 = midValue( bz.m_cy2, m_cy1 );
91 inline QPointF p2()
const
93 return QPointF( m_x2, m_y2 );
97 inline double midValue(
double v1,
double v2 )
99 return 0.5 * ( v1 + v2 );
117 : m_tolerance( qwtMaxF( tolerance, 0.0 ) )
118 , m_flatness( BezierData::minFlatness( m_tolerance ) )
144 m_flatness = BezierData::minFlatness( m_tolerance );
158 const QPointF& cp1,
const QPointF& cp2,
const QPointF& p2 )
const
162 if ( m_flatness > 0.0 )
187 const QPointF& cp2,
const QPointF& p2, QPolygonF& polygon )
const
189 if ( m_flatness <= 0.0 )
195 if ( polygon.isEmpty() || polygon.last() != p1 )
203 stack.push( BezierData( p1, cp1, cp2, p2 ) );
207 BezierData& bz = stack.top();
209 if ( bz.flatness() < m_flatness )
211 if ( stack.size() == 1 )
222 stack.push( bz.subdivided() );
240 const QPointF& cp1,
const QPointF& cp2,
const QPointF& p2,
double t )
242 const double d1 = 3.0 * t;
243 const double d2 = 3.0 * t * t;
244 const double d3 = t * t * t;
245 const double s = 1.0 - t;
247 const double x = ( ( s * p1.x() + d1 * cp1.x() ) * s + d2 * cp2.x() ) * s + d3 * p2.x();
248 const double y = ( ( s * p1.y() + d1 * cp1.y() ) * s + d2 * cp2.y() ) * s + d3 * p2.y();
250 return QPointF( x, y );
QPolygonF toPolygon(const QPointF &p1, const QPointF &cp1, const QPointF &cp2, const QPointF &p2) const
Interpolate a Bézier curve by a polygon.
void appendToPolygon(const QPointF &p1, const QPointF &cp1, const QPointF &cp2, const QPointF &p2, QPolygonF &polygon) const
Interpolate a Bézier curve by a polygon.
void setTolerance(double tolerance)
QwtBezier(double tolerance=0.5)
Constructor.
static QPointF pointAt(const QPointF &p1, const QPointF &cp1, const QPointF &cp2, const QPointF &p2, double t)