January 15, 2021, 06:32:40 PM

Author Topic: [bb] Angle360 for B3D/B+ by markcw [ 1+ years ago ]  (Read 739 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Angle360 for B3D/B+ by markcw [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Angle360 for B3D/B+
Author : markcw
Posted : 1+ years ago

Description : Angle360() takes 4 parameters x1,y1,x2,y2 and returns a float value from 0..359 degrees.
Point 1 is assumed to be the center of a circle and point 2 is assumed to be on the edge. The top of the screen is assumed to be zero degrees. A working example is included with the function.


Code :
Code: BlitzBasic
  1. ;Angle360 Example for B3D/B+
  2.  
  3. Graphics 640,480,0,2
  4. SetBuffer BackBuffer()
  5.  
  6. While Not KeyDown(1)
  7.  Cls
  8.  
  9.  ;Here the center of the screen is point 1 and the mouse is point 2
  10.  angle#=Angle360(320,240,MouseX(),MouseY())
  11.  Text 0,0,"angle#="+angle#
  12.  
  13.  ;Draw 2 points and 3 lines
  14.  Rect MouseX()-2,MouseY()-2,4,4
  15.  Rect 320-2,240-2,4,4
  16.  Line 320,240,MouseX(),MouseY()
  17.  Line 320,240,MouseX(),240
  18.  Line 320,240,320,MouseY()
  19.  
  20.  Flip
  21. Wend
  22. End
  23.  
  24. Function Angle360b#(x1,y1,x2,y2)
  25.  ;Returns a 360 degree angle between 2 points, 0..359 degrees
  26.  ;Author: Snarkbait, 9 Mar 2008
  27.  Return (ATan2(y2-y1,x2-x1)+450) Mod 360
  28. End Function
  29.  
  30. Function Angle360#(x1,y1,x2,y2)
  31.  ;Returns a 360 degree angle between 2 points, 0..359 degrees
  32.  ;Assumes point 1 is the center and point 2 is on the edge of a circle
  33.  ;Author: markcw, edited 12 Mar 2008
  34.  
  35.  Local adj,opp,hyp#,angle#
  36.  adj=Abs(x2-x1)
  37.  opp=Abs(y2-y1)
  38.  hyp#=Sqr((adj*adj)+(opp*opp)) ;Pythagoras
  39.  If hyp#=0 Then hyp#=1 ;Avoid divide by zero
  40.  If opp<=adj : angle#=ASin(opp/hyp#) ;Sin=Opp/Hyp
  41.  Else : angle#=ACos(adj/hyp#) ;Cos=Adj/Hyp
  42.  EndIf
  43.  If x2>x1 And y2<=y1 : angle#=90-angle# ;0..90
  44.  ElseIf x2>x1 And y2>y1 : angle#=90+angle# ;90..180
  45.  ElseIf x2<=x1 And y2>y1 : angle#=180+90-angle# ;180..270
  46.  ElseIf x2<=x1 And y2<=y1 : angle#=180+90+angle# ;270..360
  47.  EndIf
  48.  If angle#=360 Then angle#=0
  49.  Return angle#
  50.  
  51. End Function


Comments :


Snarkbait(Posted 1+ years ago)

 simpler:
Code: [Select]
Function Angle360#(x1,y1,x2,y2)
return (ATan2(y2 - y1, x2 - x1) + 450) Mod 360
End Function



Naughty Alien(Posted 1+ years ago)

 :)


tonyg(Posted 1+ years ago)

 I am guessing this is something to do with <a href="http://www.blitzmax.com/Community/posts.php?topic=76478#855398" target="_blank"> this [/url]


Who was John Galt?(Posted 1+ years ago)

 It appears so.


markcw(Posted 1+ years ago)

 Thanks Snarkbait. Stunning bit of code. Now I know how Atan2 works. Yours works exactly like mine so is a better version to use.tonyg: yes, I noticed there wasn't a decent entry for this in the code archives after looking at Nebula's code so I thought I'd add one. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal