Recent

Author Topic: Probably more a math problem, but working out how - [SOLVED]  (Read 429 times)

zxandris

  • Full Member
  • ***
  • Posts: 141
Probably more a math problem, but working out how - [SOLVED]
« on: January 25, 2025, 09:05:33 pm »
Hi there, I'm resizing images, or I have a dialog that does.

I would like to work out when setting say the width how to then proportionally set the height to keep the aspect.

I'm using this code

Code: Pascal  [Select][+][-]
  1.  
  2. function ProportionalSize(phisicalsize,newsize:tpoint):Tpoint;
  3. var
  4.   Percent      : single;
  5.   tempx,tempy  : Single;
  6.   fs:tpoint;
  7. begin
  8.   if phisicalsize.x<>0 then
  9.     tempx:=((newsize.x)/phisicalsize.x)*100
  10.   else tempx:=0;
  11.   If Phisicalsize.y<>0 then
  12.     tempy:=((newsize.y)/phisicalsize.y)*100
  13.   else Tempy:=0;
  14.   if tempx<=tempy then
  15.     percent:=tempx
  16.   else
  17.     percent:=tempy;
  18.   fs.x:=trunc((phisicalsize.x/100)*percent);
  19.   fs.y:=trunc((phisicalsize.y/100)*percent);
  20.   result:=fs;
  21. end;
  22.  
  23.  
  24. // Then this to work out the height when changing the width.
  25.  
  26. procedure TfrmDLGImageResize.seWidthChange(Sender: TObject);
  27. var
  28.    os, ns, ps : TPoint;
  29. begin
  30.       if FNoChange then exit;
  31.       os.x := intImageWidth;
  32.       os.y := intImageHeight;
  33.       ns.x := intImageWidth;
  34.       ns.y := seHeight.value;
  35.       FNoChange := true;
  36.       ps := ProportionalSize(os, ns);
  37.       seHeight.value := ps.y;
  38.       FNoChange := false;
  39. end;
  40.  
  41.  

It doesn't rightly work, I'm curious if anyone is willing to lend their math aid.  I'm afaid I am really quite rotten at it.  I post this here since it's for bgra.Resample.  I just wanted the dialog that set's the sizes to be able to work out the say height proportional to the width when setting the value.

Please, any help with this would be marvellous, if this is really in the wrong place I do humbly apologise.

CJ
« Last Edit: January 26, 2025, 04:26:03 am by zxandris »

Josh

  • Hero Member
  • *****
  • Posts: 1361
hi

Not tested but maybe

If you need prop width
NewWidth:=NewHeight*(OrginalWidth/OriginalHeight);
or for height
NewHeight:=NewWidth*(OriginalHeight/OriginalWidth);

Non tested, done on the fly

Code: Pascal  [Select][+][-]
  1. function propSize(Var OriginalWH,NewWH:tpoint):Boolean;
  2. begin
  3.   //sanity checks
  4.   If (OriginalWH.X<1) or (OriginalWH.Y<1) then Exit(False); // invalid exit
  5.   If (NewWH.X=0) and (NewWH.Y=0) then Exit(False) // no values exit
  6.   If NewWH.X>0 then
  7.   begin
  8.      //calculate new height
  9.     NewWH.Y:=trunc(NewWH.X*(OriginalWH.Y/OriginalWH.X));
  10.   end
  11.   else
  12.   begin
  13.     //calculate new width
  14.      NewWH.X:= trunc(NewWH.Y:*(OriginalWH.X/OriginalWH.Y));
  15.   end;
  16.   Result:=True;
  17. end;

to use the Var sent to NewWH, only one of the x or y must hold the new width/height the other must be set to 0;
« Last Edit: January 25, 2025, 10:07:49 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

zxandris

  • Full Member
  • ***
  • Posts: 141
That appears to work like a charm, thank you VERY much!

hi

Not tested but maybe

If you need prop width
NewWidth:=NewHeight*(OrginalWidth/OriginalHeight);
or for height
NewHeight:=NewWidth*(OriginalHeight/OriginalWidth);

Non tested, done on the fly

Code: Pascal  [Select][+][-]
  1. function propSize(Var OriginalWH,NewWH:tpoint):Boolean;
  2. begin
  3.   //sanity checks
  4.   If (OriginalWH.X<1) or (OriginalWH.Y<1) then Exit(False); // invalid exit
  5.   If (NewWH.X=0) and (NewWH.Y=0) then Exit(False) // no values exit
  6.   If NewWH.X>0 then
  7.   begin
  8.      //calculate new height
  9.     NewWH.Y:=trunc(NewWH.X*(OriginalWH.Y/OriginalWH.X));
  10.   end
  11.   else
  12.   begin
  13.     //calculate new width
  14.      NewWH.X:= trunc(NewWH.Y:*(OriginalWH.X/OriginalWH.Y));
  15.   end;
  16.   Result:=True;
  17. end;

to use the Var sent to NewWH, only one of the x or y must hold the new width/height the other must be set to 0;

 

TinyPortal © 2005-2018