Converting between Screen and World coordinates


I am looking for the most optimum way for the following: I need to draw a line in the Viewport in such a way that X coordinate (for both ends of the line) would be always constant and referring to the value defined by screen coordinates.
Y coordinate(for both points) should vary depending on the input 3D point.

I was thinking to use next routine:

  1. Convert 3D World point to 2D Screen point
  2. Modify X value of this screen point, keep Y coordinate
  3. Convert modified point to 3D point in order to be used in Display.DrawLine()

Is it correct way maybe smth easier exists? Am i missing smth?

Kind regards,

Sounds reasonable. Here is how to convert screen coordinates to world coordinates:

Shouldn’t be hard to figure out the opposite.

Thanks, Dale

Works perfect.

Hi, @dale,
Would you have an example to convert screen coordinates to world coordinates in .net?

Forget it, I got the solution:

            Dim loc As Drawing.Point = New Drawing.Point((My.Computer.Screen.Bounds.Width) / 2, (My.Computer.Screen.Bounds.Height) / 2)
            Dim doc As RhinoDoc = RhinoDoc.ActiveDoc
            Dim view As RhinoView = doc.Views.ActiveView
            loc = view.ActiveViewport.ScreenToClient(loc)
            Dim CoordLine As Line = view.ActiveViewport.ClientToWorld(loc)
            Dim CoordWordFromScreen As Point3d = New Point3d(CoordLine.FromX, CoordLine.FromY, 0)

Hi @Matthieu_from_NAVINN,

This is how I would compute the transform:

Dim view as RhinoView = doc.Views.ActiveView
If (Not IsNothing(view)) Then
  Dim xform as Transform = view.ActiveViewport.GetTransform(CoordinateSystem.Screen, CoordinateSystem.World)
  ' TODO: transform objects...
End If

– Dale

Thanks @dale, that seems cleaner :slight_smile: