Converting between Screen and World coordinates


#1

Hi,

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,
Dmitriy


(Dale Fugier) #2

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

Shouldn’t be hard to figure out the opposite.


#3

Thanks, Dale

Works perfect.


(Matthieu Arnold) #4

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


(Matthieu Arnold) #5

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)

(Dale Fugier) #6

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


(Matthieu Arnold) #7

Thanks @dale, that seems cleaner :slight_smile: