Posted By

GeorgeCross on 12/15/12


Tagged

Shape vertices colision


Versions (?)

Is point inside a convex shape?


 / Published in: ActionScript 3
 

The convex polygon algorithm, which, as the name says, only works for convex polygons and is based on whether or not a point is on a certain “side” of every edge of the polygon.

  1. public function isPointInsideShape(_point:Vector3D, _shapeVerices:Vector.<Vector3D>):Boolean
  2. {
  3. var _numberOfSides:int = shapeVertices.length;
  4. var _i:int = 0;
  5. var _leftOrRightSide:Boolean = false;
  6. var _firstEdgePoint:Vector3D = null;
  7. var _secondEdgePoint:Vector3D = null;
  8.  
  9. while(_i < _numberOfSides)
  10. {
  11. _firstEdgePoint = _shapeVertices[_i];
  12. _secondEdgePoint = _shapeVertices[(_i + 1) % _numberOfSides];
  13.  
  14. if(_i == 0){
  15. // Determining if the point is to the left or to the right of first edge true for left, false for right
  16. _leftOrRightSide = ((_point.y - _firstEdgePoint.y) * (_secondEdgePoint.x - _firstEdgePoint.x)) - ((_point.x - _firstEdgePoint.x) * (_secondEdgePoint.y - _firstEdgePoint.y)) > 0;
  17. }
  18. else
  19. {
  20. // Now all edges must be on the same side
  21. if(_leftOrRightSide && ((_point.y - _firstEdgePoint.y) * (_secondEdgePoint.x - _firstEdgePoint.x)) - ((_point.x - _firstEdgePoint.x) * (_secondEdgePoint.y - _firstEdgePoint.y)) < 0)
  22. {
  23. // Not all edges are on the same side!
  24. return false;
  25. }
  26. else if(!_leftOrRightSide && ((_point.y - _firstEdgePoint.y) * (_secondEdgePoint.x - _firstEdgePoint.x)) - ((_point.x - _firstEdgePoint.x) * (_secondEdgePoint.y - _firstEdgePoint.y)) > 0)
  27. {
  28. // Not all edges are on the same side!
  29. return false;
  30. }
  31. }
  32. _i++;
  33. }
  34.  
  35. // We looped through all vertices and didn't detect different sides
  36. return true;
  37. }

Report this snippet  

You need to login to post a comment.