GetDistanceBetweenPoints2D: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (prefer normal note)
 
(9 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
==Description==
{{Server client function}}{{Note|This function is equivalent to the [[Vector2]] class ''getLength'' method when used with a vector that holds the direction and distance between two points. In other words, it produces exactly the same result as substracting the points' coordinates and getting the length of the result vector.}}
 
This function returns the distance between two 2 dimensional points using the pythagorean theorem.
This function returns the distance between two 2 dimensional points using the pythagorean theorem.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">getDistanceBetweenPoints2D ( x1, y1, x2, y2 )</syntaxhighlight>
<syntaxhighlight lang="lua">float getDistanceBetweenPoints2D ( float x1, float y1, float x2, float y2 )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
* '''x1''': The X position of the first point
* '''x1''': The X position of the first point
* '''y1''': The Y position of the first point
* '''y1''': The Y position of the first point
* '''x2''': The X position of the second point
* '''x2''': The X position of the second point
* '''y2''': The Y position of the second point
* '''y2''': The Y position of the second point
===Returns===
Returns a float containing the 2D distance between the two points. Returns ''false'' if invalid parameters are passed.


==Example==
==Example==
<syntaxhighlight lang="lua">vehiclex, vehicley, vehiclez = getVehiclePosition ( vehicle )
<section name="Server and client" class="both" show="true">
vehicle2x, vehicle2y, vehicle2z = getVehiclePosition ( vehicle2 )
This example gets the distance between two vehicles, stored in variables ''vehicle1'' and ''vehicle2''.
outputChatBox ( "The map distance between vehicle and vehicl2 is ", getDistanceBetweenPoints2D ( vehiclex, vehicley, vehicle2x, vehicle2y ) )</syntaxhighlight>
<syntaxhighlight lang="lua">
vehicle1x, vehicle1y, vehicle1z = getElementPosition ( vehicle1 )
vehicle2x, vehicle2y, vehicle2z = getElementPosition ( vehicle2 )
outputChatBox ( "The map distance between vehicle1 and vehicle2 is ", getDistanceBetweenPoints2D ( vehicle1x, vehicle1y, vehicle2x, vehicle2y ) )
</syntaxhighlight>
</section>
 
<section name="Server and client" class="both" show="true">
This example shows the maths behind getting the distance between points and is also more efficient if your script is calling this function extremely often.
<syntaxhighlight lang="lua">
math.sqrt((p2x - p1x) * (p2x - p1x) + (p2y - p1y) * (p2y - p1y))
</syntaxhighlight>
</section>
 
==See Also==
{{Utility functions}}

Latest revision as of 08:30, 7 September 2019

[[{{{image}}}|link=|]] Note: This function is equivalent to the Vector2 class getLength method when used with a vector that holds the direction and distance between two points. In other words, it produces exactly the same result as substracting the points' coordinates and getting the length of the result vector.

This function returns the distance between two 2 dimensional points using the pythagorean theorem.

Syntax

float getDistanceBetweenPoints2D ( float x1, float y1, float x2, float y2 )

Required Arguments

  • x1: The X position of the first point
  • y1: The Y position of the first point
  • x2: The X position of the second point
  • y2: The Y position of the second point

Returns

Returns a float containing the 2D distance between the two points. Returns false if invalid parameters are passed.

Example

Click to collapse [-]
Server and client

This example gets the distance between two vehicles, stored in variables vehicle1 and vehicle2.

vehicle1x, vehicle1y, vehicle1z = getElementPosition ( vehicle1 )
vehicle2x, vehicle2y, vehicle2z = getElementPosition ( vehicle2 )
outputChatBox ( "The map distance between vehicle1 and vehicle2 is ", getDistanceBetweenPoints2D ( vehicle1x, vehicle1y, vehicle2x, vehicle2y ) )
Click to collapse [-]
Server and client

This example shows the maths behind getting the distance between points and is also more efficient if your script is calling this function extremely often.

math.sqrt((p2x - p1x) * (p2x - p1x) + (p2y - p1y) * (p2y - p1y))

See Also