January 15, 2021, 06:32:40 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bb] Angle360 for B3D/B+ by markcw [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bb] Angle360 for B3D/B+ by markcw [ 1+ years ago ] (Read 739 times)
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
;Angle360 Example for B3D/B+
Graphics
640
,
480
,
0
,
2
SetBuffer
BackBuffer
(
)
While
Not
KeyDown
(
1
)
Cls
;Here the center of the screen is point 1 and the mouse is point 2
angle#=Angle360
(
320
,
240
,
MouseX
(
)
,
MouseY
(
)
)
Text
0
,
0
,
"angle#="
+angle#
;Draw 2 points and 3 lines
Rect
MouseX
(
)
-
2
,
MouseY
(
)
-
2
,
4
,
4
Rect
320
-
2
,
240
-
2
,
4
,
4
Line
320
,
240
,
MouseX
(
)
,
MouseY
(
)
Line
320
,
240
,
MouseX
(
)
,
240
Line
320
,
240
,
320
,
MouseY
(
)
Flip
Wend
End
Function
Angle360b#
(
x1,y1,x2,y2
)
;Returns a 360 degree angle between 2 points, 0..359 degrees
;Author: Snarkbait, 9 Mar 2008
Return
(
ATan2
(
y2-y1,x2-x1
)
+
450
)
Mod
360
End Function
Function
Angle360#
(
x1,y1,x2,y2
)
;Returns a 360 degree angle between 2 points, 0..359 degrees
;Assumes point 1 is the center and point 2 is on the edge of a circle
;Author: markcw, edited 12 Mar 2008
Local
adj,opp,hyp#,angle#
adj=
Abs
(
x2-x1
)
opp=
Abs
(
y2-y1
)
hyp#=
Sqr
(
(
adj*adj
)
+
(
opp*opp
)
)
;Pythagoras
If
hyp#=
0
Then
hyp#=
1
;Avoid divide by zero
If
opp<=adj : angle#=
ASin
(
opp/hyp#
)
;Sin=Opp/Hyp
Else
: angle#=
ACos
(
adj/hyp#
)
;Cos=Adj/Hyp
EndIf
If
x2>x1
And
y2<=y1 : angle#=
90
-angle#
;0..90
ElseIf
x2>x1
And
y2>y1 : angle#=
90
+angle#
;90..180
ElseIf
x2<=x1
And
y2>y1 : angle#=
180
+
90
-angle#
;180..270
ElseIf
x2<=x1
And
y2<=y1 : angle#=
180
+
90
+angle#
;270..360
EndIf
If
angle#=
360
Then
angle#=
0
Return
angle#
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]
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bb] Angle360 for B3D/B+ by markcw [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal