Recent

Author Topic: Extending a straight line  (Read 4459 times)

kapibara

  • Hero Member
  • *****
  • Posts: 656
Extending a straight line
« on: June 24, 2014, 12:01:04 am »
There is a sloping LineSeries with two points showing a straight line.
How can I calculate the Y value so the line continues in the same direction?

This line is like a guide-line that should follow along when values are added to another Series in the same chart. But must never change angle.

Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Extending a straight line
« Reply #1 on: June 24, 2014, 02:12:11 am »
the formula to calculate the slope of a line is m=(y1-y2)/(x1-x2) having two points (x1,y1) and (x2,y2) of the line you can calculate the slope after that you use the same formula but now you use (x2,y2) as the first point and (x3,y3) as the second point which is the new point you are searching for. I'm assuming you already know the x3 value and you are searching for the y3 value and that is calculated by taking the slope formula and solving for y3 something along the lines of m=(y2-y3)/(x2-x3)=>m*(x2-x3)=(y2-y3)=>(m*(x2-x3)) /y2 = -y3=>y3=-((m(x2-x3)) /y2) and that is the final formula you have to use to find your new Y point.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Extending a straight line
« Reply #2 on: June 24, 2014, 02:48:18 am »
y3=-((m(x2-x3)) /y2) and that is the final formula you have to use to find your new Y point.
Building on Taazz's work:
y3=-((m(x2-x3)) /y2)

you can calculate m and c once:
Code: [Select]
m := (y2-y1)/(x2-x1);
c := -m*x2/y2;

then you can find y for any x using:
Code: [Select]
y := m*x+c;
« Last Edit: June 24, 2014, 02:50:19 am by engkin »

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: Extending a straight line
« Reply #3 on: June 24, 2014, 09:23:41 am »
Or you can use the built-in TFitSeries (add a "Least-squares fit series") and assign the first two points to it. It will automatically calculate the line through both points and paint it beyond the two points with these settings:
- FitEquation = fePolynomial
- ParamCount = 2
- DrawFitRangeOnly = false


kapibara

  • Hero Member
  • *****
  • Posts: 656
Re: Extending a straight line
« Reply #4 on: June 25, 2014, 07:06:38 am »
Thanks for helping, but the angle of the GuideLine changes when it is extended. I cant find out why:

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  { Some values for the GuideLine }
  Pt1.X:=3;
  Pt1.Y:=1.3;
  Pt2.X:=6;
  Pt2.Y:=1.65;

  { Populate GuideLine Series }
  GuideLine.AddXY(Pt1.X, Pt1.Y);
  GuideLine.AddXY(Pt2.X, Pt2.Y);

  { Populate Line Series, for comparison }
  LineSeries.AddXY(1, 1);
  LineSeries.AddXY(10, 2);
end;

procedure TForm1.btnExtendClick(Sender: TObject);
const
  LAST_REC = 1;
begin
  m := (Pt2.Y - Pt1.Y) / (Pt2.X - Pt1.X);
  c := -m * (Pt2.X / Pt2.Y);
  Pt2.X:= LineSeries.GetXMax;
  Pt2.Y := m * Pt2.X + c;

  GuideLine.SetXValue(LAST_REC, Pt2.X);
  GuideLine.SetYValue(LAST_REC, Pt2.Y);
end;

@wp: The FitSeries extends as you said. So far I have used a TLineSeries that the user can resize and move around. Maybe it can be substituted with the FitSeries. But I'm anyway interested in how to do this with a TLineSeries.
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Extending a straight line
« Reply #5 on: June 25, 2014, 07:21:37 am »
the angle of a line can't change when you extend it unless it stops being a line and breaks in to two or more lines.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

kapibara

  • Hero Member
  • *****
  • Posts: 656
Re: Extending a straight line
« Reply #6 on: June 25, 2014, 08:00:25 am »
@Taazz: You know, then you still have straight lines, just more of them.  ;D

The demo shows the problem clearly.


Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

kapibara

  • Hero Member
  • *****
  • Posts: 656
Re: Extending a straight line
« Reply #7 on: June 25, 2014, 08:58:16 am »
This function seems to work very well:

function PointAtX(a, b: TDoublePoint; x: Double): TDoublePoint;
var
  aSlope: Double;
  y: Double;
begin
    aSlope:= (b.Y - a.Y) / (b.X - a.X);
    y:= a.Y + (x - a.X) * aSlope;
    Result.X:= x;
    Result.Y:= y;
end;
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

 

TinyPortal © 2005-2018