TAChart: Protect PointDist functions from integer overflow.

This fixes issue #18348 (Reticule doesn't work when zoomed)

git-svn-id: trunk@28854 -
This commit is contained in:
ask 2011-01-01 08:21:49 +00:00
parent 3039874a64
commit c7b1bd7bda
2 changed files with 7 additions and 4 deletions

View File

@ -705,17 +705,17 @@ end;
function PointDist(const A, B: TPoint): Integer;
begin
Result := Sqr(A.X - B.X) + Sqr(A.Y - B.Y);
Result := Min(Sqr(Int64(A.X) - B.X) + Sqr(Int64(A.Y) - B.Y), MaxInt);
end;
function PointDistX(const A, B: TPoint): Integer;
begin
Result := Abs(A.X - B.X);
Result := Min(Abs(Int64(A.X) - B.X), MaxInt);
end;
function PointDistY(const A, B: TPoint): Integer; inline;
begin
Result := Abs(A.Y - B.Y);
Result := Min(Abs(Int64(A.Y) - B.Y), MaxInt);
end;
function PointLineSide(AP, A1, A2: TPoint): TValueSign;

View File

@ -772,10 +772,13 @@ begin
Result := UseReticule and (Count > 0);
minDist := MaxInt;
for i := 0 to Count - 1 do begin
// Since axis transformation may be non-linear, the distance should be
// measured in screen coordinates. With high zoom ratios this may lead to
// an integer overflow, so ADistFunc should use saturation arithmetics.
pt := Point(GetXImgValue(i), GetYImgValue(i));
dist := ADistFunc(APoint, pt);
if dist >= minDist then
Continue;
continue;
minDist := dist;
AIndex := i;
AImg := pt;