TAChart: Deprecate functions PointDist and LinePointDist in favor of new PointDistSq and PointLineDistSq.

This commit is contained in:
wp_xyz 2024-01-10 14:02:49 +01:00
parent 94dc3369d9
commit 0f5a732909
10 changed files with 88 additions and 34 deletions

View File

@ -514,7 +514,7 @@ begin
paramName,
FloatToStrEx(FitSeries.Param[i], PRECISION, STD_FMT, EXP_FMT),
FloatToStrEx(FitSeries.ParamError[i], PRECISION, STD_FMT, EXP_FMT),
FloatToStrEx(FitSeries.Param_tValue[i], PRECISION, STD_FMT, EXP_FMT)
FloatToStrEx(FitSeries.Param_tValue[i], PRECISION, STD_FMT, EXP_FMT),
FloatToStrEx(FitSeries.Param_pValue[i], PRECISION, STD_FMT, EXP_FMT)
]));
end;

View File

@ -83,7 +83,7 @@ begin
t := (Now() - FStartTime) * SecsPerDay;
exp_factor := exp(-t/td);
SinCols(TWO_PI * t / t0, sin_factor, cos_factor);
SinCos(TWO_PI * t / t0, sin_factor, cos_factor);
// Position: an exponentially damped sinusoidal motion
x := A0 * sin_factor * exp_factor;

View File

@ -858,7 +858,7 @@ begin
ts := ms;
while t < ParamMax do begin
p := PointAt(t + ts);
if PointDist(p, pp) > Sqr(Step) then
if PointDistSq(p, pp) > Sqr(Step) then
ts /= 2
else begin
ADrawer.LineTo(p);
@ -1101,7 +1101,7 @@ var
m: Double;
pm: TPoint;
begin
if (level > INF_SENTINEL) or (PointDist(APL, APR) <= Sqr(Step)) then
if (level > INF_SENTINEL) or (PointDistSq(APL, APR) <= Sqr(Step)) then
// Left-then-right recursive call order guarantees that
// the last drawn segment is the immediately preceding one.
ADrawer.LineTo(APR)

View File

