2014-09-29 3 views
0

Qt 라이브러리를 사용하여 포물선 또는 다른 다항식을 페인트하는 방법이 있습니까? QPainter를 사용해 보았지만 거기에 그런 옵션이 없습니다. 감사합니다.Qt를 사용하여 포물선 또는 다른 다항식을 그립니다.

+0

을 그리고 당신은 HTTP 여기처럼 앤티 앨리어싱 회화로 연결해야합니다 :

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QImage> #include <QPainter> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: void drawMyFunction(qreal xMin,qreal xMax); qreal myFunction(qreal x); QImage * pic; Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 

이 당신의 MainWindow.cpp 될 것입니다//qt-project.org/doc/qt-4.8/coordsys.html? –

답변

0

QPainterPath을 사용해 보셨습니까? 도움이 될 QPainterPath::quadToQPainterPath::cubicTo 멤버 함수가 있습니다.

+0

고마워,하지만 나는 그 funtions를 사용하여 간단한 포물선을 그리는 방법을 모르겠다. – kakush

+1

@kakush 포물선은 [cubic 베 지어 경로로 쉽게 변환 될 수있다] (http://math.stackexchange.com/questions/335226/) 포물선 - 2 차 - 베 지어 곡선으로 변환). –

0

은 다항식 함수에서 이산 포인트를 계산하고 QPainter::drawLines을 사용하여 그림을 그립니다.

예를 들어

, y = x^2는 :

float xmin = 0; 
    float xmax = 2; 
    float step = 0.1; // experiment with values 

    QVector<QPointF> points; 
    float x = xmin; 
    while(x < xmax) 
    { 
     float y = x^x; //f(x) 
     lines.push_back(QPointF(x,y)); 
     x+= step; 
    } 
    painter.drawLines(points); 

Because the top corner in qt coordonates is (0,0) 당신은 Y를 계산 한 후 x와 y에 대한 기하학적 변환을 확인해야합니다.

+0

감사합니다. 이것은 내가 지금하고있는 것과 정확히 일치하지만, 좀 더 효율적인 것을 원했습니다. – kakush

+0

@kakush. 천천히? 큰 값의 단계는 무서운 포물선을 만들어내는 반면, 낮은 값은 루핑과 드로잉에 소요되는 시간을 늘려줌으로써 더 나은 결과를 얻을 수 있다는 것을 알고 있기 때문입니다. – UmNyobe

1

QPainter를 사용하려면 QImage 또는 QPixmap을 사용하여 출력을 표시하거나 저장해야합니다.

이 코드는 Qt 위젯 응용 프로그램에서 어떻게 완료되었는지 보여주는 간단한 코드입니다.

이 파일은 MainWindow.h 파일입니다. 어쩌면 먼저 방정식을 맞는 몇 가지 포인트를 찾아야한다

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    drawMyFunction(-3,3); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::drawMyFunction(qreal xMin, qreal xMax) 
{ 
    qreal step = (xMax - xMin)/1000; 
    QPainterPath painterPath; 
    QSize picSize(300,300); 
    for(qreal x = xMin ; x <= xMax ; x = x + step) 
    { 
     if(x == xMin) 
     { 
      painterPath.moveTo(x,myFunction(x)); 
     } 
     else 
     { 
      painterPath.lineTo(x,myFunction(x)); 
     } 
    } 
    // Scaling and centering in the center of image 
    qreal xScaling = picSize.width()/painterPath.boundingRect().width(); 
    qreal yScaling = picSize.height()/painterPath.boundingRect().height(); 
    qreal scale; 
    if(xScaling > yScaling) 
     scale = yScaling; 
    else 
     scale = xScaling; 
    for(int i = 0 ; i < painterPath.elementCount() ; i++) 
    { 
     painterPath.setElementPositionAt(i,painterPath.elementAt(i).x*scale +  picSize.width()/2,-painterPath.elementAt(i).y*scale + picSize.height()/2); 
    } 
    // Drawing to the image 
    pic = new QImage(picSize,QImage::Format_RGB32); 
    pic->fill(Qt::white); 
    QPainter picPainter(pic); 
    QPen myPen; 
    myPen.setColor(Qt::black); 
    myPen.setWidth(10); 
    picPainter.drawPath(painterPath); 
    ui->label->setPixmap(QPixmap::fromImage(*pic)); // label is an added label to the ui 
    // you can also do this instead of just showing the picture 
    // pic->save("myImage.bmp"); 
} 

qreal MainWindow::myFunction(qreal x) 
{ 
    return x*x; // write any function you want here 
}