From 79e6ed504729a96cf6e875342976f33baa605377 Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Sun, 14 Jun 2009 14:12:09 +0000 Subject: [PATCH] * fixed CenterPoint() function (patch by Alexander S. Klenin, mantis #13972) git-svn-id: trunk@13274 - --- .gitattributes | 1 + rtl/objpas/types.pp | 17 ++++++++++++----- tests/webtbs/tw13872.pp | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/webtbs/tw13872.pp diff --git a/.gitattributes b/.gitattributes index 1f10011fea..3a509264fd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9157,6 +9157,7 @@ tests/webtbs/tw1376.pp svneol=native#text/plain tests/webtbs/tw13763.pp svneol=native#text/plain tests/webtbs/tw13813.pp svneol=native#text/plain tests/webtbs/tw13820.pp svneol=native#text/plain +tests/webtbs/tw13872.pp svneol=native#text/plain tests/webtbs/tw13890.pp svneol=native#text/plain tests/webtbs/tw13948.pp svneol=native#text/plain tests/webtbs/tw1398.pp svneol=native#text/plain diff --git a/rtl/objpas/types.pp b/rtl/objpas/types.pp index b3bdac65d8..6c4836971a 100644 --- a/rtl/objpas/types.pp +++ b/rtl/objpas/types.pp @@ -406,13 +406,20 @@ begin OffsetRect:=false; end; -function CenterPoint(const Rect: TRect): TPoint; - +function Avg(a, b: Longint): Longint; begin - With Rect do + if a < b then + Result := a + ((b - a) shr 1) + else + Result := b + ((a - b) shr 1); +end; + +function CenterPoint(const Rect: TRect): TPoint; +begin + with Rect do begin - Result.X:=(Left+Right) div 2; - Result.Y:=(Top+Bottom) div 2; + Result.X := Avg(Left, Right); + Result.Y := Avg(Top, Bottom); end; end; diff --git a/tests/webtbs/tw13872.pp b/tests/webtbs/tw13872.pp new file mode 100644 index 0000000000..676a8c0bd2 --- /dev/null +++ b/tests/webtbs/tw13872.pp @@ -0,0 +1,35 @@ +uses + types; + +procedure TestAvg(const r: trect); +var + res: tpoint; + o1, o2: int64; +begin + res:=centerpoint(r); + o1 := (int64(r.left) + r.right) div 2; + o2 := (int64(r.top) + r.bottom) div 2; + if (res.x <> o1) or + (res.y <> o2) then + halt(1); +end; + +var + r: trect; +begin + r.left := 1; + r.right := 3; + r.top := 3; + r.bottom := 1; + testavg(r); + r.left:=2000000000; + r.right:=2000000002; + r.top:=-2000000000; + r.bottom:=-2000000002; + testavg(r); + r.left:=-2000000000; + r.right:=2000000002; + r.top:=2000000000; + r.bottom:=-2000000002; +end. +