均匀分布的 scatterViewItems 不重叠
我有一个应用程序,它根据表面表上放置的标记对象来创建可变数量的 ScatterviewItems。
ScatterViewItems 根据在数据库中查找的信息以编程方式添加到
ScatterView Scatterview 在显示此信息方面做得很好
但是,我希望它们
均匀分布在表中,
没有任何项目重叠
任何想法如何做到这一点?
I have an app that creates a variable number of ScatterviewItems based on which tagged object is placed on the surface table.
The ScatterViewItems are added programatically to the ScatterView based on info looked up in a DB
The Scatterview does a good job of displaying this info
However, I would like them to be
evenly distributed across the table and
not have any items overlapping
Any ideas how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你需要碰撞检测。
这个问题有两个部分:检测和解决。检测是查看任何项目的边界是否与任何其他项目的边界相交。如果物品是矩形或圆形,这就非常简单了。如果您正在处理其他几何形状,它可能会变得复杂。
解决方案是指检测到碰撞后应采取的措施。谷歌将帮助你找到无数的算法。以下是 stackoverflow 讨论的几个链接:WPF:使用旋转方块进行碰撞检测, 在碰撞解决方法中应用恢复系数, 检测精灵之间碰撞的最佳方法?。
您可以实现碰撞来工作,以便项目在分散时相互绑定。根据物品的数量,这可能会导致过多的碰撞,导致物品无法很好地分散。如果发生这种情况,只需对已停止移动的物品运行碰撞检测即可。
Sounds like you need collision detection.
There's two parts to this problem: detection and resolution. Detection is seeing if any item's bounding intersects with any other item's bounding. If the items are retangular or circular this is pretty straightforeward. It can get complex if you're dealing with other geometries.
Resoltion is what to do once you've detected a collision. Google will help you find the myriad algorithms for this. Here's a couple links to stackoverflow discussions: WPF: Collision Detection with Rotated Squares, Applying Coefficient of Restitution in a collision resolution method, Best way to detect collision between sprites?.
You can implement collision to work so that items bound off of each other as they scatter. Depending on the number of items, this might cause so much collision that the items don't scatter well. If this happens, just run the collision detection one items have stopped moving.
统一网格
?您还可以通过继承
Panel
来创建自己的面板。您将在 Dr. WPF ItemsControl 操作方法系列中找到一些非常有价值的信息: http://drwpf.com/blog/itemscontrol-a-to-z/
这是必读的内容。
UniformGrid
?You can also create your own panel by iheriting from
Panel
.You will find some uber-valuable info in the Dr. WPF ItemsControl How-To series : http://drwpf.com/blog/itemscontrol-a-to-z/
That's a must-read, period.
ScatterViewItem 具有 Center 和 Orientation 属性,您可以使用它们以编程方式定位项目。如果您知道每个项目的大小,您应该能够使用这些属性以最适合您情况的方式定位它们。通过挂钩每个的 Loaded 事件并检查 ActualWidth/ActualHeight,您可以获得尺寸。如果您可以为所有 SVI 使用固定的初始大小,那就更容易了。
您可以通过计算简单的网格(加上一些随机性以使其看起来更自然)来布局它们,或者您可能正在寻找所谓的“力定向布局”,它为每个对象提供相对于其大小的排斥力。一段时间后,元素自然会彼此均匀间隔,但如果空间不足,它们可能仍会重叠。我还没有见过这方面的 WPF 示例,但请参阅flare.prefused.org/demo(布局>力)了解我在 Flash 中的意思。
ScatterViewItem has properties Center and Orientation which you can use to programmatically position items. If you know the size of each item you should be able to use these properties to position them in whichever way is ideal for your situation. By hooking into the Loaded event of each and checking ActualWidth/ActualHeight, you can get the dimensions. If you can use a fixed initial size for all of your SVIs, that's even easier.
You could lay them out by calculating a simple grid (plus some randomness to make it look more natural), or you may be looking for what's called a "force directed layout", which gives each object a repellent force relative to its size. After a while the elements will naturally be evenly spaced from one another, though they may still overlap if they run out of room. I haven't seen a WPF example of this, but see flare.prefused.org/demo (layout > force) for what I mean in Flash.