简体   繁体   English

qt使用弧度角在两点之间绘制弧线

[英]qt draw an arc between two points using angle in radians

I want to draw an arc between two points.我想在两点之间画一条弧线。 I know position of the two points and the angle in radians.我知道两点的位置和弧度的角度。 I succeeded to wrote a little program to calculate the centre of the circle before to effective draw the arc.我成功地写了一个小程序来计算圆心,然后才能有效地绘制圆弧。 But when I draw a circle to verify, when I use small values for radians, the circle line do not cross the two points given.但是当我画一个圆来验证时,当我对弧度使用较小的值时,圆线不会与给定的两个点相交。

#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>
#include <cmath>
#include <QPainter>

void cross(QPainterPath* path, double x, double y);

int main( int argc, char **argv )
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setSceneRect( 0.0, 0.0, 500.0, 500.0 );
    QPainterPath* path = new QPainterPath();

    double x1, x2, y1, y2, l, rad, r;
    double x3, y3, xx, yy;
    const double PI = 3.14159265358979323846;

    //first point
    x1=250;
    y1=250;
    //second point
    x2=350;
    y2=300;
    //radians - play with it. This is low value - this is buggy
    rad=0.002;

    l=sqrt (pow((x1-x2),2) + pow((y1-y2),2)); //distance between (x1,y) and (x2,y2)
    u=180.0 * rad / PI; //transform radians in angle
    r=(l/2.0)/sin(rad/2.0); //this is radius

    //point in the middle of (x1,y) and (x2,y2)... half of l
    x3 = (x1+x2)/2;
    y3 = (y1+y2)/2;

    //find center of circle
    if(rad>0){
        xx = x3 + sqrt(pow(r,2)-pow((l/2),2))*(y1-y2)/l;
        yy = y3 + sqrt(pow(r,2)-pow((l/2),2))*(x2-x1)/l;
    }else{
        xx = x3 - sqrt(pow(r,2)-pow((l/2),2))*(y1-y2)/l;
        yy = y3 - sqrt(pow(r,2)-pow((l/2),2))*(x2-x1)/l;
    }


    //draw circle to verify
    path->moveTo(xx, yy);
    path->addEllipse(QRectF(xx-r,yy-r,r*2,r*2));

    cross(path, x3,y3);
    cross(path, xx,yy);
    cross(path, x1,y1);
    cross(path, x2,y2);


    qDebug() << "r =" << r << " xx =" << xx << " yy =" << yy ;
    qDebug() << "Verify r - distance from (x1,y1) to center of circle" << sqrt (pow((x1-xx),2) + pow((y1-yy),2));
    qDebug() << "Verify r - distance from (x2,y2) to center of circle" << sqrt (pow((x2-xx),2) + pow((y2-yy),2));

    scene.addPath(*path);

    QGraphicsView view( &scene );
    view.show();
    return app.exec();
}

void cross(QPainterPath* path, double x, double y){
    path->moveTo(x, y-5);
    path->lineTo(x, y+5);
    path->moveTo(x-5, y);
    path->lineTo(x+5, y);
}

弧度 = 1.0 弧度 = 0.002

However, the distance from the two points to the circle center is equal to the calculated radius.但是,两点到圆心的距离等于计算出的半径。 where am I wrong?我错在哪里?

I know this post is old, but since it's been viewed 1K times and not been answered I thought I'd weigh in.我知道这篇文章很旧,但由于它已经被浏览了 1000 次并且没有得到回复,我想我会权衡一下。

I believe the draw ellipse function in QT is using 4 bezier curves to create the shape.我相信 QT 中的绘制椭圆函数使用 4 条贝塞尔曲线来创建形状。 There is no perfect way to represent a circle using bezier curves, instead you need to aim for the closest approximation you can get.没有完美的方法可以使用贝塞尔曲线来表示圆,相反,您需要瞄准可以获得的最接近的近似值。

For my purposes, I found this bezier curve circle approximation Stack Overflow post incredibly helpful.出于我的目的,我发现这个贝塞尔曲线圆近似堆栈溢出帖子非常有用。 The solution I settled on was to approximate the circle with a high quantity of arcs starting at the point itself to guarantee that my circle appeared to go through it.我决定的解决方案是用大量的弧线来近似圆,从点本身开始,以保​​证我的圆似乎穿过它。

Another option is to write your own bresenham's circle drawing function using the QT painter "drawPoint" function to put the individual pixels.另一种选择是使用 QT 画家“drawPoint”函数编写自己bresenham 圆绘制函数来放置单个像素。 I found that this solution was too slow at increasing zoom levels in my QT app, however, it was incredibly accurate.我发现这个解决方案在我的 QT 应用程序中增加缩放级别时速度太慢,但是,它非常准确。

Hopefully this helps someone next time this crops up.希望这对下次出现这种情况的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM