views:

201

answers:

3

Hi

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

  1. evenly distributed across the table and

  2. not have any items overlapping

Any ideas how to do that?

A: 

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.

Aurélien Ribon
thanks - I'll check out your reference.I need to use a ScatterView as that is what allows easy manipulation of items on a Surface Table. A uniformGrid wouldn't allow that manipulation. Just need to work out how to prevent ScatterViewItems from not overlapping when they first load
Christo Fur
A: 

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: http://stackoverflow.com/questions/597764/wpf-collision-detection-with-rotated-squares, http://stackoverflow.com/questions/1678908/applying-coefficient-of-restitution-in-a-collision-resolution-method, http://stackoverflow.com/questions/98642/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.

Scott J
A: 

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.

Josh Santangelo