Try this

Excellent solution Stefan

I may be overlooking something obvious, but whilst studying your solution I struggled to understand how the following two expressions determine that the two lines are within the given proximity, independent of the end point used:

Could you shed some light please?

Sure

Each line is defined by an angle

*a* and the distance

*y* (signed) from origin.

Angle

*a* is normalized in range -pi/2...pi/2 in order to match

*a* and

*a+pi*.

The distance from origin

*y* can be calculated analytically given the 2 points defining the line, but I think is to complicated in lisp. For that reason, I uses the Y component of the rotational formula.

Basically, I rotated the line around 0,0 to get a horizontal line, then I got the Y position. Well, you don't need to rotate the entire line, one point is enough because Y is constant in a horizontal line. It could be p2 as well,

The rotational formula, for a point x,y and angle a, counterclockwise (trigonometric), is:

X = x cos(a) - y sin (a)

Y = x sin(a) + y cos (a)

To complicate the things a little bit, the rotation needed is -a; sin (-a) = -sin (a), cos (-a) = cos (a), so Y = - x sin (a) + y cos (a). So it's negative of what I used (to avoid reversing some lists), but it doesn't matter.

The equal function is obvious, it just pairs up 2 lines within a gap d.