Results 1 to 6 of 6

Thread: [RESOLVED] Flip a GraphicsPath onto itself

Hybrid View

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Resolved [RESOLVED] Flip a GraphicsPath onto itself

    Ok... I have a graphics path ... and I want it to be symmetrical ... so I thought I would just draw the right 1/2 with positive X values... then use 0-x on the points x values to flip it by cloning and reversing the PathData ...

    but I can't seem to be able to append to point data??

    I was doing it like this:

    vb Code:
    1. gpPerson.PathData.Points = gpPerson.PathData.Points.ToArray.Concat(gpPerson.PathData.Points.ToArray.Select(Function(x) New PointF(-x.X, x.Y)).Reverse).ToArray
    2.             gpPerson.PathData.Types = gpPerson.PathData.Types.ToArray.Concat(gpPerson.PathData.Types.ToArray.Reverse).ToArray

    'I went abit overboard with the .ToArray's above while I was testing

    Any ideas how I could do this?

    Also I don't want to just flip it with a transformation and redraw the 2nd 1/2 separately... I want it to be the one graphics path.

    Thanks,
    Kris

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Flip a GraphicsPath onto itself

    Flip and translate the path by whatever means you see fit. Then add it back to the original path with AddPath. Here's an example that does use a matrix for flip and translate:
    Code:
    	Private Function ReflectPath(gp As Drawing2D.GraphicsPath) As Drawing2D.GraphicsPath
    		Using mirror As New Drawing2D.GraphicsPath(gp.PathPoints, gp.PathTypes)
    			Dim mtx As New Drawing2D.Matrix(-1, 0, 0, 1, gp.GetBounds.Width * 2.1F, 0) 'flip and translate X
    			mirror.Transform(mtx)
    			gp.AddPath(mirror, False)
    		End Using
    		Return gp
    	End Function
    BB

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Flip a GraphicsPath onto itself

    Thanks... Almost there, but... I need the paths to be connected ... and the problem with this is shown below (I expanded the gap in between the mirror a little so that you can see what is going on)... this is why I reversed it in my example ...

    Name:  PathProb.jpg
Views: 701
Size:  14.8 KB

    Thanks again,
    Kris

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Flip a GraphicsPath onto itself

    To prevent the cross-line, reverse the direction of the reflected part with GraphicsPath.Reverse. Add CloseFigure to complete the joins. See the improved version of the ReflectPath function in this little ready-to-run example. Scribble with the left mouse button to see the effect.
    Code:
    Public Class Form1
    
    	Private startDraw As Point
    	Private pth, dblPath As Drawing2D.GraphicsPath
    
    	Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    		If e.Button = Windows.Forms.MouseButtons.Left Then
    			pth = New Drawing2D.GraphicsPath
    			dblPath = New Drawing2D.GraphicsPath
    			startDraw = e.Location
    		End If
    	End Sub
    
    	Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    		If e.Button = Windows.Forms.MouseButtons.Left Then
    			pth.AddLine(startDraw, e.Location)
    			startDraw = e.Location
    			Me.Invalidate()
    		End If
    	End Sub
    
    	Private Sub Form1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    		dblPath = ReflectPath(pth)
    		Me.Invalidate()
    	End Sub
    
    	Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    		If dblPath IsNot Nothing Then
    			Using pn As New Pen(Color.Red, 2)
    				e.Graphics.DrawPath(pn, dblPath)
    			End Using
    		End If
    		If pth IsNot Nothing Then
    			Using pn As New Pen(Color.Gray, 2)
    				e.Graphics.DrawPath(pn, pth)
    			End Using
    		End If
    	End Sub
    
    	Private Function ReflectPath(gp As Drawing2D.GraphicsPath) As Drawing2D.GraphicsPath		
    		Dim Xtranslation As Single = 2 * gp.GetBounds.Right
    		Dim mtx As New Drawing2D.Matrix(-1, 0, 0, 1, Xtranslation, 0)
    		Dim pth As New Drawing2D.GraphicsPath(gp.PathPoints, gp.PathTypes)
    		Using mirror As New Drawing2D.GraphicsPath(gp.PathPoints, gp.PathTypes)
    			mirror.Transform(mtx)
    			mirror.Reverse()
    			pth.AddPath(mirror, True)
    			pth.CloseFigure()
    		End Using
    		Return pth
    	End Function
    
    End Class
    You may need to play around with the XTranslation value to get the two halves to line up nicely. 2*GetBounds.Right worked out in the above example but I am not sure if it will apply in your code.

    BB

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Flip a GraphicsPath onto itself

    Isn't checking for left mouse in the Mouse Up event so, ......
    drag with the left mouse, and tap the right mouse periodically while continuing to drag with the left, for interesting side effect.
    Wasn't trying to draw anything, just a few back and forth up the form with periodic right clicks, and release, and it looks rather bunny like to me.
    Attached Images Attached Images  
    Last edited by passel; Dec 9th, 2014 at 07:07 PM.

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Flip a GraphicsPath onto itself

    Thanks... didn't know about the GraphicsPath.Reverse

    I did it mirroring from x=0 so made it:

    vb Code:
    1. Private Shared Function ReflectPath(gp As Drawing2D.GraphicsPath) As Drawing2D.GraphicsPath
    2.         Dim Xtranslation As Single = 2 * gp.GetBounds.Right
    3.         Dim pth As New Drawing2D.GraphicsPath(gp.PathPoints, gp.PathTypes)
    4.         Using mirror As New Drawing2D.GraphicsPath(gp.PathPoints, gp.PathTypes)
    5.             Using mtx As New Drawing2D.Matrix
    6.                 mtx.Scale(-1, 1)
    7.                 mirror.Transform(mtx)
    8.             End Using
    9.             mirror.Reverse()
    10.             pth.AddPath(mirror, True)
    11.             pth.CloseFigure()
    12.         End Using
    13.         Return pth
    14.     End Function

    Works like a treat now ... thanks for all of your help ... karma given!
    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width