Strategy to check whether two parallel lines are inline/aligned?

Hello everybody,

I’m looking for a way to check whether two parallel lines are inline/aligned or not!? The solution should be independent of the position of the lines in world space, meaning that it shouldn’t rely on comparisons to world axes.

50

Whether the lines overlap each other partially, have an intersection at one of their end points, or don’t come into contact at all doesn’t matter! The case where both lines could be exactly the same was previously excluded.

I want to incorporate this strategy in a larger Python script, but a vanilla Grasshopper strategy, reliant on math, is also welcome! :slight_smile:

Have a nice week everybody! Thanks.

Example file:
inline_01.gh (5.3 KB)

Check this as well:

2 Likes

Thanks @PeterFotiadis, but I don’t get how this could help me?
I already know that my lines are parallel, meaning that the slope of the first line is equal to the slope of the second line (m1 = m2).
I’m also unsure what the Y and X values represent, or how to use the Y value to evaluate if parallel lines are aligned?

Here’s what I’ve come up with so far:

def get_slope(line):
    """Returns the slope of a line."""
    dx = line.To.X - line.From.X
    if dx != 0:
        return (line.To.Y - line.From.Y) / dx

def get_y(line, x):
    m = get_slope(line)
    b = line.From.Y - (m * line.From.X)
    return m * x + b

print "Y for Line A:", get_y(LineA, LineB.From.X)
print "Y for Line B:", get_y(LineB, LineA.From.X)

I also understand some C#, if you are uncomfortable with Python.

inline_02.gh (12.8 KB)

use https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Intersect_Intersection_CurveCurve.htm and check the resulting intersection events for overlap.

Unfortunately, not what I’m looking for. I’m aiming to see if parallel lines (not curves) are aligned/inline, even if they aren’t touching. I guess I could check for intersections of infinite lines, but maybe there’s a smarter solution. Thanks anyway!

If one is gonna solve this using RhinoCommon, might as well go with:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Intersect_Intersection_LineLine_1.htm

And then checking if they are parallel by measuring angles between:

https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Geometry_Line_Direction.htm

line.ToNurbsCurve() solves that for you btw.
In that case, go with LineLine intersection as Anders also suggested, nothing much smarter than a simple function call.

Thanks @AndersDeleuran and @rgr! Do you guys understand @PeterFotiadis’ suggestion? It seems interesting to me, but I don’t fully get it.

I’ll write a small C# on that matter later.

It would work exactly as the attached with regard plane co-planarity (i.e. just dealing with the parameters).

Planes_CoPlanar_V1.gh (11.4 KB)

In the mean time:

http://www.ambrsoft.com/TrigoCalc/Line3D/Line3D_.htm

1 Like

Thanks again, @PeterFotiadis! This seems to work, but I’m curious what your other script is going to look like.

@AndersDeleuran and @rgr, line-line-intersections can’t be found for parallel lines!
Since, I already know that all my lines are parallel, this method won’t work for weeding out aligned, parallel lines (cf. “Remarks” Documentation).

I’d use
Line.DistanceTo (with the limitToFiniteSegment option set to false)
to check whether both ends of one line lie (within some tolerance) on the infinite extension of the other

1 Like

Very nice, suggestion @DanielPiker. Thanks!

Well … it may look a bit freaky (implementing various “options”/freaky things like the determinant of a 3*3 coord Matrix) … but I haven’t found time to do it (yet). In the mean time:

Update:

Cross.IsZero is not safe (tol and the likes). See inside.

Lines_CoLinear_V1.gh (9.1 KB)

2 Likes

Thank you, Peter!

Hi @p1r4t3b0y, @PeterFotiadis,

I know I’m late to the party, but here’s a possible solution that just uses standard components, no code:

Lines_CoLinear_NoCode.gh (18.0 KB)

on the basis that two lines are aligned if they have a common unit vector and the line between their start points has the same unit vector (or their start points coincide).

Regards
Jeremy

1 Like

Indeed … but PirateBoy wants to implement some solution within some bigger P code.

Hi @PeterFotiadis,
True

All solutions are welcome, guys! And I’m thankful for everybody who tried to help out.