# Math Problem??

#### ilan

##### Expert
Licensed User
hi

i need some help in math,

ineed to rotate an image depends on a position of another image i will put a drawing to make me clear enough

all images on the screen should rotate depends on the image that is moving

something like robbery bob, if the camera saw bob it will rotate depends on bob position

so both axis (x/y) are important...

#### Beja

##### Expert
Licensed User
Hi Ilan,
Could you elaborate a little more on this..
* Do the 3 squares represent 3 images?
** which image is the center or the original?
*** are the 45o and the 135o just examples or the rotation step?
**** does an image rotate around itself or around another image (center or original image axis)... etc..

#### strat

##### Active Member
Licensed User
Formula of degree between two coordinates:

Degree=Arctan[ (y2-y1) / (x2-x1) ]

Degree may be radian for some language so you may have to convert degree it.

#### ilan

##### Expert
Licensed User
Hi Ilan,
Could you elaborate a little more on this..
* Do the 3 squares represent 3 images?
** which image is the center or the original?
*** are the 45o and the 135o just examples or the rotation step?
**** does an image rotate around itself or around another image (center or original image axis)... etc..
hi beja,

there will be few images on the screen that will not move only should be rotated according to one image that is moving (x/y)

imagine like you have a thief and all cameras should look on him when he is moving

those cameras are not moving only rotating according to the thief location(x/y)

they need watch him so every image rotate on his own center point ...

#### ilan

##### Expert
Licensed User
Formula of degree between two coordinates:

Degree=Arctan[ (y2-y1) / (x2-x1) ]

Degree may be radian for some language so you may have to convert degree it.
thanx strat could you tell me please if y2/x2 is the image that should rotate and y1/x1 is the moving image??

can I use arctan in b4a??

#### strat

##### Active Member
Licensed User
thanx strat could you tell me please if y2/x2 is the image that should rotate and y1/x1 is the moving image??

can I use arctan in b4a??

x2,y2 is moving image's coordinates. B4a has most of trigonometry functions so you can use Arctan. Look here:
http://www.basic4ppc.com/android/help/core.html#keywords_atan

You can use both degree or radian. It depends on which library you will use to rotate an image.

#### ilan

##### Expert
Licensed User
x2,y2 is moving image's coordinates. B4a has most of trigonometry functions so you can use Arctan. Look here:
http://www.basic4ppc.com/android/help/core.html#keywords_atan

You can use both degree or radian. It depends on which library you will use to rotate an image.
I am not using any lib I draw it with canvas.drawrotatebitmap.. and enter the degrees...

thank you very much, I will try it when I get home

#### Beja

##### Expert
Licensed User
Hi Ilan, I think I understand your question now..
If the surface is 2D, monitoring cameras are stationary and only the thief image is moving, then you can use a timer and the simple
equation for calculating the distance and update a line contentiously .. you will only need to know the initial coordinates of the moving image.
You need a canvas and a few tricks .. no angle calculations needed.

#### ilan

##### Expert
Licensed User
Hi beja, i am drawing the image with canvas.drawrotatedbitmap so i need to put the degrees the image will be drawed.

You are correct i am using a timer for that but i will need to calculate on what degrees to draw the image and this depends on the x/y of the image that is not moving (like camera) and the x/y of the moving image (like thief)

i will try strat suggestion and see how it works

#### Beja

##### Expert
Licensed User
What I understand is: several cameras on the wall are monitoring a single moving object (the thief). is that correct?
If the answer is yes, then I am still with the original idea of drawing a line between the camera and the thief.. but since you want to to just move the
head of the camera and not showing the line, then still you can draw a short line just outside the camera image, with the same formula but modified.. you can play with this and draw another (slave) image
in the shape of a camera head and link it with the line head so it can move around the camera image along with the line.. you may even make the line color transparent and have a perfectly rotating camera.

#### ilan

##### Expert
Licensed User
I dont want to draw lines only to rotate the camera image (draw it rotated)

For that i need to know what degrees i need to draw the image.

sorry i dont understand how can i link the camera head with the line head.
dont i need for that to know the degrees?

#### Beja

##### Expert
Licensed User
sorry i dont understand how can i link the camera head with the line head.
dont i need for that to know the degrees?
It's kind of compromise.. if you want a very realistic camera movement then you are right, you will need to calculate the angles and I remember Klaus posted some examples long ago.. but for the poor's man animation of the camera then you make the lens circular and facing you (sideways) so you can avoid the direction problem.. and draw a line between the camera center and the moving object's center and make it transparent.. see the image.

