Prüfen ob ein Punkt im Dreieck liegt.

  • GM 8
  • Prüfen ob ein Punkt im Dreieck liegt.

    Wie kann man besonders schnell(performancesparend) berechnen ob ein Punkt im einem Dreieck liegt.

    Mein bisheriger Code funktioniert nur begrenzt. Sobalt man Punkte vertauscht gibt es Fehler. Zusätzlich ist er wahrscheinlich zu langsam für dass was ich vorhabe:
    Spoiler anzeigen

    GML-Quellcode

    1. var ax, ay, bx, by, cx, cy, px, py, pos_h1, pos_h2, pos_h3, w1, h1, w2, h2, w3, h3;
    2. ax = 64;
    3. ay = 128;
    4. bx = 128;
    5. by = 256;
    6. cx = 512;
    7. cy = 384;
    8. px = mouse_x;
    9. py = mouse_y;
    10. w1 = max(ax,bx) - min(ax,bx);
    11. h1 = max(ay,by) - min(ay,by);
    12. pos_h1 = (py - min(ay,by))/h1;
    13. w2 = max(cx,bx) - min(cx,bx);
    14. h2 = max(cy,by) - min(cy,by);
    15. pos_h2 = (py - min(cy,by))/h2;
    16. w3 = max(ax,cx) - min(ax,cx);
    17. h3 = max(ay,cy) - min(ay,cy);
    18. pos_h3 = (py - min(ay,cy))/h3;
    19. if(bx < ax)
    20. pos_h1 = 1-pos_h1;
    21. if(cx < bx)
    22. pos_h2 = 1-pos_h2;
    23. if(cx < ax)
    24. pos_h3 = 1-pos_h3;
    25. arg1 = px >= pos_h1*w1+min(ax,bx);
    26. arg2 = px >= pos_h2*w2+min(cx,bx);
    27. arg3 = px <= pos_h3*w3+min(ax,cx);
    28. if(arg1 and arg2 and arg3)
    29. draw_set_color(c_lime);
    30. else
    31. draw_set_color(c_black);
    32. draw_triangle(ax, ay, bx, by, cx, cy, false);
    Alles anzeigen


    Weswegen ich soviel Wert auf Geschwindigkeit lege, hat den Grund, dass der Code bis zu 1000 mal pro Step ausgeführt werden könnte.

    Hoffe ihr könnt mir weiterhelfen.
  • Benutzer online 2

    2 Besucher