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 7706995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:14:58+00:00 2026-06-01T00:14:58+00:00

Lets say I have an XNA Rectangle with X = 0, Y = 0,

  • 0

Lets say I have an XNA Rectangle with

X = 0, Y = 0, Width = 25, Height = 25.

What I want is a method to clamp another rectangle inside, so that the smaller rectangle never ‘leaves’ the bounds of the larger one.

For example, if I make a new Rectangle with:

X = 23, Y = 20, Width = 10, Height = 10,

I want the method to return a Rectangle with:

X = 23, Y = 20, Width = 2, Height = 5.

Also, if I make a new Rectangle with:

X = -8, Y = 8, Width = 10, = 10,

I want the method to return a Rectangle with:

X = 0, Y = 8, Width = 2, Height = 10.

In the meanwhile I got the clamping to work. Still, its a bulk of ugly code, so the new question is, how can I simplify this?

if (SelectionRectangle.X < 0)
        {
            SelectionRectangle.Width = SelectionRectangle.Width + SelectionRectangle.X;
            SelectionRectangle.X = 0;
        }
        if (SelectionRectangle.X + SelectionRectangle.Width > tileset.Width)
        {
            if (SelectionRectangle.X > tileset.Width)
            {
                SelectionRectangle.X = tileset.Width;
                SelectionRectangle.Width = 0;
            }
            else
            {
                SelectionRectangle.Width = tileset.Width - SelectionRectangle.X;
            }
        }
        if (SelectionRectangle.Y < 0)
        {
            SelectionRectangle.Height = SelectionRectangle.Height + SelectionRectangle.Y;
            SelectionRectangle.Y = 0;
        }
        if (SelectionRectangle.Y + SelectionRectangle.Height > tileset.Height)
        {
            if (SelectionRectangle.Y > tileset.Height)
            {
                SelectionRectangle.Y = tileset.Height;
                SelectionRectangle.Height = 0;
            }
            else
            {
                SelectionRectangle.Height = tileset.Height - SelectionRectangle.Y;
            }
        }
  • 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-06-01T00:14:59+00:00Added an answer on June 1, 2026 at 12:14 am

    Isn’t this just Rectangle.Intersect?

    Creates a Rectangle defining the area where one rectangle overlaps another rectangle.

    If the smaller rectangle is entirely within the larger rectangle, then the intersection will be the same as the smaller rectangle. If the smaller rectangle extends outside of the larger rectangle, that portion will not be returned.

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

Sidebar

Related Questions

Lets say I have a single object of type Car which I want to
Lets say I have a form: <form id=form1 method=post> <ul> <li class=button> Button 1!
Lets say i have created a small game with XNA. Now i can change
Let’s say that I have two C# applications - game.exe (XNA, needs to support
Lets say i have a rectangle Now i wish to apply it transformations such
Lets say I have a link to svn://another/repo/tags/1.0 and the author has updated the
Lets say I have heap allocated A* , which I want to pass as
Lets say you have website that keeps balance for each user. You give each
Lets say I have some binary value: 0b100 and want to convert it to
Lets say I have a class that is doing something like: public class Foo

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.