TRectの Left/Right Top/Bottom の並びを正しくする
104 RectNormalize 動作確認 Delphi2007 更新日 2010/07/06(火)

TRectはメンバーにLeft/Right/Top/Bottomを持ちます
これは、通常は
Leftが小さい値で、Rightが大きい値
Topが小さい値で、Bottomが大きい値

と、思いこんでしまいがちですが
そんな制限をされているわけではありません。

ですので、
正しくLeftとTopが小さくRightとBottomが大きくなるために
次のような関数を作りました。

────────────────────
function RectNormalization(Value: TRect): TRect;

    function Min(A, B: Integer): Integer;
    begin
      if A <= B then Result := A else Result := B;
    end;
    function Max(A, B: Integer): Integer;
    begin
      if A <= B then Result := B else Result := A;
    end;

begin
  Result := Rect(
    Min(Value.Left, Value.Right),
    Min(Value.Top,  Value.Bottom),
    Max(Value.Left, Value.Right),
    Max(Value.Top,  Value.Bottom) );
end;
────────────────────
MinとMaxはMath.pasに含まれるので
上記のように関数内関数で実装するのではなく
    uses Math
を追加してもよいでしょう。