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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.
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
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With