Number of points lying inside a rectangle as well as a triangle | 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
function getArea ( x1 , y1 , x2 , y2 , x3 , y3 ) { return Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) ; } function isInside ( triangle , point ) { let A = triangle [ 0 ] ; let B = triangle [ 1 ] ; let C = triangle [ 2 ] ; let x = point [ 0 ] ; let y = point [ 1 ] ; let ABC = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; let BPC = getArea ( x , y , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; let APC = getArea ( A [ 0 ] , A [ 1 ] , x , y , C [ 0 ] , C [ 1 ] ) ; let APB = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , x , y ) ; return ABC == ( APC + APB + BPC ) ? 1 : 0 ; } function countPoints ( rectangle , triangle , points ) { let n = rectangle . length ; let triangle1 = [ ] ; for ( let i = 1 ; i < n ; i ++ ) triangle1 . push ( rectangle [ i ] ) ; let triangle2 = [ ] ; for ( let i = 0 ; i < 3 ; i ++ ) { triangle2 . push ( rectangle [ i ] ) ; } let triangle3 = [ ] ; for ( let i = 0 ; i < 2 ; i ++ ) { triangle3 . push ( rectangle [ i ] ) ; } triangle3 . push ( rectangle [ 3 ] ) ; let triangle4 = [ ] ; for ( let i = n - 2 ; i < n ; i ++ ) { triangle4 . push ( rectangle [ i ] ) ; } triangle4 . push ( rectangle [ 0 ] ) ; let ans = 0 ; for ( let point = 0 ; point < points . length ; point ++ ) { let condOne = isInside ( triangle1 , points [ point ] ) ; let condTwo = isInside ( triangle2 , points [ point ] ) ; let condThree = isInside ( triangle3 , points [ point ] ) ; let condFour = isInside ( triangle4 , points [ point ] ) ; let condFive = isInside ( triangle , points [ point ] ) ; if ( ( condOne != 0 condTwo != 0 condThree != 0 condFour != 0 ) && condFive != 0 ) ans += 1 ; } document . write ( ans + " " ) ; } let rectangle = [ ] ; let points = [ ] ; let triangle = [ ] ; rectangle . push ( [ 6 , 5 ] ) ; rectangle . push ( [ 2 , 2 ] ) ; rectangle . push ( [ 2 , 1 ] ) ; rectangle . push ( [ 5 , 5 ] ) ; points . push ( [ 1 , 1 ] ) ; points . push ( [ 6 , 1 ] ) ; points . push ( [ 6 , 6 ] ) ; points . push ( [ 1 , 6 ] ) ; triangle . push ( [ 4 , 4 ] ) ; triangle . push ( [ 0 , 4 ] ) ; triangle . push ( [ 0 , - 2 ] ) ; countPoints ( points , triangle , rectangle ) ;