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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:49:56+00:00 2026-05-29T16:49:56+00:00

I have an enumeration of just under 32 absolute rectangle sizes and I need

  • 0

I have an enumeration of just under 32 absolute rectangle sizes and I need to given dimensions and find the best approximation among my enumeration.

Is there any better (ie more readable and maintainable) way than the spaghetti code I am formulating out of lots of nested if‘s and else‘s?

At the moment I have just:

enum imgOptsScale {
    //Some relative scales
    w005h005 = 0x8,
    w010h010 = 0x9,
    w020h020 = 0xA,
    w040h040 = 0xB,
    w070h070 = 0xC,
    w100h100 = 0xD,
    w150h150 = 0xE,
    w200h200 = 0xF,
    w320h320 = 0x10,
    w450h450 = 0x11,
    w200h010 = 0x12,
    w200h020 = 0x13,
    w200h070 = 0x14,
    w010h200 = 0x15,
    w020h200 = 0x16,
    w070h200 = 0x17
};
imgOptsScale getClosestSizeTo(int width, int height);

and I thought I’d ask for help before I got too much further into coding up. I should emphasise a bias away from too elaborate libraries though I am more interested in algorithms than containers this is supposed to run on a resource constrained system.

  • 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-29T16:50:00+00:00Added an answer on May 29, 2026 at 4:50 pm

    I think I’d approach this with a few arrays of structs, one for horizontal measures and one for vertical measures.

    Read through the arrays to find the next larger size, and return the corresponding key. Build the final box measure from the two keys. (Since 32 only allows 5 bits, this is probably not very ideal — you’d probably want 2.5 bits for the horizontal and 2.5 bits for the vertical, but my simple approach here requires 6 bits — 3 for horizontal and 3 for vertical. You can remove half the elements from one of the lists (and maybe adjust the << 3 as well) if you’re fine with one of the dimensions having fewer degrees of freedom. If you want both dimensions to be better represented, this will probably require enough re-working that this approach might not be suitable.)

    Untested pseudo-code:

    struct dimen {
        int x;
        int key;
    }
    
    struct dimen horizontal[] = { { .x = 10, .key = 0 },
                                  { .x = 20, .key = 1 },
                                  { .x = 50, .key = 2 },
                                  { .x = 90, .key = 3 },
                                  { .x = 120, .key = 4 },
                                  { .x = 200, .key = 5 },
                                  { .x = 300, .key = 6 },
                                  { .x = 10000, .key = 7 }};
    
    struct dimen vertical[] = { { .x = 10, .key = 0 },
                               { .x = 20, .key = 1 },
                               { .x = 50, .key = 2 },
                               { .x = 90, .key = 3 },
                               { .x = 120, .key = 4 },
                               { .x = 200, .key = 5 },
                               { .x = 300, .key = 6 },
                               { .x = 10000, .key = 7 }};
    
    /* returns 0-63 as written */
    int getClosestSizeTo(int width, int height) {
        int horizontal_key = find_just_larger(horizontal, width);
        int vertical_key = find_just_larger(vertical, height);
        return (horizontal_kee << 3) & vertical_key;
    }
    
    int find_just_larger(struct dimen* d, size) {
        int ret = d.key;
        while(d.x < size) {
            d++;
            ret = d.key;
        }
        return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a enumeration for elements in a JTree When I find some specific
I have an enumeration where I need to display the values as localized strings.
I have enumeration in JAVA code ERequestTypes my enumeration contains more then 20 elements,
I have this enumeration: public enum Translation { English=1, Russian, French, German } I
I have this enumeration: public enum SymbolPair { AUDJPY = 0, AUDNZD, AUDUSD, CADJPY,
I have an enumeration like Enum Complexity { NotSoComplex, LittleComplex, Complex, VeryComplex } And
I have an enumeration for my Things like so: public enum Things { OneThing,
I have a Enumeration as shown in below program public class Test { public
So, I have an enumeration that is as follows: +---------+ |TypeCours| | Enum |
I have registered an enumeration type ClefType within my header file - this enum

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.