blindwig Posted May 6, 2006 Share Posted May 6, 2006 I'm working on a 360-degree snake game in AutoIt, and I needed some math functions to help me calculate angles.I thought I'd share them here if anyone else needs them:expandcollapse popupGlobal Const $pi = 3.14159265358979 ;Find the angle of a point relative to the origin Func GetAnglePO($x, $y) Select Case $x > 0 If $y >= 0 Then Return ATan($y / $x) Else Return ATan($y / $x) + 2 * $pi EndIf Case $x = 0 If $y = 0 Then Return 0 ElseIf $y > 0 Then Return $pi / 2 Else Return 3 * $pi / 2 EndIf Case $x < 0 Return ATan($y / $x) + $pi EndSelect EndFunc ;Find the angle of a point relative to the first point Func GetAnglePP($x1, $y1, $x2, $y2) Return GetAnglePO($x2 - $x1, $y2 - $y1) EndFunc ;Find the angle at point 2 between line segment 1-2 and line segment 2-3 Func GetAnglePPP($x1,$y1,$x2,$y2,$x3,$y3) $A1 = GetAnglePP($x2, $y2, $x1, $y1) $A2 = GetAnglePP($x2, $y2, $x3, $y3) $A3 = GetAngleAA($A1,$A2) Return $A3 EndFunc ;Find the difference between the two angles Func GetAngleAA($A1, $A2) Local $A3 = $A2 - $A1 While $A3 > $pi $A3 -= 2 * $pi WEnd While $A3 < -$pi $A3 += 2 * $pi WEnd Return $A3 EndFunc My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
blindwig Posted May 12, 2006 Author Share Posted May 12, 2006 More:expandcollapse popup;Find the angle at point 2 between line 1-2 and line 2-3 Func GetAnglePPP($x1,$y1,$x2,$y2,$x3,$y3) $A1 = GetAnglePP($x2, $y2, $x1, $y1) $A2 = GetAnglePP($x2, $y2, $x3, $y3) $A3 = GetAngleAA($A1,$A2) Return $A3 EndFunc ;Finds a circle whose perimeter touches all three given points ;Success: ; Return = [CenterX, CenterY, Radius] ;Failure: ; Error = 1 ; Return = descriptive message Func GetCirclePPP($x1,$y1,$x2,$y2,$x3,$y3) Local $a, $b, $c, $d, $e, $f, $g Local $h, $k, $r Local $Return='', $Error=0 ;*** (x1-h)^2 + (y1-k)^2 = (x2-h)^2 + (y2-k)^2 $a = 2 * ($x2 - $x1) $b = 2 * ($y1 - $y2) $c = $x2 ^ 2 + $y2 ^ 2 - $x1 ^ 2 - $y1 ^ 2 ;*** h = (k * B + C) / A ;*** (x2-h)^2 + (y2-k)^2 = (x3-h)^2 + (y3-k)^2 $d = 2 * ($y3 - $y2) $e = 2 * ($x2 - $x3) $f = $x3 ^ 2 + $y3 ^ 2 - $x2 ^ 2 - $y2 ^ 2 ;*** k = (h * E + F) / D $g = ($a * $d - $b * $e) If ($g <> 0) And ($d <> 0) Then $h = ($b * $f + $c * $d) / $g $k = ($h * $e + $f) / $d $r = Sqrt(($x1 - $h) ^ 2 + ($y1 - $k) ^ 2) Dim $Return[3] = [$h, $k, $r] Else $Error = 1 $Return = 'Could not plot a circle on given points: (' & $x1 & ',' & $y1 & '), (' & $x2 & ',' & $y2 & '), (' & $x3 & ',' & $y3 & ')' EndIf SetError($Error) Return $Return EndFuncSee also:Drawing Bezier Curves From An Array Of Points My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now