2012-01-28 23 views
3

我無法弄清楚如何優化包含NSBezierPath的NSView的繪圖。NSBezierPath圖

讓我試着解釋我的意思。我有一張線條圖,大約40K點,我想畫。我把所有的點,它是容易的,我畫一次使用下面的代碼全圖:

NSInteger npoints=[delegate returnNumOfPoints:self]; //get the total number of points 
aRange=NSMakeRange(0, npoints); //set the range 
absMin=[delegate getMinForGraph:self inRange:aRange]; //get the Minimum y value 
absMax=[delegate getMaxForGraph:self inRange:aRange]; //get the Maximum y value 
float delta=absMax-absMin; //get the height of bound 
float aspectRatio=self.frame.size.width/self.frame.size.heigh //compensate for the real frame 
float xscale=aspectRatio*(absMax-absMin); // get the width of bound 
float step=xscale/npoints; //get the unit size 
[self setBounds:NSMakeRect(0.0, absMin, xscale, delta)]; //now I can set the bound 
NSSize unitSize={1.0,1.0}; 
unitSize= [self convertSize:unitSize fromView:nil]; 
[NSBezierPath setDefaultLineWidth:MIN(unitSize.height,unitSize.width)]; 
fullGraph=[NSBezierPath bezierPath]; 
[fullGraph moveToPoint:NSMakePoint(0.0, [delegate getValueForGraph:self forPoint:aRange.location])]; 
//Create the path 
for (long i=1; i<npoints; i++) 
    { 
     y=[delegate getValueForGraph:self forPoint:i]; 
     x=i*step; 
     [fullGraph lineToPoint:NSMakePoint(x,y)]; 
} 
[[NSColor redColor] set]; 
[fullGraph stroke]; 

所以現在我有存儲在NSBezierPath形式實時全圖的座標,我可以撫摸。但是,讓我們假設現在我想要儘可能快地顯示添加一個點的圖。

我不想每次都畫整個點集。如果可能的話,我想使用完整的圖形,並只顯示一小部分。假設我想在同一幀中只渲染前1000個點。是否有任何可能(修改邊界並最終以某種方式縮放路徑)僅將圖的第一部分渲染爲正確的邊界?

我無法獲得結果,因爲如果我修改邊界,那麼比例會發生變化,我無法用線寬修復問題。

回答

1

您可以創建只新的數據,中風,然後是附加到現有圖形中的新路徑:

NSBezierPath* newPath = [NSBezierPath bezierPath]; 
//... draw the new lines in newPath ... 
[newPath stroke]; 
[fullGraph appendBezierPath:newPath]; 
+0

謝謝您的建議。我一定會嘗試遵循這種方法。但是,我的描述可能有點誤導,所以讓我試着重述一下。第一次調用drawRect方法時,我可以創建包含整個圖形的完整圖形貝塞爾路徑(超過30K行,我知道它太多了),並修改視圖邊界以匹配我的點數單位。但現在我不想顯示整個圖表。我只想顯示,比方說,前兩行。在下面的調用中,我想增加x座標以顯示更大的部分,依此類推。 – user1175172 2012-01-30 11:10:40

+0

問題是,對於每個不正確的調用,我嘗試只更改視圖邊界以匹配要顯示的圖形部分。但是在界線變化之後,線寬是巨大的。而且我無法理解如何縮小它。 – user1175172 2012-01-30 11:11:00