Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Compute screen proportion (https://www.delphipraxis.net/169462-compute-screen-proportion.html)

WojTec 21. Jul 2012 13:39

Compute screen proportion
 
I'm just working with images in PS and start think about image proportions. Some images has quite big size and I'm interested what is its proportion. So, I ran Delphi and wrote:

Delphi-Quellcode:
var
  Ratio: Cardinal;
  Min, Max: Cardinal;
begin
  Min := Math.Min(AWidth, AHeight);
  Max := Math.Max(AWidth, AHeight);
  Ratio := Max - Min;

  Result := Round(Max / Ratio) + (Round(Min / Ratio) / 10);
end;
Returns 3:2, 4:3, 5:4, but not 5:3 (result is 2:2), 16:9 (result is 2:1), 16:10 (result is 3:2), 17:10 (result is 2:1). What's wrong?

Namenloser 21. Jul 2012 14:29

AW: Compute screen proportion
 
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.

freeway 21. Jul 2012 16:59

AW: Compute screen proportion
 
not sure, if this method works for each ratio

16:9

9 / 16 = 0,5625
1 - 0,5625 = 0,4375
1 / 0,4375 = 2,2857
16 - 9 = 7
7 * 2,2857 = 15,9999 ~ 16
16 * 0,5625 = 9


5:3

3 / 5 = 0,6
1 - 0,6 = 0,4
1 / 0,4 = 2,5
5 - 3 = 2
2 * 2,5 = 5
5 * 0,6 = 3

Namenloser 21. Jul 2012 17:24

AW: Compute screen proportion
 
Well, take a look at what happens when you fill in your intermediate terms instead of using their results:

(16 - 9) * 1 / (1 - (9 / 16)) = 15,9999 ~ 16
16 * (9 / 16) = 9

So at least the second part will always work, but it’s also completely pointless since you need to know the ratio in advance for it to give the correct result ;)

You really shouldn’t just randomly throw together expressions not knowing what you’re doing...

WojTec 21. Jul 2012 18:43

Re: Compute screen proportion
 
Next attempt:

Delphi-Quellcode:
function ToFrac(const AValue: Extended; const ADivisor: Word; const AFrac: Boolean): string;
var
  A, B: Integer;
  I, F: Extended;
begin
  B := ADivisor;
  I := Int(AValue);
  F := Frac(AValue);
  A := Round(F / (1 / B));

  if (F = 0) or (A = 0) then
  begin
    Result := Format('%.0f', [I]);
    Exit;
  end;

  while (A mod 2 = 0) and (B mod 2 = 0) do
  begin
    A := A div 2;
    B := B div 2;
  end;
  if (A = B) and (A > 0) then
  begin
    I := I + A;
    Result := Format('%.0f', [I])
  end
  else
    Result := Format('%.0f %.0d/%.0d', [I, A, B])
  ;

  if AFrac then
    Result := Format('%.0f/%.0d', [(I * B) + A, B])
  ;
end;
This one allows to get valid value, but need to know divisor. How to compute it? Or maybe there is some existing open sour ce to work with fractions?

freeway 21. Jul 2012 21:25

AW: Compute screen proportion
 
first part of your question, i realy don´t understand ???

work with fractions
http://en.wikipedia.org/wiki/Fraction_(mathematics)

Furtbichler 22. Jul 2012 07:05

AW: Compute screen proportion
 
Do you know the difference between display resolution and aspect ratio? Why do you name your thread 'screen proportion' but you refer to images?
Did you bother using google?
I found all answers.

But what the heck:

Most modern screen resulutions are not exactly 16:9, 4:3 etc.

I would recommend using a list of standard ratios, e.g. (4:3, 5:4, 16:9, 16:10), compare the screen dimension (widht / height) with the entries in the list and return the closest candidate or 'none' if the difference is out of tolerance.

Delphi-Quellcode:
Function AspectRatio (aWidth, aHeight : Integer) : String;
Const
  RatioX : Array [0..3] of integer = (4,5,16,16);
  RatioY : Array [0..3] of integer = (3,4,10, 9);

  tolerance = 1E-2;

Var
  diff,dmin, ratio : Double;
  i,j : Integer;

Begin
  ratio := aWidht/aHeight;
  dmin := 999;
  j:=-1;
  for i:=0 to High(RatioX) do begin
    diff := abs(RatioX[i]/RatioY[i] - ratio);
    if diff < dmin then begin
      j := i;
      dmin := diff;
    end
  end;

  if diff<tolerance then
     result := Format('%d:%d',[RatioX[j], RatioY[j]])
  else
     result := 'nonstandard';
End;

WojTec 22. Jul 2012 08:50

Re: Compute screen proportion
 
Thanks, but for eg. 1024x768 returns 'nonstandard' :(

Furtbichler 22. Jul 2012 11:01

AW: Compute screen proportion
 
The code is untested.

What would you do go make it work?
Do you understand the Code?

Amateurprofi 22. Jul 2012 11:45

AW: Compute screen proportion
 
This should work:

Delphi-Quellcode:
FUNCTION AspectRatio(width,height:Integer):String;
FUNCTION LCD(v1,v2:integer):integer;
begin
   result:=0;
   if (v1>0) and (v2>0) then begin
      repeat
         result:=v1 mod v2;
         v1:=v2;
         v2:=result;
      until v2=0;
      result:=v1;
   end;
end;
var divisor:integer; s:string;
begin
   divisor:=LCD(width,height);
   if divisor=0 then result:='Width or Height <= 0'
      else result:=IntToStr(width div divisor)+':'+IntToStr(height div divisor);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:54 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz