Quellcode durchsuchen

* fixed CenterPoint() function (patch by Alexander S. Klenin, mantis #13972)

git-svn-id: trunk@13274 -
Jonas Maebe vor 16 Jahren
Ursprung
Commit
79e6ed5047
3 geänderte Dateien mit 47 neuen und 4 gelöschten Zeilen
  1. 1 0
      .gitattributes
  2. 11 4
      rtl/objpas/types.pp
  3. 35 0
      tests/webtbs/tw13872.pp

+ 1 - 0
.gitattributes

@@ -9157,6 +9157,7 @@ tests/webtbs/tw1376.pp svneol=native#text/plain
 tests/webtbs/tw13763.pp svneol=native#text/plain
 tests/webtbs/tw13763.pp svneol=native#text/plain
 tests/webtbs/tw13813.pp svneol=native#text/plain
 tests/webtbs/tw13813.pp svneol=native#text/plain
 tests/webtbs/tw13820.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/tw13890.pp svneol=native#text/plain
 tests/webtbs/tw13948.pp svneol=native#text/plain
 tests/webtbs/tw13948.pp svneol=native#text/plain
 tests/webtbs/tw1398.pp svneol=native#text/plain
 tests/webtbs/tw1398.pp svneol=native#text/plain

+ 11 - 4
rtl/objpas/types.pp

@@ -406,13 +406,20 @@ begin
     OffsetRect:=false;
     OffsetRect:=false;
 end;
 end;
 
 
-function CenterPoint(const Rect: TRect): TPoint;
+function Avg(a, b: Longint): Longint;
+begin
+  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
 begin
-  With Rect do
+  with Rect do
     begin
     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;
 end;
 end;
 
 

+ 35 - 0
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.
+