• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipTransformPoints

Started by José Roca, June 23, 2008, 05:03:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a Graphics object and sets its world transformation to a translation 40 units right and 30 units down. Then the code creates an array of points and passes the address of that array to the GdipTransformPoints method of the same Graphics object. The points in the array are transformed by the world transformation of the Graphics object. The code calls the GdipDrawLine method twice: once to connect the two points before the transformation and once to connect the two points after the transformation.

C++


VOID Example_TransformPoints(HDC hdc)
{
   Graphics graphics(hdc);
   Pen pen(Color(255, 0, 0, 255));

   // Create an array of two Point objects.
   Point points[2] = {Point(0, 0), Point(100, 50)};

   // Draw a line that connects the two points.
   // No transformation has been performed yet.
   graphics.DrawLine(&pen, points[0], points[1]);

   // Set the world transformation of the Graphics object.
   graphics.TranslateTransform(40.0f, 30.0f);

   // Transform the points in the array from world to page coordinates.
   graphics.TransformPoints(
      CoordinateSpacePage,
      CoordinateSpaceWorld,
      points,
      2);

   // It is the world transformation that takes points from world
   // space to page space. Because the world transformation is a
   // translation 40 to the right and 30 down, the
   // points in the array are now (40, 30) and (140, 80).

   // Draw a line that connects the transformed points.
   graphics.ResetTransform();
   graphics.DrawLine(&pen, points[0], points[1]);
}


PowerBASIC


SUB GDIP_TransformPoints (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPen AS DWORD
   DIM   pt(1) AS POINTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Draw a line that connects the two points.
   ' // No transformation has been performed yet.
   Pt(0).x = 0 : pt(0).y = 0 : pt(1).x = 100 : pt(1).y = 50
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1, %UnitWorld, pPen)
   hStatus = GdipDrawLine(pGraphics, pPen, pt(0).x, pt(0).y, pt(1).x, pt(1).y)

   ' // Set the world transformation of the Graphics object.
   hStatus = GdipTranslateWorldTransform(pGraphics, 40, 30, %MatrixOrderPrepend)

   ' // Transform the points in the array from world to page coordinates.
   hStatus = GdipTransformPoints(pGraphics, %CoordinateSpacePage, %CoordinateSpaceWorld, pt(0), 2)

   ' // It is the world transformation that takes points from world
   ' // space to page space. Because the world transformation is a
   ' // translation 40 to the right and 30 down, the
   ' // points in the array are now (40, 30) and (140, 80).

   ' // Draw a line that connects the transformed points.
   hStatus = GdipResetWorldTransform(pGraphics)
   hStatus = GdipDrawLine(pGraphics, pPen, pt(0).x, pt(0).y, pt(1).x, pt(1).y)

   ' // Cleanup
   IF pPen THEN GdipDeletePen(pPen)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB