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
def getArea ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) NEW_LINE DEDENT def isInside ( triangle , point ) : NEW_LINE INDENT A , B , C = triangle NEW_LINE x , y = point NEW_LINE ABC = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) NEW_LINE BPC = getArea ( x , y , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) NEW_LINE APC = getArea ( A [ 0 ] , A [ 1 ] , x , y , C [ 0 ] , C [ 1 ] ) NEW_LINE APB = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , x , y ) NEW_LINE return ABC == ( APC + APB + BPC ) NEW_LINE DEDENT def countPoints ( rectangle , triangle , points ) : NEW_LINE INDENT triangle1 = rectangle [ 1 : ] NEW_LINE triangle2 = rectangle [ : 3 ] NEW_LINE triangle3 = rectangle [ : 2 ] NEW_LINE triangle3 . append ( rectangle [ 3 ] ) NEW_LINE triangle4 = rectangle [ - 2 : ] NEW_LINE triangle4 . append ( rectangle [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for point in points : NEW_LINE INDENT condOne = isInside ( triangle1 , point ) NEW_LINE condTwo = isInside ( triangle2 , point ) NEW_LINE condThree = isInside ( triangle3 , point ) NEW_LINE condFour = isInside ( triangle4 , point ) NEW_LINE condFive = isInside ( triangle , point ) NEW_LINE if ( condOne or condTwo or condThree   \ or condFour ) and condFive : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT rectangle = [ [ 6 , 5 ] , [ 2 , 2 ] , [ 2 , 1 ] , [ 5 , 5 ] ] NEW_LINE points = [ [ 1 , 1 ] , [ 6 , 1 ] , [ 6 , 6 ] , [ 1 , 6 ] ] NEW_LINE triangle = [ [ 4 , 4 ] , [ 0 , 4 ] , [ 0 , - 2 ] ] NEW_LINE countPoints ( points , triangle , rectangle ) NEW_LINE