SRL Archive - SRL Snippets

SPS.isInPolygon();


The Mayor

2014-11-13 04:32:51

This has been added to SPS-RS3, so no need to copy into your scripts


A function that checks whether you are inside a polygon. This is more customisable than using a simple pointInBox. The polygon can have 3+ vertices, as demonstrated with the SPS Path Generator:





Just declare your polygon as a TPA, exactly the same way you do when making a SPS path (you can just copy the TPA from the path generator):


function TSPSArea.isInPolygon(const poly: array of TPoint): boolean;
var
p: TPoint;
i, j: integer;
begin
result := false;
p := self.getPlayerPos();
j := high(poly);

for i := low(poly) to high(poly) do
begin
if ((((poly[i].y <= p.y) and (p.y < poly[j].y)) or ((poly[j].y <= p.y) and
(p.y < poly[i].y)) ) and (p.x < ((poly[j].x - poly[i].x) *
(p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))) then
result := not result;

j := i;
end;
end;


Some test code would look something like:


var
Polygon: array of TPoint;

begin
clearDebug();
setupSRL();
sps.setup('DRAYNOR_01', RUNESCAPE_OTHER);

Polygon := [Point(159, 259), Point(117, 345), Point(224, 346), Point(173, 271)];

repeat
writeLn(sps.isInPolygon(Polygon));
until false;
end.

Hoodz

2014-11-13 04:50:32

Gj mayor

NKN

2014-11-13 06:38:30

Nice work, mate.

Fitta

2014-11-13 07:31:32

From: The Mayor
A function that checks whether you are inside a polygon. This is more customisable than using a simple pointInBox. The polygon can have 3+ vertices, as demonstrated with the SPS Path Generator:





Just declare your polygon as a TPA, exactly the same way you do when making a SPS path (you can just copy the TPA from the path generator):


function TSPSArea.isInPolygon(const poly: array of TPoint): boolean;
var
p: TPoint;
i, j: integer;
begin
result := false;
p := self.getPlayerPos();
j := high(poly);

for i := low(poly) to high(poly) do
begin
if ((((poly[i].y <= p.y) and (p.y < poly[j].y)) or ((poly[j].y <= p.y) and
(p.y < poly[i].y)) ) and (p.x < ((poly[j].x - poly[i].x) *
(p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))) then
result := not result;

j := i;
end;
end;


Some test code would look something like:


var
Polygon: array of TPoint;

begin
clearDebug();
setupSRL();
sps.setup('DRAYNOR_01', RUNESCAPE_OTHER);

Polygon := [Point(159, 259), Point(117, 345), Point(224, 346), Point(173, 271)];

repeat
writeLn(sps.isInPolygon(Polygon));
until false;
end.


Nice, would you mind if I steal this for OSRS? (sometime in the future)

The Mayor

2014-11-13 16:47:34

From: Fitta
Nice, would you mind if I steal this for OSRS? (sometime in the future)


Go for it