Dead men's shot


#1

Hi i am running into problems. I can solve the triangle and the square, but i run into problems with the other shapes.
since i was able to solve the triangle I thought I will split the polygon into many triangles and then check on them, but i always fail. then i decided to also check on the lines of the triangles since i was not sure if they are included in the insideTriangle.

....
bool runPolygonAsTriangle(List polygon, List shot){
    bool hit = false;
    for (var i = 0, len = polygon.length; i+2 < len; ++i) {
      List point1 = polygon[0];
      List point2 = polygon[i+1];
      List point3 = polygon[i+2];
      List polygonTriangle = [point1,point2,point3];
      bool inside = isInside(polygonTriangle, polygonTriangle.length, shot);    
      bool onLine = isOnLineOfTriangle(point1,point2,point3,shot);
      stderr.writeln('shot $shot hit $inside inside triangle  on lines $onLine of $polygonTriangle');
      if(inside || onLine){
          hit = true;
         break;
          
       }
    }
    return hit;
}
bool isOnLineOfTriangle(List p1,List p2,List p3, List shot){
    bool value = false;
    if(isOnLine(p1,p2,shot) || isOnLine(p1,p3,shot) || isOnLine(p2,p3,shot)){
        value = true;
    }
    return value;
}
bool isOnLine(List p1,List p2,List shot){
    return getDistance(p1, shot) + getDistance(p2, shot) == getDistance(p1, p2);
}

num getDistance(List p1, List p2) {
  return sqrt(pow((p2[0] - p1[0]), 2) + pow((p2[1] - p1[1]), 2));
}
....

love any help :slight_smile:


#2

Yo,

You can’t just take every 3 vertices make a triangle and expect to cover the whole polygon.
See https://rechneronline.de/pi/img/w/hexagon-short-diagonals.png
You’re missing out like 50% of the space

It could work if you were testing every possible triplet.
But you can also find a better solution


#3

Hey,

thank you! actuially with the hexagon, my code would work haha
but not with complex polygon.

thanks for pointing that out!

:slight_smile: