I'm a late-comer to C# and WPF having written a couple of quick programs in the last couple of years.
My latest is a solar system simulation where I draw a user definable number of "planets" on a WPF canvas and then simulate their interaction in 2D wrt gravity and velocity.
I want to leave a trail of 100 dots behind each planet to show their recent motion. I want to draw these dots as if they were being emitted from the center of the planet.
I've translated the canvas' central point to be in the middle of the window.
TransformGroup gr = new TransformGroup();
gr.Children.Add(new TranslateTransform(canvas.ActualWidth / 2, canvas.ActualHeight / 2));
canvas.RenderTransform = gr;
The planets are drawn on the canvas using:
planet.Margin = new System.Windows.Thickness((loc.X * xyLogicalToScreenScaleFactor), (loc.Y * xyLogicalToScreenScaleFactor), 0.0, 0.0);
where loc is a Point representing the planet's logical location.
I was then trying to translate the origin of the planet to be in the middle of the planet but I got all sorts of weird results. It became more clear when I started using rectangles instead of ellipses for the planets and drawing a smaller rectangle around the origin. What I got was this:
origins.jpg - Google Drive
(The little rectangle in the middle is the origin of the canvas.)
As you can see, in the bottom right quarter the origins are where the documentation says the origin will be, i.e. the top left corner. The top left quarter they are where I want them to be (note: this is the default behaviour, I have not done any translation!). The right top has the origins middle left and the bottom left has them central top.
The simple approach would be to just translate based on which quarter the shape was in but note the black rectangle to the right of the canvas origin. It is on the left edge and has y = 0 i.e. directly to the right of the origin.
I'd prefer to understand why the canvas was drawing the shapes with this odd assortment of origins and set it up so that WPF draws them in the correctly in the first place. After all, that's what WPF is supposed to do.
Can anyone give me any ideas?
What I have tried:
I've looked through every canvas property and I can't see anything that controls the origin of children shapes added in C# code.