Jump to content
Sign in to follow this  
Andreik

Analytical Geometry

Recommended Posts

Andreik

I wrote some useful features especially for those who use the GDI + as well as useful for those interested in analytic geometry.

Func _DistanceBeetwen2Points($anPoint1,$anPoint2)
    Local $nDistance
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nDistance = Sqrt((($anPoint2[0]-$anPoint1[0])^2)+(($anPoint2[1]-$anPoint1[1])^2))
    Return $nDistance
EndFunc ;==>_DistanceBeetwen2Points

Func _StraightLineEquationDetBy2Points($anPoint1,$anPoint2)
    Local $nXValue, $nYValue, $nFreeValue
    Local $sXValue, $sYValue, $sFreeValue
    Local $avEquation[4]
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nXValue = ($anPoint2[1]-$anPoint1[1])
    $nYValue = ($anPoint2[0]-$anPoint1[0])*(-1)
    $nFreeValue = ((-1)*(($anPoint2[1]-$anPoint1[1])*$anPoint1[0]))+(($anPoint2[0]-$anPoint1[0])*$anPoint1[1])
    $sXValue = String($nXValue)
    $sYValue = String($nYValue)
    $sFreeValue = String($nFreeValue)
    If StringLeft($sYValue,1) <> "-" Then $sYValue = "+" & $sYValue
    If StringLeft($sFreeValue,1) <> "-" Then $sFreeValue = "+" & $sFreeValue
    $avEquation[0] = $sXValue & "x" & $sYValue & "y" & $sFreeValue & "=0"
    $avEquation[1] = $nXValue
    $avEquation[2] = $nYValue
    $avEquation[3] = $nFreeValue
    Return $avEquation
EndFunc ;==>_StraightLineEquationDetBy2Points

Func _DistanceFromPointToStraightLine($anPoint,$avEquation)
    Local $nDistance
    If Not IsArray($anPoint) Or Not IsArray($avEquation) Then
        SetError(1)
        Return -1
    ElseIf UBound($anPoint) <> 2 Or UBound($avEquation) <> 4 Then
        SetError(2)
        Return -1
    EndIf
    $nDistance = Abs(($avEquation[1]*$anPoint[0])+($avEquation[2]*$anPoint[1])+$avEquation[3])/Sqrt(($avEquation[1]^2)+($avEquation[2]^2))
    Return $nDistance
EndFunc ;==>_DistanceFromPointToStraightLine

Func _AreaOfTriangleDetBy3Points($anPoint1,$anPoint2,$anPoint3)
    Local $nPositive, $nNegative, $nDelta
    Local $nArea
    If Not IsArray($anPoint1) Or Not IsArray($anPoint2) Or Not IsArray($anPoint3) Then
        SetError(1)
        Return 0
    ElseIf UBound($anPoint1) <> 2  Or UBound($anPoint2) <> 2 Or UBound($anPoint3) <> 2 Then
        SetError(2)
        Return 0        
    EndIf
    $nPositive = ($anPoint1[0]*$anPoint2[1])+($anPoint1[1]*$anPoint3[0])+($anPoint2[0]*$anPoint3[1])
    $nNegative = -(($anPoint2[1]*$anPoint3[0])+($anPoint1[1]*$anPoint2[0])+($anPoint1[0]*$anPoint3[1]))
    $nDelta = $nPositive + $nNegative
    MsgBox(0,"",$nDelta)
    $nArea = (1/2)*Abs($nDelta)
    Return $nArea
EndFunc ;==>_AreaOfTriangleDetBy3Points

And here is an example:

#Include <AnalyticalGeometry.au3>
Dim $A[2]=[-5,0]
Dim $B[2]=[0,0]
Dim $C[2]=[-2.5,-2.5]
$AB = _DistanceBeetwen2Points($A,$B)
$BC = _DistanceBeetwen2Points($B,$C)
$AC = _DistanceBeetwen2Points($A,$C)
ConsoleWrite("AB = " & $AB & @CRLF & "BC = " & $BC & @CRLF & "AC = " & $AC & @CRLF)
$EQUATION_AB = _StraightLineEquationDetBy2Points($A,$B)
ConsoleWrite("AB: " & $EQUATION_AB[0] & @CRLF)
$dC_AB = _DistanceFromPointToStraightLine($C,$EQUATION_AB)
ConsoleWrite("d(C,AB) = " & $dC_AB & @CRLF)
$Area_ABC = _AreaOfTriangleDetBy3Points($A,$B,$C)
ConsoleWrite("A(ABC) = " & $Area_ABC & @CRLF)

To understand better I drawn three graphics of mathematical functions and applied functions of analytic geometry to find some relationships and sizes.

Posted ImagePosted Image

AnalyticalGeometry.au3

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
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
Sign in to follow this  

×