Skip to content

Commit

Permalink
Add: isInBox and isInPolygon methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTS committed May 22, 2015
1 parent 2a29df7 commit f322021
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/sps-rs3.simba
Expand Up @@ -405,6 +405,55 @@ begin
print(self.getName()+'.blindWalk(): result = '+boolToStr(result));
end;

(*
Returns true if the player is inside the TBox 'box'

Example:
myArea := intToBox(10, 10, 100, 150);
sps.isInBox(myArea);
*)
function TSPSArea.isInBox(box: TBox): boolean;
begin
result := PointInBox(self.getPlayerPos(), box);
print(self.getName() + '.isInBox: result = ' + boolToStr(result));
end;

(*
Returns true if the player is inside the polygon 'polygon'

Example:
myPoly := [Point(10, 10), Point(15, 10), Point(25, 20)];
sps.isInPolygon(myPoly);
*)
function TSPSArea.isInPolygon(polygon: TPointArray): boolean;
var
p: TPoint;
i, j: integer;
begin
result := false;

if (length(polygon) < 3) then
begin
print(self.getName() + '.isInPolygon: less than 3 vertice points in TPA', TDebug.ERROR);
exit;
end;

p := self.getPlayerPos();
j := high(polygon);

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

j := i;
end;

print(self.getName() + '.isInPolygon: result = ' + boolToStr(result));
end;




0 comments on commit f322021

Please sign in to comment.