搜尋此網誌

2011年11月4日 星期五

如何紀錄一個矩形區塊(rb)在一個矩形範圍(ra)的相對位置 ,並依比例還原至絕對位置及範圍

A. 紀錄相對位置及範圍
1. 利用 QPointF 分別紀錄rb在ra當中的左上角的點,與右下角的點的相對位置
ex:

   // 假設變數定義如下
   // s_topleft_x  (rb 左上角x在ra的絕對位置)
   // s_topleft_y  (rb 左上角y在ra的絕對位置)
   // s_bottomright_x  (rb 右下角x在ra的絕對位置)
   // s_bottomright_y   (rb 右下角y在ra的絕對位置)
   // picWidth    (ra的寬)
   // picHeight   (ra的高)
   
   QPointF topPF = QPointF((double)s_topleft_x/picWidth, (double)s_topleft_y/picHeight);
     QPointF bottomPF = QPointF((double)s_bottomright_x/picWidth, (double)s_bottomright_y/picHeight);
   
2. 設定 QRectF
ex:
    QRectF newRect(topPF, bottomPF);
   
   
以上newRect就記錄了rb在ra中的相對位置及範圍了


B.還原相對位址

  // 假設變數定義如下
  // newPicWidth  (欲還原ra的寬)
  // newPicHeight (欲還原ra的高)
  // newRect (之前紀錄的rb的相對位置的矩形範圍) 
 
  QRect mRect;
    mRect.setX((int)(((double)newPicWidth)* newRect.x()));
    mRect.setY((int)(((double)newPicHeight)* newRect.y()));
    mRect.setBottom((int)(((doublenewPicHeight)* newRect.bottom()));
    mRect.setRight((int)(((double)newPicWidth)* newRect.right()));
 
以上 mRect 就是 rb在新的ra當中的絕對位置及範圍了

沒有留言: