GetDistanceBetweenPointAndSegment2D

From Multi Theft Auto: Wiki

Jump to: navigation, search

Takes point coordinates and line (a segment) starting and ending coordinates. It returns the shortest distance between the point and the line.

Syntax

float getDistanceBetweenPointAndSegment2D(float pointX, float pointY, float x1, float y1, float x2, float y2)

Required Arguments

  • pointX: The X coordinate of the point.
  • pointY: The Y coordinate of the point.
  • x1: The X coordinate of the starting point of the line.
  • y1: The Y coordinate of the starting point of the line.
  • x2: The X coordinate of the ending point of the line.
  • y2: The Y coordinate of the ending point of the line.

Code

Click to collapse [-]
Function source

function getDistanceBetweenPointAndSegment2D(pointX, pointY, x1, y1, x2, y2)
	local A = pointX - x1
	local B = pointY - y1
	local C = x2 - x1
	local D = y2 - y1
 
	local point = A * C + B * D
	local lenSquare = C * C + D * D
	local parameter = point / lenSquare
 
	local shortestX
	local shortestY
 
	if parameter < 0 then
		shortestX = x1
    		shortestY = y1
	elseif parameter > 1 then
		shortestX = x2
		shortestY = y2
	else
		shortestX = x1 + parameter * C
		shortestY = y1 + parameter * D
	end
 
	local distance = getDistanceBetweenPoints2D(pointX, pointY, shortestX, shortestY)
 
	return distance
end

Example

Click to collapse [-]
Client

This code draws a red line, enables mouse cursor and tells client how far from the line was clicked.

-- Draw a red line
addEventHandler("onClientRender", getRootElement(),
	function()
		dxDrawLine(20, 20, 200, 100, tocolor(255, 0, 0))
	end
)
 
-- Show cursor
showCursor(true)
 
-- Fires when left mouse button is clicked
function cursorPositionFromTheRedLine(button, state, clickedX, clickedY)
	-- Allow function to continue only if left button was pressed and button was pressed down
	if button ~= "left" or state == "up" then
		return
	end
 
	-- Calculate the distance
	local distance = getDistanceBetweenPointAndSegment2D(clickedX, clickedY, 20, 20, 200, 100)
 
	-- Tell user the distance
	outputChatBox("You clicked "..distance.." away from the Red Line!")
end
 
-- Read mouse clicks
addEventHandler("onClientClick", root, cursorPositionFromTheRedLine)

Code from: http://www.allegro.cc/forums/thread/589720

See Also

Dialog-information.png This article needs checking.

Reason: The function I added at the bottom should be scripted by someone. --Ransom 21:45, 2 May 2012 (UTC)
  • callClientFunction » This function allows you to call any clientside function from the server's side.
  • callServerFunction » This function allows you to call any server-side function from the client's side.
  • centerWindow » This function center the window in any resolution.
  • Check » This function checks if it's arguments are of the right types and calls the error-function if one isn't.
  • doForAllElements » This function can be used to execute a specified function for all elements of a specified type.
  • dxDrawColorText » This function draws a dx text with #RRGGBB color codes support.
  • findRotation » Takes two points and returns the direction from point A to point B.
  • FormatDate » Formats a date on the basis of a format string and returns it.
  • getAge » This function calculates the age of a birthday.
  • getCursorMoveOn » This function checks in which way the cursor is currently moving.
  • getElementSpeed » This function allows you to get element speed in kph or mph units.
  • IfElse » Returns one of two values based on a boolean expression.
  • isLeapYear » Checks if the given year is a leap year.
  • iterElements » Returns an iterator for your for loops saving time typing ipairs( getElementsByType( type ) ), instead you type: iterElements( type ).
  • math.round » Rounds a number whereas the number of decimals to keep and the method may be set.
  • onVehicleWeaponFire » This code implements an event that is triggered when a player in a vehicle fires a vehicles weapon.
  • RGBToHex » This function returns a string representing the color in hexadecimal.
  • setElementSpeed » This function allows you to set moving element speed in kph or mph units.
  • setVehicleGravityPoint » This clientside function sets a vehicle's gravity in the direction of a 3 dimensional coordinate with the strength specified.
  • string.explode » This function splits a string at a given separator pattern and returns a table with the pieces.
  • table.copy » This function copies a whole table and all the tables in that table.
  • table.map » This function goes through a table and replaces every field with the return of the passed function, where the field's value is passed as first argument and optionally more arguments.
  • table.size » Finds the absolute size of a table.
  • toHex » This function converts a decimal number to a hexadecimal number, as a fix to be used clientside.
  • var_dump »This function outputs information about one or more variables using outputConsole().
  • multi_check » This function checks one element to many, handy and clean.
  • isElementInPhotograph » This function checks if an element was in the player's camera picture. This needs writing still. --Ransom 21:45, 2 May 2012 (UTC)