Vector Library
This library provides a set of functions for working with 2D vectors. A 2D vector is a geometric object that has both magnitude (length) and direction and can be represented by an ordered pair of real numbers (x, y).
The functions in this library allow you to create, manipulate, and perform calculations with 2D vectors. Some of the things you can do with these functions include:
- Set the x and y components of a 2D vector using SetVector2D
- Convert polar coordinates (angle and radius) to Cartesian coordinates (x and y) using SetPolar2D
- Copy the values of one 2D vector to another using CopyVector2D
- Add or subtract two 2D vectors using AddVectors2D or SubVectors2D
- Multiply or divide two 2D vectors component-wise using MultVectors2D or DivVectors2D
- Multiply or divide a 2D vector by a scalar value using MultVector2D or DivVector2D
- Linearly interpolate between two 2D vectors using LerpVector2D
- Normalize a 2D vector (i.e., set its magnitude to 1) using GetVectorNormal2D
- Calculate the length or squared length of a 2D vector using GetVectorLength2D or GetVectorSquaredLength2D
- Check if two 2D vectors are equal using AreVectorsEqual2D
- Calculate the dot product or cross product of two 2D vectors using DotProduct2D or CrossProduct2D
Download Vector Source Code
-- File Attached bellow
This library provides a set of functions for working with 2D vectors. A 2D vector is a geometric object that has both magnitude (length) and direction and can be represented by an ordered pair of real numbers (x, y).
The functions in this library allow you to create, manipulate, and perform calculations with 2D vectors. Some of the things you can do with these functions include:
- Set the x and y components of a 2D vector using SetVector2D
- Convert polar coordinates (angle and radius) to Cartesian coordinates (x and y) using SetPolar2D
- Copy the values of one 2D vector to another using CopyVector2D
- Add or subtract two 2D vectors using AddVectors2D or SubVectors2D
- Multiply or divide two 2D vectors component-wise using MultVectors2D or DivVectors2D
- Multiply or divide a 2D vector by a scalar value using MultVector2D or DivVector2D
- Linearly interpolate between two 2D vectors using LerpVector2D
- Normalize a 2D vector (i.e., set its magnitude to 1) using GetVectorNormal2D
- Calculate the length or squared length of a 2D vector using GetVectorLength2D or GetVectorSquaredLength2D
- Check if two 2D vectors are equal using AreVectorsEqual2D
- Calculate the dot product or cross product of two 2D vectors using DotProduct2D or CrossProduct2D
PlayBASIC Code:
; PROJECT : Vector Library ; AUTHOR : Kevin Picone - PlayBASIC Tutor - https://PlayBASIC.com ; CREATED : 12/05/2022 ; EDITED : 28/05/2022 ; --------------------------------------------------------------------- // Use a typed array to hold all the vectors we'll be using Dim vectors(100) as vector2d for lp =0 to getArrayElements(vectors()) vectors(lp)= new vector2D next // declare some pointers of type vector for use in the examples Dim N as vector2d pointer Dim V1 as vector2d pointer Dim V2 as vector2d pointer Dim OldPoint as vector2D pointer // Get pointers to some pre--alloated vectors in our cache array n = Vectors(0).vector2d V1 = Vectors(1).vector2d V2 = Vectors(2).vector2d Dim CurrentPoint as vector2D pointer CurrentPOint = Vectors(3).vector2d Dim OldPoint as vector2D pointer OldPOint = Vectors(4).vector2d Dim Lerp1 as vector2D pointer Lerp1 = Vectors(10).vector2d Dim Lerp2 as vector2D pointer Lerp2 = Vectors(11).vector2d SetVector2d lerp1, 500,100 SetPolar2d lerp2, Rnd(36),rndrange(10,100) Addvectors2d Lerp2, Lerp2, Lerp1 // Set up this vector Setvector2d OldPOint,Mousex(),MouseY() Flushmouse // ----------------------------------------------------------- // ----------------------------------------------------------- // ----------------------------------------------------------- Do // ----------------------------------------------------------- // ----------------------------------------------------------- // ----------------------------------------------------------- CLS // Set up this vecor Setvector2d CurrentPoint,Mousex(),MouseY() circlec OldPoint.X,OldPoint.Y, 50,true, $00ff00 circle CurrentPoint.X,CurrentPOint.Y, 50,true // Subtract target from current N = Old - Current Subvectors2d(N , oldPoint,CurrentPoint) ypos= 50 text 10,Ypos, "Delta:"+StrVector2d$(N) // Get the length of this delta vector Dist#=GetVectorLenght2D(N) // normalize N = and store it back in N GetVectorNormal2D(N , N) text 10,Ypos+30, "Normalized:"+StrVector2d$(N) // Scale Normal by Dist# Multvector2d(N , N, Dist#) text 10,Ypos+60, "Scaled Normalized:"+StrVector2d$(N) // Add origin (current point) back on. Addvectors2d(N , N, CurrentPoint) text 10,Ypos+90, "Result:"+StrVector2d$(N) // Scale it so we can draw line between them line CurrentPOint.x,CurrentPOint.y,n.x,n.y if Mousebutton()=1 CopyVector2d OldPoint,CurrentPOint endif // test lerp Linec Lerp1.x,Lerp1.y,Lerp2.x,Lerp2.y, $ff00ff // LerpFrame=mod(LerpFrame+1,100) LerpScale#=LerpFrame/100.0 LerpVector2d N,Lerp1,Lerp2,LerpScale# Circlec n.x , n.y, 5, true ,$ff00ff // Curve Lerp vector to the one between LerpVector2d V1,Lerp1,Lerp2,LerpScale# LerpVector2d V2,CurrentPoint,OldPoint,LerpScale# LerpVector2d V1,V2,V1,LerpScale# Circlec v1.x , v1.y, 5, true ,$ff00ff LerpVector2d V1,Lerp1,Oldpoint,LerpScale# LerpVector2d V2,lerp2,CurrentPoint,LerpScale# LerpVector2d V1,V2,V1,LerpScale# Circlec v1.x , v1.y, 5, true ,$ff00ff Sync loop Spacekey()
Download Vector Source Code
-- File Attached bellow