Recent

Author Topic: [SOLVED] Infinite line defined by two points?  (Read 1702 times)

wittbo

  • Full Member
  • ***
  • Posts: 150
[SOLVED] Infinite line defined by two points?
« on: March 10, 2020, 06:01:47 pm »
Is it possible to draw a line defined by 2 points in such a way that it is not only limited by the 2 points but is infinitely extended on both sides? The purpose is that the user can directly modify this line by moving the two points.

I have tried to work with a second infinite line and not connect the two points of the first line, so it looks like the two points are on the infinite line. If you now move the two points, the infinite line should follow the points. Unfortunately, the dragging tool only reacts on the third or fourth click and not immediately, which is obviously due to the underlying infinite line.
« Last Edit: March 10, 2020, 11:23:56 pm by wittbo »
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Infinite line defined by two points?
« Reply #1 on: March 10, 2020, 06:06:26 pm »
the easy way out is to calculate the slop of the line and use it to calculate any point on the line.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Infinite line defined by two points?
« Reply #2 on: March 10, 2020, 06:17:39 pm »
Hi!

Easy solution:

Code: Pascal  [Select][+][-]
  1. function GetPart (P1,P2: TPointF; part: single) : TPointF; // inline;
  2. begin
  3. result := PointF ( (P2.x-P1.x) * part + P1.x, (P2.y-P1.y)*part+P1.y );
  4. end;
  5.  
  6.  


If part is between 0..1 you get a point between P1 and P2.
If you use a value below zero or above 1 you get a point outside the line P1_P2

The single part is the percetage of the length between P1 and P2.

Btw.: Do NOT try to inline this code. It  results in compiler error: 'internal error  2009112601'

Winni
« Last Edit: March 10, 2020, 06:20:23 pm by winni »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Infinite line defined by two points?
« Reply #3 on: March 10, 2020, 07:42:35 pm »
Calculate slope and intercept from the coordinates of the two points.Then use a TFunctionSeries which fires an event OnCalculate for each curve element to be drawn. Calculate the y value associated to the x value given as a parameter using slope and intercept.

Use a standard TLineSeries to draw the two pivot data points. Turn ShowLines off and ShowPoints on.

If you want user interaction to modify the pivots you can add a TDatapointDragTool. Assign the left mouse button (ssLeft) to the Shift property and link the tool to the Tools property of the chart. Then everything runs automatically: the function series is automatically redrawn and adjusts to the dragged data point.

One trap though: Since at runtime the drag tool often hits the function series, but not the data point which makes the program to ignore the drag. To avoi this, you must set the index of the line series to the AffectedSeries property of the drag tool, so that the function series is ignored by the drag tool.

It is also a good idea to lock the automatic scaling of the chart because scaling changes the position of the reference points, and it will be very frustrating to drag the point in such a case.

The attached demo shows everything. Ask if you have further questions.

wittbo

  • Full Member
  • ***
  • Posts: 150
Re: Infinite line defined by two points?
« Reply #4 on: March 10, 2020, 11:23:15 pm »
Great!
That is exactly what I was looking for.
Thanks a lot, WP.
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

wittbo

  • Full Member
  • ***
  • Posts: 150
Re: [SOLVED] Infinite line defined by two points?
« Reply #5 on: March 11, 2020, 12:03:33 am »
Important addition:
It was this remark, wp:  "One trap though: Since at runtime the drag tool often hits the function series, but not the data point which makes the program to ignore the drag. To avoi this, you must set the index of the line series to the AffectedSeries property of the drag tool, so that the function series is ignored by the drag tool."

Since I did not use the AffectedSeries property of the drag tool, the drag tool did not often hit the draggable data points. Just focused AffectedProperty to the draggable lineseries, everything worked as expected.

Use of the TFuncSeries (i have been using TExpressionSeries instead) is quite elegant.
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: [SOLVED] Infinite line defined by two points?
« Reply #6 on: March 11, 2020, 12:40:00 am »
Since I did not use the AffectedSeries property of the drag tool, the drag tool did not often hit the draggable data points. Just focused AffectedProperty to the draggable lineseries, everything worked as expected.
This is probably because in my initial version the two series had different z order. When, depending on the z order the tool finds the function series first it does not pass on to the data point and thus the data point is not movable - I had a hard time with that! Later I reversed the z order so that the tool could find the data point and the function series did not see the event. So depending on the order in which you add the function series and the data point series to the chart you may need AffectedSeries or not.

wittbo

  • Full Member
  • ***
  • Posts: 150
Re: [SOLVED] Infinite line defined by two points?
« Reply #7 on: March 11, 2020, 08:17:54 am »
Good hint!
Up to now I could not imagine the meaning of z-order.
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.2.4
MacStudio with MacOS 13.0.1 / Lazarus 2.2.4

 

TinyPortal © 2005-2018