How to "best fit" a collection of controls (like Outlook appointments)

  • Thread starter Thread starter Jared
  • Start date Start date
J

Jared

This is really a generic programming approach question, but I happen to
be working in C# and the folks on the dotnet forums seem to be pretty
helpful. =)

I've got an Outlook-style "one-day schedule view" control just about
completed, but I can't seem to get the layout logic quite right. I'm
looking for suggestions about how to programmatically determine where
"appointment" objects should be positioned on my control such that
there are no overlapping objects.

My explanation kinda sucks, but try creating a few overlapping
appointments in Outlook (on the one-day view) to see what I mean. If
you overlap appointments, Outlook somehow figures out how to "best fit"
them on the schedule, and that's exactly what I'm trying to accomplish.

TIA
Jared
 
Well, I got the old gears grinding away, and here's my newest approach,
which I think is going to work well. Copy/paste this into a
fixed-width text editor to get things to line up.

Notes:

+ An "event" is something on the calendar (e.g. an appointment)
+ Each "event" has a start/end date/time
+ I already have reliable methods for determining event conflicts
+ The "columns" below are really array indexers


STEP 1: Initialize
Position each event in its own column.

1 2 3 4 5 6
-------------------------
| A | | | | | |
| | | | | | |
| | B | | | | |
| | B | C | | | |
| | B | C | D | | |
| | | C | D | | |
| | | C | | | |
| | | C | | | |
| | | C | | E | |
| | | | | E | |
| | | | | E | F |
| | | | | | F |


STEP 2: Collapse to fewest columns
Put each event in the first column it will fit w/o conflicts.

1 2 3 4 5 6
-------------------------
| A | | | | | |
| | | | | | |
| B | | | | | |
| B | C | | | | |
| B | C | D | | | |
| | C | D | | | |
| | C | | | | |
| | C | | | | |
| E | C | | | | |
| E | | | | | |
| E | F | | | | |
| | F | | | | |


STEP 3: Expand each event's width
Expand the width for each event until there's a conflict

1 2 3 4 5 6
-------------------------
| A | A | A | | | |
| | | | | | |
| B | | | | | |
| B | C | | | | |
| B | C | D | | | |
| | C | D | | | |
| | C | | | | |
| | C | | | | |
| E | C | | | | |
| E | | | | | |
| E | F | F | | | |
| | F | F | | | |

From there, it should just be a matter of drawing the controls, which I
already have (creates a calendar that is a spittin' image of Outlook!)
 
Back
Top