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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:50:32+00:00 2026-05-24T09:50:32+00:00

I wrote both of these for correcting rectangles. For example, if a rectangle has

  • 0

I wrote both of these for correcting rectangles. For example, if a rectangle has these properties:

X:32, Y:32, Width:-32, Height:-32

This will make the rectangle X:0, Y:0, Width:32, Height:32

if (r.Width < 0)
{
    r.X -= Math.Abs(r.Width);
    r.Width = Math.Abs(r.Width);
}
if (r.Height < 0)
{
    r.Y -= Math.Abs(r.Height);
    r.Height = Math.Abs(r.Height);
}

Here is #2

r.X -= Math.Abs(Math.Min(0, r.Width));
r.Width = Math.Abs(r.Width);

r.Y -= Math.Abs(Math.Min(0, r.Height));
r.Height = Math.Abs(r.Height);

They both work just fine. My question is which? I feel like the first may be faster and more readable but the second has less lines of code. Also, I feel like this is something that may have been invented already. Is there a method that does this already in the .NET or XNA Framework?

Thanks!!

Edit: Someone posted a shortened version of the second way which I really like and combined it into this:

r = new Rectangle(r.X + Math.Min(0, r.Width), r.Y + Math.Min(0, r.Height), Math.Abs(r.Width), Math.Abs(r.Height));

Honestly I prefer less lines to more and I don’t think this is too complex. If anything I can place a comment or encapsulate it in to a method with a descriptive name.

  • 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-24T09:50:32+00:00Added an answer on May 24, 2026 at 9:50 am

    If you know r.Width and r.Height are < 0 why do you need to Abs on them? Remove that and it will be even faster, and simpler.

    if (r.Width < 0)
    {
        r.X += r.Width;
        r.Width = -r.Width;
    }
    if (r.Height < 0)
    {
        r.Y += r.Height;
        r.Height = -r.Height;
    }
    

    IMHO the second way you’ve written of doing this is just plain ugly. You don’t look at it and immediately realise what it’s doing. Remember KISS.

    EDIT: I understand you like your code in the fewest lines possible so I re-engineered the above:

    if (r.Width < 0) { r.X += r.Width; r.Width = -r.Width; } if (r.Height < 0) { r.Y += Height; r.Height = -r.Height; }
    

    Just as simple – and all in one line of code! It’s even less characters on the line than your alternative….

    Line count is generally not a good measure of programming beauty – but of course it’s your code so you’ve got to go with what brings you the most joy.

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

Sidebar

Related Questions

I'm trying to write a python regular expression that will match both these URLs:
I get a conflict with ++ and -- in bison. I wrote these two
When I initially wrote this script, the site design did not require the execution
I wrote these two classes that simple encrypt and decrypt strings: Encode.php class Encode
We have an application wrote in C#, which broken into several projects. These projects
i have an exe loading a dll. i wrote both. i am dllexporting a
I wrote a simple webform that will take the user's information (via POST) and
I wrote a page 'captcha.php' to generate a math captcha and send the result
Background: I need to create an NDIS filter intermediate driver. This driver will pass
Both my Rails model and controller code need to write files to the file

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.