Qt 通過重寫QGraphicItem實現繪制、拖動、縮放、旋轉矩形


本例程通過重寫了一個類,繼承自QGraphicItem,來實現了在qgraphicsScene上繪制、拖動、縮放、旋轉矩形。
效果如下:
在這里插入圖片描述

其實要實現繪制、拖動、縮放矩形都不難,難的是在旋轉之后還要支持縮放。
我的思路是:
1.實現繪制矩形:只要定義一個全局變量QRectF m_oldRect,在外面矩形大小傳進來,然后在paint函數里面繪制這個矩形就行
2.實現拖動矩形:重寫mousePressEvent,mouseMoveEvent,mouseReleaseEvent,記錄鼠標按下的起始點和移動時候的點,並用moveBy()函數來移動矩形即可
3.實現縮放:在矩形內部靠近4條邊的地方定義4個矩形,當鼠標按下的時候在這4個矩形方框內,則將矩形往4個方向拉伸
4.實現旋轉:
我給之前定義的矩形全部配一個QPolygonF,因為我只能通過繪制多邊形的方式來畫出旋轉之后的矩形。
矩形正上方的圓圈我是通過三角函數+直線方程來計算,讓其始終繪制在矩形左右兩個頂點的中垂線上方。
當鼠標落在圓形內部的時候可以控制矩形旋轉。
在矩形旋轉之后,再進行拉伸的時候,我是通過的計算鼠標的點離對面的邊的距離來重新計算矩形的大小,然后計算對應的QPolygonF的大小。

主要代碼如下:
mygraphicrectitem.h

 1 #ifndef MYGRAPHICRECTITEM_H  2 #define MYGRAPHICRECTITEM_H
 3 #include <QObject>
 4 #include <QWidget>
 5 #include <QMouseEvent>
 6 #include <QGraphicsScene>
 7 #include <QGraphicsRectItem>
 8 #include <QGraphicsSceneMouseEvent>
 9 #include <QRect>
10 #include <QPainter>
11 #include <QPolygon>
12 #include <QList>
13 enum STATE_FLAG{ 14     DEFAULT_FLAG=0, 15     MOV_LEFT_LINE,//標記當前為用戶按下矩形的左邊界區域
16     MOV_TOP_LINE,//標記當前為用戶按下矩形的上邊界區域
17     MOV_RIGHT_LINE,//標記當前為用戶按下矩形的右邊界區域
18     MOV_BOTTOM_LINE,//標記當前為用戶按下矩形的下邊界區域
19     MOV_RIGHTBOTTOM_RECT,//標記當前為用戶按下矩形的右下角
20     MOV_RECT,//標記當前為鼠標拖動圖片移動狀態
21     ROTATE//標記當前為旋轉狀態
22 }; 23 class myGraphicRectItem:public QObject,public QGraphicsItem 24 { 25  Q_OBJECT 26 public: 27     myGraphicRectItem(QGraphicsItem *parent = nullptr); 28     //myGraphicRectItem(QRectF m_OriginRect = QRectF(0,0,100,100));
29     QRectF  boundingRect() const; 30     ~myGraphicRectItem(); 31     void setRectSize(QRectF mrect,bool bResetRotateCenter = true); 32     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 33     void mousePressEvent(QGraphicsSceneMouseEvent *event); 34     void mouseMoveEvent(QGraphicsSceneMouseEvent *event); 35     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); 36     void SetRotate(qreal RotateAngle,QPointF ptCenter=QPointF(-999,-999)); 37     QPointF getRotatePoint(QPointF ptCenter, QPointF ptIn, qreal angle);//獲取旋轉后的點
38     QList<QPointF> getRotatePoints(QPointF ptCenter,QList<QPointF> ptIns,qreal angle);//獲取多個旋轉后的點
39     QPolygonF getRotatePolygonFromRect(QPointF ptCenter,QRectF rectIn,qreal angle);//將矩形旋轉之后返回多邊形
40  QRectF getCrtPosRectToSceen(); 41     QRectF m_SmallRotateRect;//矩形頂部用來表示旋轉的標記的矩形
42     QPolygonF m_SmallRotatePolygon;//矩形頂部用來表示旋轉的標記的矩形旋轉后形成的多邊形
43     QPointF getSmallRotateRectCenter(QPointF ptA,QPointF ptB);//獲取旋轉時候矩形正上方的旋轉標記矩形
44  QRectF getSmallRotateRect(QPointF ptA,QPointF ptB); 45     bool m_bRotate; 46  qreal m_RotateAngle; 47  QPointF m_RotateCenter; 48 
49 private: 50  QRectF m_oldRect; 51  QPolygonF m_oldRectPolygon; 52  QRectF m_RotateAreaRect; 53     bool m_bResize; 54  QPolygonF m_insicedPolygon; 55  QRectF m_insicedRectf; 56  QPolygonF m_leftPolygon; 57  QRectF m_leftRectf; 58  QPolygonF m_topPolygon; 59  QRectF m_topRectf; 60  QPolygonF m_rightPolygon; 61  QRectF m_rightRectf; 62  QPolygonF m_bottomPolygon; 63  QRectF m_bottomRectf; 64 // QPolygonF m_rbPolygon; 65 // QRectF m_rbRectf;
66  QPointF m_startPos; 67  STATE_FLAG m_StateFlag; 68     QPointF *pPointFofSmallRotateRect; 69 protected: 70 
71 }; 72 #endif // MYGRAPHICRECTITEM_H

