fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.awt.geom.Point2D;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. static class CPoint extends Point2D.Double
  12. {
  13. int id;
  14. double theta;
  15. CPoint prev, next;
  16.  
  17. public CPoint(int id, double x, double y)
  18. {
  19. super(x, y);
  20. this.id = id;
  21. theta = Math.atan2(y, x);
  22. if(theta < 0) theta = 2*Math.PI + theta;
  23. }
  24.  
  25. public String toString()
  26. {
  27. return String.format("%d : (%f,%f) : %f [(%f,%f) : (%f,%f)]", id, getX(), getY(), theta, prev.getX(), prev.getY(), next.getX(), next.getY());
  28. }
  29. }
  30.  
  31. public static void main(String[] args)
  32. {
  33. double[][] coords = {{-0.708, 0.707, 0.309, -0.951, 0.587, -0.809},
  34. {1, 0, 0, 1, -1, 0, 0, -1, 0.708, -0.708}};
  35.  
  36. double areaSum = 0;
  37.  
  38. List<CPoint> pts = new ArrayList<>();
  39. for(int i=0; i<coords.length; i++)
  40. {
  41. List<CPoint> poly = new ArrayList<>();
  42. for(int j=0; j<coords[i].length; j+=2)
  43. {
  44. poly.add(new CPoint(i, coords[i][j], coords[i][j+1]));
  45. }
  46.  
  47. poly.sort((a, b) -> Double.compare(a.theta, b.theta));
  48. pts.addAll(poly);
  49.  
  50. int n = poly.size();
  51. for(int j=0; j<n; j++)
  52. {
  53. poly.get(j).prev = poly.get((j + n - 1) % n);
  54. poly.get(j).next = poly.get((j + 1) % n);
  55. }
  56.  
  57. areaSum += area(poly);
  58. }
  59.  
  60. pts.sort((a, b) -> Double.compare(a.theta, b.theta));
  61.  
  62. List<Point2D> intersections = new ArrayList<>();
  63. int n = pts.size();
  64. for(int i=0, j=n-1; i<n; j=i++)
  65. {
  66. if(pts.get(j).id != pts.get(i).id)
  67. {
  68. intersections.add(intersect(pts.get(j), pts.get(j).next, pts.get(i).prev, pts.get(i)));
  69. }
  70. }
  71.  
  72. double areaInt = area(intersections);
  73. double iou = areaInt/(areaSum - areaInt);
  74. System.out.println(iou);
  75. }
  76.  
  77. static double area(List<? extends Point2D> poly)
  78. {
  79. double area = 0;
  80. for(int i=0, j=poly.size()-1; i<poly.size(); j=i++)
  81. area += (poly.get(j).getX() * poly.get(i).getY()) - (poly.get(i).getX() * poly.get(j).getY());
  82. return Math.abs(area)/2;
  83. }
  84.  
  85. // https://r...content-available-to-author-only...e.org/wiki/Find_the_intersection_of_two_lines#Java
  86. static Point2D intersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  87. {
  88. double a1 = p2.getY() - p1.getY();
  89. double b1 = p1.getX() - p2.getX();
  90. double c1 = a1 * p1.getX() + b1 * p1.getY();
  91.  
  92. double a2 = p4.getY() - p3.getY();
  93. double b2 = p3.getX() - p4.getX();
  94. double c2 = a2 * p3.getX() + b2 * p3.getY();
  95.  
  96. double delta = a1 * b2 - a2 * b1;
  97. return new Point2D.Double((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);
  98. }
  99. }
Success #stdin #stdout 0.09s 48172KB
stdin
Standard input is empty
stdout
0.12403616470027268