3

So I'm having a bit of trouble getting these coordinates nailed down.

When I do e.Graphics.RotateTransform(45) the result is this:

e.Graphics.RotateTransform result

What I'd like to happen, and what happens in any photo editing software is this:

Desired outcome

Here's the code. I know the sqrt(2) ratio isn't perfect for rotating this, but I'll fix it after I've gotten this down.

Here's the code I use, that produces the faulty output:

Using G As Graphics = Graphics.FromImage(Canvas)
        With G

            .TextRenderingHint = Text.TextRenderingHint.AntiAlias
            .CompositingQuality = CompositingQuality.HighQuality
            .SmoothingMode = SmoothingMode.AntiAlias

            ' ROTATE AT 45 DEGREES
            '.RotateTransform(45)
            .DrawRectangle(New Pen(Color.Black, LineWidth), New Rectangle(New Point((Dims - NewSize) / 2, (Dims - NewSize) / 2), New Size(NewSize, NewSize)))

            ' NON-TRANSFORMED STUFF
            ' G.DrawString("ayy lmoa", New Font(New FontFamily("Arial"), 16, FontStyle.Bold), Brushes.Black, New Point(0, 0))
        End With
End Using

Has anyone dealt with something like this before?

TaW
  • 53,122
  • 8
  • 69
  • 111
user292010
  • 35
  • 2
  • _Has anyone dealt with something like this before?_ You must be kidding. There are 10trillion questions and answers about rotating. Why didn't you do your reasearch before adding another one??? You are missing the TranslateTransform to move the origin to the targetted centerpoint. See [here](http://stackoverflow.com/questions/26525965/rotate-a-graphics-bitmap-at-its-center/26527737#26527737) – TaW Jun 18 '16 at 21:18
  • If you also want to restrict the site you indeed must calculate the new size for 45° as sqrt(2) or for other angles with trigonomerty. You always should use the new center as the (temporary) origin! – TaW Jun 18 '16 at 21:23
  • Okay, so I've tried to implement TranslateTransform. Here's the code: `TranslateTransform(Dims / 2.0F, Dims / 2.0F)` and here's the [result](http://i.imgur.com/vnXcw7W.png). – user292010 Jun 18 '16 at 21:50
  • This is c#, but you will not find it hard to follow: `int loc = 30; int size = 50; int newsize = (int)(size / Math.Sqrt(2)); int center = loc + size / 2; int nloc = loc + (size - newsize) / 2; e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(loc, loc, size, size));` – TaW Jun 18 '16 at 22:19
  • `e.Graphics.TranslateTransform(center, center); e.Graphics.RotateTransform(45); e.Graphics.TranslateTransform(-center, -center); e.Graphics.DrawRectangle(Pens.Black, new Rectangle(loc, loc, size, size)); e.Graphics.ResetTransform(); e.Graphics.TranslateTransform(center, center); e.Graphics.RotateTransform(45); e.Graphics.TranslateTransform(-center, -center); e.Graphics.DrawRectangle(Pens.Red, new Rectangle(nloc, nloc, newsize, newsize));` – TaW Jun 18 '16 at 22:19
  • It draws the original, the rotated and the rotated and resized squares – TaW Jun 18 '16 at 22:20

0 Answers0