GDI+ Transormations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabio

    GDI+ Transormations

    Hi all!

    A question about transformations .

    Let's have this piece of code that transforms the coordinates of the
    Graphics object:

    ---------
    Graphics g = e.Graphics;
    Matrix m = g.Transform;
    m.RotateAt(45, new Point(100, 100));
    g.Transform = m;
    g.DrawRectangle (Pens.Blue, 50, 50, 100, 100);
    ---------

    This works, ok.
    But let's say that I need to get if a point falls into the transformed
    rectangle, i.e.:

    Rectangle r = new Rectangle(50, 50, 100, 100);
    Console.Write(r .Contains(55, 55);

    Obviously, this fails, since the point is computed with the "normal"
    coordinates.
    There is a way to transform also the coordinates of the control surface?

    Thanks!

    --

    Free .Net Reporting Tool - http://www.neodatatype.net


  • Martijn Mulder

    #2
    Re: GDI+ Transormations

    > A question about transformations .[color=blue]
    >
    > Let's have this piece of code that transforms the coordinates of the
    > Graphics object:
    >
    > ---------
    > Graphics g = e.Graphics;
    > Matrix m = g.Transform;
    > m.RotateAt(45, new Point(100, 100));
    > g.Transform = m;
    > g.DrawRectangle (Pens.Blue, 50, 50, 100, 100);
    > ---------
    >
    > This works, ok.
    > But let's say that I need to get if a point falls into the transformed
    > rectangle, i.e.:
    >
    > Rectangle r = new Rectangle(50, 50, 100, 100);
    > Console.Write(r .Contains(55, 55);
    >
    > Obviously, this fails, since the point is computed with the "normal"
    > coordinates.
    > There is a way to transform also the coordinates of the control surface?[/color]



    Bob Powell addresses this point in one of his excellent tutorials:




    Comment

    • Fabio

      #3
      Re: GDI+ Transormations

      "Martijn Mulder" <i@m> ha scritto nel messaggio
      news:445881cf$0 $23336$dbd45001 @news.wanadoo.n l...


      [color=blue]
      >
      > Bob Powell addresses this point in one of his excellent tutorials:
      >
      > http://www.bobpowell.net/manipulate_graphics.htm[/color]

      Wow, works great!

      Thanks!

      Jast a question: I don't understand why in this case I need to do a
      Matrix.Invert() while it is not required when I draw on the Graphics.


      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: GDI+ Transormations

        Fabio,

        Transformations are applied when you draw and they don't affect the data in
        the Rectangle object.

        I have two suuggestion:
        1. Add the rectangle to a GraphicsPath, apply the transformation on the
        graphics path (this will modify the coordinates inside the path) and now use
        IsVisible or IsOutlineVisibl e GraphicsPath method to hit test.
        2. Create a Matrix that does the oposite inversion (form trasnformed
        coorinates to normal (not-trasnformed) and apply the trasnformation on the
        the point you want to hittest against and then check with the rectangle.

        Matrix m = new Matrix();
        m.RotateAt(90, new Point(100, 100));
        m.Invert();

        Rectangle r = new Rectangle(50, 75, 100, 50);
        Point[] pts = new Point[]{new Point(100,50)};
        m.TransformPoin ts(pts);
        Console.Write(r .Contains(pts[0]));


        --
        HTH
        Stoitcho Goutsev (100)

        "Fabio" <znt.fabio@virg ilio.it> wrote in message
        news:eKKyyYpbGH A.3856@TK2MSFTN GP03.phx.gbl...[color=blue]
        > Hi all!
        >
        > A question about transformations .
        >
        > Let's have this piece of code that transforms the coordinates of the
        > Graphics object:
        >
        > ---------
        > Graphics g = e.Graphics;
        > Matrix m = g.Transform;
        > m.RotateAt(45, new Point(100, 100));
        > g.Transform = m;
        > g.DrawRectangle (Pens.Blue, 50, 50, 100, 100);
        > ---------
        >
        > This works, ok.
        > But let's say that I need to get if a point falls into the transformed
        > rectangle, i.e.:
        >
        > Rectangle r = new Rectangle(50, 50, 100, 100);
        > Console.Write(r .Contains(55, 55);
        >
        > Obviously, this fails, since the point is computed with the "normal"
        > coordinates.
        > There is a way to transform also the coordinates of the control surface?
        >
        > Thanks!
        >
        > --
        >
        > Free .Net Reporting Tool - http://www.neodatatype.net
        >[/color]


        Comment

        Working...