I have an array containing a list of “realbasic.rect” objects , these objects are static
there is also one other “rect” that will be dragged around by the mouse
What I need to do is compare the corners and center (horizontal,vertical) of the dynamic rect to every other rect, but in the quickest way possible
if any of the horizontal values (left.x, center.x or right.x) are equal (abs(diff)<1) then I need to draw a line between those points…
the top of the dynamic rect will compare to the bottom of any static rect
similar needs to be done for the left side, right side and bottom
The algorithm needs to loop thru all the static rectangles, and skip any side the obviously can’t happen…
for instance if the right side of Rect-A is less than the left side of Rect-B then top/bottom checks can be skipped over
Think alignment guides in an IDE
I assume the key part is ‘quickly’ as I know the brute force method is easily within your grasp.
If instead of using an array, you store in a database table, and have the left, right, top and bottom values held as fields
(rather than left, top, width and height)
After any movement of the rect, you can select from the table where
therect.left = record.left or therect.right = record.right
and again for
therect.top= record.top or therect.top= record.bottom
I havent coded it up to test the speed, but what do you think?
I would use three dictionaries of the static left, center, right values as key and the rect as value.
If dictLeft.HasKey then
Dim rec as Rect = dictLeft.Value
DrawGuide( me.left, me.top, rec.left, rec.top )
or circumventing the problem of there being more than one Rect to align
If dictLeft.HasKey then
DrawGuide( me.left, 0, me.left, canvas.height )
PseudoCode, not tried.
well I have actually considered both (or similar) methods in my own train of thought.
Part of the problem is the values will not be “exact”… they will be if the difference is <1 as they are DOUBLES not INTEGERS
Also its not matching just LEFT to LEFT, but LEFT to LEFT, LEFT to MIDDLE and/or LEFT to RIGHT
do basically any EDGE (or center) matching any other EDGE (or center)
And yet the dynamic rectangle “could” have vertices that align with multiple vertices of multpile rectangles
[quote=363895:@Dave S]well I have actually considered both (or similar) methods in my own train of thought.
Part of the problem is the values will not be “exact”… they will be if the difference is <1 as they are DOUBLES not INTEGERS[/quote]
But as you compare within a certain spread anyway you can just use the integer values for your dictionary. That would be “exact enough” to say it’s time for a guide to be drawn.
[quote]Also its not matching just LEFT to LEFT, but LEFT to LEFT, LEFT to MIDDLE and/or LEFT to RIGHT
do basically any EDGE (or center) matching any other EDGE (or center)[/quote]
I did assume that you know that you can compare the left, center, and right value to any of the dictionaries, I just didn’t see the point of writing very similar code several times
So? It’s up to you how many guide lines you want to have. And if it lines up on the left with ten others then you would still only need one guide as the fractional difference in lines is too small to be seen and acted upon by the user anyway. However you might want to highlight the other rects that you align with.