12

I am relatively new to programming. I need to calculate the aspect ratio(16:9 or 4:3) from a given dimension say axb. How can I achieve this using C#. Any help would be deeply appreciated.

public string AspectRatio(int x, int y)
{
 //code am looking for
 return ratio
}

Thanks.

user1321391
  • 123
  • 1
  • 1
  • 5

4 Answers4

15

You need to find Greatest Common Divisor, and divide both x and y by it.

static int GCD(int a, int b)
{
    int Remainder;

    while( b != 0 )
    {
        Remainder = a % b;
        a = b;
        b = Remainder;
    }

    return a;
}

return string.Format("{0}:{1}",x/GCD(x,y), y/GCD(x,y));

PS

If you want it to handle something like 16:10 (which can be divided by two, 8:5 will be returned using method above) you need to have a table of predefined ((float)x)/y-aspect ratio pairs

Pavel Krymets
  • 6,253
  • 1
  • 22
  • 35
6

Since you only need to decide between 16:9 and 4:3, here's a much simpler solution.

public string AspectRatio(int x, int y)
{
    double value = (double)x / y;
    if (value > 1.7)
        return "16:9";
    else
        return "4:3";
}
Jack
  • 5,680
  • 10
  • 49
  • 74
5

There're only several standard ratios like: 4:3, 5:4, 16:10, 16:9. GCD is a good idea, but it will fail for at least 16:10 ratios and 1366x768 resolution.

Pure GCD algorithm will get 683:384 for 1366x768, cause 683 is a prime, while resolution is almost 16:9 (16.0078125).

I suppose, that for real tasks, one will need to implement rather complicated algorithm:

First try known aspect ratios (look for them at wikipedia), allowing some errors and only then use GCD as fallback.

Don't forget about 32:10 ;-)

kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • Thanks a lot. Yes there are few exceptional cases like you mentioned – user1321391 Apr 09 '12 at 09:35
  • @kirilloid I've been using GCD and am looking for insight on those "rather complicated algorithms". Do you know where to start for something more thorough? – Ben Racicot Jan 14 '21 at 14:59
  • @TR3B find the best approximation with small (<=20) denominator and then use additional custom mappings like 8:5 is actually 16:10 or 39:18 is actually 19.5:9 (some modern smartphones). Google various screen resolutions, read on wikipedia, to get insights. – kirilloid Jan 19 '21 at 18:52
  • So it all comes down to this... Why are we all talking about calculating AR when you can't cover all the specifications without mapping? – Ben Racicot Jan 19 '21 at 22:12
  • Because there is much fewer aspect ratios than resolutions. And even fewer exceptions. Good luck enumerating all exceptions, esp. if you are looking for dp (device-independent pixels), with custom settings—they are just non-GCD-able. – kirilloid Jan 21 '21 at 08:36
  • 1
    I think that aged well with introduction of long smartphones with 19:9, 19.5:9, 20:9 etc – kirilloid Jan 01 '22 at 01:46
1

You need to find the GCD (http://en.wikipedia.org/wiki/Greatest_common_divisor) and then:

return x/GCD + ":" + y/GCD;
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94