使用数组构造的 .NET PointCollection 与从 PointCollection 构造的 .NET PointCollection 的行为不同

发布于 2024-11-23 15:16:30 字数 1320 浏览 2 评论 0原文

我有一个 System.Windows.Controls.Canvas,我以编程方式在其上放置了一个 System.Windows.Shapes.Polygon:

private Canvas mMainCanvas = new Canvas();
private Polygon mHistogram = new Polygon();

稍后,我使用大量事件(约 1,000,000 个左右)更新多边形。我一直在努力使其尽可能快速和高效(这本身就是一场斗争)。我最近的尝试是将这些值累积到 PointCollection 中,并定期重新设置多边形的集合(mHistogram):

int i = 10000;
PointCollection mPc = new PointCollection(256);
double y;
Point p;

private void EventProcessor( int bin ) {
    if (0 < i--) {
        p = mPc[bin];
        y = p.Y + 1;
        p.X = p.X;
        p.Y = y;
        mPc[bin] = p;
        if (mMainCanvas.Height < p.Y)
            mMainCanvas.Height = p.Y;
    }
    else {
        i = 10000;
        mHistogram.Points = new PointCollection( mPc ); /* This works if mPc
                                                           is a PointCollection.
                                                           It does not work if 
                                                           mPc is a Point[]
    }
}

这似乎工作正常,尽管仍然不够快。因此,我将 mPc 的类型从 PointCollection 更改为简单的点数组 (Point[]),希望这将使访问速度更快一些。但是,当我这样做时,我的多边形(mHistogram)根本无法更新。

这让我很困惑。我从 IEnumerable (mPc) 创建一个新的 PointCollection,它应该创建一个新的 PointCollection,其行为与任何其他 PointCollection 一样。为什么使用数组 (Point[]) 创建时它的行为有所不同?

谢谢。

I have a System.Windows.Controls.Canvas on to which I programatically place a System.Windows.Shapes.Polygon:

private Canvas mMainCanvas = new Canvas();
private Polygon mHistogram = new Polygon();

Later on, I update the polygon with a very large number of events (~1,000,000 or so). I have been trying to make this as fast and efficient as possible (a struggle in its self). My latest attempt was to accumulate the values into a PointCollection and periodically re-set the collection of the Polygon (mHistogram):

int i = 10000;
PointCollection mPc = new PointCollection(256);
double y;
Point p;

private void EventProcessor( int bin ) {
    if (0 < i--) {
        p = mPc[bin];
        y = p.Y + 1;
        p.X = p.X;
        p.Y = y;
        mPc[bin] = p;
        if (mMainCanvas.Height < p.Y)
            mMainCanvas.Height = p.Y;
    }
    else {
        i = 10000;
        mHistogram.Points = new PointCollection( mPc ); /* This works if mPc
                                                           is a PointCollection.
                                                           It does not work if 
                                                           mPc is a Point[]
    }
}

This seems to work OK, albeit still not fast enough. So, I changed the type of mPc from a PointCollection to a simple array of Points (Point[]), hoping this would make access a little faster. However, when I do this, my Polygon (mHistogram) fails to update at all.

This is baffling to me. I create a new PointCollection from an IEnumerable (mPc) which should create a new PointCollection that behaves just like any other PointCollection. Why does it behave differently when created with an Array (Point[])?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-11-30 15:16:30

您描述的问题似乎不太可能,并且可能与您的代码的另一个问题有关。查看这个简单的测试,您可以验证实际上是否创建了相同的集合:

PointCollection collection1 = new PointCollection(new Point[] { new Point(1, 1), new Point(2, 2) });
PointCollection collection2 = new PointCollection(collection1);
for (int i = 0; i < collection2.Count; i++) {
    if (collection1[i] != collection2[i])
        throw new InvalidOperationException();
}

对于 Points 属性的分配,Polygon 不会关心 PointsCollection 是如何创建的。

这两段代码产生相同的结果:

poly.Points = new PointCollection(new Point[] {
    new Point(100, 100),
    new Point(200, 200)
});

并且:

var coll = new PointCollection();
coll.Add(new Point(100, 100));
coll.Add(new Point(200, 200));
poly.Points = new PointCollection(coll);

请记住以下几点是不同的:

var collection = new PointCollection(256);
var array = new Point[256];

集合包含 0 个项目,但有空间容纳 256 个预分配的项目。该数组包含 256 个项目,并且只能有空间容纳 256 个项目。

The problem you describe seems unlikely and is probably related to another issue with your code. Looking at this simple test, you can verify that the same collections are in fact created:

PointCollection collection1 = new PointCollection(new Point[] { new Point(1, 1), new Point(2, 2) });
PointCollection collection2 = new PointCollection(collection1);
for (int i = 0; i < collection2.Count; i++) {
    if (collection1[i] != collection2[i])
        throw new InvalidOperationException();
}

As for the assignment to the Points property, the Polygon wouldn't care how the PointsCollection was created.

Both these pieces of code produce the same results:

poly.Points = new PointCollection(new Point[] {
    new Point(100, 100),
    new Point(200, 200)
});

and:

var coll = new PointCollection();
coll.Add(new Point(100, 100));
coll.Add(new Point(200, 200));
poly.Points = new PointCollection(coll);

Keep in mind that the following are different:

var collection = new PointCollection(256);
var array = new Point[256];

The collection contains 0 items, but has space to hold 256 preallocated. The array contains 256 items and will only ever have space to hold 256 items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文