C++ qt使用弧度角在两点之间绘制圆弧

C++ qt使用弧度角在两点之间绘制圆弧,c++,qt,drawing,trigonometry,qpainterpath,C++,Qt,Drawing,Trigonometry,Qpainterpath,我想在两点之间画一条弧。我知道两点的位置和以弧度表示的角度。 我成功地编写了一个小程序来计算圆心,然后才能有效地画出圆弧。但是当我画一个圆来验证时,当我使用较小的弧度值时,圆线不会穿过给定的两点 #include <QApplication> #include <QGraphicsEllipseItem> #include <QGraphicsScene> #include <QGraphicsView> #include <QDebug&g

我想在两点之间画一条弧。我知道两点的位置和以弧度表示的角度。 我成功地编写了一个小程序来计算圆心,然后才能有效地画出圆弧。但是当我画一个圆来验证时,当我使用较小的弧度值时,圆线不会穿过给定的两点

#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);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
虚线交叉(QPainterPath*路径,双x,双y);
int main(int argc,字符**argv)
{
QApplication应用程序(argc、argv);
QsCENE场景;
场景设置(0.0,0.0500.0500.0);
QPainterPath*路径=新的QPainterPath();
双x1,x2,y1,y2,l,rad,r;
双x3,y3,xx,yy;
常数双PI=3.14159265358979323846;
//第一点
x1=250;
y1=250;
//第二点
x2=350;
y2=300;
//弧度-玩它。这是低值-这是马车
rad=0.002;
l=sqrt(pow((x1-x2),2)+pow((y1-y2),2));//在(x1,y)和(x2,y2)之间的距离
u=180.0*rad/PI;//以角度变换弧度
r=(l/2.0)/sin(rad/2.0);//这是半径
//点在(x1,y)和(x2,y2)的中间…L的一半
x3=(x1+x2)/2;
y3=(y1+y2)/2;
//求圆心
如果(rad>0){
xx=x3+sqrt(功率(r,2)-pow((l/2),2))*(y1-y2)/l;
yy=y3+sqrt(pow(r,2)-pow((l/2),2))*(x2-x1)/l;
}否则{
xx=x3-sqrt(功率(r,2)-pow((l/2),2))*(y1-y2)/l;
yy=y3-sqrt(pow(r,2)-pow((l/2),2))*(x2-x1)/l;
}
//画圆验证
路径->移动到(xx,yy);
路径->加法器(QRectF(xx-r,yy-r,r*2,r*2));
交叉(路径,x3,y3);
交叉(路径,xx,yy);
交叉(路径x1,y1);
交叉(路径x2、y2);

qDebug()我知道这篇文章很老了,但是因为它已经被浏览了1K次,而且没有得到回复,我想我应该加入进来

我相信QT中的绘制椭圆函数是使用4条贝塞尔曲线来创建形状的。没有完美的方法可以使用贝塞尔曲线来表示圆,相反,您需要以能够获得的最接近的近似值为目标

出于我的目的,我发现这个堆栈溢出帖子非常有用。我确定的解决方案是从点本身开始,用大量的圆弧来近似圆,以保证我的圆看起来穿过它

另一个选择是使用QT painter“drawPoint”函数编写自己的函数来放置各个像素。我发现在我的QT应用程序中,这个解决方案在增加缩放级别时速度太慢,但是,它非常精确


我知道这篇文章已经很老了,但是因为它已经被浏览了1K次,而且没有得到回复,我想我应该加入进来

我相信QT中的绘制椭圆函数是使用4条贝塞尔曲线来创建形状的。没有完美的方法可以使用贝塞尔曲线来表示圆,相反,您需要以能够获得的最接近的近似值为目标

出于我的目的,我发现这个堆栈溢出帖子非常有用。我确定的解决方案是从点本身开始,用大量的圆弧来近似圆,以保证我的圆看起来穿过它

另一个选择是使用QT painter“drawPoint”函数编写自己的函数来放置各个像素。我发现在我的QT应用程序中,这个解决方案在增加缩放级别时速度太慢,但是,它非常精确


希望这能在下一次出现时帮助到某人。

只是一个旁白:计算两点之间笛卡尔距离的更方便的方法
(x1,y1)
(x2,y2)
是通过-在这种情况下
std::hypot(x2-x1,y2-y1)
。只是一个旁白:计算两点之间笛卡尔距离的更方便的方法
(x1,y1)
(x2,y2)
是通过-在这种情况下
std::hypot(x2-x1,y2-y1)