Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating a PathGeometry in WPF

Tags:

c#

.net

wpf

Is there an easy way of translating the PathGeometry in WPF (C#)?

Having a PathGeometry (System.Windows.Media) and a Vector (System.Windows.Vector) i would like to translate the whole Geometry without needing to analyze each single Figure with their Segments and the corresponding Points. The code is pretty straight forward but i'm looking for an approach that uses standard functionality already provided by WPF (C#). Any ideas?

like image 499
Klaus Stefan Gerber Avatar asked Mar 17 '26 06:03

Klaus Stefan Gerber


2 Answers

Set the Geometry's Transform property to an appropriate TranslateTransform, e.g.

Geometry geometry = ...
Vector translation = ...

geometry.Transform = new TranslateTransform(translation.X, translation.Y);
like image 124
Clemens Avatar answered Mar 19 '26 19:03

Clemens


After looking around a bit more I found the solution to my own question:

public PathGeometry TranslatePathGeometry(PathGeometry originalGeometry, Vector translationVector)
{
   originalGeometry.Transform = new TranslateTransform(translationVector.X, translationVector.Y);
   return PathGeometry.CreateFromGeometry(originalGeometry);
}

I found other options using the Geometry.Combine method, but this solution i think is more elegant.

As explained by Clemens it's not even necessary to create a new GeometryPath, you can just apply the TranslateTransform and it will work perfectly.

like image 34
Klaus Stefan Gerber Avatar answered Mar 19 '26 20:03

Klaus Stefan Gerber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!