@ -75,12 +75,16 @@ function MaxPoint(const A, B: TPoint): TPoint; inline;
function MeasureRotatedRect(const ASize: TPoint; AAngle: Double): TSize;
function NextNumberSeq(
const APoints: array of TDoublePoint; var AStart, AEnd: Integer): Boolean;
function PointDist(const A, B: TPoint): Integer; inline;
function PointDist(const A, B: TDoublePoint): Double; inline;
function PointDistSq(const A, B: TPoint): Integer; inline;
function PointDistSq(const A, B: TDoublePoint): Double; inline;
function PointDist(const A, B: TPoint): Integer; inline; deprecated 'Use PointDistSq'; // to be removed in v4.99
function PointDist(const A, B: TDoublePoint): Double; inline; deprecated 'Use PointDistSq'; // to be removed in v4.99
function PointDistX(const A, B: TPoint): Integer; inline;
function PointDistY(const A, B: TPoint): Integer; inline;
function PointLineDist(const P, A, B: TPoint): Integer; overload;
function PointLineDist(const P, A,B: TPoint; out Q: TPoint; out Inside: Boolean): Integer; overload;
function PointLineDistSq(const P, A, B: TPoint): Integer; overload;
function PointLineDistSq(const P, A,B: TPoint; out Q: TPoint; out Inside: Boolean): Integer; overload;
function PointLineDist(const P, A, B: TPoint): Integer; overload; deprecated 'Use PointLineDistSq'; // to be removed in v4.99
function PointLineDist(const P, A,B: TPoint; out Q: TPoint; out Inside: Boolean): Integer; overload; deprecated 'Use PointLineDistSq'; // to be removed in v4.99
function ProjToLine(const P, A, B: TDoublePoint): TDoublePoint; overload;
function ProjToLine(const P, A, B: TPoint): TPoint; overload;
function ProjToRect(
@ -388,7 +392,7 @@ var
begin
Result :=
ScalarProduct(AShape[AIndex] - ATarget, AVector) /
Sqrt(Double(PointDist(AShape[AIndex], ATarget)) * PointDist(ACenter, ATarget));
Sqrt(Double(PointDistSq(AShape[AIndex], ATarget)) * PointDistSq(ACenter, ATarget));
end;
function LineIntersectsRay(
@ -554,16 +558,28 @@ begin
Result := AStart <= High(APoints);
end;
function PointDist(const A, B: TPoint): Integer;
function PointDistSq(const A, B: TPoint): Integer;
begin
Result := Min(Sqr(Int64(A.X) - B.X) + Sqr(Int64(A.Y) - B.Y), MaxInt);
end;
function PointDist(const A, B: TDoublePoint): Double;
// Deprecated, to be removed in v4.99
function PointDist(const A, B: TPoint): Integer;
begin
Result := PointDistSq(A, B);
end;
function PointDistSq(const A, B: TDoublePoint): Double;
begin
Result := Sqrt(Sqr(A.X - B.X) + Sqr(A.Y - B.Y));
end;
// Deprecated, to be removed in v4.99
function PointDist(const A, B: TDoublePoint): Double;
begin
Result := PointDistSq(A, B);
end;
function PointDistX(const A, B: TPoint): Integer;
begin
Result := Min(Abs(Int64(A.X) - B.X), MaxInt);
@ -574,25 +590,31 @@ begin
Result := Min(Abs(Int64(A.Y) - B.Y), MaxInt);
end;
function PointLineDist(const P, A,B: TPoint): Integer;
function PointLineDistSq(const P, A,B: TPoint): Integer;
var
v, w, Q: TPoint;
dot: Int64;
lv: Integer;
begin
if A = B then
Result := PointDist(A, P)
Result := PointDistSq(A, P)
else begin
v := B - A; // Vector pointing along line from A to B
w := P - A; // Vector pointing from A to P
v := B - A; // Vector pointing along line from A to B
w := P - A; // Vector pointing from A to P
dot := Int64(v.x) * w.x + Int64(v.y) * w.y; // dot product v . w
lv := PointDist(A, B); // Length of vector AB
Q := (v * dot) div lv; // Projection of P onto line A-B, seen from A
Result := PointDist(Q, w); // Length from A to Q
lv := PointDistSq(A, B); // Length of vector AB
Q := (v * dot) div lv; // Projection of P onto line A-B, seen from A
Result := PointDistSq(Q, w); // Length from A to Q
end;
end;
function PointLineDist(const P, A,B: TPoint; out Q: TPoint;
// Deprecated, to be removed in v4.99
function PointLineDist(const P, A,B: TPoint): Integer;
begin
Result := PointLineDistSq(P, A,B);
end;
function PointLineDistSq(const P, A,B: TPoint; out Q: TPoint;
out Inside: Boolean): Integer;
var
v, w: TPoint;
@ -602,26 +624,33 @@ var
aq, bq: Integer;
begin
if A = B then begin
Result := PointDist(A, P);
Result := PointDistSq(A, P);
Inside := false;
Q := A;
end else begin
v := B - A;
w := P - A;
dot := Int64(v.x) * w.x + Int64(v.y) * w.y;
lv := PointDist(A, B);
lv := PointDistSq(A, B);
Q := (v * dot) div lv;
Result := PointDist(Q, w);
Result := PointDistSq(Q, w);
// Check whether the projection point Q is inside the A-B line.
// In this case the lengths AQ and BQ are shorter than AB.
aq := sqr(Q.x) + sqr(Q.y); // note: Q is seen from A, not from origin.
bq := PointDist(v, Q);
bq := PointDistSq(v, Q);
Inside := (aq <= lv) and (bq <= lv);
Q := Q + A;
end;
end;
// Deprecated, to be removed in v4.99
function PointLineDist(const P, A,B: TPoint; out Q: TPoint;
out Inside: Boolean): Integer;
begin
Result := PointLineDistSq(P, A,B, Q, Inside);
end;
function ProjToLine(const P, A,B: TDoublePoint): TDoublePoint;
var
v, s: TDoublePoint;
@ -928,7 +957,7 @@ var
pt: TPoint;
begin
pt := RoundPoint(GetPoint(AHi));
if PointDist(APoly.LastPoint, pt) <= Sqr(AStep) then
if PointDistSq(APoly.LastPoint, pt) <= Sqr(AStep) then
SafeAddPoint(pt, AHi)
else begin
Rec(ALo, (ALo + AHi) / 2);

View File

@ -824,7 +824,7 @@ begin
phi := -arctan2(AParams.FPoint.Y - p.y, AParams.FPoint.X - p.x);
SinCos(phi, sinphi, cosphi);
dperim := round(sqrt(sqr(rx * cosPhi) + sqr(ry * sinPhi)));
d := round(sqrt(PointDist(p, AParams.FPoint)));
d := round(sqrt(PointDistSq(p, AParams.FPoint)));
if (d < dist) and (d < dperim + AParams.FRadius) then begin // not quite exact...
dist := d;
AResults.FDist := d;
@ -973,7 +973,7 @@ begin
rx := (iRect.Right - iRect.Left) div 2;
ry := (iRect.Bottom - iRect.Top) div 2;
p := ParentChart.GraphToImage(AxisToGraph(item^.Point));
d := round(sqrt(PointDist(p, AParams.FPoint))); // dist between data pt and clicked pt
d := round(sqrt(PointDistSq(p, AParams.FPoint))); // dist between data pt and clicked pt
phi := -arctan2(AParams.FPoint.Y - p.y, AParams.FPoint.X - p.x);
SinCos(phi, sinphi, cosphi);
dperim := round(sqrt((sqr(rx * cosPhi) + sqr(ry * sinPhi))));
@ -981,7 +981,7 @@ begin
if AYIdx = 1 then
Result := sqr(abs(d - dperim))
else begin
Result := PointDist(p, AParams.FPoint);
Result := PointDistSq(p, AParams.FPoint);
if sqrt(Result) > dperim then
Result := MaxInt;
end;
@ -2392,7 +2392,7 @@ begin
(nptCustom in AParams.FTargets) and
(nptCustom in ToolTargets)
then begin
d := PointLineDist(AParams.FPoint, pt1, pt2); // distance of point from line
d := PointLineDistSq(AParams.FPoint, pt1, pt2); // distance of point from line
if d < dist then begin
dist := d;
xidx := -1;

View File

@ -823,8 +823,8 @@ begin
idx := FindContainingSlice(FDragOrigin);
if idx > -1 then begin
p := ParentChart.GraphToImage(ANewPos);
r1 := sqrt(PointDist(FDragOrigin, FCenter));
r2 := sqrt(PointDist(p, FCenter));
r1 := sqrt(PointDistSq(FDragOrigin, FCenter));
r2 := sqrt(PointDistSq(p, FCenter));
dist := Source.Item[idx]^.X + (r2 - r1) / FRadius;
if dist < 0 then dist := 0; // Don't let value go negative
ListSource.BeginUpdate;

View File

@ -834,7 +834,7 @@ begin
ip1 := ParentChart.GraphToImage(FGraphPoints[0]);
for pointIndex := 1 to FUpBound - FLoBound do begin
ip2 := ParentChart.GraphToImage(FGraphPoints[pointIndex]);
d := PointLineDist(AParams.FPoint, ip1, ip2, q, isInside);
d := PointLineDistSq(AParams.FPoint, ip1, ip2, q, isInside);
if isInside and (d < dmin) then begin
dmin := d;
AResults.FIndex := -1; //pointIndex + FLoBound;

View File

@ -1705,7 +1705,7 @@ begin
exit;
if not IsActive then begin
if PointDist(FOrigin, APoint) < Sqr(MinDragRadius) then
if PointDistSq(FOrigin, APoint) < Sqr(MinDragRadius) then
exit;
Activate;
end;
@ -1890,7 +1890,7 @@ procedure TDataPointTool.FindNearestPoint(APoint: TPoint);
const
DIST_FUNCS: array [TChartDistanceMode] of TPointDistFunc = (
@PointDist, @PointDistX, @PointDistY);
@PointDistSq, @PointDistX, @PointDistY);
var
s, bestS: TCustomChartSeries;
p: TNearestPointParams;
@ -2016,7 +2016,7 @@ procedure TDataPointClickTool.MouseUp(APoint: TPoint);
begin
if
Assigned(OnPointClick) and (FSeries <> nil) and
(FSeries.SpecialPointPos or (PointDist(APoint, FMouseDownPoint) <= Sqr(GrabRadius)))
(FSeries.SpecialPointPos or (PointDistSq(APoint, FMouseDownPoint) <= Sqr(GrabRadius)))
then
OnPointClick(Self, FMouseDownPoint);
FSeries := nil;

View File

@ -50,6 +50,8 @@ type
procedure TestPointOnLine;
procedure TestPointOperations;
procedure TestPolygonIntersectsPolygon;
procedure TestPointDist;
procedure TestPointLineDist;
end;
TColorTest = class(TTestCase)
@ -390,6 +392,24 @@ begin
AssertFalse(IsPolygonIntersectsPolygon(p1, OffsetPolygon(p1, Point(0, -6))));
end;
procedure TGeometryTest.TestPointDist;
var
P: array[0..2] of TPoint = ((X:2; Y:2), (X:3; Y:3), (X:3; Y:4));
begin
AssertEquals(2, PointDistSq(P[0], P[1]));
AssertEquals(5, PointDistSq(P[0], P[2]));
end;
procedure TGeometryTest.TestPointLineDist;
var
Line: array[0..1] of TPoint = ((X:0; Y:100), (X:100; Y:0));
P: TPoint = (X:0; Y:0);
begin
AssertEquals(100*100 div 2, PointLineDistSq(P, Line[0], Line[1]));
end;
{ TColorTest }
procedure TColorTest.AssertEqualsHex(Expected, Actual: Integer);

View File

@ -82,6 +82,11 @@
<OverflowChecks Value="True"/>
</Checks>
</CodeGeneration>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="5">