Site Tools


Differences

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

Link to this comparison view

developer:sdksamples:worldtoscreen [2015/09/14] (current)
Line 1: Line 1:
 +====== Transform World Coordinates to Screen Coordinates ======
 +> **Developer:​** //​[[developer:​cplusplusplugins|C++]]//​
 +> **Summary:​** //​Demonstrates how to transform world coordinates to screen coordinates using Rhino SDK.//
  
 +=====Question=====
 +How to I get the screen coordinates of a view? That is, I need to transform world coordinates into a screen coordinates for a specified view. The resulting coordinatess are represented as 2-D points. How can I do this?
 +
 +=====Answer=====
 +Every Rhino view is capable of returning the transformation from one coordiate system to another. This is accomplished by the ON_Viewport::​GetXform member. The following demonstrates how to use this to covert world coordinates to screen coordinates.
 +
 +<code c++>
 +CRhinoCommand::​result CCommandTest::​RunCommand( const CRhinoCommandContext&​ context )
 +{
 +  // Pick a point
 +  CRhinoGetPoint gp;
 +  gp.SetCommandPrompt( L"Pick point" );
 +  gp.GetPoint();​
 +  if( gp.CommandResult() != CRhinoCommand::​success )
 +    return gp.CommandResult();​
 +
 +  // Get the view the point was picked in
 +  CRhinoView* view = gp.View();
 +  if( 0 == view )
 +    return CRhinoCommand::​failure;​
 +
 +  // Obtain the view's world-to-screen transformation
 +  ON_Xform world_to_screen;​
 +  view->​ActiveViewport().VP().GetXform( ON::​world_cs,​ ON::​screen_cs,​ world_to_screen );
 +
 +  // Get the picked point
 +  ON_3dPoint picked_pt = gp.Point();
 +
 +  // Create a 3-D point
 +  ON_3dPoint screen_pt = picked_pt;
 +  // Transform it
 +  screen_pt.Transform( world_to_screen );
 +
 +  // Create a Windows 2-D point from the transformed point
 +  POINT pt2d;
 +  pt2d.x = (int)screen_pt.x;​
 +  pt2d.y = (int)screen_pt.y;​
 +
 +  // TODO...
 +
 +  RhinoApp().Print( L"​Screen point = %d, %d\n", pt2d.x, pt2d.y );
 +
 +  return CRhinoCommand::​success;​
 +}
 +</​code>​
 +
 +\\
 +
 +{{tag>​Developer cplusplus}}
developer/sdksamples/worldtoscreen.txt ยท Last modified: 2015/09/14 (external edit)