Tuesday, February 10, 2015

Determine if two line segments intersect



Write a function to tell if two line segments intersect or not
This is a very interesting geometry problem. To solve this problem, first let's consider several cases:
Intersect, not parallel

Intersect, parallel

Intersect, q lies on rs
Not intersect, not parallel

Not intersect, parallel

So, the problem becomes :
1. determine if any end point on one lines lies on the other line, if there is one, then of course two lines intersect
2. determine if two lines are parallel

For the second problem, we need to introduce a new notion: orientation. Orientation of an ordered triplet of point on a plane can be:

  • clockwise;
  • counterclockwise;
  • parallel


Figure source: http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/

Two lines intersect if one of these conditions are satisfied:

1. (p, q, r) and (p, q, s) have different orientations AND (r, s, p) and (r, s, q) have different orientations;
Different orientation, intersect

Same orientation, not intersect

Determine the orientation, we need to use a little bit our high school or college math: cross product.


 Ok, now the problem becomes very clear. 



/**
  * check if two line segments pq and rs intersect with each other
  * @param p
  * @param q
  * @param r
  * @param s
  * @return
  */
 public static boolean hasIntersection(Point p, Point q, Point r, Point s) {
  if (p == null || q == null || r == null || s == null)
   throw new NullPointerException("Null points!");
  if (p.equals(q) || r.equals(s))
   throw new IllegalArgumentException("Not a line!");
  int ori1 = orientation(p, q, r);
  int ori2 = orientation(p, q, s);
  int ori3 = orientation(r, s, p);
  int ori4 = orientation(r, s, q);
  if (ori1 != ori2 && ori3 != ori4)
   return true;
  if (ori1 == 0 && isOnSegment(p, q, r))
   return true;
  if (ori2 == 0 && isOnSegment(p, q, s))
   return true;
  if (ori3 == 0 && isOnSegment(r, s, p))
   return true;
  if (ori4 == 0 && isOnSegment(r, s, q))
   return true;
  return false;
 }
 /**
  * check if point r is on line segment pq
  * @param p
  * @param q
  * @param r
  */
 private static boolean isOnSegment(Point p, Point q, Point r) {
  return r.x <= Math.max(p.x, q.x) && r.x >= Math.min(p.x, q.x) 
    && r.y <= Math.max(p.y, q.y) && r.y >= Math.min(p.y, q.y);
 }
 /**
  * return the orientation of the triplet
  * 0: parallel
  * 1: clockwise
  * 2: counterclockwise
  * cross product
  * http://en.wikipedia.org/wiki/Cross_product
  * pq & pr
  * @param p
  * @param q
  * @param r
  * @return
  */
 private static int orientation(Point p, Point q, Point r) {
  double orientation = (p.x - q.x) * (p.y - r.y) - (p.y - q.y) * (p.x - r.x);
  if (orientation == 0)
   return 0;
  return orientation > 0 ? 1 : 2;
 }

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete