# Number of points lying inside a rectangle as well as a triangle

• Last Updated : 09 Mar, 2022

Given two 2D arrays rectangle[][] and triangle[][], representing the coordinates of vertices of a rectangle and a triangle respectively, and another array points[][] consisting of N coordinates, the task is to count the number of points that lies inside both the rectangle and the triangle.

Examples:

Input: rectangle[][] = {{1, 1}, {6, 1}, {6, 6}, {1, 6}}, triangle[][] = {{4, 4}, {0, 4}, {0, -2}}, points[][] = {{6, 5}, {2, 2}, {2, 1}, {5, 5}}
Output: 2
Explanation: From the above image, it is clear that the coordinates (2, 1) and (2, 2) lie inside both the given rectangle and triangle.
Therefore, the count is 2.

Input: rectangle[][] = {{-2, -2}, {2, -2}, {2, 2}, {-2, 2}}, triangle[][] = {{0, 0}, {1, 1}, {-1, -1}}, points[][] = {{0, 2}, {-2, -2}, {2, -2}}
Output: 2

Approach: The given problem can be solved based on the following observation:

Any three vertices of a rectangle can be connected to form a triangle.
Therefore, the number of triangles possible from a given rectangle is 4.

Therefore, to solve the problem, the idea is to check if the given point lies inside the given triangle and any one of the four triangles obtained from the rectangle or not. Follow the steps below to solve the problem:

