Vector/Vector2: Difference between revisions
No edit summary |
(Updated Vector2 class) |
||
Line 1: | Line 1: | ||
==Methods== | ==Methods== | ||
===create=== | ===create=== | ||
This is default constructor for the Vector2 class and returns a Vector2 object. | This is the default constructor for the Vector2 class and returns a Vector2 object. | ||
====Syntax==== | ====Syntax==== | ||
<syntaxhighlight lang="lua">vector2 Vector2 ( | <syntaxhighlight lang="lua"> | ||
vector2 Vector2 ( mixed vectorOrX [, float y ] ) | |||
</syntaxhighlight> | |||
* ''' | ====Required Arguments==== | ||
* '''vectorOrX''': [[{{PAGENAME}}|Vector2]], [[table|Table]] or [[float|Float]] indicating vector's coordinates | |||
====Optional Arguments==== | |||
* '''y''': if ''vectorOrX'' is a float, this will indicate the vector's Y coordinate | |||
====Example==== | ====Example==== | ||
This example checks if the player is using a low resolution. | This example checks if the player is using a low resolution. | ||
<section name=" | <section name="client" class="client" show="true"> | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function | addEventHandler ( "onClientResourceStart", resourceRoot, function() | ||
local screenSize = Vector2(guiGetScreenSize()) | |||
if screenSize.x < 1360 and screenSize.y < 768 then | |||
outputChatBox ("You are running on a low resolution") | |||
end | |||
end | end) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
===normalize=== | ===normalize=== | ||
=== | This function normalizes the vector | ||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
bool Vector2.normalize ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Normalizes a vector|[[{{PAGENAME}}|Vector2]]:normalize}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to normalize | |||
====Example==== | |||
This example demonstrates how to draw a line that is 200 pixels long and red. | |||
The line is drawn from the center of the screen to the mouse cursor's position. | |||
<section name="client" class="client" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
local screenW, screenH = guiGetScreenSize() -- get screen size | |||
local centerVec = Vector2(screenW / 2, screenH / 2) -- get screen center point | |||
local mouseVec = Vector2() -- predefined variable that will change later | |||
local lineLength = 200 -- sets length of the drawn line | |||
local lineColor = tocolor(255, 0, 0) -- set line color | |||
showCursor(true) -- show cursor | |||
addEventHandler('onClientRender', root, function() | |||
local curX, curY = getCursorPosition() | |||
mouseVec.x, mouseVec.y = curX * screenW, curY * screenH -- get absolute cursor position | |||
local dVec = Vector2(mouseVec - centerVec) -- get the distance between the mouse position and the center of the screen | |||
local scaleFactor = lineLength / dVec.length | |||
mouseVec = centerVec + (dVec * scaleFactor) | |||
dxDrawLine(centerVec.x, centerVec.y, mouseVec.x, mouseVec.y, color) | |||
end) | |||
</syntaxhighlight> | |||
</section> | |||
===getX=== | |||
This function gets the X coordinate of a vector. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
float Vector2.getX ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Gets X coordinate of a vector|[[{{PAGENAME}}|Vector2]]:getX|x}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to get X coordinate from | |||
===getY=== | |||
This function gets the Y coordinate of a vector. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
float Vector2.getY ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Gets Y coordinate of a vector|[[{{PAGENAME}}|Vector2]]:getY|y}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to get Y coordinate from | |||
===setX=== | |||
This function sets the X coordinate of a vector. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
bool Vector2.setX ( vector2 vector, float x ) | |||
</syntaxhighlight> | |||
{{OOP|Sets X coordinate of a vector|[[{{PAGENAME}}|Vector2]]:setX|x}} | |||
=== | ====Required Arguments==== | ||
* '''vector''': [[{{PAGENAME}}|Vector2]] to set X coordinate from | |||
* | * '''x''': New X coordinate | ||
==== | ===setY=== | ||
<syntaxhighlight lang="lua">bool | This function sets the Y coordinate of a vector. | ||
* ''' | |||
* | ====Syntax==== | ||
<syntaxhighlight lang="lua"> | |||
bool Vector2.setY ( vector2 vector, float y ) | |||
</syntaxhighlight> | |||
{{OOP|Sets Y coordinate of a vector|[[{{PAGENAME}}|Vector2]]:setY|y}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to set Y coordinate from | |||
* '''Y''': New Y coordinate | |||
===getNormalized=== | ===getNormalized=== | ||
This function gets a normalized vector. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
vector2 Vector2.getNormalized ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Gets a normalized vector|[[{{PAGENAME}}|Vector2]]:getNormalized|normalized}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to get normalized version of | |||
===getLength=== | |||
This function gets the length of a vector. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
vector2 Vector2.getLength ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Gets the length of a vector|[[{{PAGENAME}}|Vector2]]:getLength|length}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to get length from | |||
===getSquaredLength=== | ===getSquaredLength=== | ||
=== | This function gets the squared length of a vector. | ||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
vector2 Vector2.getSquaredLength ( vector2 vector ) | |||
</syntaxhighlight> | |||
{{OOP|Gets the squared length of a vector|[[{{PAGENAME}}|Vector2]]:getSquaredLength|squaredLength}} | |||
====Required Arguments==== | |||
* '''vector''': [[{{PAGENAME}}|Vector2]] to get squared length from | |||
===dot=== | |||
This function gets the dot product of two vectors. | |||
====Syntax==== | |||
<syntaxhighlight lang="lua"> | |||
vector2 Vector2.dot( vector2 vectorOne, vector2 vectorTwo ) | |||
</syntaxhighlight> | |||
{{OOP|Gets the dot product of two vectors|[[{{PAGENAME}}|Vector2]]:dot}} | |||
====Required Arguments==== | |||
* '''vectorOne''': First [[{{PAGENAME}}|Vector2]] to calculate the dot product of | |||
* '''vectorTwo''': Second [[{{PAGENAME}}|Vector2]] to calculate the dot product of |
Revision as of 12:09, 1 August 2023
Methods
create
This is the default constructor for the Vector2 class and returns a Vector2 object.
Syntax
vector2 Vector2 ( mixed vectorOrX [, float y ] )
Required Arguments
Optional Arguments
- y: if vectorOrX is a float, this will indicate the vector's Y coordinate
Example
This example checks if the player is using a low resolution.
addEventHandler ( "onClientResourceStart", resourceRoot, function() local screenSize = Vector2(guiGetScreenSize()) if screenSize.x < 1360 and screenSize.y < 768 then outputChatBox ("You are running on a low resolution") end end)
normalize
This function normalizes the vector
Syntax
bool Vector2.normalize ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Normalizes a vector
- Method: Vector2:normalize(...)
Required Arguments
- vector: Vector2 to normalize
Example
This example demonstrates how to draw a line that is 200 pixels long and red. The line is drawn from the center of the screen to the mouse cursor's position.
local screenW, screenH = guiGetScreenSize() -- get screen size local centerVec = Vector2(screenW / 2, screenH / 2) -- get screen center point local mouseVec = Vector2() -- predefined variable that will change later local lineLength = 200 -- sets length of the drawn line local lineColor = tocolor(255, 0, 0) -- set line color showCursor(true) -- show cursor addEventHandler('onClientRender', root, function() local curX, curY = getCursorPosition() mouseVec.x, mouseVec.y = curX * screenW, curY * screenH -- get absolute cursor position local dVec = Vector2(mouseVec - centerVec) -- get the distance between the mouse position and the center of the screen local scaleFactor = lineLength / dVec.length mouseVec = centerVec + (dVec * scaleFactor) dxDrawLine(centerVec.x, centerVec.y, mouseVec.x, mouseVec.y, color) end)
getX
This function gets the X coordinate of a vector.
Syntax
float Vector2.getX ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Gets X coordinate of a vector
- Method: Vector2:getX(...)
- Variable: .x
Required Arguments
- vector: Vector2 to get X coordinate from
getY
This function gets the Y coordinate of a vector.
Syntax
float Vector2.getY ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Gets Y coordinate of a vector
- Method: Vector2:getY(...)
- Variable: .y
Required Arguments
- vector: Vector2 to get Y coordinate from
setX
This function sets the X coordinate of a vector.
Syntax
bool Vector2.setX ( vector2 vector, float x )
OOP Syntax Help! I don't understand this!
- Note: Sets X coordinate of a vector
- Method: Vector2:setX(...)
- Variable: .x
Required Arguments
- vector: Vector2 to set X coordinate from
- x: New X coordinate
setY
This function sets the Y coordinate of a vector.
Syntax
bool Vector2.setY ( vector2 vector, float y )
OOP Syntax Help! I don't understand this!
- Note: Sets Y coordinate of a vector
- Method: Vector2:setY(...)
- Variable: .y
Required Arguments
- vector: Vector2 to set Y coordinate from
- Y: New Y coordinate
getNormalized
This function gets a normalized vector.
Syntax
vector2 Vector2.getNormalized ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Gets a normalized vector
- Method: Vector2:getNormalized(...)
- Variable: .normalized
Required Arguments
- vector: Vector2 to get normalized version of
getLength
This function gets the length of a vector.
Syntax
vector2 Vector2.getLength ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Gets the length of a vector
- Method: Vector2:getLength(...)
- Variable: .length
Required Arguments
- vector: Vector2 to get length from
getSquaredLength
This function gets the squared length of a vector.
Syntax
vector2 Vector2.getSquaredLength ( vector2 vector )
OOP Syntax Help! I don't understand this!
- Note: Gets the squared length of a vector
- Method: Vector2:getSquaredLength(...)
- Variable: .squaredLength
Required Arguments
- vector: Vector2 to get squared length from
dot
This function gets the dot product of two vectors.
Syntax
vector2 Vector2.dot( vector2 vectorOne, vector2 vectorTwo )
OOP Syntax Help! I don't understand this!
- Note: Gets the dot product of two vectors
- Method: Vector2:dot(...)