Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package rs.ac.uns.ftn.nansi.util;
  2.  
  3. import com.badlogic.gdx.math.Vector2;
  4. import com.badlogic.gdx.math.Vector3;
  5.  
  6. public final class Intersector {
  7.  
  8.     static Vector3 tmp = new Vector3();
  9.     static Vector3 tmp1 = new Vector3();
  10.     static Vector3 tmp2 = new Vector3();
  11.     static Vector3 tmp3 = new Vector3();
  12.  
  13.     public static boolean intersectSegmentCircle(Vector2 start, Vector2 end,
  14.             Vector2 center, float squareRadius) {
  15.         tmp.set(end.x - start.x, end.y - start.y, 0);
  16.         tmp1.set(center.x - start.x, center.y - start.y, 0);
  17.         float l = tmp.len();
  18.         float u = tmp1.dot(tmp.nor());
  19.         if (u <= 0) {
  20.             tmp2.set(start.x, start.y, 0);
  21.         } else if (u >= l) {
  22.             tmp2.set(end.x, end.y, 0);
  23.         } else {
  24.             tmp3.set(tmp.scl(u));
  25.             tmp2.set(tmp3.x + start.x, tmp3.y + start.y, 0);
  26.         }
  27.  
  28.         float x = center.x - tmp2.x;
  29.         float y = center.y - tmp2.y;
  30.  
  31.         return x * x + y * y <= squareRadius;
  32.     }
  33.  
  34.     public static boolean intersectSegments(Vector2 p1, Vector2 p2, Vector2 p3,
  35.             Vector2 p4, Vector2 intersection) {
  36.         float x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y, x4 = p4.x, y4 = p4.y;
  37.  
  38.         float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  39.         if (d == 0)
  40.             return false;
  41.  
  42.         float yd = y1 - y3;
  43.         float xd = x1 - x3;
  44.         float ua = ((x4 - x3) * yd - (y4 - y3) * xd) / d;
  45.         if (ua < 0 || ua > 1)
  46.             return false;
  47.  
  48.         float ub = ((x2 - x1) * yd - (y2 - y1) * xd) / d;
  49.         if (ub < 0 || ub > 1)
  50.             return false;
  51.  
  52.         if (intersection != null)
  53.             intersection.set(x1 + (x2 - x1) * ua, y1 + (y2 - y1) * ua);
  54.         return true;
  55.     }
  56.    
  57.     public static boolean intersect(Vector2 p1, Vector2 p2, Vector2 p3,
  58.             Vector2 p4, Vector2 intersection) {
  59.         float x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y, x4 = p4.x, y4 = p4.y;
  60.         if (x1 == x2 && x3 == x4) {
  61.             if (x1 == x3) {
  62.                 if ((y2 >= Math.min(y1, y3) && y2 <= Math.max(y1, y3)) || (y4 >= Math.min(y1, y3) && y4 <= Math.max(y1, y3))){
  63.                     if (intersection != null) {
  64.                         intersection.set(x1, (y1 + y2 + y3 + y4) / 4);
  65.                     }
  66.                     return true;
  67.                 } else {
  68.                     return false;
  69.                 }
  70.             } else {
  71.                 return false;
  72.             }
  73.         } else if (x1 == x2) {
  74.             if ((x3 < x1 && x4 < x1) || (x3 > x1 && x4 > x1)) {
  75.                 return false;
  76.             }
  77.            
  78.             float k = (y4 - y3) / (x4 - x3);
  79.             float n = y4 - k * x4;
  80.            
  81.             if (intersection != null) {
  82.                 intersection.set(x1, k * x1 + n);
  83.             }
  84.             return true;
  85.         } else if (x3 == x4) {
  86.             if ((x1 < x3 && x2 < x3) || (x1 > x3 && x2 > x3)) {
  87.                 return false;
  88.             }
  89.            
  90.             float k = (y2 - y1) / (x2 - x1);
  91.             float n = y2 - k*x2;
  92.             if (intersection != null) {
  93.                 intersection.set(x3, k * x3 + n);  
  94.             }
  95.             return true;
  96.            
  97.         } else {
  98.             float k1 = (y2 - y1) / (x2 - x1);
  99.             float n1 = y2 - k1 * x2;
  100.             float k2 = (y4 - y3) / (x4 - x3);
  101.             float n2 = y4 - k2 * x4;
  102.            
  103.             float [][] A = new float[2][];
  104.             A[0] = new float[2];
  105.             A[1] = new float[2];
  106.            
  107.             float[] b = new float[2];
  108.            
  109.             A[0][0] = -k1;
  110.             A[0][1] = 1;
  111.             A[1][0] = -k2;
  112.             A[1][1] = 1;
  113.            
  114.             b[0] = n1;
  115.             b[1] = n2;
  116.            
  117.             float x0 = x1;
  118.             float y0 = y1;
  119.            
  120.             float x[] = new float[2];
  121.             x[0] = x0;
  122.             x[1] = y0;
  123.            
  124.             float[] asd = gz(A, b, x);
  125.             x0 = asd[0];
  126.             y0 = asd[1];
  127.            
  128.             if (x0 >= Math.min(x1, x2) && x0 <= Math.max(x1, x2) && x0 >= Math.min(x3, x4) && x0 <= Math.max(x3, x4) &&
  129.                 y0 >= Math.min(y1, y2) && y0 <= Math.max(y1, y2) && y0 >= Math.min(y3, y4) && y0 <= Math.max(y3, y4)) {
  130.                 if (intersection != null) {
  131.                     intersection.set(x0, y0);
  132.                 }
  133.                 System.out.println(x0 + "  " + y0);
  134.                 return true;
  135.             } else {
  136.                 return false;
  137.             }
  138.            
  139.         }
  140.        
  141.        
  142.     }
  143.  
  144.     private static float[] gz(float[][] a, float[] b, float[] x0) {
  145.         int n = a.length;
  146.         float x[] = new float[n];
  147.         x[0] = 0;
  148.         x[1] = 0;
  149.         float s;
  150.         for (int it = 0; it < 10; ++it) {
  151.             for (int i = 0; i < n; ++i) {
  152.                 s = 0;
  153.                 for (int j = 0; j < i; ++j) {
  154.                     s += a[i][j] * x[j];
  155.                 }
  156.                 for (int j = i; j < n; ++j) {
  157.                     s += a[i][j] * x0[j];
  158.                 }
  159.                 x[i] = (b[i] - s) / a[i][i];
  160.             }
  161.             float err = 0;
  162.             for (int i = 0; i < n; ++i) {
  163.                 err += Math.pow(x[i] - x0[i], 2);
  164.             }
  165.             if (err < 0.0002) {
  166.                 return x;
  167.             }
  168.             for (int i = 0; i < n; ++i) {
  169.                 x0[i] = x[i];
  170.             }
  171.         }
  172.         return x;
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement