Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:53:05+00:00 2026-05-27T06:53:05+00:00

Given two simple, rectangles: class Rectangle { int x; int y; int width; int

  • 0

Given two simple, rectangles:

class Rectangle
{
    int x;
    int y;
    int width;
    int height;
}

Rectangle a;
Rectangle b;

and the following enumeration:

[Flags]
enum Edges
{
    None,
    Top,
    Bottom,
    Left,
    Right,
    Inside,
}

What is the quickest way to detect the edges on rectangle a which are collided with by rectangle b?

Edges e = EdgeDetect(a, b);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:53:05+00:00Added an answer on May 27, 2026 at 6:53 am

    First of all, you have to defines explicitly values of your enum in order to have flags working correctly. In you case Left == Top + Bottom + None. Here is a possible declaration :

    [Flags]
    public enum Edges
    {
        None = 0,
        Top = 1,
        Bottom = 2,
        Left = 4,
        Right = 8,
        Identical = Top + Bottom + Left + Right,
        Inside = 16,
        Covers = 32
    }
    

    Next, a possible implementation of edge collision detection. Note that I use the builtin System.Drawing.Rectangle instead of rewriting the class. The immediate advantage is the availability of the Intersect method. :

    public static Edges DetectEdgesCollision(Rectangle a, Rectangle b)
    {
        var result = Edges.None;
    
        if (a == b) return Edges.Identical;
        b.Intersect(a);
        if (b.IsEmpty) return Edges.None;
        if (a == b) return Edges.Covers;
    
    
        if (a.Top == b.Top && (a.Right >= b.Right && a.Left<=b.Left )) 
            result |= Edges.Top;
        if (a.Bottom == b.Bottom && (a.Right >= b.Right && a.Left<=b.Left ))
            result |= Edges.Bottom;
        if (a.Left == b.Left && (a.Bottom >= b.Bottom && a.Top <= b.Top)) 
            result |= Edges.Left;
        if (a.Right == b.Right && (a.Bottom >= b.Bottom && a.Top <= b.Top)) 
            result |= Edges.Right;
    
    
        return result == Edges.None ? Edges.Inside : result;
    }
    

    Here is a set of tests that validates this implementation :

        [TestMethod]
        public void RectDoesNotIntersect()
        {
            var a = new Rectangle(0, 0, 10, 10);
            var b = new Rectangle(20, 20, 10, 10);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.None, result);
        }
    
    
        [TestMethod]
        public void RectAreNested()
        {
            var a = new Rectangle(0, 0, 30,30);
            var b = new Rectangle(10, 10, 10, 10);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.Inside, result);
        }      
        [TestMethod]
        public void RectCollidesOnTopAndLeft()
        {
            var a = new Rectangle(10, 10, 10, 10);
            var b = new Rectangle(0, 0, 10, 10);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.Left | Edges.Top, result);
        }     
        [TestMethod]
        public void RectCollidesOnBottom()
        {
            var a = new Rectangle(0, 0, 20, 20);
            var b = new Rectangle(10, 10, 5, 50);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.Bottom , result);
        }        
    
        [TestMethod]
        public void RectAreIdenticals()
        {
            var a = new Rectangle(10, 10, 10, 10);
            var b = new Rectangle(10, 10, 10, 10);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.Identical, result);
        }  
        [TestMethod]
        public void RectBCoversA()
        {
            var a = new Rectangle(10, 10, 10, 10);
            var b = new Rectangle(0, 0, 30, 30);
    
            var result = Program.DetectEdgesCollision(a, b);
    
            Assert.AreEqual<Edges>(Edges.Covers, result);
        }     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a simple class MyClass with a constructor that accepts two int , how
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
I've been given the task to create a simple Silverlight chat box for two
Given two simple comboboxes I am able to drop down and select items but
I have two simple domains: public class Hotel { static searchable = true Source
if I have two simple models: class Tag(models.Model): name = models.CharField(max_length=100) class Post(models.Model): title
Given a simple case of two tables - Term and Definition - where Term
anyone came up with simple solutions? question: Given the two model classes with a
I'm looking for a simple way to calculate the difference between two rectangles. I
Here's a simple problem - given two urls, is there some built-in method, or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.