In general, I have a program on which there are two rectangle , when we mouse_down ( mouse_down ) the first rectangle we fix the coordinates of the mouse, while moving ( mouse_move ) we update the coordinates of the rectangle and start using FindVisualChildren<Rectangle>(childUI) and FillContainsWithDetail to check cyclically that there is an intersection if the intersection catches something from the second (on which it is superimposed) we change the Rectangle color, and if we release it at the intersection, we apply the first rectangle to the second and change the color of the first.

So, if I have 200 rectangles then the program will crash because of my cycle, tell me how you could optimize all this?

 public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } } 

enter image description here

    1 answer 1

    And why do you need to call FindVisualChildren at each iteration? Seems to me enough

    1. Have and keep up to date a list of all the rectangles
    2. When moving an element, check the intersection with each list.

    With this approach, FindVisualChildren not needed.

    • Do you think it will be faster? - alex-rudenkiy
    • @ alex-rudenkiy: Where does it go? :) Unless you somewhere else are not doing something else slow. - VladD
    • I'll check it out well in the evening alex - alex-rudenkiy