Einzelnen Beitrag anzeigen

Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#2

AW: Compute screen proportion

  Alt 21. Jul 2012, 15:29
It does not work because it does not make any sense.

The ratio X:Y means nothing but the fraction X/Y (“X divided by Y”). You get the ratio by doing this division.

If we do this on a computer, however, the result is naturally going to be a decimal number, e.g. 1.3333[…] for 400:300. Of course we are not happy with this, because what we wanted was a proper fraction.

So we have to go back to fraction arithmetic and do it by hand: How would we tackle 400/300 if we were in 6th grade and not allowed to use decimals? Well, of course we would cancel the fraction. The way we do this is, we find the greatest common divisor, and then divide both sides of the fraction by it. In the case of 400/300, the greatest common divisor is 100, so we end up with our result of 4:3.

Now you only have to translate this into code and you’re done. The only somewhat hard part is to find the greatest common divisor, but luckily you’ll find plenty of algorithms along with their implementations for that on the web.

Geändert von Namenloser (21. Jul 2012 um 15:33 Uhr)
  Mit Zitat antworten Zitat