fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. static class PointF {
  7. public float x;
  8. public float y;
  9. public PointF(float x, float y) {this.x=x; this.y=y;}
  10. }
  11.  
  12. private static PointF sub(PointF a, PointF b) {
  13. return new PointF(a.x-b.x, a.y-b.y);
  14. }
  15.  
  16. private static float getPerpDotProduct(final PointF p1, final PointF p2) {
  17. return p1.x * p2.y - p1.y * p2.x;
  18. }
  19.  
  20. private static boolean isInside(PointF a, PointF b, PointF c, PointF p) {
  21. final float c1 = getPerpDotProduct(sub(a,b), sub(a,p));
  22. final float c2 = getPerpDotProduct(sub(b,c), sub(b,p));
  23. final float c3 = getPerpDotProduct(sub(c,a), sub(c,p));
  24. return ((c1 >= 0 && c2 >= 0 & c3 >= 0) || (c1 <= 0 && c2 <= 0 && c3 <= 0));
  25. }
  26.  
  27. public static void main (String[] args) throws java.lang.Exception
  28. {
  29. PointF a = new PointF(10, 1);
  30. PointF b = new PointF(1, 10);
  31. PointF c = new PointF(2, 2);
  32. PointF p1 = new PointF(3, 3);
  33. PointF p2 = new PointF(0, 0);
  34. System.out.println(isInside(a,b,c,p1));
  35. System.out.println(isInside(a,b,c,p2));
  36. }
  37. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
true
false