Useful Extension Methods for Points and friends in WPF
Every once in a while you may be down to low-level element calculation and placement of elements in WPF. For this situation, I have found the following extension methods quite useful:
Translating a point
The first method is a low level application of a double-tuple as offset to a given point. All others use this method to create a new point from an old point and some additional info. Some examples:
Point Sources
A point source is an object that implements IEnumerable<Point>. One type of Point Source I have used is one that takes a seed point and to which you can provide a function how to get from previous to the next point. the following example uses this class to be able to iterate over equal-spaced points along the vertical axis:
By entering the IEnumerable realm, you open your code to all the shiny LINQiness there is out there.
Arcs and the like
In WPF you can work with vectors and matrices. That gives you a nice base to implement a “Rotate” method:
In combination with a different point source that takes an IEnumerable<T>
and provides a conversion to an IEnumerable<Point>
you can create arcs and circles of points quite easily:
Note that above, you could also get the functionality by using the LINQ-Select on the list of vectors. Also note the lazy nature of all involved items. If you only need 45 items, that’s all that is calculated.
I am just thinking of putting this kind of stuff into a small library to help dealing with that low-level stuff, which can be useful for Custom arrangers and the like. I am wondering what else could be in that library…or maybe there is already one?