Java Program to Perform Polygon Containment Test

This is a Java Program to check whether a polygon contains another polygon or not. The class returns true if one polygon contains another, false otherwise.

Here is the source code of the Java Program to Perform Polygon Containment Test. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to test whether a polygon is inside another polygon
  2. class Point
  3. {
  4.     int x, y;
  5.  
  6.     Point()
  7.     {
  8.     }
  9.  
  10.     Point(int p, int q)
  11.     {
  12.         x = p;
  13.         y = q;
  14.     }
  15. }
  16.  
  17. public class Position_Point_WRT_Polygon
  18. {
  19.  
  20.     public static boolean onSegment(Point p, Point q, Point r)
  21.     {
  22.         if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x)
  23.                 && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
  24.             return true;
  25.         return false;
  26.     }
  27.  
  28.     public static int orientation(Point p, Point q, Point r)
  29.     {
  30.         int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  31.  
  32.         if (val == 0)
  33.             return 0;
  34.         return (val > 0) ? 1 : 2;
  35.     }
  36.  
  37.     public static boolean doIntersect(Point p1, Point q1, Point p2, Point q2)
  38.     {
  39.  
  40.         int o1 = orientation(p1, q1, p2);
  41.         int o2 = orientation(p1, q1, q2);
  42.         int o3 = orientation(p2, q2, p1);
  43.         int o4 = orientation(p2, q2, q1);
  44.  
  45.         if (o1 != o2 && o3 != o4)
  46.             return true;
  47.  
  48.         if (o1 == 0 && onSegment(p1, p2, q1))
  49.             return true;
  50.  
  51.         if (o2 == 0 && onSegment(p1, q2, q1))
  52.             return true;
  53.  
  54.         if (o3 == 0 && onSegment(p2, p1, q2))
  55.             return true;
  56.  
  57.         if (o4 == 0 && onSegment(p2, q1, q2))
  58.             return true;
  59.  
  60.         return false;
  61.     }
  62.  
  63.     public static boolean isInside(Point polygon[], int n, Point p)
  64.     {
  65.         int INF = 10000;
  66.         if (n < 3)
  67.             return false;
  68.  
  69.         Point extreme = new Point(INF, p.y);
  70.  
  71.         int count = 0, i = 0;
  72.         do
  73.         {
  74.             int next = (i + 1) % n;
  75.             if (doIntersect(polygon[i], polygon[next], p, extreme))
  76.             {
  77.                 if (orientation(polygon[i], p, polygon[next]) == 0)
  78.                     return onSegment(polygon[i], p, polygon[next]);
  79.  
  80.                 count++;
  81.             }
  82.             i = next;
  83.         } while (i != 0);
  84.  
  85.         return (count & 1) == 1 ? true : false;
  86.     }
  87.  
  88.     public static Boolean DoesPolygonContainPolygon(Point[] p1, Point[] p2)
  89.     {
  90.         Point p;
  91.         for (int i = 0; i < p2.length; i++)
  92.         {
  93.             p = new Point(p2[i].x, p2[i].y);
  94.             if (!isInside(p1, p1.length, p))
  95.                 return false;
  96.         }
  97.         return true;
  98.     }
  99.  
  100.     public static void main(String args[])
  101.     {
  102.         Point polygon1[] = { new Point(0, 0), new Point(10, 0),
  103.                 new Point(10, 10), new Point(0, 10) };
  104.  
  105.         Point polygon2[] = { new Point(0, 0), new Point(15, 5), new Point(5, 0) };
  106.  
  107.         Point polygon3[] = { new Point(0, 0), new Point(10, 0),
  108.                 new Point(10, 10), new Point(0, 10), new Point(5, 5) };
  109.  
  110.         System.out.println("Polygon 1 contains Polygon 2 :"
  111.                 + DoesPolygonContainPolygon(polygon1, polygon2));
  112.  
  113.         System.out.println("Polygon 3 contains Polygon 1 :"
  114.                 + DoesPolygonContainPolygon(polygon3, polygon1));
  115.  
  116.     }
  117. }

Output:

$ javac Polygon_Containment_Test.java
$ java Polygon_Containment_Test
 
Polygon 1 contains Polygon 2 :false
Polygon 3 contains Polygon 1 :true

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.