Jump to content

Math: Some Geometry/trig Functions


blindwig
 Share

Recommended Posts

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:

Global 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
Link to comment
Share on other sites

More:

;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
EndFunc

See also:

Drawing Bezier Curves From An Array Of Points

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...