Advertisement
dawdadwg

Untitled

Sep 15th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import processing.core.PApplet;
  2.  
  3. public class Line {
  4.  
  5.     private float x1, x2, y1, y2;
  6.     private double iX, iY;
  7.    
  8.     public Line(double x1, double y1, double x2, double y2)
  9.     {
  10.         this.x1 = (float)x1;
  11.         this.y1 = (float)y1;
  12.         this.x2 = (float)x2;
  13.         this.y2 = (float)y2;
  14.     }
  15.    
  16.     public void setPoint2(double x2, double y2)
  17.     {
  18.         this.x2 = (float)x2;
  19.         this.y2 = (float)y2;
  20.     }
  21.    
  22.     public void draw(PApplet drawer)
  23.     {
  24.         drawer.line(x1, y1, x2, y2);
  25.        
  26. //      drawer.line(50, 10, 100, 10); // horizontal parallel lines for testing intersecting
  27. //      drawer.line(50, 40, 100, 40);
  28.        
  29.         drawer.ellipse((float)iX, (float)iY, 5, 5);
  30.     }
  31.    
  32.     public boolean intersects(Line other)
  33.     {  
  34.         if (((x1 - x2) * (other.y1 - other.y2)) - ((y1 - y2) * (other.x1 - other.x2)) == 0)
  35.         {
  36.             return false;
  37.         } else
  38.         {  
  39.             iX = ((x1 * y2 - y1 * x2) * (other.x1 - other.x2) - (x1 - x2) * (other.x1 * other.y2 - other.y1 * other.x2))
  40.             /((x1 - x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 - other.x2));
  41.             iY = ((x1 * y2 - y1 * x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 * other.y2 - other.y1 * other.x2))
  42.             /((x1 - x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 - other.x2));
  43.            
  44.             if (x1 < other.x1 && x2 < other.x2 && y1 < other.y1 && y2 < other.y2) // if red line is smaller
  45.             {
  46.                 if (x1 > x2 && y1 > y2)
  47.                 {
  48.                     if (iX <= x1 && iX >= x2 && iY <= y1 && iY >= y2)
  49.                     {
  50.                         return true;
  51.                     } else
  52.                     {
  53.                         return false;
  54.                     }
  55.                 } else if (x2 > x1 && y1 > y2)
  56.                 {
  57.                     if (iX <= x2 && iX >= x1 && iY <= y1 && iY >= y2)
  58.                     {
  59.                         return true;
  60.                     } else
  61.                     {
  62.                         return false;
  63.                     }
  64.                 } else if (x1 > x2 && y2 > y1)
  65.                 {
  66.                     if (iX <= x1 && iX >= x2 && iY <= y2 && iY >= y1)
  67.                     {
  68.                         return true;
  69.                     } else
  70.                     {
  71.                         return false;
  72.                     }
  73.                 } else if (x2 > x1 && y2 > y1)
  74.                 {
  75.                     if (iX <= x2 && iX >= x1 && iY <= y2 && iY >= y1)
  76.                     {
  77.                         return true;
  78.                     } else
  79.                     {
  80.                         return false;
  81.                     }
  82.                 } else
  83.                 {
  84.                     return false;
  85.                 }
  86.             } else // if green line is smaller
  87.             {
  88.                 if (other.x1 > other.x2 && other.y1 > other.y2)
  89.                 {
  90.                     if (iX <= other.x1 && iX >= other.x2 && iY <= other.y1 && iY >= other.y2)
  91.                     {
  92.                         return true;
  93.                     } else
  94.                     {
  95.                         return false;
  96.                     }
  97.                 } else if (other.x2 > other.x1 && other.y1 > other.y2)
  98.                 {
  99.                     if (iX <= other.x2 && iX >= other.x1 && iY <= other.y1 && iY >= other.y2)
  100.                     {
  101.                         return true;
  102.                     } else
  103.                     {
  104.                         return false;  
  105.                     }
  106.                 } else if (other.x1 > other.x2 && other.y2 > other.y1)
  107.                 {
  108.                     if (iX <= other.x1 && iX >= other.x2 && iY <= other.y2 && iY >= other.y1)
  109.                     {
  110.                         return true;
  111.                     } else
  112.                     {
  113.                         return false;
  114.                     }
  115.                 } else if (other.x2 > other.x1 && other.y2 > other.y1)
  116.                 {
  117.                     if (iX <= other.x2 && iX >= other.x1 && iY <= other.y2 && iY >= other.y1)
  118.                     {
  119.                         return true;
  120.                     } else
  121.                     {
  122.                         return false;
  123.                     }
  124.                 } else
  125.                 {
  126.                     return false;
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement