Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.core.PApplet;
- public class Line {
- private float x1, x2, y1, y2;
- private double iX, iY;
- public Line(double x1, double y1, double x2, double y2)
- {
- this.x1 = (float)x1;
- this.y1 = (float)y1;
- this.x2 = (float)x2;
- this.y2 = (float)y2;
- }
- public void setPoint2(double x2, double y2)
- {
- this.x2 = (float)x2;
- this.y2 = (float)y2;
- }
- public void draw(PApplet drawer)
- {
- drawer.line(x1, y1, x2, y2);
- // drawer.line(50, 10, 100, 10); // horizontal parallel lines for testing intersecting
- // drawer.line(50, 40, 100, 40);
- drawer.ellipse((float)iX, (float)iY, 5, 5);
- }
- public boolean intersects(Line other)
- {
- if (((x1 - x2) * (other.y1 - other.y2)) - ((y1 - y2) * (other.x1 - other.x2)) == 0)
- {
- return false;
- } else
- {
- iX = ((x1 * y2 - y1 * x2) * (other.x1 - other.x2) - (x1 - x2) * (other.x1 * other.y2 - other.y1 * other.x2))
- /((x1 - x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 - other.x2));
- iY = ((x1 * y2 - y1 * x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 * other.y2 - other.y1 * other.x2))
- /((x1 - x2) * (other.y1 - other.y2) - (y1 - y2) * (other.x1 - other.x2));
- if (x1 < other.x1 && x2 < other.x2 && y1 < other.y1 && y2 < other.y2) // if red line is smaller
- {
- if (x1 > x2 && y1 > y2)
- {
- if (iX <= x1 && iX >= x2 && iY <= y1 && iY >= y2)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (x2 > x1 && y1 > y2)
- {
- if (iX <= x2 && iX >= x1 && iY <= y1 && iY >= y2)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (x1 > x2 && y2 > y1)
- {
- if (iX <= x1 && iX >= x2 && iY <= y2 && iY >= y1)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (x2 > x1 && y2 > y1)
- {
- if (iX <= x2 && iX >= x1 && iY <= y2 && iY >= y1)
- {
- return true;
- } else
- {
- return false;
- }
- } else
- {
- return false;
- }
- } else // if green line is smaller
- {
- if (other.x1 > other.x2 && other.y1 > other.y2)
- {
- if (iX <= other.x1 && iX >= other.x2 && iY <= other.y1 && iY >= other.y2)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (other.x2 > other.x1 && other.y1 > other.y2)
- {
- if (iX <= other.x2 && iX >= other.x1 && iY <= other.y1 && iY >= other.y2)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (other.x1 > other.x2 && other.y2 > other.y1)
- {
- if (iX <= other.x1 && iX >= other.x2 && iY <= other.y2 && iY >= other.y1)
- {
- return true;
- } else
- {
- return false;
- }
- } else if (other.x2 > other.x1 && other.y2 > other.y1)
- {
- if (iX <= other.x2 && iX >= other.x1 && iY <= other.y2 && iY >= other.y1)
- {
- return true;
- } else
- {
- return false;
- }
- } else
- {
- return false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement