2016-08-15 197 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