Android Question revolve image in circle ?

AbdurRahman

Member
Hi, I'm trying to revolve earth image around sun image making an orbit without using 3rd party libraries or builtin functions.
So here's what I achieved:

If you notice its revolves in rectangle but not in circle.
So whats correct formula for moving it in circle ?

Here's my current code:
This is function that takes child image and revolve around parent image in within specified speed (latency in ms).
B4X:
Sub revolve( parent As ImageView, child As ImageView, latency As Int)
    Do While True 

        Dim parent_vertical_center As Int = parent.Top + ( parent.Height / 2 )
        Dim child_vertical_center As Int    = child.Top    + ( child.Height   / 2 )

        Dim child_horizontal_center As Int    = child.Left   + ( child.Width      / 2 )
        Dim parent_horizontal_center As Int = parent.Left + ( parent.Width  / 2 )

        Dim IsChildUpperThanParent As Boolean  = child_vertical_center   <=  parent_vertical_center
        Dim IsChildBehindThanParent As Boolean = child_horizontal_center <=  parent_horizontal_center

        child.Left = child.Left + IIf(IsChildUpperThanParent, 1,-1)      'If child is topper than parent, then send child to right, else to left
        child.Top = child.Top + IIf(IsChildBehindThanParent, -1, 1)    'If child is behind parent, then send child to bottom, else to top

        Sleep(latency)

    Loop
End Sub

I call it like:

B4X:
Sub Globals
    Private sun As ImageView
    Private earth As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
  
    sun.Bitmap= LoadBitmap(File.DirAssets, "sun.png")
    earth.Bitmap= LoadBitmap(File.DirAssets, "earth.png") 
  
    revolve(sun, earth, 20)
End Sub
 
Top