Number of points lying inside a rectangle as well as a triangle | C # program for the above approach ; Function to calculate area of a triangle ; Return the resultant area ; Function to check if a point lies inside a triangle or not ; Calculate area of triangle ABC ; Calculate area of triangle formed by connecting B , C , point ; Calculate area of triangle formed by connecting A , C , point ; Calculate area of triangle formed by connecting A , B , point ; Check if the sum of the areas of above three triangles the same as ABC ; Function to count the number of points lying inside a triangle & rectangle ; Stores the coordinates of the vertices of the triangles ; Stores the number of points lying inside the triangle and rectangle ; Traverse the array of points ; Stores whether the current point lies inside triangle1 or not ; Stores whether the current point lies inside triangle2 or not ; Stores whether the current point lies inside triangle3 or not ; Stores whether the current point lies inside triangle4 or not ; Stores whether the current point lies inside given triangle or not ; If current point lies inside given triangle as well as inside any of the four obtained triangles ; Print the count of points ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int getArea ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . Abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) ; } static int isInside ( List < List < int > > triangle , List < int > point ) { List < int > A = triangle [ 0 ] ; List < int > B = triangle [ 1 ] ; List < int > C = triangle [ 2 ] ; int x = point [ 0 ] ; int y = point [ 1 ] ; int ABC = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; int BPC = getArea ( x , y , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; int APC = getArea ( A [ 0 ] , A [ 1 ] , x , y , C [ 0 ] , C [ 1 ] ) ; int APB = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , x , y ) ; return ABC == ( APC + APB + BPC ) ? 1 : 0 ; } static void countPoints ( List < List < int > > rectangle , List < List < int > > triangle , List < List < int > > points ) { int n = rectangle . Count ; List < List < int > > triangle1 = new List < List < int > > ( ) ; for ( int i = 1 ; i < n ; i ++ ) triangle1 . Add ( rectangle [ i ] ) ; List < List < int > > triangle2 = new List < List < int > > ( ) ; for ( int i = 0 ; i < 3 ; i ++ ) { triangle2 . Add ( rectangle [ i ] ) ; } List < List < int > > triangle3 = new List < List < int > > ( ) ; for ( int i = 0 ; i < 2 ; i ++ ) { triangle3 . Add ( rectangle [ i ] ) ; } triangle3 . Add ( rectangle [ 3 ] ) ; List < List < int > > triangle4 = new List < List < int > > ( ) ; for ( int i = n - 2 ; i < n ; i ++ ) { triangle4 . Add ( rectangle [ i ] ) ; } triangle4 . Add ( rectangle [ 0 ] ) ; int ans = 0 ; foreach ( List < int > point in points ) { int condOne = isInside ( triangle1 , point ) ; int condTwo = isInside ( triangle2 , point ) ; int condThree = isInside ( triangle3 , point ) ; int condFour = isInside ( triangle4 , point ) ; int condFive = isInside ( triangle , point ) ; if ( ( condOne != 0 condTwo != 0 condThree != 0 condFour != 0 ) && condFive != 0 ) ans += 1 ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { List < List < int > > rectangle = new List < List < int > > ( ) ; List < List < int > > points = new List < List < int > > ( ) ; List < List < int > > triangle = new List < List < int > > ( ) ; rectangle . Add ( new List < int > ( ) { 6 , 5 } ) ; rectangle . Add ( new List < int > ( ) { 2 , 2 } ) ; rectangle . Add ( new List < int > ( ) { 2 , 1 } ) ; rectangle . Add ( new List < int > ( ) { 5 , 5 } ) ; points . Add ( new List < int > ( ) { 1 , 1 } ) ; points . Add ( new List < int > ( ) { 6 , 1 } ) ; points . Add ( new List < int > ( ) { 6 , 6 } ) ; points . Add ( new List < int > ( ) { 1 , 6 } ) ; triangle . Add ( new List < int > ( ) { 4 , 4 } ) ; triangle . Add ( new List < int > ( ) { 0 , 4 } ) ; triangle . Add ( new List < int > ( ) { 0 , - 2 } ) ; countPoints ( points , triangle , rectangle ) ; } }