#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[] )
{
// Scene with axes
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 )
{
path.arcTo( -A, -B, 2.0*A, 2.0*B, startAngle*180.0/PI,
sweepLength*180.0/PI );
scene->addItem( ellipse );
}
else
{
ellipse->setStartAngle( startAngle*180.0/PI*16 );
ellipse->setSpanAngle( sweepLength*180.0/PI*16 );
scene->addItem( ellipse );
}
// Lines at the angles derived above
-400.0*sin( angleS ) );
lineS
->setPen
( QPen( Qt
::darkYellow ) );
scene->addItem( lineS );
-400.0*sin( angleE ) );
lineE
->setPen
( QPen( Qt
::darkYellow ) );
scene->addItem( lineE );
// Markers for intended start and end points of arc
10.0 );
startPoint
->setPen
( QPen( Qt
::darkGreen ) );
scene->addItem( startPoint );
endPoint
->setPen
( QPen( Qt
::darkGreen ) );
scene->addItem( endPoint );
view->show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks