Get the point of intersection of two lines between points

13 views
Skip to first unread message

ampwi...@gmail.com

unread,
May 28, 2021, 6:23:55 PM5/28/21
to JSXGraph
So I was just wondering, I have two lines, and they cross each other, I would like to return that intersection point where the lines cross. At the same time though, I do not want to return anything if they don actually physically cross. (please refer to the images if I am not making myself clear). 

I thought meetLineLine, but that is basically "infinite lines" that cross. And meetSegmentSegment seems correct, and I thought I could just pass through the start and finish points of each line, and that would be that. But doesn't seem to work. 

Any thoughts? Should be something correct??

Thanks so much for the help!
-Colin
crossingLines.png
nonCrossingOLines.png

Alfred Wassermann

unread,
May 28, 2021, 7:05:41 PM5/28/21
to JSXGraph
The most convenient way seems to be the following:
  • Create an intersection point i with the attribute `alwaysIntersect:false`
  • Test if the Z-component of that point (i.Z()) is 0 (intersection does not exist) or 1 (intersection exists).
  • The coordinates of the intersection are available via i.X() and i.Y().
Example (see it live at https://jsfiddle.net/29xwzupt/1/)

var p1 = board.create('point', [-2, 2]);
var p2 = board.create('point', [1, -1]);
var q1 = board.create('point', [-1.5, 1]);
var q2 = board.create('point', [1, 0.5]);

var seg1 = board.create('segment', [p1, p2]);
var seg2 = board.create('segment', [q1, q2]);

var i = board.create('intersection', [seg1, seg2], {alwaysIntersect: false});
var txt = board.create('text', [1, 3, function() {
         return i.Z() +' ' +  i.X() + ' ' + i.Y();
}]);

Best wishes,
Alfred

Alfred Wassermann

unread,
May 28, 2021, 7:21:37 PM5/28/21
to JSXGraph
One more thing:
The low-level function meetSegmentSegment(p1, p2, q1, q2) needs the following input:
Four coordinate arrays of length 3, defining the start and end points of the two segments.
Usually, a coordinate array has the form [1, x, y].
If you are dealing with a JSXGraph point P, you can get this array via P.coords.usrCoords.

The method meetSegmentSegment() returns an array containing [c, t, u].
As above, c is an array of length 3, containing the intersection coordinates.
t and u are the positions (to be precise, the affine ratio) of the intersection point in the first and second segment.
That means, the intersection point exists iff 0 <= t, u <= 1.

Best wishes,
Alfred
Reply all
Reply to author
Forward
0 new messages