PDA

View Full Version : Angles in QPainterPath::arcTo



hector1984
16th March 2010, 01:51
I'm trying to draw the piece of an ellipse between two points on the ellipse. I plug in what I think makes sense for the start angle and span length in QPainterPath::arcTo, but the results are not what I expect. Here's what I mean:


#include <QtGui>
#include <cmath>
#include <algorithm>
#include <iostream>


using namespace std;


static const bool PATH_NOT_ELLIPSE = true;
static const double PI = 3.14159265359;
static const double A = 300.0;
static const double B = 200.0;
static const double XSTART = 200.0;
static const double YSTART = B/A*sqrt( A*A - XSTART*XSTART );
static const double XEND = 100.0;
static const double YEND = B/A*sqrt( A*A - XEND*XEND );


int main( int argc, char* argv[] )
{
QApplication app( argc, argv );

// Scene with axes
QGraphicsScene* scene = new QGraphicsScene( -10.0, -400.0, 410.0, 410.0 );
QGraphicsLineItem* xAxis = new QGraphicsLineItem( 0.0, 0.0, 375.0, 0.0 );
QGraphicsLineItem* yAxis = new QGraphicsLineItem( 0.0, -375.0, 0.0, 0.0 );
xAxis->setPen( QPen( Qt::red ) );
yAxis->setPen( QPen( Qt::red ) );
scene->addItem( xAxis );
scene->addItem( yAxis );

// Piece of an ellipse
double angleS = atan2( YSTART, XSTART );
double angleE = atan2( YEND, XEND );
if ( angleS < 0.0 ) angleS += 2.0*PI;
if ( angleE < 0.0 ) angleE += 2.0*PI;
double startAngle = min( angleS, angleE );
double sweepLength = abs( angleS - angleE );
if ( PATH_NOT_ELLIPSE )
{
QPainterPath path;
path.arcTo( -A, -B, 2.0*A, 2.0*B, startAngle*180.0/PI,
sweepLength*180.0/PI );
QGraphicsPathItem* ellipse
= new QGraphicsPathItem( path );
scene->addItem( ellipse );
}
else
{
QGraphicsEllipseItem* ellipse
= new QGraphicsEllipseItem( -A, -B, 2.0*A, 2.0*B );
ellipse->setStartAngle( startAngle*180.0/PI*16 );
ellipse->setSpanAngle( sweepLength*180.0/PI*16 );
scene->addItem( ellipse );
}

// Lines at the angles derived above
QGraphicsLineItem* lineS
= new QGraphicsLineItem( 0.0, 0.0, 400.0*cos( angleS ),
-400.0*sin( angleS ) );
lineS->setPen( QPen( Qt::darkYellow ) );
scene->addItem( lineS );
QGraphicsLineItem* lineE
= new QGraphicsLineItem( 0.0, 0.0, 400.0*cos( angleE ),
-400.0*sin( angleE ) );
lineE->setPen( QPen( Qt::darkYellow ) );
scene->addItem( lineE );

// Markers for intended start and end points of arc
QGraphicsEllipseItem* startPoint
= new QGraphicsEllipseItem( XSTART - 5.0, -( YSTART + 5.0 ), 10.0,
10.0 );
startPoint->setPen( QPen( Qt::darkGreen ) );
scene->addItem( startPoint );
QGraphicsEllipseItem* endPoint
= new QGraphicsEllipseItem( XEND - 5.0, -( YEND + 5.0 ), 10.0, 10.0 );
endPoint->setPen( QPen( Qt::darkGreen ) );
scene->addItem( endPoint );

QGraphicsView* view = new QGraphicsView( scene );
view->show();

return app.exec();
}

The points between which I want the segment are the greenish circles; the angles I used are represented by the dark yellow lines (which intersect the circles, of course), and the black line is what Qt gave me. Every example I can find used increments of 90 degrees, and for those cases, everything just works. Does anybody know how the angles are supposed to be defined? I've been using Qt 4.5.3 under Cygwin, if that matters. Thanks!

littlefree
27th January 2013, 05:28
Does anyone reply?
We're confused the definition of the angle, too!!