2 点間に円弧を描きたい。2点の位置とラジアンでの角度を知っています。円弧を効果的に描く前に、円の中心を計算する小さなプログラムを書くことに成功しました。しかし、検証するために円を描くとき、ラジアンに小さな値を使用すると、円の線は指定された 2 つの点と交差しません。
#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);
}
ただし、2 点から円の中心までの距離は、計算された半径に等しくなります。どこが間違っていますか?