The angles are crazy!

Asked

Viewed 38 times

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:

inserir a descrição da imagem aqui

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::

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

So I tested with 120:

inserir a descrição da imagem aqui

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[?]:

inserir a descrição da imagem aqui

1 answer

2


Well, digging around and reading a little bit about trigonometry I just divided the angle by 180 and multiplied by PI, like this:

Dim a = Angle / 180 * PI

and it worked. I honestly didn’t know how to fix it, and magically a light from the sky came and solved my problem spontaneously.

  • 1

    That’s because Angle must be informed in Radians, not Degrees...

  • I’m not good at math... P

  • 1

    https://en.m.wikipedia.org/wiki/Radiano

  • 1

    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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.