NSBezierPath图

时间:2012-01-28 13:05:13

标签: objective-c nsview nsbezierpath

我无法弄清楚如何优化包含NSBezierPath的NSView的绘制。

让我试着解释一下我的意思。我有一个大约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 个答案:

答案 0 :(得分:1)

您可以仅使用新数据创建新路径,将其描边,然后将其附加到现有图表中:

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