PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Invert Y axis to draw Real World coordinate
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3759
Page 1 of 1

Author:  olibara [ Sun Apr 08, 2018 4:28 pm ]
Post subject:  Invert Y axis to draw Real World coordinate

Hello

I use to work with GDI Graphics to draw Diagram, normaly the origin of coordinate is 0,0 : Bottom Left
XGraphix and GDI Graphics have the Origin on TOP Left

So when Using a GDI Graphic I set a Transformation Matrix
Code SetTransformMatrix below
But I'm trying do to Similar with
XGraphics like this :
Code:
     gfx.TranslateTransform(-offset_x, offset_y, XMatrixOrder.Append);
      gfx.ScaleTransform(scaleX, scaleY, XMatrixOrder.Append);


Full code below
Without any succes
What I'm doing wrong ??
Is it possible to invert Origin with Xgraphics
Thanks for any help

Code for GDI : (working well)
Code:
    // *************************************************************************
    public static Matrix SetTransformMatrix(RectangleF World, SizeF Dest, bool Centered)
    {
      double map_W = World.Width;
      double map_H = World.Height;

      double rh = Dest.Height / (map_H);
      double rw = Dest.Width / (map_W);

      double scaleX = rw;
      double scaleY = rh;

      double ofY = 0;
      double ofX = 0;


      ofX = ((Dest.Width / scaleX) - World.Width) / 2;
      ofY = ((Dest.Height / scaleY) - World.Height) / 2;


      double offset_x = World.X;
      double offset_y = World.Y + World.Height;

      if (Centered)
      {
        offset_x -= ofX;
        offset_y += ofY;
      }
      float offset_Xf = (float)offset_x;
      float offset_Yf = (float)offset_y;

      Matrix m = new Matrix(1.0f,
                            0.0f,
                            0.0f,
                            -1.0f,
                            0.0f,
                            0.0f);

      m.Translate(-offset_Xf, offset_Yf, MatrixOrder.Append);
      m.Scale((float)scaleX, (float)scaleY, MatrixOrder.Append);
      return m;
    }


Code for Xgraphics : Graph is Upside Down
Code:
   internal static void SetTransformMatrix(XRect World, XSize Dest, bool Centered, XGraphics gfx)
    {
      double map_W = World.Width;
      double map_H = World.Height;

      double rh = Dest.Height / (map_H);
      double rw = Dest.Width / (map_W);

      double scaleX = rw;
      double scaleY = rh;

      double ofY = 0;
      double ofX = 0;


      ofX = ((Dest.Width / scaleX) - World.Width) / 2;
      ofY = ((Dest.Height / scaleY) - World.Height) / 2;


      double offset_x = World.X;
      double offset_y = -World.Y;  // Not good but it shows something upsisde down

 

      gfx.TranslateTransform(-offset_x, offset_y, XMatrixOrder.Append);
      gfx.ScaleTransform(scaleX, scaleY, XMatrixOrder.Append);
    }

Author:  TH-Soft [ Sun Apr 08, 2018 4:43 pm ]
Post subject:  Re: Invert Y axis to draw Real World coordinate

Hi!
olibara wrote:
Full code below
To provide "full code", please use the IssueSubmissionTemplate.
See also:
viewtopic.php?f=2&t=832

To me it is somewhat unclear what your code does versus what you expect.

Author:  olibara [ Mon Apr 09, 2018 4:44 am ]
Post subject:  Re: Invert Y axis to draw Real World coordinate

Thank you TH-Soft

My issue was that the graph displayed using the Transformation I had was upside down
My difficulty was to understand how to set the transformation to Xgraphics to inver the Y axis the same way as I'm doing with the Matrix of GDI+

But in the meantime I've found the way : the trick is to change the sign of the Y scale

This is it :D

Code:
   
      double offset_x = World.X;
      double offset_y = (World.Y+World.Height);  // Offset is Y origin + Height

      gfx.TranslateTransform(-offset_x, -offset_y, XMatrixOrder.Append);
      gfx.ScaleTransform(scaleX, -scaleY, XMatrixOrder.Append);   // Y scale must be inverted

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/