Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a UserControl, which I m placing to a panal Randomly. I want any two conrol should not overlap. Is there any method to get that perticular location have any control or nothing.....

please read my comment...
Posted
Updated 22-Sep-10 9:50am
v2
Comments
himani_jha 22-Sep-10 15:48pm    
I should know details on every pixel. Because later I want to implement capy paste feature by selecting particular region .. So what if I maintain a dictionary for every pixel.. the screen is of 1000*800 approx .. it will reduce calculation of all controls at the time of moving.. but single calculation will be done only of that control.. Here arises time space complexity.. I cant compromise with speed.. So is this method is feasible or not

Hi,

It's possible but it will take a bit of maths.

First, you need to get the controls in the panel.

Panel panel = new Panel();
// add controls
List<Point>[] controlLocations = new List<Point>();
foreach(Control c in p.Controls)
{
  controlLocations.Add(c.Location);
}


That will give you a list of all the locations of the controls in the panel.
Then based on size of controls you might be able to determine if another one already exists in that area of the panel

Dave
 
Share this answer
 
v3
Comments
himani_jha 20-Sep-10 5:09am    
thanks but this is very lenthy procedure there are many non rectangular controls on a panel. this can slow down working of my project. Is there any another way? Or Is there any way to determine which control is there at a particular location?
DavidKiryazi 20-Sep-10 5:28am    
Hi i updated the message to reflect a way to get all the control locations for controls on a particular panell, best I can do :) hope someone else has a better answer
What you're trying to do is not going to be very efficient. As you add a control, you're going to have to check each control already on the form to see if the rectangles intersect, and then every time you move one so it doesn't overlap a given control, you're going to have to make sure that the process of moving the control in questions doesn't overlap any other control.

It may be faster to map the parts of the form that don't have controls and see if the current control can be placed within that area.

Have fun.
 
Share this answer
 
Comments
himani_jha 21-Sep-10 6:28am    
U understand my problem exactly what I want. But ur solution will slow down my application and this is real time application.

When mousemove occurs, we can identify which mouse move arised and what is there on that mouse location. Is there any way to find out what is there on particular loaction. ... Or any other efficient solution is also appreciable... thanx
One solution might be to:

Part 1 - non-rectangular overlap detection

1. Maintain a 1 bit deep bitmap (possibly a System.Drawing.Bitmap object, perhaps something custom) - where each bit represents a pixel in the panel, being set 1 if the area is covered by a control and 0 otherwise.

2. Have a similar (but smaller) 1 bit deep bitmap for the control being moved/added to the panel.

3. As the mouse moves calculate the bitwise AND for the bitmap of the movable control and the area of the panel bitmap 'under' it. As soon as you get a non-zero result, you know you have a collision.

Personally, I would implement this in a custom object, not a System.Drawing.Bitmap object, for performance benefits, although this will require some extra effort to transform a control's image to its detection-bitmap representation.

Note - although the bitmap uses a single bit per panel pixel, for efficiency I might use all 32 bits of an Int32, so one collision = here & there operation compares 32 pixels simultaneously. This will, of course, mean that the bitmap representing the movable control will need its bits shifted up to 32 bits (depending on the current mouse position). Or, just generate 32 distinct bitmaps for the movable control, and pick the one that indexes correctly with the current mouse X position.

Part 2 - bounding rectangle overlap detection

Since Part 1 involves the bitwise AND of x*y/1k Int32s for every mouse-move event (assuming that the 1 bit deep bitmap is stacked to use all the bits in an int), further optimization may be required.

(Note - a 64 pixel * 64 pixel control represented this way would have 2x64 Int32s as its bitmap, meaning for each mouse move event 128 Int32 AND operations, and sundry other actions, will be required).

This optimization would take the form of a bounding-rectangle intersection test on all the controls on the form. Even if you have a huge number of controls, this need not actually inspect each control's position and size:

I would implement an N-ary tree on the controls' X position (Google N-ary tree[^]) so that with a very small number of comparisons you can find the subset of controls whose X position + width overlaps (in part or in whole) the moveable control's surface area.

If you have a really large number of controls, I'd nest another nary tree, on the controls' Y position, inside each node of the X position tree.
 
Share this answer
 
Comments
himani_jha 22-Sep-10 23:13pm    
thanks I think 1st method is my solution.. but I didnt understand one thing "how to use 32 bit of int32" as we will represent each pixel by boolean and bitwise operation can be done on boolean. Can u provide me some more details with code.
Another Question arises what should i use a dictionary<point,boolean> (Here point is every pixel location, and boolean represent collision) or boolean array[x][y] . which one is more efficient?
Chris Trelawny-Ross 23-Sep-10 10:51am    
I think you'll ultimately need to use both parts, but certainly start with part 1 - perhaps it'll be sufficient. Using an Int32 instead of 32 distinct bool values is a memory and performance optimization. I would start using bool and, when you have your algorithm working, change it to 'pack' 32 bools into a single Int32. If I get some time I'll add an answer with sample code. Regarding using a dictionary - I do NOT recommend using a dictionary; an array will be far more efficient - but you may have to experiment with bool [x,y] and bool [x][y] to see which gives better performance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900