#### Attachments

• 43.1 KB Views: 109

#### ilan

##### Expert
Licensed User
Formula of degree between two coordinates:

Degree=Arctan[ (y2-y1) / (x2-x1) ]

Degree may be radian for some language so you may have to convert degree it.
thanx strat, i tried it and it works, i dont get exaxtly what i want but i am sure this is the right direction.

the image is not always rotating correctly, maybe something is missing?

this is my code:

B4X:
``````Dim deg As Int

For i = 0 To enemiesize -1
deg = ATanD((ball.Top-enemies(i).top) / (ball.Left-enemies(i).Left))
enemiescnv(i).DrawBitmapRotated(enemybmp(enemies(i).enemybmpstr),Null,enemiesrect,deg)'newdeg)
Next``````

#### strat

##### Active Member
Licensed User
Klaus's code works better. Thank you Klaus.

Here example I tried. It works now.

B4X:
``````#Region  Project Attributes
#ApplicationLabel: JHSImageFlip
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim timer1 As Timer
End Sub

Sub Globals
Dim x,y As Float
Dim radius,angle,degree As Float
Private Button1 As Button
Private ImageView1 As ImageView
Dim c As Canvas
Dim rect1 As Rect
Private ImageView2 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")

timer1.Initialize("timer1",50)
timer1.Enabled=True
angle=0
radius=40%x

rect1.Initialize(0,0,ImageView1.Width,ImageView1.Height)
ImageView1.Left=50%x-(ImageView1.Width/2)
ImageView1.Top=50%y-(ImageView1.Height/2)
ImageView2.Left=101%x
End Sub

Sub Activity_Resume

End Sub
Sub timer1_Tick
Dim c As Canvas
c.Initialize(ImageView1)
angle=angle+1
'If angle>359 Then
'    angle=0
'End If
x=CosD(angle)*radius+50%x
y=SinD(angle)*radius+50%y
Button1.Left=x
Button1.Top=y
degree = ATan2D((Button1.Top-ImageView1.top), (Button1.Left-ImageView1.Left))
c.DrawBitmapRotated(ImageView2.Bitmap,Null,rect1,degree)
Log(degree)
End Sub``````

#### Attachments

• 294.7 KB Views: 83

#### ilan

##### Expert
Licensed User
Try this:
B4X:
``deg = ATan2D(ball.Top - enemies(i).to), ball.Left - enemies(i).Left)``
thanx Klaus, I have tried that too and the result was the same for me.
I will try again strat example and see why it worked for him better...

#### Erel

##### Administrator
Staff member
Licensed User
B4X:
``deg = (ATan2D (...) + 360) Mod 360``

#### klaus

##### Expert
Licensed User
ATan2D returns values between +180 and -180°
But DrawBitmapRotated accepts also negative values.
290° is the same as -70°.

#### ilan

##### Expert
Licensed User
Klaus's code works better. Thank you Klaus.

Here example I tried. It works now.

B4X:
``````#Region  Project Attributes
#ApplicationLabel: JHSImageFlip
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim timer1 As Timer
End Sub

Sub Globals
Dim x,y As Float
Dim radius,angle,degree As Float
Private Button1 As Button
Private ImageView1 As ImageView
Dim c As Canvas
Dim rect1 As Rect
Private ImageView2 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")

timer1.Initialize("timer1",50)
timer1.Enabled=True
angle=0
radius=40%x

rect1.Initialize(0,0,ImageView1.Width,ImageView1.Height)
ImageView1.Left=50%x-(ImageView1.Width/2)
ImageView1.Top=50%y-(ImageView1.Height/2)
ImageView2.Left=101%x
End Sub

Sub Activity_Resume

End Sub
Sub timer1_Tick
Dim c As Canvas
c.Initialize(ImageView1)
angle=angle+1
'If angle>359 Then
'    angle=0
'End If
x=CosD(angle)*radius+50%x
y=SinD(angle)*radius+50%y
Button1.Left=x
Button1.Top=y
degree = ATan2D((Button1.Top-ImageView1.top), (Button1.Left-ImageView1.Left))
c.DrawBitmapRotated(ImageView2.Bitmap,Null,rect1,degree)
Log(degree)
End Sub``````

hi strat i tried your example and its working great on yours but not on mine, and i dont understand why