If checking the intersection of two rectangles is done like this:

ERect.IntersectsWith(EchRect); 

And setting a new position for a rectangle like this:

 ERect.Location = new Point(Canvas.GetLeft(bubble), Canvas.GetTop(bubble)); 

How to do a similar operation for two ellipses?

1 answer 1

Take 2 center rectangles (x1, y1) и (x2, y2) . Find the tangent of the angle between them.

 tangens = (y2-y1)/(x2-x1) 

By tangent determine the radius, which has an ellipse at a given angle. To do this, calculate the cosine and sine of the angle.

 cosinus = sqrt( 1 / (tangens*tangens + 1)) sinus = sqrt( 1 - cosinus*cosinus ) 

The cosine modulus varies from 0 at the point (x1, h1-y1) to 1 at the point (w1-x1, y1) . The radius changes from y1top-y1 to x1right-x1 , where h1 is the height of rectangle 1, and w1 is the length of rectangle 1.

But the radius does not change linearly, but along an elliptic curve, and it can be calculated using the formula:

 radius1 = abs(w1-x1)*abs(h1-y1) / sqrt( (h1-y1)*(h1-y1)*cosinus*cosinus + (w1-x1)*(w1-x1)*sinus*sinus ) radius2 = abs(w2-x2)*abs(h2-y2) / sqrt( (h2-y2)*(h2-y2)*cosinus*cosinus + (w2-x2)*(w2-x2)*sinus*sinus ) 

where radius1 is the radius of the first rectangle, radius2 is the radius of the second rectangle.

Calculate the distance between the centers:

 rasstoyanie = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) 

If the rasstoyanie less than or equal to the sum of radius1+radius2 , then the ellipses intersect. If the rasstoyanie greater than the sum of radius1+radius2 , then the ellipses do not intersect.

  • Thanks for the tip! In my case, it turned out even a little bit simpler. - Viewed