Page 1 of 1

On screen angle calculation

Posted: Wed Mar 15, 2023 10:24 am
by captain_skank
I need to measure an angle of something in an image.

Loading the image etc - no problem.

Plotting the 3 x,y points and overlaying the 2 lines to highlight the angle - no problem

How do i then calculate the angle described by the 2 lines ( math especialy trigonometry is not my strong point )

I know i cold use opencv for this but i want to understand how it's done.

Below is some quick and dirty plotting code - postion the mouse on the window where you wish to start your first line and right click then move to the 2nd and 3rd points and left click.

Code: Select all

Procedure.i PROC_draw(PVAR_sx.i, PVAR_sy.i, PVAR_fx.i, PVAR_fy.i)
  
  StartDrawing(WindowOutput(0))
    Line(PVAR_sx, PVAR_sy, PVAR_fx-PVAR_sx, PVAR_fy-PVAR_sy, RGB(255, 0, 0)) 
  StopDrawing()
  
EndProcedure


If OpenWindow(0, 0, 0, 1200, 900, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  LVAR_ww.i = WindowWidth(0)
  LVAR_wh.i = WindowHeight(0)
  LVAR_start.i = #False
  LVAR_point_count.i = 1
  LVAR_sx.i
  LVAR_sy.i
  LVAR_fx.i
  LVAR_fy.i
  
  Repeat
    Event = WaitWindowEvent()
    
    Select Event;EventType()
      Case #PB_Event_LeftClick
        If LVAR_start = #True And LVAR_point_count < 4
        LVAR_fx = WindowMouseX(0)
        LVAR_fy = WindowMouseY(0)
        Debug "Point " + Str(LVAR_point_count) + " - x,y : " + Str(LVAR_fx) + ", " + Str(LVAR_fy) 
        PROC_draw(LVAR_sx, LVAR_sy, LVAR_fx, LVAR_fy)
          If LVAR_point_count = 3
            LVAR_start = #False
          Else
            LVAR_sx = LVAR_fx
            LVAR_sy = LVAR_fy
            LVAR_point_count + 1
          EndIf
        EndIf
      Case #PB_Event_RightClick
        LVAR_point_count = 1
        LVAR_start = #True
        LVAR_sx = WindowMouseX(0)
        LVAR_sy = WindowMouseY(0)
        Debug "Point " + Str(LVAR_point_count) + " - x,y : " + Str(LVAR_sx) + ", " + Str(LVAR_sy) 
        
        LVAR_point_count + 1
    EndSelect
    
  Until Event = #PB_Event_CloseWindow
EndIf

Re: On screen angle calculation

Posted: Wed Mar 15, 2023 10:37 am
by STARGĂ…TE
The angle is the arcus cosine of the dot product of the normalized directions of the two lines.
Image
In other words: you have to calculate the distances between your points like:
d12x = x1-x2 and d12y = y1-y2 and d32x = x3-x2 and d32y = y3-y2
and the you can use:

Code: Select all

angle = Degree(ACos( (d12x*d32x+d12y*d32y)/(Sqr(d12x*d12x+d12y*d12y)*Sqr(d32x*d32x+d32y*d32y)) ))

Re: On screen angle calculation

Posted: Wed Mar 15, 2023 12:21 pm
by captain_skank
Thanks very much stargate - works like a charm.

Re: On screen angle calculation

Posted: Fri Mar 17, 2023 7:23 pm
by mestnyi
captain_skank wrote: Wed Mar 15, 2023 12:21 pm Thanks very much stargate - works like a charm.
you can show?

Re: On screen angle calculation

Posted: Tue Mar 21, 2023 11:25 am
by captain_skank
I can, but i'm up to my eyes in an equipment audit at the mo, so may be a few days + i want to tidy up and add a few other things before i post it.

N.B : In the end i found it easier to plot and calculate using a right angled triangle