I don't have time atm to create any examples but you might find my vector class useful.
Code: Select all
;************
;Vector Class
;**************************************************************************************************************************************************************************
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;**************************************************************************************************************************************************************************
Class Vector {
;Written By: HB
;Date: Sept 23rd, 2022
;Last Edit: Sept 24th, 2022
;Purpose: Vector math class
;Credit: Rohwedder
;Resources:
;Line intercept concepts and code: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=37175
;Create an Arrow: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=92039&p=479129#p478944
;Getting an angle: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=108760&p=483661#p483678
;Setting an Angle: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=108760&p=483786#p483811
;
static RadToDeg := 45 / ATan( 1 )
, DegToRad := ATan( 1 ) / 45
__New( x := 0 , y := 0 , rotate := 0 ){
if( IsObject( x ) ){
if( rotate = 3 ){
This.X := x.X * -1
,This.Y := x.Y * -1
}else if( rotate = 2 ){
This.X := x.Y
,This.Y := x.X * -1
}else if( rotate = 1 ){
This.X := x.Y * -1
,This.Y := x.X
}else{
This.X := x.X
,This.Y := x.Y
}
}else{
if( rotate = 3 ){
This.X := X * -1
,This.Y := Y * -1
}else if( rotate = 2 ){
This.X := Y
,This.Y := X * -1
}else if( rotate = 1 ){
This.X := Y * -1
,This.Y := X
}else{
This.X := X
,This.Y := Y
}
}
}
Add( x , y := "" ){
if( IsObject( x ) ){
This.X += x.X
,This.Y += x.Y
}else if( y = "" ){
This.X += x
,This.Y += x
}else{
This.X += x
,This.Y += y
}
}
Sub( x , y := "" ){
if( IsObject( x ) ){
This.X -= x.X
,This.Y -= x.Y
}else if( y = "" ){
This.X -= X
,This.Y -= X
}else{
This.X -= X
,This.Y -= Y
}
}
Div( x , y := "" ){
if( IsObject( x ) ){
This.X /= x.X
,This.Y /= x.Y
}else if( x && y = "" ){
This.X /= x
,This.Y /= x
}else{
This.X /= X
,This.Y /= Y
}
}
Mult( x , y := "" ){
if( IsObject( x ) ){
This.X *= x.X
,This.Y *= x.Y
}else if( x && y = "" ){
This.X *= x
,This.Y *= x
}else{
This.X *= X
,This.Y *= Y
}
}
Dist( x , y := "" ){
if( IsObject( x ) )
return Sqrt( ( ( This.X - x.X ) **2 ) + ( ( This.Y - x.Y ) **2 ) )
else
return Sqrt( ( ( This.X - X ) **2 ) + ( ( This.Y - Y ) **2 ) )
}
GetMag(){
return Sqrt( This.X * This.X + This.Y * This.Y )
}
SetMag( magnitude ){
local m := This.GetMag()
This.X := This.X * magnitude / m
,This.Y := This.Y * magnitude / m
}
MagSq(){
return This.GetMag()**2
}
Dot( x , y := "" ){
if( IsObject( x ) )
return ( This.X * x.X ) + ( This.Y * x.Y )
else
return ( This.X * X ) + ( This.Y * Y )
}
Cross( x , y := "" ){
if( IsObject( x ) )
return This.X * x.Y - This.Y * x.X
else
return This.X * Y - This.Y * X
}
Norm(){
local m := This.GetMag()
This.X /= m
This.Y /= m
}
GetAngle(){
local angle
( ( angle := Vector.RadToDeg * DllCall( "msvcrt\atan2" , "Double" , This.Y , "Double" , This.X , "CDECL Double" ) ) < 0 ) ? ( angle += 360 )
return angle
}
SetAngle( newAngle := 0 , NewVector := 0 ){
local Angle := This.GetAngle()
, ChangeAngle := newAngle - Angle
, Co := Cos( Vector.DegToRad * ChangeAngle )
, Si := Sin( Vector.DegToRad * ChangeAngle )
, X := This.X
, Y := This.Y
, X2 := X * Co - Y * Si
, Y2 := X * Si + Y * Co
if( !NewVector )
This.X := X2 , This.Y := Y2
else
return New Vector( X2 , Y2 )
}
RotateAngle( rotationAmount := 90 , NewVector := 0 ){
local Co := Cos( Vector.DegToRad * rotationAmount )
, Si := Sin( Vector.DegToRad * rotationAmount )
, X := This.X
, Y := This.Y
, X2 := X * Co - Y * Si
, Y2 := X * Si + Y * Co
if( !NewVector )
This.X := X2 , This.Y := Y2
else
return New Vector( X2 , Y2 )
}
;********************************************
;class methods
TestLineInterceptPoint( interceptPoint , Line1 , Line2 ){ ; Line = { Start: { X: , Y: } , End: { X: , Y: } } , interceptPoint = { X: , Y: }
local
for k , v in [ "X" , "Y" ]
M%v%_Min := min( Line1.Start[ v ] , Line1.End[ v ] )
,M%v%_Max := max( Line1.Start[ v ] , Line1.End[ v ] )
,L%v%_Min := min( Line2.Start[ v ] , Line2.End[ v ] )
,L%v%_Max := max( Line2.Start[ v ] , Line2.End[ v ] )
if( !( interceptPoint.X < Mx_Min || interceptPoint.X > Mx_Max || interceptPoint.X < Lx_Min || interceptPoint.X > Lx_Max ) && !( interceptPoint.Y < My_Min || interceptPoint.Y > My_Max || interceptPoint.Y < Ly_Min || interceptPoint.Y > Ly_Max ) )
return 1
return 0
}
GetLineInterceptPoint( Line1 , Line2 ){ ; Line = { Start: { X: , Y: } , End: { X: , Y: } }
local A1 := Line1.End.Y - Line1.Start.Y
,B1 := Line1.Start.X - Line1.End.X
,C1 := A1 * Line1.Start.X + B1 * Line1.Start.Y
,A2 := Line2.End.Y - Line2.Start.Y
,B2 := Line2.Start.X - Line2.End.X
,C2 := A2 * Line2.Start.X + B2 * Line2.Start.Y
,Denominator := A1 * B2 - A2 * B1
return New Vector( { X: ( ( B2 * C1 - B1 * C2 ) / Denominator ) , Y: ( ( A1 * C2 - A2 * C1 ) / Denominator ) } )
}
;********************************************
}
;**************************************************************************************************************************************************************************
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000 00000 <<<>>> 00000
;**************************************************************************************************************************************************************************