我想在两点之间画一条弧线。我知道两点的位置和弧度的角度。我成功地编写了一个小程序来计算圆心,然后才能有效地绘制圆弧。但是当我画一个圆来验证时,当我使用小的弧度值时,圆线不会穿过给定的两个点。
#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);
}
但是,从两点到圆心的距离等于计算的半径。我哪里错了?