Scripting/Squirrel/Functions/InPoly: Difference between revisions
Jump to navigation
Jump to search
This wiki is using an old backup from 2020
Some information may be old/missing
(Created page with "== Syntax == <pre> InPoly( player.Pos.x, player.Pos.y, -898.2125, -325.3511, -898.2125, -355.5225, -967.0000, -355.5225, -967.0000, -325.3511 ) == true ) </pre>") |
No edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
'''<tt>InPoly</tt>''' tests if a point lies within a polygon consisting of at least three and up to 128 points. If the given point is inside the polygon, the function returns true. If not, it returns false. If the points given do not form a valid polygon, the function throws an error. | |||
This function only uses two-dimensional space. Passing z-coordinates will return undesired output. | |||
== Syntax == | == Syntax == | ||
=== Type 1 === | |||
<source lang=squirrel>InPoly(testX, testY, x1, y1, x2, y2, x3, y3, ...)</source> | |||
'''Arguments''' | |||
* ''float'' '''testX''' - the X-coordinate for the test point | |||
* ''float'' '''testY''' - the Y-coordinate for the test point | |||
* ''float'' '''x1, y1, ...''' - the coordinates of the points of the polygon | |||
=== Type 2 === | |||
<source lang=squirrel>InPoly(testX, testY, [x1, y1, x2, y2, x3, y3, ...])</source> | |||
'''Arguments''' | |||
* ''float'' '''testX''' - the X-coordinate for the test point | |||
* ''float'' '''testY''' - the Y-coordinate for the test point | |||
* ''array'' '''[x1, y1, ...]''' - an array of coordinates for the points of the polygon, similar to Type 1 | |||
=== Type 3 === | |||
<source lang=squirrel>InPoly(testX, testY, "x1, y1, x2, y2, x3, y3")</source> | |||
'''Arguments''' | |||
* ''float'' '''testX''' - the X-coordinate for the test point | |||
* ''float'' '''testY''' - the Y-coordinate for the test point | |||
* ''string'' '''"x1, y1, ..."''' - a string of comma-separated coordinates for the points of the polygon. Passing non-numeric values for the coordinates will not fail, but will produce garbage output. | |||
<gallery> | |||
File:Polydiagram.png|This is a valid illustration of a polygon test... | |||
File:Polydiagram2.png|... as is this... | |||
File:Polydiagram3.png|... and as is this. | |||
</gallery> | |||
== Example == | |||
The following snippets will all test if a player is around the police station in Downtown Vice City. | |||
<source lang=squirrel> | |||
function IsPlayerNearStation(player) { | |||
local x = player.Pos.x; | |||
local y = player.Pos.y; | |||
return InPoly(x, y, -676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311); | |||
} | |||
</source> | |||
<source lang=squirrel> | |||
function IsPlayerNearStation(player) { | |||
local x = player.Pos.x; | |||
local y = player.Pos.y; | |||
local points = [-676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311]; | |||
return InPoly(x, y, points); | |||
} | |||
</source> | |||
<source lang=squirrel> | |||
function IsPlayerNearStation(player) { | |||
local x = player.Pos.x; | |||
local y = player.Pos.y; | |||
local points = "-676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311"; | |||
return InPoly(x, y, points); | |||
} | |||
</source> | |||
== Related Functions == | |||
{{Scripting/Squirrel/Functions/Game Functions}} | |||
Latest revision as of 16:12, 23 April 2016
InPoly tests if a point lies within a polygon consisting of at least three and up to 128 points. If the given point is inside the polygon, the function returns true. If not, it returns false. If the points given do not form a valid polygon, the function throws an error.
This function only uses two-dimensional space. Passing z-coordinates will return undesired output.
Syntax
Type 1
InPoly(testX, testY, x1, y1, x2, y2, x3, y3, ...)
Arguments
- float testX - the X-coordinate for the test point
- float testY - the Y-coordinate for the test point
- float x1, y1, ... - the coordinates of the points of the polygon
Type 2
InPoly(testX, testY, [x1, y1, x2, y2, x3, y3, ...])
Arguments
- float testX - the X-coordinate for the test point
- float testY - the Y-coordinate for the test point
- array [x1, y1, ...] - an array of coordinates for the points of the polygon, similar to Type 1
Type 3
InPoly(testX, testY, "x1, y1, x2, y2, x3, y3")
Arguments
- float testX - the X-coordinate for the test point
- float testY - the Y-coordinate for the test point
- string "x1, y1, ..." - a string of comma-separated coordinates for the points of the polygon. Passing non-numeric values for the coordinates will not fail, but will produce garbage output.
-
This is a valid illustration of a polygon test...
-
... as is this...
-
... and as is this.
Example
The following snippets will all test if a player is around the police station in Downtown Vice City.
function IsPlayerNearStation(player) {
local x = player.Pos.x;
local y = player.Pos.y;
return InPoly(x, y, -676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311);
}
function IsPlayerNearStation(player) {
local x = player.Pos.x;
local y = player.Pos.y;
local points = [-676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311];
return InPoly(x, y, points);
}
function IsPlayerNearStation(player) {
local x = player.Pos.x;
local y = player.Pos.y;
local points = "-676.405, 742.559, -674.1, 829.714, -586.714, 832.304, -587.028, 743.311";
return InPoly(x, y, points);
}