Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points | Java program to implement the above approach ; Function to get the count of ractangles ; Store distinct horizontal lines ; Store distinct Vertical lines ; Insert horizontal line passing through 0 ; Insert vertical line passing through 0. ; Insert horizontal line passing through rectangle [ 3 ] [ 0 ] ; Insert vertical line passing through rectangle [ 3 ] [ 1 ] ; Insert all horizontal and vertical lines passing through the given array ; Insert all horizontal lines ; Insert all vertical lines ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int cntRect ( int points [ ] [ ] , int N , int rectangle [ ] [ ] ) { HashSet < Integer > cntHor = new HashSet < > ( ) ; HashSet < Integer > cntVer = new HashSet < > ( ) ; cntHor . add ( 0 ) ; cntVer . add ( 0 ) ; cntHor . add ( rectangle [ 3 ] [ 0 ] ) ; cntVer . add ( rectangle [ 3 ] [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { cntHor . add ( points [ i ] [ 0 ] ) ; cntVer . add ( points [ i ] [ 1 ] ) ; } return ( cntHor . size ( ) - 1 ) * ( cntVer . size ( ) - 1 ) ; } public static void main ( String args [ ] ) { int rectangle [ ] [ ] = { { 0 , 0 } , { 0 , 5 } , { 5 , 0 } , { 5 , 5 } } ; int points [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; int N = points . length ; System . out . println ( cntRect ( points , N , rectangle ) ) ; } }