Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Rectangles
- {
- public static class RectanglesTask
- {
- public static Rectangle Left_r, Right_r, Top_r, Bottom_r;
- // Пересекаются ли два прямоугольника (пересечение только по границе также считается пересечением)
- public static bool AreIntersected(Rectangle r1, Rectangle r2)
- {
- PositionOfRectangles(r1,r2);
- return !(Left_r.Top > Right_r.Bottom || Left_r.Bottom < Right_r.Top || Left_r.Right < Right_r.Left);
- }
- // Площадь пересечения прямоугольников
- public static int IntersectionSquare(Rectangle r1, Rectangle r2)
- {
- int intersection_height = Top_r.Bottom - Bottom_r.Bottom > 0 ?
- Top_r.Height - Bottom_r.Top + Top_r.Top - Top_r.Bottom + Bottom_r.Bottom :
- Top_r.Height - Bottom_r.Top + Top_r.Top;
- int intersection_width = Left_r.Right - Right_r.Right > 0 ?
- Left_r.Width - Right_r.Left + Left_r.Left - Left_r.Right + Right_r.Right :
- Left_r.Width - Right_r.Left + Left_r.Left;
- if (intersection_height > 0 && intersection_width > 0) return intersection_height * intersection_width;
- return 0;
- }
- // Если один из прямоугольников целиком находится внутри другого — вернуть номер (с нуля) внутреннего.
- // Иначе вернуть -1
- // Если прямоугольники совпадают, можно вернуть номер любого из них.
- public static int IndexOfInnerRectangle(Rectangle r1, Rectangle r2)
- {
- if (CheckForAccessories(r1, r2)) return 1;
- if (CheckForAccessories(r2, r1)) return 0;
- return -1;
- }
- public static void PositionOfRectangles(Rectangle r1, Rectangle r2)
- {
- if (r1.Left <= r2.Left) { Left_r = r1; Right_r = r2; }
- else { Left_r = r2; Right_r = r1; }
- if (r1.Top <= r2.Top) { Top_r = r1; Bottom_r = r2; }
- else { Top_r = r2; Bottom_r = r1; }
- }
- public static bool CheckForAccessories(Rectangle r1, Rectangle r2)
- {
- return (r1.Left <= r2.Left && r2.Left <= r1.Right) &&
- (r1.Right >= r2.Right) &&
- (r1.Top <= r2.Top && r1.Bottom >= r2.Top) &&
- (r1.Bottom >= r2.Bottom);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement