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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:17:36+00:00 2026-05-11T04:17:36+00:00

Here’s the situation: We have some generic graphics code that we use for one

  • 0

Here’s the situation:

We have some generic graphics code that we use for one of our projects. After doing some clean-up of the code, it seems like something isn’t working anymore (The graphics output looks completely wrong).

I ran a diff against the last version of the code that gave the correct output, and it looks like we changed one of our functions as follows:

static public Rectangle FitRectangleOld(Rectangle rect, Size targetSize) {     if (rect.Width <= 0 || rect.Height <= 0)     {         rect.Width = targetSize.Width;         rect.Height = targetSize.Height;     }     else if (targetSize.Width * rect.Height >          rect.Width * targetSize.Height)     {         rect.Width = rect.Width * targetSize.Height / rect.Height;         rect.Height = targetSize.Height;     }     else     {         rect.Height = rect.Height * targetSize.Width / rect.Width;         rect.Width = targetSize.Width;     }      return rect; } 

to

static public Rectangle FitRectangle(Rectangle rect, Size targetSize) {     if (rect.Width <= 0 || rect.Height <= 0)     {         rect.Width = targetSize.Width;         rect.Height = targetSize.Height;     }     else if (targetSize.Width * rect.Height >               rect.Width * targetSize.Height)     {         rect.Width *= targetSize.Height / rect.Height;         rect.Height = targetSize.Height;     }     else     {         rect.Height *= targetSize.Width / rect.Width;         rect.Width = targetSize.Width;     }      return rect; } 

All of our unit tests are all passing, and nothing in the code has changed except for some syntactic shortcuts. But like I said, the output is wrong. We’ll probably just revert back to the old code, but I’m curious if anyone has any idea what’s going on here.

Thanks.

  • 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. 2026-05-11T04:17:37+00:00Added an answer on May 11, 2026 at 4:17 am

    Sounds like you don’t have sufficient unit tests :]

    Unfortunately, your statement

    ‘Nothing in the code has changed except for some syntactic shortcuts’

    is wrong, and I’m guessing that’s where your problem is. (It’s certainly one of your problems!)

    Yes,

    a *= b; 

    is equivalent to

    a = a * b; 

    but

    a *= b / c; 

    is NOT the same as

    a = a * b / c; 

    instead

    a *= b / c;    // equivalent to a = a * (b / c) a = a * b / c; // equivalent to a = (a * b) / c 

    (See c# operator precedence on msdn)

    I’m guessing you’re running into trouble when your target height is not an exact multiple of the original rectangle height (or the same for the width).

    Then you’d end up with the following sort of situation:

    Let’s assume rect.Size = (8, 20), targetSize = (15, 25)

    Using your original method, you’d arrive at the following calculation:

    rect.Width     = rect.Width * targetSize.Height / rect.Height; //             = 8          * 25                / 20 //             = 200 / 20 (multiplication happens first) //             = 10 // rect.Width  = 10 

    Using your new code, you’d have

    rect.Width    *= targetSize.Height / rect.Height; //            *= 25 / 20 //            *= 1 (it's integer division!) // rect.Width  = rect.Width * 1 //             = 8 // rect.Width  = 8 

    which isn’t the same. (It get’s worse if the target size is less than your original size; in this case the integer division will result in one of the dimensions being 0!)

    If ‘[your] unit tests are all passing’ then you definitely need some additional tests, specifically ones that deal with non-integer multiples.

    Also note that your calculation

    else if(targetSize.Width * rect.Height >          rect.Width * targetSize.Height) 

    isn’t reliable; for very large rectangles, it has the potential to overflow and give you incorrect results. You’d be better off casting to a larger type (i.e. a long) as part of the multiplication. (Again, there should be some unit tests to this effect)

    Hope that helps!

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

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I changed QObject::connect to only connect and it works. So… May 11, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer Your code would animate as expected if you were setting… May 11, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer you might mention that you are using cck, imagefield, filefield… May 11, 2026 at 5:01 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.