2016-08-15 193 views
0

所以我試圖創建一個小圖,我將允許用戶添加隨機線等作爲學習項目。最令人沮喪的部分是弄清楚如何讓放大/縮小工作 - 我將ZoomScale變量綁定到鼠標滾輪,並且它「起作用」,但我希望能夠標記軸,具體取決於它們的大小放大並計算它的距離測量(米,釐米等,因爲我也有一個MM每像素變量,所以我們應該能夠計算出來),所以它需要更精確的科學,而不僅僅是「它的工作」放大和縮小計算

double X1 = ((actualline[i].X1 + actualWidth - VisibleXMax - VisibleXMin) * ZoomScale); //Calculate modified coordinates based on 
double X2 = ((actualline[i].X2 + actualWidth - VisibleXMax - VisibleXMin) * ZoomScale); // window width, window height, Visible X/Y, and Zoom. 
double Y1 = ((actualline[i].Y1 + actualHeight - VisibleYMax - VisibleYMin) * ZoomScale); 
double Y2 = ((actualline[i].Y2 + actualHeight - VisibleYMax - VisibleYMin) * ZoomScale); 

而不是努力工作,讓我們嘗試一個簡單的1維方程,我可以重寫爲x和y。

因此,可以說,我們在x方向

. . . . . 

目前,填補了我們的整個屏幕(以及窗口實際上)5個單位寬線。從0到5一路穿過。現在用戶滾動來放大前三個單位。現在這3個單位應該填滿整個窗口,因爲用戶放大了它。它應該像這樣在窗口

. . . 

所以originially線爲X1 = 0,X2 = 5.0至5。由於我們的窗口是5個單位寬它填補了窗口。現在,用戶只想看到單位x1 = 0到x2 = 3. 0到3.但是我們希望這些單位在整個窗口中伸展,所以通過某種縮放計算(如上所述),我們想要將0,3變爲0,5使用可用的變量。變量是:

窗口寬度(5個單位在這種情況下)

原始X1和X2(0和5在這種情況下)

可見X min和max(0和3在這種情況下)

並且每次向上滾動時縮放比例爲1並且增量爲0.05。

有什麼建議嗎?

+0

@FirstStep OK的聲音好!不急! – Fivestar

回答

1

以下是我的工作方式。按照意見,讓我知道,如果你有任何問題:

public void ZoomImage(int ScrollDelta) 
{ 

    OldUnitInPixels = UnitInPixels; // UnitInPixels is something similar to what you called mm/pixel - we need to keep the old one 

    if (ScrollDelta > 0) // this `if` means zoom in - the `else` means zoom out 
    { 
     if (ZoomLevel != 20) // to control my maximum zoom in (I want 20 zoom levels only) 
     { 
      ZoomLevel++; // increment zoom level 

      UnitInPixels += initialUnitInPixels; // I add certain value when I zoom in - for ex: initially it was 3 mm per pix, on zoom in it would be 4 mm per pixel 
      dotSize++; // I want some points to increase in size a little bit 
      lineSize += 0.1; // I want some liness to increase in size a little bit 


      // adjust all coord (points) 
      for (var i = 0; i < DisplayedCoords.Count; i++) 
      { 
       DisplayedCoords[i].X -= XCoordOffset; // remove value of what you called VisibleX from current x value - means go back to the very original coord - ex: actual value is (1,1) but I added (49, 49) for it so it looks (50,50) better on the screen. So I want to remove whatever I added which is (49,49) 
       DisplayedCoords[i].Y -= YCoordOffset; // remove value of what you called VisibleY from current Y value 

       DisplayedCoords[i].X /= OldUnitInPixels; // divide by old 
       DisplayedCoords[i].Y /= OldUnitInPixels; 

       DisplayedCoords[i].X *= UnitInPixels; // multiply by new 
       DisplayedCoords[i].Y *= UnitInPixels; 

       DisplayedCoords[i].X += XCoordOffset; // add back whatever we removed earlier. So it looks back again "better" 
       DisplayedCoords[i].Y += YCoordOffset; 
      } 
      DrawImage(); // draw 
     } 
    } 
    else 
    { 
     // else is super similar but instead of all the (++)s I do (--)s 
    } 
} 

看到它的工作:(放大/縮小,從/鼠標位置還沒有完)

enter image description here

+0

你是如何處理滾動?我在你的代碼中看到它沒有看着窗口的寬度/高度。在我的版本中,我將實際值保留在自己的集合中,所以我不需要添加/分開偏移量,因爲我將實際值分開存儲。 – Fivestar

+0

我會認爲你的意思是平移。平移是與縮放不同的功能。這是非常複雜的,但如果你喜歡它,你可以嘗試一下。這裏的代碼只是「_輸入:鼠標滾動值和輸出:不同scale_」,而平移具有「_Input:單擊,鼠標速度,鼠標StartPosition,鼠標CurrentPosition和輸出:不同的View_」 –

+0

我明白了。在我的代碼中,它是由可見的x最小值和最大值處理的 - 就像在你平移鼠標時它改變這些值並且這是線條的繪製位置一樣。這個邏輯應該完全分開嗎? – Fivestar