Math Problem??

Discussion in 'Chit Chat' started by ilan, Jun 27, 2015.

  1. ilan

    ilan Expert Licensed User


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

  2. Beja

    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..
  3. strat

    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 likes this.
  4. ilan

    ilan Expert Licensed User

    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 ...
  5. ilan

    ilan Expert 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??
  6. strat

    strat Active Member Licensed User

    x2,y2 is moving image's coordinates. B4a has most of trigonometry functions so you can use Arctan. Look here:

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

    ilan Expert Licensed User

    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 :)
  8. Beja

    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.
  9. ilan

    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
  10. Beja

    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.
  11. ilan

    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?
  12. Beja

    Beja Expert Licensed User

    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.

    Attached Files:

  13. ilan

    ilan Expert Licensed User

    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:

    Dim deg As Int
    For i = 0 To enemiesize -1
            deg = 
    ATanD((ball.Top-enemies(i).top) / (ball.Left-enemies(i).Left))
  14. klaus

    klaus Expert Licensed User

    Try this:
    deg = ATan2D(ball.Top - enemies(i).to), ball.Left - enemies(i).Left)
  15. strat

    strat Active Member Licensed User

    Klaus's code works better. Thank you Klaus.

    Here example I tried. It works now.

    #Region  Project Attributes
    #ApplicationLabel: JHSImageFlip
    #VersionCode: 1
    '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)
    End Sub

    Sub Activity_Resume

    End Sub
    Sub timer1_Tick
    Dim c As Canvas
    'If angle>359 Then
        '    angle=0
        'End If
        degree = 
    ATan2D((, (Button1.Left-ImageView1.Left))
    End Sub

    Attached Files:

  16. ilan

    ilan Expert Licensed User

    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...
  17. MaFu

    MaFu Well-Known Member Licensed User

    ATan2D() may return a negative value. In this case you must add 360° to the result.
    ilan likes this.
  18. Erel

    Erel Administrator Staff Member Licensed User

    deg = (ATan2D (...) + 360Mod 360
    ilan likes this.
  19. klaus

    klaus Expert Licensed User

    ATan2D returns values between +180 and -180°
    But DrawBitmapRotated accepts also negative values.
    290° is the same as -70°.
    Beja and Erel like this.
  20. ilan

    ilan Expert Licensed User

    hi strat i tried your example and its working great on yours but not on mine, and i dont understand why :(
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice