# Differences

This shows you the differences between two versions of the page.

 — developer:sdksamples:intersectlines [2015/09/14] (current) Line 1: Line 1: + ====== Intersecting Line Curves ====== + ====== C++, .NET ====== + > **Summary:​** //​Demonstrates how to intersect line curves using the Rhino SDK.// + + ====Question==== + I have two line curves that are not parallel and that do not intersect. How can I find the intersection of them? + + ====Answer==== + You can find the intersection of two non-parallel line curves using the ON_Intersect function. + + ===== C++ ===== + + CRhinoCommand::​result CCommandTest::​RunCommand( const CRhinoCommandContext&​ context ) + { + CRhinoGetObject go; + go.SetCommandPrompt( L"​Select lines" ); + go.SetGeometryFilter( ON::​curve_object ); + go.GetObjects( 2, 2 ); + if( go.CommandResult() != success ) + return go.CommandResult();​ + + if( go.ObjectCount() != 2 ) + return failure; + + const ON_LineCurve* crv0 = ON_LineCurve::​Cast( go.Object(0).Geometry() ); + const ON_LineCurve* crv1 = ON_LineCurve::​Cast( go.Object(1).Geometry() ); + if( 0 == crv0 | 0 == crv1 ) + return failure;  ​ + + ON_Line line0 = crv0->​m_line;​ + ON_Line line1 = crv1->​m_line;​ + + ON_3dVector v0 = line0.to - line0.from; + v0.Unitize();​ + + ON_3dVector v1 = line1.to - line1.from; + v1.Unitize();​ + + if( v0.IsParallelTo(v1) != 0 ) + { + RhinoApp().Print( L"​Selected lines are parallel.\n"​ ); + return nothing; + } + + ON_Line ray0( line0.from, line0.from + v0 ); + ON_Line ray1( line1.from, line1.from + v1 ); + + double s = 0, t = 0; + if( !ON_Intersect(ray0,​ ray1, &s, &t) ) + { + RhinoApp().Print( L"No intersection found.\n"​ ); + return nothing; + } + + ON_3dPoint pt0 = line0.from + s * v0; + ON_3dPoint pt1 = line1.from + t * v1; + + // pt0 and pt1 should be equal, so we will + // only add pt0 to the document + context.m_doc.AddPointObject( pt0 ); + context.m_doc.Redraw();​ + + return CRhinoCommand::​success;​ + } + + ​ + ===== C# ===== + + public override IRhinoCommand.result RunCommand(IRhinoCommandContext context) + { + MRhinoGetObject go = new MRhinoGetObject();​ + go.SetCommandPrompt( "​Select lines" ); + go.SetGeometryFilter( IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object ); + go.GetObjects( 2, 2 ); + if( go.CommandResult() != IRhinoCommand.result.success ) + return go.CommandResult();​ + if( go.ObjectCount() != 2 ) + return IRhinoCommand.result.failure;​ + + IOnLineCurve crv0 = OnLineCurve.ConstCast( go.Object(0).Geometry() ); + IOnLineCurve crv1 = OnLineCurve.ConstCast( go.Object(1).Geometry() ); + if( crv0 == null | crv1 == null ) + return IRhinoCommand.result.failure;​ + + OnLine line0 = new OnLine(crv0.m_line);​ + OnLine line1 = new OnLine(crv1.m_line);​ + + On3dVector v0 = line0.to - line0.from; + v0.Unitize();​ + On3dVector v1 = line1.to - line1.from; + v1.Unitize();​ + + if( v0.IsParallelTo(v1) != 0 ) + { + RhUtil.RhinoApp().Print( "​Selected lines are parallel.\n"​ ); + return IRhinoCommand.result.nothing;​ + } + + OnLine ray0 = new OnLine( line0.from, line0.from + v0 ); + OnLine ray1 = new OnLine( line1.from, line1.from + v1 ); + + double s = 0.0, t = 0.0; + if( !OnUtil.ON_Intersect(ray0,​ray1,​ref s, ref t) ) + { + RhUtil.RhinoApp().Print( "No intersection found.\n"​ ); + return IRhinoCommand.result.nothing;​ + } + + On3dPoint pt0 = line0.from + v0 * s; + On3dPoint pt1 = line1.from + v1 * t; + + // pt0 and pt1 should be equal, so we will + // only add pt0 to the document + context.m_doc.AddPointObject( pt0 ); + context.m_doc.Redraw();​ + + return IRhinoCommand.result.success;​ + } + ​ + ===== VB.NET ===== + + Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) _ + As RMA.Rhino.IRhinoCommand.result + Dim go As New MRhinoGetObject() + go.SetCommandPrompt("​Select lines"​) + go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object) + go.GetObjects(2,​ 2) + If (go.CommandResult() <> IRhinoCommand.result.success) Then + Return go.CommandResult() + End If + If (go.ObjectCount() <> 2) Then Return IRhinoCommand.result.failure + + Dim crv0 As IOnLineCurve = OnLineCurve.ConstCast(go.Object(0).Geometry()) + Dim crv1 As IOnLineCurve = OnLineCurve.ConstCast(go.Object(1).Geometry()) + If (crv0 Is Nothing Or crv1 Is Nothing) Then + Return IRhinoCommand.result.failure + End If + + Dim line0 As New OnLine(crv0.m_line) + Dim line1 As New OnLine(crv1.m_line) + + Dim v0 As On3dVector = line0.to - line0.from + v0.Unitize() + Dim v1 As On3dVector = line1.to - line1.from + v1.Unitize() + + If (v0.IsParallelTo(v1) <> 0) Then + RhUtil.RhinoApp().Print("​Selected lines are parallel."​ + vbCrLf) + Return IRhinoCommand.result.nothing + End If + + Dim ray0 As New OnLine(line0.from,​ line0.from + v0) + Dim ray1 As New OnLine(line1.from,​ line1.from + v1) + + Dim s As Double = 0.0, t As Double = 0.0 + If (Not OnUtil.ON_Intersect(ray0,​ ray1, s, t)) Then + RhUtil.RhinoApp().Print("​No intersection found."​ + vbCrLf) + Return IRhinoCommand.result.nothing + End If + + Dim pt0 As On3dPoint = line0.from + v0 * s + Dim pt1 As On3dPoint = line1.from + v1 * t + + ' pt0 and pt1 should be equal, so we will + ' only add pt0 to the document + context.m_doc.AddPointObject(pt0) + context.m_doc.Redraw() + + Return IRhinoCommand.result.success + End Function + + ​ + + + {{tag>​Developer cplusplus dotnet}}