Camel
2015-05-20 23:04:16
I made a function that returns the trendline of a TPA. I found it useful for doing things around the wild wall.
Here is an example
function TPointArray.Trendline() : TPointArray;
var
TPA : TPointArray;
LowX, HighX, i, SumX, SumY, A, B, C, D : integer;
Slope, Yintercept :Extended;
begin
TPA := Self;
TPA.sortByXValue(true);
LowX := TPA[0].X;
HighX := TPA[Length(TPA) - 1].X;
For i := 0 to Length(TPA) -1 do
begin
A := A + (TPA[i].x * TPA[i].y);
SumX := SumX + TPA[i].X;
SumY := SumY + TPA[i].Y;
C := C + (TPA[i].X * TPA[i].X);
end;
A := A * Length(TPA);
B := SumX * SumY;
C := C * Length(TPA);
D := SumX * SumX;
Slope := (A - B) / (C - D);
YIntercept := (SumY - (Slope * SumX)) / Length(TPA);
Result := TPAFromLine(LowX,Round(Slope * LowX + YIntercept),HighX,Round(Slope * HighX + YIntercept));
end; Here is an example