Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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?

Thanks,

nick

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.
Posted
Updated 22-May-21 3:11am
Comments
[no name] 22-May-21 17:36pm    
Plotting orbits is like rotating a line (radius); you're trying to come from the outside in (using Margins and unknown "scaling factors").
nickOppen 23-May-21 8:11am    
I agree Gerry, Margin and scaling factors are a bit stupid. I was looking for some sort of (x,y) plot but I couldn't see anything like that so Margin seemed to the the only alternative.
How does one do a radius like you suggest?
[no name] 23-May-21 14:27pm    
The end of the line (radius) is a point on the circumference of the circle. You can create "frames" by stepping through the angle and coming up with new points. The shapes can be positioned on the canvas using Canvas.SetTop, etc. Where things get weird is when you use "negative" x and y. Since the origin of the Canvas is top left, if you add it to the bottom right cell of a 2x2 grid, then the "negative" x's and y's stay in view (and you don't need to do extra translating). Yes, it looks like you're adding "outside" the canvas; one needs to think "outside the box" with WPF / UWP :op

https://stackoverflow.com/questions/839899/how-do-i-calculate-a-point-on-a-circle-s-circumference

nickOppen 23-May-21 20:51pm    
Thanks Gerry. I'll give that a try.
[no name] 23-May-21 23:42pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900