AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Compute screen proportion

Compute screen proportion

Ein Thema von WojTec · begonnen am 21. Jul 2012 · letzter Beitrag vom 24. Jul 2012
Antwort Antwort
Seite 1 von 2  1 2   
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Compute screen proportion

  Alt 21. Jul 2012, 14:39
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?
  Mit Zitat antworten Zitat
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
freeway

Registriert seit: 11. Jul 2009
57 Beiträge
 
Delphi XE Professional
 
#3

AW: Compute screen proportion

  Alt 21. Jul 2012, 17:59
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

Geändert von freeway (21. Jul 2012 um 18:03 Uhr)
  Mit Zitat antworten Zitat
Namenloser

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

AW: Compute screen proportion

  Alt 21. Jul 2012, 18:24
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...
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Compute screen proportion

  Alt 21. Jul 2012, 19:43
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?
  Mit Zitat antworten Zitat
freeway

Registriert seit: 11. Jul 2009
57 Beiträge
 
Delphi XE Professional
 
#6

AW: Compute screen proportion

  Alt 21. Jul 2012, 22:25
first part of your question, i realy don´t understand ???

work with fractions
http://en.wikipedia.org/wiki/Fraction_(mathematics)
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#7

AW: Compute screen proportion

  Alt 22. Jul 2012, 08:05
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;
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#8

Re: Compute screen proportion

  Alt 22. Jul 2012, 09:50
Thanks, but for eg. 1024x768 returns 'nonstandard'
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#9

AW: Compute screen proportion

  Alt 22. Jul 2012, 12:01
The code is untested.

What would you do go make it work?
Do you understand the Code?
  Mit Zitat antworten Zitat
Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.039 Beiträge
 
Delphi XE2 Professional
 
#10

AW: Compute screen proportion

  Alt 22. Jul 2012, 12:45
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;
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2   

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:03 Uhr.
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