• Initialize four lists, say triangle1, triangle2, triangle3 and triangle4, to store the coordinates of the vertices of the four triangles possible from a rectangle.
• Populate the above initialized lists by considering three vertices of the rectangle at a time.
• Initialize a variable, say ans as 0, to store the number of points that lies inside the triangle as well as the rectangle.
• Traverse the array points[][] and check if there exists any point that lies inside any of the four obtained triangles as well as inside the given triangle or not. If found to be true, then increment ans by 1.
• After completing the above steps, print the value of ans as the resultant count.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate area of a triangle``int` `getArea(``int` `x1,``int` `y1,``int` `x2,``int` `y2,``int` `x3,``int` `y3)``{``  ` `    ``// Return the resultant area``    ``return` `abs``((x1 * (y2 - y3) +``                ``x2 * (y3 - y1) +``                ``x3 * (y1 - y2)) / 2);``}` `// Function to check if a point``// lies inside a triangle or not``int` `isInside(vector> triangle, vector<``int``> point)``{` `    ``vector<``int``> A = triangle;``    ``vector<``int``> B = triangle;``    ``vector<``int``> C = triangle;``    ``int` `x = point;``    ``int` `y = point;` `    ``// Calculate area of triangle ABC``    ``int` `ABC = getArea(A, A,``                ``B, B,``                ``C, C);` `    ``// Calculate area of triangle``    ``// formed by connecting B, C, point``    ``int` `BPC = getArea(x, y, B,``                ``B, C,``                ``C);` `    ``// Calculate area of triangle``    ``// formed by connecting A, C, point``    ``int` `APC = getArea(A, A, x,``                ``y, C, C);` `    ``// Calculate area of triangle``    ``// formed by connecting A, B, point``    ``int` `APB = getArea(A, A, B,``               ``B, x, y);` `    ``// Check if the sum of the areas of``    ``// above three triangles the same as ABC``    ``return` `ABC == (APC + APB + BPC);``}` `// Function to count the number of points``// lying inside a triangle & rectangle``void` `countPoints(vector> rectangle,vector> triangle,vector> points){` `    ``// Stores the coordinates of the``    ``// vertices of the triangles``    ``int` `n = rectangle.size();``    ``vector> triangle1;``    ``for``(``int` `i = 1; i < n; i++) triangle1.push_back(rectangle[i]);``    ``vector> triangle2;` `    ``for``(``int` `i = 0; i < 3; i++) triangle2.push_back(rectangle[i]);``    ``vector> triangle3;` `    ``for``(``int` `i = 0; i < 2; i++) triangle3.push_back(rectangle[i]);``    ``triangle3.push_back(rectangle);``    ``vector> triangle4;` `    ``for``(``int` `i = n - 2; i < n; i++) triangle4.push_back(rectangle[i]);` `    ``triangle4.push_back(rectangle);` `    ``// Stores the number of points lying``    ``// inside the triangle and rectangle``    ``int` `ans = 0;` `    ``// Traverse the array of points``    ``for``(``auto` `point:points)``    ``{` `        ``// Stores whether the current point``        ``// lies inside triangle1 or not``        ``int` `condOne = isInside(triangle1, point);` `        ``// Stores whether the current point``        ``// lies inside triangle2 or not``        ``int` `condTwo = isInside(triangle2, point);` `        ``// Stores whether the current point``        ``// lies inside triangle3 or not``        ``int` `condThree = isInside(triangle3, point);` `        ``// Stores whether the current point``        ``// lies inside triangle4 or not``        ``int` `condFour = isInside(triangle4, point);` `        ``// Stores whether the current point``        ``// lies inside given triangle or not``        ``int` `condFive = isInside(triangle, point);` `        ``// If current point lies inside``        ``// given triangle as well as inside``        ``// any of the four obtained triangles``        ``if` `((condOne || condTwo || condThree || condFour) && condFive)``            ``ans += 1;``        ``}` `    ``// Print the count of points``    ``cout << ans;``}` `// Driver Code``int` `main()``{``  ``vector> rectangle = {{6, 5}, {2, 2}, {2, 1}, {5, 5}};``  ``vector> points = {{1, 1}, {6, 1}, {6, 6}, {1, 6}};``  ``vector> triangle = {{4, 4}, {0, 4}, {0, -2}};``  ``countPoints(points, triangle, rectangle);``  ``return` `0;``}` `// This code is contributed by mohit kumar 29.`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG{``    ` `// Function to calculate area of a triangle``static` `int` `getArea(``int` `x1, ``int` `y1, ``int` `x2,``                   ``int` `y2, ``int` `x3, ``int` `y3)``{``    ` `    ``// Return the resultant area``    ``return` `Math.abs((x1 * (y2 - y3) +``                     ``x2 * (y3 - y1) +``                     ``x3 * (y1 - y2)) / ``2``);``}`` ` `// Function to check if a point``// lies inside a triangle or not``static` `int` `isInside(ArrayList> triangle,``                    ``ArrayList point)``{``    ``ArrayList A = triangle.get(``0``);``    ``ArrayList B = triangle.get(``1``);``    ``ArrayList C = triangle.get(``2``);``    ``int` `x = point.get(``0``);``    ``int` `y = point.get(``1``);`` ` `    ``// Calculate area of triangle ABC``    ``int` `ABC = getArea(A.get(``0``), A.get(``1``),``                      ``B.get(``0``), B.get(``1``),``                      ``C.get(``0``), C.get(``1``));`` ` `    ``// Calculate area of triangle``    ``// formed by connecting B, C, point``    ``int` `BPC = getArea(x, y, B.get(``0``),``                  ``B.get(``1``), C.get(``0``),``                            ``C.get(``1``));`` ` `    ``// Calculate area of triangle``    ``// formed by connecting A, C, point``    ``int` `APC = getArea(A.get(``0``), A.get(``1``), x,``                   ``y, C.get(``0``), C.get(``1``));`` ` `    ``// Calculate area of triangle``    ``// formed by connecting A, B, point``    ``int` `APB = getArea(A.get(``0``), A.get(``1``), B.get(``0``),``                      ``B.get(``1``), x, y);`` ` `    ``// Check if the sum of the areas of``    ``// above three triangles the same as ABC``    ``return` `ABC == (APC + APB + BPC) ? ``1` `:``0``;``}`` ` `// Function to count the number of points``// lying inside a triangle & rectangle``static` `void` `countPoints(ArrayList> rectangle,``                        ``ArrayList> triangle,``                        ``ArrayList> points)``{`` ` `    ``// Stores the coordinates of the``    ``// vertices of the triangles``    ``int` `n = rectangle.size();``    ``ArrayList> triangle1 = ``new` `ArrayList>();``    ` `    ``for``(``int` `i = ``1``; i < n; i++)``        ``triangle1.add(rectangle.get(i));``        ` `    ``ArrayList> triangle2 = ``new` `ArrayList>();`` ` `    ``for``(``int` `i = ``0``; i < ``3``; i++)``    ``{``        ``triangle2.add(rectangle.get(i));``    ``}``    ``ArrayList> triangle3 = ``new` `ArrayList>();`` ` `    ``for``(``int` `i = ``0``; i < ``2``; i++)``    ``{``        ``triangle3.add(rectangle.get(i));``    ``}``    ``triangle3.add(rectangle.get(``3``));``    ``ArrayList> triangle4 = ``new` `ArrayList>();`` ` `    ``for``(``int` `i = n - ``2``; i < n; i++)``    ``{``        ``triangle4.add(rectangle.get(i));``    ``}`` ` `    ``triangle4.add(rectangle.get(``0``));`` ` `    ``// Stores the number of points lying``    ``// inside the triangle and rectangle``    ``int` `ans = ``0``;`` ` `    ``// Traverse the array of points``    ``for``(ArrayList point:points)``    ``{`` ` `        ``// Stores whether the current point``        ``// lies inside triangle1 or not``        ``int` `condOne = isInside(triangle1, point);`` ` `        ``// Stores whether the current point``        ``// lies inside triangle2 or not``        ``int` `condTwo = isInside(triangle2, point);`` ` `        ``// Stores whether the current point``        ``// lies inside triangle3 or not``        ``int` `condThree = isInside(triangle3, point);`` ` `        ``// Stores whether the current point``        ``// lies inside triangle4 or not``        ``int` `condFour = isInside(triangle4, point);`` ` `        ``// Stores whether the current point``        ``// lies inside given triangle or not``        ``int` `condFive = isInside(triangle, point);`` ` `        ``// If current point lies inside``        ``// given triangle as well as inside``        ``// any of the four obtained triangles``        ``if` `((condOne != ``0` `|| condTwo != ``0` `||``           ``condThree != ``0` `|| condFour != ``0``) &&``            ``condFive != ``0``)``                ``ans += ``1``;``        ``}`` ` `    ``// Print the count of points``    ``System.out.println(ans);``}`` ` `// Driver Code` `public` `static` `void` `main (String[] args)``{``    ``ArrayList> rectangle = ``new` `ArrayList>();``    ``ArrayList> points = ``new` `ArrayList>();``    ``ArrayList> triangle = ``new` `ArrayList>();``    ` `    ``rectangle.add(``new` `ArrayList(Arrays.asList(``6``, ``5``)));``    ``rectangle.add(``new` `ArrayList(Arrays.asList(``2``, ``2``)));``    ``rectangle.add(``new` `ArrayList(Arrays.asList(``2``, ``1``)));``    ``rectangle.add(``new` `ArrayList(Arrays.asList(``5``, ``5``)));``    ` `    ``points.add(``new` `ArrayList(Arrays.asList(``1``, ``1``)));``    ``points.add(``new` `ArrayList(Arrays.asList(``6``, ``1``)));``    ``points.add(``new` `ArrayList(Arrays.asList(``6``, ``6``)));``    ``points.add(``new` `ArrayList(Arrays.asList(``1``, ``6``)));``    ` `    ``triangle.add(``new` `ArrayList(Arrays.asList(``4``, ``4``)));``    ``triangle.add(``new` `ArrayList(Arrays.asList(``0``, ``4``)));``    ``triangle.add(``new` `ArrayList(Arrays.asList(``0``, -``2``)));``    ` `    ``countPoints(points, triangle, rectangle);``}``}` `// This code is contributed by avanitrachhadiya2155`

## Python3

 `# Python3 program for the above approach` `# Function to calculate area of a triangle``def` `getArea(x1, y1, x2, y2, x3, y3):` `    ``# Return the resultant area``    ``return` `abs``((x1 ``*` `(y2 ``-` `y3) ``+``                ``x2 ``*` `(y3 ``-` `y1) ``+``                ``x3 ``*` `(y1 ``-` `y2)) ``/` `2``)` `# Function to check if a point``# lies inside a triangle or not``def` `isInside(triangle, point):` `    ``A, B, C ``=` `triangle``    ``x, y ``=` `point` `    ``# Calculate area of triangle ABC``    ``ABC ``=` `getArea(A[``0``], A[``1``],``                ``B[``0``], B[``1``],``                ``C[``0``], C[``1``])` `    ``# Calculate area of triangle``    ``# formed by connecting B, C, point``    ``BPC ``=` `getArea(x, y, B[``0``],``                ``B[``1``], C[``0``],``                ``C[``1``])` `    ``# Calculate area of triangle``    ``# formed by connecting A, C, point``    ``APC ``=` `getArea(A[``0``], A[``1``], x,``                ``y, C[``0``], C[``1``])` `    ``# Calculate area of triangle``    ``# formed by connecting A, B, point``    ``APB ``=` `getArea(A[``0``], A[``1``], B[``0``],``                ``B[``1``], x, y)` `    ``# Check if the sum of the areas of``    ``# above three triangles the same as ABC``    ``return` `ABC ``=``=` `(APC ``+` `APB ``+` `BPC)` `# Function to count the number of points``# lying inside a triangle & rectangle``def` `countPoints(rectangle, triangle, points):` `    ``# Stores the coordinates of the``    ``# vertices of the triangles``    ``triangle1 ``=` `rectangle[``1``:]``    ` `    ``triangle2 ``=` `rectangle[:``3``]``    ` `    ``triangle3 ``=` `rectangle[:``2``]``    ``triangle3.append(rectangle[``3``])``    ` `    ``triangle4 ``=` `rectangle[``-``2``:]``    ``triangle4.append(rectangle[``0``])` `    ``# Stores the number of points lying``    ``# inside the triangle and rectangle``    ``ans ``=` `0` `    ``# Traverse the array of points``    ``for` `point ``in` `points:``    ` `        ``# Stores whether the current point``        ``# lies inside triangle1 or not``        ``condOne ``=` `isInside(triangle1, point)``        ` `        ``# Stores whether the current point``        ``# lies inside triangle2 or not``        ``condTwo ``=` `isInside(triangle2, point)``        ` `        ``# Stores whether the current point``        ``# lies inside triangle3 or not``        ``condThree ``=` `isInside(triangle3, point)``        ` `        ``# Stores whether the current point``        ``# lies inside triangle4 or not``        ``condFour ``=` `isInside(triangle4, point)` `        ``# Stores whether the current point``        ``# lies inside given triangle or not``        ``condFive ``=` `isInside(triangle, point)` `        ``# If current point lies inside``        ``# given triangle as well as inside``        ``# any of the four obtained triangles``        ``if` `(condOne ``or` `condTwo ``or` `condThree \``            ``or` `condFour) ``and` `condFive:``            ``ans ``+``=` `1``            ` `    ``# Print the count of points``    ``print``(ans)`  `# Driver Code` `rectangle ``=` `[[``6``, ``5``], [``2``, ``2``], [``2``, ``1``], [``5``, ``5``]]``points ``=` `[[``1``, ``1``], [``6``, ``1``], [``6``, ``6``], [``1``, ``6``]]``triangle ``=` `[[``4``, ``4``], [``0``, ``4``], [``0``, ``-``2``]]` `countPoints(points, triangle, rectangle)`

## C#

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

## Javascript

 ``
Output:
`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up