; getAngle.pbi Procedure.f getAngle(mx,my, px,py, mode.l=1) ; returns the angle of a line going from point ; [mx,my] to point [px,py] in radian ; mode=1 (default): ; pointing to the right means 0 degrees ; pointing to the top means 90 degrees, ect ; [math y-axis] ; mode=-1: ; pointing to the right means 0 degrees ; pointing to the bottom means 90 degrees, ect ; [screen y-axis.] Static xd.l, yd.l, alpha.f xd=px-mx yd=py-my If xd>0 If yd<0 :: alpha= ATan(-yd / xd ) ElseIf yd>=0 :: alpha= 2*#PI - ATan( yd / xd ) EndIf ElseIf xd<0 If yd<0 :: alpha= #PI - ATan(-yd /-xd ) ElseIf yd>=0 :: alpha= #PI + ATan( yd /-xd ) EndIf ElseIf xd=0 If yd>0 : alpha= #PI*1.5 ElseIf yd<0 : alpha= #PI*0.5 EndIf EndIf If mode=-1 alpha = 2*#PI-alpha EndIf ; If Not Random(200) ; Debug Str(xd) + " | " + Str(yd) + " | " + StrF(alpha) ; EndIf ProcedureReturn alpha EndProcedure ; [merge_end] ; Debug "---- EXAMPLE - getAngle.pbi" ; Debug getAngle(400,400, 450,350) *180/#PI ; Debug getAngle(400,400, 350,350) *180/#PI ; Debug getAngle(400,400, 350,450) *180/#PI ; Debug getAngle(400,400, 450,450) *180/#PI ; Debug "" ; Debug getAngle(0,0, -50,-1)*180/#PI ; Debug getAngle(0,0, -50,0)*180/#PI ; Debug getAngle(0,0, -50,1)*180/#PI ; Debug "" ; Debug getAngle(100,100, 50,99)*180/#PI ; Debug getAngle(100,100, 50,100)*180/#PI ; Debug getAngle(100,100, 50,101)*180/#PI ; Debug "----" ; OpenWindow(0,100,100,400,400,"") ; Repeat ; StartDrawing(WindowOutput(0)) :: Box(0,0,400,400,$333333) ; Circle(200,200,2,$ff00)::Circle(WindowMouseX(0),WindowMouseY(0),2,$ff00) ; a.f = getAngle(200,200,WindowMouseX(0),WindowMouseY(0)) ; LineXY(200,200,WindowMouseX(0),WindowMouseY(0),$ff0000) :: DrawText(10,10,Str(a*180/#PI)+"°") ; StopDrawing() ; Until WaitWindowEvent(100) = #PB_Event_CloseWindow ; IDE Options = PureBasic 4.20 Beta 2 (Windows - x86) ; CursorPosition = 16 ; Folding = - ; CompileSourceDirectory