On screen angle calculation

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

On screen angle calculation

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: On screen angle calculation

Post 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)) ))
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: On screen angle calculation

Post by captain_skank »

Thanks very much stargate - works like a charm.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: On screen angle calculation

Post by mestnyi »

captain_skank wrote: Wed Mar 15, 2023 12:21 pm Thanks very much stargate - works like a charm.
you can show?
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 636
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: On screen angle calculation

Post 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
Post Reply