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

  • Home
  • SEARCH
  • 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 6935999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:10:36+00:00 2026-05-27T12:10:36+00:00

I have this formula for simple circle collision detection: private bool CircleCollision(Rectangle Circle1, Rectangle

  • 0

I have this formula for simple circle collision detection:

private bool CircleCollision(Rectangle Circle1, Rectangle Circle2)
{
    int X1 = Circle1.Left;
    int Y1 = Circle1.Top;
    int X2 = Circle2.Left;
    int Y2 = Circle2.Top;

    int R1 = Circle1.Width / 2;
    int R2 = Circle2.Width / 2;
    int Radius = R1 + R2;

    int dX = X2 - X1;
    int dY = Y2 - Y1;

    if (Math.Sqrt((dX * dX) + (dY * dY)) <= Math.Sqrt(Radius * Radius)) 
        return true; 
    else 
        return false;
}

But it just expose detection whenever the two circles have same radius. What am I doing wrong?

Solved

int X1 = Circle1.Left + (Circle1.Width / 2);
int Y1 = Circle1.Top + (Circle1.Height / 2);
int X2 = Circle2.Left + (Circle2.Width / 2);
int Y2 = Circle2.Top + (Circle2.Height / 2);
  • 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-27T12:10:37+00:00Added an answer on May 27, 2026 at 12:10 pm

    To check if two circles overlap you can do:

    var radius=circle1.Radius+circle2.Radius;
    var deltaX=circle1.CenterX-circle2.CenterX;
    var deltaY=circle1.CenterY-circle2.CenterY;
    return deltaX*deltaX + deltaY*deltaY <= radius*radius;
    

    Note that I’m calculating the distance of the centers, not of the top left corners. I’m also comparing with the squared radius so I don’t need to use the expensive Math.Sqrt function, but that doesn’t affect correctness.

    Your code doesn’t work because you use Left and Top instead of the position of the center. The difference between the top-left corners is the same as the difference between the centers if the radius is the same. This explains why your code only works in that special case.

    Not sure why you use a rectangle to represent a circle. You can calculate the center as centerX = 0.5*(Left+Right). You should also add a check that Width==Height, else you might get an ellipse as parameter, and then this algorithm won’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this formula in a function below. It's a fairly simple concept, yet
I have a calculated field in a list with this formula: =CID & -
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
this seems like such a simple problem but I have been unable to find
What's more efficient? decimal value1, value2, formula This: for(int i = 0; i>1000000000000; i++);
I have a feeling that this question is very simple, but I just can't
This should be a simple formula but I can not figure it out. I
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM

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.