mygraphicrectitem.cpp

 1 #include "mygraphicrectitem.h"
 2 #include <QtMath>
 3 #include <QDebug>
 4 
 5 myGraphicRectItem::myGraphicRectItem(QGraphicsItem *parent):  6     m_bResize(false),  7     m_oldRect(0,0,100,100),  8     m_bRotate(false),  9     m_RotateAngle(0),  10  m_StateFlag(DEFAULT_FLAG)  11 {  12     //setParent(parent);
 13  setRectSize(m_oldRect);  14     setToolTip("Click and drag me!");  //提示
 15     setCursor(Qt::ArrowCursor);   //改變光標形狀,手的形狀
 16  setFlag(QGraphicsItem::ItemIsMovable);  17     // setAcceptDrops(true);
 18     pPointFofSmallRotateRect = new QPointF[4];  19     SetRotate(0);  20     setFlag(QGraphicsItem::ItemIsSelectable);//  21 }  22 
 23 QRectF myGraphicRectItem::boundingRect() const
 24 {  25     //return m_oldRectPolygon.boundingRect();
 26     QRectF boundingRectF = m_oldRectPolygon.boundingRect();  27     return QRectF(boundingRectF.x()-40,boundingRectF.y()-40,boundingRectF.width()+80,boundingRectF.height()+80);  28 }  29 
 30 myGraphicRectItem::~myGraphicRectItem()  31 {  32     delete []pPointFofSmallRotateRect;  33     pPointFofSmallRotateRect = nullptr;  34 }  35 
 36 void myGraphicRectItem::setRectSize(QRectF mrect, bool bResetRotateCenter)  37 {  38     m_oldRect = mrect;  39     if(bResetRotateCenter)  40  {  41         m_RotateCenter.setX(m_oldRect.x()+m_oldRect.width()/2);  42         m_RotateCenter.setY(m_oldRect.y()+m_oldRect.height()/2);  43  }  44     m_oldRectPolygon = getRotatePolygonFromRect(m_RotateCenter,m_oldRect,m_RotateAngle);  45 
 46     m_insicedRectf = QRectF(m_oldRect.x()+8,m_oldRect.y()+8,m_oldRect.width()-16,m_oldRect.height()-16);  47     m_insicedPolygon =getRotatePolygonFromRect(m_RotateCenter,m_insicedRectf,m_RotateAngle);  48 
 49     m_leftRectf = QRectF(m_oldRect.x(),m_oldRect.y(),8,m_oldRect.height()-8);  50     m_leftPolygon = getRotatePolygonFromRect(m_RotateCenter,m_leftRectf,m_RotateAngle);  51 
 52     m_topRectf = QRectF(m_oldRect.x()+8,m_oldRect.y(),m_oldRect.width()-8,8);  53     m_topPolygon = getRotatePolygonFromRect(m_RotateCenter,m_topRectf,m_RotateAngle);  54 
 55     m_rightRectf = QRectF(m_oldRect.right()-8,m_oldRect.y()+8,8,m_oldRect.height()-16);  56     m_rightPolygon = getRotatePolygonFromRect(m_RotateCenter,m_rightRectf,m_RotateAngle);  57 
 58     m_bottomRectf = QRectF(m_oldRect.x(),m_oldRect.bottom()-8,m_oldRect.width()-8,8);  59     m_bottomPolygon = getRotatePolygonFromRect(m_RotateCenter,m_bottomRectf,m_RotateAngle);  60 
 61 // m_rbRectf = QRectF(m_oldRect.right()-8,m_oldRect.bottom()-8,8,8);  62 // m_rbPolygon = getRotatePolygonFromRect(m_RotateCenter,m_rbRectf,m_RotateAngle);
 63 
 64     m_SmallRotateRect = getSmallRotateRect(mrect.topLeft(),mrect.topRight());//矩形正上方的旋轉標記矩形
 65     m_SmallRotatePolygon = getRotatePolygonFromRect(m_RotateCenter,m_SmallRotateRect,m_RotateAngle);  66 }  67 
 68 void myGraphicRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)  69 {  70     QPen mPen = QPen(Qt::yellow);  71     painter->setPen(mPen);  72     //繪制旋轉后的矩形
 73     painter->drawPolygon(m_oldRectPolygon);  74     //繪制旋轉圓形
 75     mPen.setWidth(2);  76  mPen.setColor(Qt::green);  77     painter->setPen(mPen);  78     QPointF pf = getSmallRotateRectCenter(m_oldRectPolygon[0],m_oldRectPolygon[1]);  79     QRectF rect = QRectF(pf.x()-10,pf.y()-10,20,20);  80     painter->drawEllipse(rect);//繪制圓形
 81     painter->drawPoint(pf);//繪制點
 82 }  83 
 84 void myGraphicRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)  85 {  86     if(event->button()== Qt::LeftButton)  87  {  88         m_startPos = event->pos();//鼠標左擊時,獲取當前鼠標在圖片中的坐標,
 89         if(m_SmallRotatePolygon.containsPoint(m_startPos,Qt::WindingFill))//旋轉矩形
 90  {  91  setCursor(Qt::PointingHandCursor);  92             m_StateFlag = ROTATE;  93  }  94         else if(m_insicedPolygon.containsPoint(m_startPos,Qt::WindingFill))//在矩形內框區域時按下鼠標,則可拖動圖片
 95  {  96             setCursor(Qt::ClosedHandCursor);   //改變光標形狀,手的形狀
 97             m_StateFlag = MOV_RECT;//標記當前為鼠標拖動圖片移動狀態
 98  }  99         else if(m_leftPolygon.containsPoint(m_startPos,Qt::WindingFill)) 100  { 101  setCursor(Qt::SizeHorCursor); 102             m_StateFlag = MOV_LEFT_LINE;//標記當前為用戶按下矩形的左邊界區域
103  } 104         else if(m_rightPolygon.containsPoint(m_startPos,Qt::WindingFill)) 105  { 106  setCursor(Qt::SizeHorCursor); 107             m_StateFlag = MOV_RIGHT_LINE;//標記當前為用戶按下矩形的右邊界區域
108  } 109         else if(m_topPolygon.containsPoint(m_startPos,Qt::WindingFill)) 110  { 111  setCursor(Qt::SizeVerCursor); 112             m_StateFlag = MOV_TOP_LINE;//標記當前為用戶按下矩形的上邊界區域
113  } 114         else if(m_bottomPolygon.containsPoint(m_startPos,Qt::WindingFill)) 115  { 116  setCursor(Qt::SizeVerCursor); 117             m_StateFlag = MOV_BOTTOM_LINE;//標記當前為用戶按下矩形的下邊界區域
118  } 119 // else if(m_rbPolygon.containsPoint(m_startPos,Qt::WindingFill)) 120 // { 121 // setCursor(Qt::SizeFDiagCursor); 122 // m_StateFlag = MOV_RIGHTBOTTOM_RECT;//標記當前為用戶按下矩形的右下角 123 // }
124         else
125  { 126             m_StateFlag = DEFAULT_FLAG; 127  } 128  } 129     else
130  { 131         QGraphicsItem::mousePressEvent(event); 132  } 133 } 134 
135 void myGraphicRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 136 { 137     if(m_StateFlag == ROTATE) 138  { 139        int nRotateAngle = atan2((event->pos().x()-m_RotateCenter.x()),(event->pos().y()-m_RotateCenter.y()))*180/M_PI; 140        SetRotate(180-nRotateAngle); 141  setRectSize(m_oldRect); 142        //qDebug()<<nRotateAngle;
143  } 144     else if(m_StateFlag == MOV_RECT) 145  { 146         QPointF point = (event->pos() - m_startPos); 147  moveBy(point.x(), point.y()); 148  setRectSize(m_oldRect); 149         scene()->update(); 150  } 151     else if(m_StateFlag == MOV_LEFT_LINE) 152  { 153         QPointF pf = QPointF((m_oldRectPolygon.at(1).x()+m_oldRectPolygon.at(2).x())/2,((m_oldRectPolygon.at(1).y()+m_oldRectPolygon.at(2).y())/2)); 154         //計算到右側邊中點的距離
155         qreal dis = sqrt((event->pos().x()-pf.x())*(event->pos().x()-pf.x()) +(event->pos().y()-pf.y())*(event->pos().y()-pf.y())); 156         qreal dis2LT = sqrt((event->pos().x()-m_oldRectPolygon.at(0).x())*(event->pos().x()-m_oldRectPolygon.at(0).x()) +(event->pos().y()-m_oldRectPolygon.at(0).y())*(event->pos().y()-m_oldRectPolygon.at(0).y())); 157         qreal dis2RT = sqrt((event->pos().x()-m_oldRectPolygon.at(1).x())*(event->pos().x()-m_oldRectPolygon.at(1).x()) +(event->pos().y()-m_oldRectPolygon.at(1).y())*(event->pos().y()-m_oldRectPolygon.at(1).y())); 158         if(dis<16||dis2LT>dis2RT) 159  { 160             return; 161  } 162         else
163  { 164  QRectF newRect(m_oldRect); 165             newRect.setLeft(m_oldRect.right()-dis); 166  newRect.setRight(m_oldRect.right()); 167             setRectSize(newRect,false); 168             m_RotateCenter=QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(2).x())/2,(m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(2).y())/2); 169  m_oldRect.moveCenter(m_RotateCenter); 170  setRectSize(m_oldRect); 171             scene()->update();//必須要用scene()->update(),不能用update();否則會出現重影
172  } 173  } 174     else if(m_StateFlag == MOV_TOP_LINE) 175  { 176         //底邊中點
177         QPointF pf = QPointF((m_oldRectPolygon.at(2).x()+m_oldRectPolygon.at(3).x())/2,((m_oldRectPolygon.at(2).y()+m_oldRectPolygon.at(3).y())/2)); 178         //計算到底邊中點的距離
179         qreal dis = sqrt((event->pos().x()-pf.x())*(event->pos().x()-pf.x()) +(event->pos().y()-pf.y())*(event->pos().y()-pf.y())); 180         qreal dis2LT = sqrt((event->pos().x()-m_oldRectPolygon.at(0).x())*(event->pos().x()-m_oldRectPolygon.at(0).x()) +(event->pos().y()-m_oldRectPolygon.at(0).y())*(event->pos().y()-m_oldRectPolygon.at(0).y())); 181         qreal dis2LB = sqrt((event->pos().x()-m_oldRectPolygon.at(3).x())*(event->pos().x()-m_oldRectPolygon.at(3).x()) +(event->pos().y()-m_oldRectPolygon.at(3).y())*(event->pos().y()-m_oldRectPolygon.at(3).y())); 182         if(dis<16||dis2LT>dis2LB) 183  { 184             return; 185  } 186         else
187  { 188  QRectF newRect(m_oldRect); 189             newRect.setTop(m_oldRect.bottom()-dis); 190  newRect.setBottom(m_oldRect.bottom()); 191             setRectSize(newRect,false); 192             m_RotateCenter=QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(2).x())/2,(m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(2).y())/2); 193  m_oldRect.moveCenter(m_RotateCenter); 194  setRectSize(m_oldRect); 195             scene()->update();//必須要用scene()->update(),不能用update();否則會出現重影
196  } 197  } 198     else if(m_StateFlag == MOV_RIGHT_LINE) 199  { 200         QPointF pf = QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(3).x())/2,((m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(3).y())/2)); 201         //計算到左側邊中點的距離
202         qreal dis = sqrt((event->pos().x()-pf.x())*(event->pos().x()-pf.x()) +(event->pos().y()-pf.y())*(event->pos().y()-pf.y())); 203         qreal dis2LT = sqrt((event->pos().x()-m_oldRectPolygon.at(0).x())*(event->pos().x()-m_oldRectPolygon.at(0).x()) +(event->pos().y()-m_oldRectPolygon.at(0).y())*(event->pos().y()-m_oldRectPolygon.at(0).y())); 204         qreal dis2RT = sqrt((event->pos().x()-m_oldRectPolygon.at(1).x())*(event->pos().x()-m_oldRectPolygon.at(1).x()) +(event->pos().y()-m_oldRectPolygon.at(1).y())*(event->pos().y()-m_oldRectPolygon.at(1).y())); 205         if(dis<16||dis2LT<dis2RT) 206  { 207             return; 208  } 209         else
210  { 211  QRectF newRect(m_oldRect); 212  newRect.setLeft(m_oldRect.left()); 213             newRect.setRight(m_oldRect.left()+dis); 214             setRectSize(newRect,false); 215             m_RotateCenter=QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(2).x())/2,(m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(2).y())/2); 216  m_oldRect.moveCenter(m_RotateCenter); 217  setRectSize(m_oldRect); 218             scene()->update();//必須要用scene()->update(),不能用update();否則會出現重影
219  } 220  } 221     else if(m_StateFlag == MOV_BOTTOM_LINE) 222  { 223         //頂邊中點
224         QPointF pf = QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(1).x())/2,((m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(1).y())/2)); 225         //計算到底邊中點的距離
226         qreal dis = sqrt((event->pos().x()-pf.x())*(event->pos().x()-pf.x()) +(event->pos().y()-pf.y())*(event->pos().y()-pf.y())); 227         qreal dis2LT = sqrt((event->pos().x()-m_oldRectPolygon.at(0).x())*(event->pos().x()-m_oldRectPolygon.at(0).x()) +(event->pos().y()-m_oldRectPolygon.at(0).y())*(event->pos().y()-m_oldRectPolygon.at(0).y())); 228         qreal dis2LB = sqrt((event->pos().x()-m_oldRectPolygon.at(3).x())*(event->pos().x()-m_oldRectPolygon.at(3).x()) +(event->pos().y()-m_oldRectPolygon.at(3).y())*(event->pos().y()-m_oldRectPolygon.at(3).y())); 229         if(dis<16||dis2LT<dis2LB) 230  { 231             return; 232  } 233         else
234  { 235  QRectF newRect(m_oldRect); 236  newRect.setTop(m_oldRect.top()); 237             newRect.setBottom(m_oldRect.top()+dis); 238             setRectSize(newRect,false); 239             m_RotateCenter=QPointF((m_oldRectPolygon.at(0).x()+m_oldRectPolygon.at(2).x())/2,(m_oldRectPolygon.at(0).y()+m_oldRectPolygon.at(2).y())/2); 240  m_oldRect.moveCenter(m_RotateCenter); 241  setRectSize(m_oldRect); 242             scene()->update();//必須要用scene()->update(),不能用update();否則會出現重影
243  } 244  } 245 } 246 
247 void myGraphicRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 248 { 249  setCursor(Qt::ArrowCursor); 250     if(m_StateFlag == MOV_RECT) 251  { 252         m_StateFlag = DEFAULT_FLAG; 253  } 254     else { 255         QGraphicsItem::mouseReleaseEvent(event); 256  } 257 } 258 
259 void myGraphicRectItem::SetRotate(qreal RotateAngle, QPointF ptCenter) 260 { 261     m_bRotate = true; 262     if(ptCenter.x()==-999 && ptCenter.y()==-999) 263  { 264         m_RotateCenter = QPointF(m_oldRect.x()+m_oldRect.width()/2,m_oldRect.y()+m_oldRect.height()/2); 265  } 266     else
267  { 268         m_RotateCenter = ptCenter; 269  } 270     m_RotateAngle = RotateAngle; 271     this->update(); 272 } 273 
274 QPointF myGraphicRectItem::getRotatePoint(QPointF ptCenter, QPointF ptIn, qreal angle) 275 { 276     double dx = ptCenter.x(); 277     double dy = ptCenter.y(); 278     double x = ptIn.x(); 279     double y = ptIn.y(); 280     double xx,yy; 281     xx = (x-dx)*cos(angle*M_PI/180)-(y-dy)*sin(angle*M_PI/180)+dx; 282     yy = (x-dx)*sin(angle*M_PI/180)+(y-dy)*cos(angle*M_PI/180)+dy; 283 
284     return QPointF(xx,yy); 285 } 286 
287 QList<QPointF> myGraphicRectItem::getRotatePoints(QPointF ptCenter, QList<QPointF> ptIns, qreal angle) 288 { 289     QList<QPointF> lstPt; 290     for(int i = 0;i<ptIns.count();i++) 291  { 292  lstPt.append(getRotatePoint(ptCenter,ptIns.at(i),angle)); 293  } 294     return lstPt; 295 } 296 
297 QPolygonF myGraphicRectItem::getRotatePolygonFromRect(QPointF ptCenter, QRectF rectIn, qreal angle) 298 { 299     QVector<QPointF> vpt; 300     QPointF pf = getRotatePoint(ptCenter,rectIn.topLeft(),angle); 301  vpt.append(pf); 302     pf = getRotatePoint(ptCenter,rectIn.topRight(),angle); 303  vpt.append(pf); 304     pf = getRotatePoint(ptCenter,rectIn.bottomRight(),angle); 305  vpt.append(pf); 306     pf = getRotatePoint(ptCenter,rectIn.bottomLeft(),angle); 307  vpt.append(pf); 308     pf = getRotatePoint(ptCenter,rectIn.topLeft(),angle); 309  vpt.append(pf); 310     return QPolygonF(vpt); 311 } 312 
313 QRectF myGraphicRectItem::getCrtPosRectToSceen() 314 { 315     QRectF retRect = QRectF(m_oldRect.x()+pos().x(),m_oldRect.y()+pos().y(),m_oldRect.width(),m_oldRect.height()); 316     return retRect; 317 } 318 QRectF myGraphicRectItem::getSmallRotateRect(QPointF ptA,QPointF ptB) 319 { 320     QPointF pt = getSmallRotateRectCenter(ptA,ptB); 321     return QRectF(pt.x()-10,pt.y()-10,20,20); 322 } 323 QPointF myGraphicRectItem::getSmallRotateRectCenter(QPointF ptA,QPointF ptB) 324 { 325     QPointF ptCenter = QPointF((ptA.x()+ptB.x())/2,(ptA.y()+ptB.y())/2);//A,B點的中點C 326     //中垂線方程式為 y=x*k + b;
327     qreal x,y;//旋轉圖標矩形的中心
328     if(abs(ptB.y()-ptA.y())<0.1) 329  { 330         if(ptA.x()<ptB.x())//矩形左上角在上方
331  { 332             x = ptCenter.x(); 333             y = ptCenter.y()-20; 334  } 335         else//矩形左上角在下方
336  { 337             x = ptCenter.x(); 338             y = ptCenter.y()+20; 339  } 340  } 341     else if(ptB.y()>ptA.y())//順時針旋轉0-180
342  { 343         qreal k = (ptA.x()-ptB.x())/(ptB.y()-ptA.y());//中垂線斜率
344         qreal b = (ptA.y()+ptB.y())/2-k*(ptA.x()+ptB.x())/2; 345         //求AB線中垂線上離AB中點20個像素的點C的坐標
346         x = 20*cos(atan(k))+ptCenter.x(); 347         y = k*x+b; 348  } 349     else if(ptB.y()<ptA.y())//順時針旋轉180-360
350  { 351         qreal k = (ptA.x()-ptB.x())/(ptB.y()-ptA.y());//中垂線斜率
352         qreal b = (ptA.y()+ptB.y())/2-k*(ptA.x()+ptB.x())/2; 353         //求AB線中垂線上離AB中點20個像素的點C的坐標
354         x = -20*cos(atan(k))+ptCenter.x(); 355         y = k*x+b; 356  } 357     return QPointF(x,y); 358 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM