NSBezier路径图
我不知道如何优化包含 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 个点。是否有可能(修改边界并最终以某种方式缩放路径)仅在正确的边界内渲染图形的第一部分?
我无法获得结果,因为如果我修改边界,比例就会改变,并且我无法解决线宽问题。
I cannot figure out how to optimize the drawing of an NSView that contains a NSBezierPath.
Let me try to explain what I mean. I have a line graph, made by about 40K points, that I want to draw. I have all the points and it's easy for me to draw once the full graph using the following code:
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];
So now I have the whole graph stored in a NSBezierPath form in real coordinate, that I can stroke. But let's suppose that now I want to display the graph adding one point at time as fast as possible.
I do not want to draw the whole set of points every time. I want to use, if possible the complete graph and visualize only a small part. Let's say that I want to render in the same frame only the first 1000 points. Is there any possibility (modifying bounds and eventually scaling the path in some way) to render only the first part of the graph in correct bounds?
I was not able to obtain the result, because if I modify the bounds then the scale changes and I'm not able to fix the problem with linewidth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以仅使用新数据创建一条新路径,对其进行描边,然后将其附加到现有图表中:
You can create a new path with just the new data, stroke it, then append that to your existing graph: