根据圆弧的起点、终点、圆心,绘制圆弧
以下是算法:
class myArc : public myShape //定义一个圆弧类,里面定义四个字段,圆弧的起点终点圆心,半径
{
public:
QPoint start;
QPoint end;
QPoint center;
int r;
myArc(const QPoint& start, const QPoint& end, const QPoint& center)
{
this->start = start;
this->end = end;
this->center = center;
}
void drawShape(QPen *pen, QPainter *g)
{
g->setPen(*pen);
QLineF ls, le;
qreal start_angle, span_angle;
ls = QLineF(center, start);
le = QLineF(center, end);
start_angle = ls.angle();
span_angle = le.angle();
double x = start.x() - center.x();
double y = start.y() - center.y();
r = (int)sqrt(x*x + y * y);
QPointF top_left = QPointF(center.x() - r, center.y() - r);
QPointF bot_right = QPointF(center.x() + r, center.y() + r);
QRectF rect = QRectF(top_left, bot_right);
g->drawArc(rect, start_angle *16 , (span_angle-start_angle) *16 );
}
};
要特别注意的是,drawArc函数有三个参数rect,start_angle,span_angle,一开始我以为后面两个参数是圆弧的起点和终点,调试的时候一直有问题。后来才发现是圆弧的起点和圆弧所在扇形的圆心角,用span_angle减去start_angle就没有问题啦。