2015-02-05 78 views
0

我想畫畫,所以我剛纔提到的例子代碼中,我們可以得出箭箭:如何獲得的終點翻譯QPolygon

http://doc.qt.io/qt-5/qtwidgets-graphicsview-elasticnodes-edge-cpp.html

我決定用畫相同的公式,並嘗試像:

theCurrentLine->setP1(QPointF(0, 0) ); 
theCurrentLine->setP2((theLineVector)); 
p->drawLine(*theCurrentLine); 

double angle = ::acos(theCurrentLine->dx()/theCurrentLine->length()); 
if (theCurrentLine->dy() >= 0) 
    angle = TwoPi - angle; 

QPointF sourcePoint = QPointF(0,0); 

QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi/3) * theArrowSize, 
               cos(angle + Pi/3) * theArrowSize); 
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi/3) * theArrowSize, 
               cos(angle + Pi - Pi/3) * theArrowSize); 

p->drawPolygon(QPolygonF() << theCurrentLine->p1() << sourceArrowP1 << sourceArrowP2); 

但現在我想繪製箭頭多邊形繪製後的線。

如何更改theCurrentLineP1()值,該值可以在多邊形之後開始,即當前的polygon(arrowHead)並且線條在同一點開始?我需要在繪製箭頭後開始線條。原因是有時如果筆寬增加箭頭看起來比線條小。

回答

1

您可以在QPolygon中獲取索引點。

QPoint QPolygon::point (int index) const 

當你知道有多少點時,這很容易。而Qt文檔是你的朋友。

而且你可以使用count(),例如:

QPoint lastPoint = myPolygon.point(myPolygon.count() - 1); 

只是爲了你的多邊形起一個名字,你會沒事的。

編輯:這些代碼的最新版本應該可以解決您的問題。我以爲我需要舉一個例子。 您需要在此順序添加點:

QPolygon myPolygon; 
myPolygon.setPoint(0, sourceArrowP1); 
myPolygon.setPoint(1, theCurrentLine->p1()); 
myPolygon.setPoint(2, sourceArrowP2); 
p->drawPolygon(myPolygon); 
QPoint lastPoint; 
lastPoint = myPolygon.point(myPolygon.count() - 1); 

您需要繪製姓氏和點之間的線路。在這裏:

p->drawLine(myPolygon.point(0, myPolygon.point(myPolygon.count() - 1)); 

如果你希望你的箭頭來填充顏色,你需要使用的,而不是QPolygon QPainterPath:

QPen pen(Qt::black); //Or whatever color you want 
pen.setWidthF(10); //Or whatever size you want your lines to be 
QBrush brush(Qt::red); //Or whatever color you want to fill the arrow head 
p->setPen(pen); 
p->setBrush(brush); 
QPainterPath arrow(sourceArrowP1); 
arrow.lineTo(theCurrentLine->p1()); 
arrow.lineTo(sourceArrowP2); 
arrow.lineTo(sourceArrowP1); //This returns to the first point. You might eliminate this line if you want to. 
p->drawPath(arrow); 
+0

感謝您的有用的職位。我還有一項任務。我需要畫出最後一點和上一點之間的界限。像箭頭的中間。 – Wagmare 2015-02-06 09:22:50

+1

@Wagmare沒問題。請務必經常查看Qt文檔以獲取答案。這是我看到的最好的文檔。 – 2015-02-06 11:27:43

0

我的實際執行情況:

theCurrentLine->setP1(QPointF(0, 0) ); // arrow line coordinates 
theCurrentLine->setP2((theLineVector)); 
double angle = ::acos(theCurrentLine->dx()/theCurrentLine->length()); // angle of the current Line 
if (theCurrentLine->dy() >= 0) 
    angle = TwoPi - angle; 

// getting arrow head points to be drawn 
QPointF sourcePoint = QPointF(0,0); 

QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi/3) * theArrowSize, 
               cos(angle + Pi/3) * theArrowSize); 
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi/3) * theArrowSize, 
               cos(angle + Pi - Pi/3) * theArrowSize); 

p->drawPolygon(QPolygonF() << theCurrentLine->p1() << sourceArrowP1 << sourceArrowP2); 

// to find the center point in the arrow head right 
QLineF perpLine = QLineF(theCurrentLine->p1(), theCurrentLine->p2()); 
QLineF arrowheadWidth = QLineF(sourceArrowP1, sourceArrowP2); 

QPointF originPoint; 
QLineF::IntersectType res = perpLine.intersect(arrowheadWidth, &originPoint); 

theCurrentLine->setP1(originPoint); 
p->drawLine(*theCurrentLine); 

如果有人知道更好的方法來實現這一點(我相信會有),請糾正我。

+0

我編輯了我的答案以回答您的評論,但我想你還沒有看到它。 – 2015-02-06 10:15:14

+0

我的答案的問題是我按照您給出的順序添加了要點。我應該做的是添加currentLine-> p1()作爲第二點。那麼我的回答將適用於你的情況。我將編輯第一個代碼。 – 2015-02-06 10:43:42

+0

現在查看我的答案,並請評論是否有問題。 – 2015-02-06 10:48:23