Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:intersectlinecircle [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Intersecting a Line with a Circle =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​FCD0CD1E-B92C-44A9-B17C-05E7044650C8"​)]
 +public class IntersectLineCircleCommand : Command
 +{
 +  public override string EnglishName { get { return "​csIntersectLineCircle";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    Circle circle;
 +    var rc = RhinoGet.GetCircle(out circle);
 +    if (rc != Result.Success)
 +      return rc;
 +    doc.Objects.AddCircle(circle);​
 +    doc.Views.Redraw();​
 +
 +    Line line;
 +    rc = RhinoGet.GetLine(out line);
 +    if (rc != Result.Success)
 +      return rc;
 +    doc.Objects.AddLine(line);​
 +    doc.Views.Redraw();​
 +
 +    double t1, t2;
 +    Point3d point1, point2;
 +    var lineCircleIntersect = Intersection.LineCircle(line,​ circle, out t1, out point1, out t2, out point2);
 +    string msg = "";​
 +    switch (lineCircleIntersect) {
 +      case LineCircleIntersection.None:​
 +        msg = "line does not intersect circle";​
 +        break;
 +      case LineCircleIntersection.Single:​
 +        msg = string.Format("​line intersects circle at point ({0},​{1},​{2})",​ point1.X, point1.Y, point1.Z);
 +        doc.Objects.AddPoint(point1);​
 +        break;
 +      case LineCircleIntersection.Multiple:​
 +        msg = string.Format("​line intersects circle at points ({0},​{1},​{2}) and ({3},​{4},​{5})",​
 +          point1.X, point1.Y, point1.Z, point2.X, point2.Y, point2.Z);
 +        doc.Objects.AddPoint(point1);​
 +        doc.Objects.AddPoint(point2);​
 +        break;
 +    }
 +    RhinoApp.WriteLine(msg);​
 +    doc.Views.Redraw();​
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​C12295A5-0EE4-4370-A754-4C8DFE8374E7"​)>​ _
 +Public Class IntersectLineCircleCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbIntersectLineCircle"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim circle As Circle
 +    Dim rc = Rhino.Input.RhinoGet.GetCircle(circle)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    doc.Objects.AddCircle(circle)
 +    doc.Views.Redraw()
 +
 +    Dim line As Line
 +    rc = Rhino.Input.RhinoGet.GetLine(line)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    doc.Objects.AddLine(line)
 +    doc.Views.Redraw()
 +
 +    Dim t1 As Double, t2 As Double
 +    Dim point1 As Point3d, point2 As Point3d
 +    Dim lineCircleIntersect = Intersection.LineCircle(line,​ circle, t1, point1, t2, point2)
 +    Dim msg As String = ""​
 +    Select Case lineCircleIntersect
 +      Case LineCircleIntersection.None
 +        msg = "line does not intersect circle"​
 +        Exit Select
 +      Case LineCircleIntersection.[Single]
 +        msg = [String].Format("​line intersects circle at point ({0},​{1},​{2})",​ point1.X, point1.Y, point1.Z)
 +        doc.Objects.AddPoint(point1)
 +        Exit Select
 +      Case LineCircleIntersection.Multiple
 +        msg = [String].Format("​line intersects circle at points ({0},​{1},​{2}) and ({3},​{4},​{5})",​ point1.X, point1.Y, point1.Z, point2.X, point2.Y, _
 +          point2.Z)
 +        doc.Objects.AddPoint(point1)
 +        doc.Objects.AddPoint(point2)
 +        Exit Select
 +    End Select
 +    RhinoApp.WriteLine(msg)
 +    doc.Views.Redraw()
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +from scriptcontext import doc
 +import Rhino
 +from Rhino.Geometry.Intersect import Intersection,​ LineCircleIntersection
 +
 +def RunCommand():​
 +  rc, circle = Rhino.Input.RhinoGet.GetCircle()
 +  if rc != Rhino.Commands.Result.Success:​
 +    return rc
 +  doc.Objects.AddCircle(circle)
 +  doc.Views.Redraw()
 +
 +  rc, line = Rhino.Input.RhinoGet.GetLine()
 +  if rc != Rhino.Commands.Result.Success:​
 +    return rc
 +  doc.Objects.AddLine(line)
 +  doc.Views.Redraw()
 +
 +  lineCircleIntersect,​ t1, point1, t2, point2 = Intersection.LineCircle(line,​ circle)
 +  message = ""​
 +  if lineCircleIntersect == LineCircleIntersection.None:​
 +    message = "line does not intersect circle"​
 +  elif lineCircleIntersect == LineCircleIntersection.Single:​
 +    message = "line intersects circle at point ({0},​{1},​{2})"​.format(point1.X,​ point1.Y, point1.Z)
 +    doc.Objects.AddPoint(point1)
 +  elif lineCircleIntersect == LineCircleIntersection.Multiple:​
 +    message = "line intersects circle at points ({0},​{1},​{2}) and ({3},​{4},​{5})"​.format(
 +      point1.X, point1.Y, point1.Z, point2.X, point2.Y, point2.Z)
 +    doc.Objects.AddPoint(point1)
 +    doc.Objects.AddPoint(point2)
 +    ​
 +  print message
 +  doc.Views.Redraw()
 +
 +if __name__ == "​__main__":​
 +    RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/intersectlinecircle.txt ยท Last modified: 2015/09/14 (external edit)