1
I’m creating a simple orbiting system, where you have the earth and the sun, created on the basis of a UserControl
, the problem is I don’t know why the angle gets crazy by placing an angle on the property Angle
. The control is this:
And here’s your code:
Public Property Angle As Integer
Private Sub Orbit_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim g_ As Graphics = e.Graphics
g_.SmoothingMode = SmoothingMode.HighQuality
Dim p As New Pen(Color.White, 1)
Dim myRect = Me.ClientRectangle
Dim origin As New Point(myRect.Width / 2, myRect.Height / 2)
g_.DrawEllipse(p, myRect)
Dim r = myRect.Height / 2
Dim a = Angle
Dim nx = r * Cos(a) + origin.X
Dim ny = r * Sin(a) + origin.Y
Dim np As New Point(nx, ny)
Dim ax_earth As New Point(np.X - 5, np.Y - 5)
Dim ax_sun As New Point(origin.X - 5, origin.Y - 5)
g_.DrawLine(New Pen(Color.Gray, 1), origin, np)
g_.FillEllipse(New SolidBrush(Color.DeepSkyBlue), New Rectangle(ax_earth, New Size(5, 5)))
g_.FillEllipse(New SolidBrush(Color.Gold), New Rectangle(ax_sun, New Size(10, 10)))
End Sub
I used polar coordinates to calculate the point x, y where planet Earth will be located, the formulas are as follows::
The r is the horizontal size divided by two. The θ is defined by the property Angle
The problem
Even so beauty, when for example I put in the property Angle
90, the correct was to stay in the perfect horizontal middle, but that’s what gave:
So I tested with 120:
I think the problem is on the line Dim a = Angle
, but I honestly don’t know the formula to solve it. As I leave the perfect angle, kind of like this[?]:
That’s because Angle must be informed in Radians, not Degrees...
– VBobCat
I’m not good at math... P
– CypherPotato
https://en.m.wikipedia.org/wiki/Radiano
– VBobCat
Now that you know it has to be in radians, try using an ellipse instead of a circle. It’s cooler, because it looks closer to the real orbit. : ) Uses the polar coordinates of the ellipse, but with different rays vertically and horizontally (a and b instead of r). https://pt.wikipedia.org/wiki/Elipse#Coordenadas_polares
– Luiz Vieira