MEESHO Coding Question – Solved

8 Live
Given three points a(x1, y1), b(x2, y2), and c(x3, y3), determine if they form a non-degenerate triangle. Then, check if two points, p(xp, yp) and q(xq, yq), are inside or on the triangle. Return the corresponding scenario number: 0. The lines do not form a valid non-degenerate triangle. 1. Point p is inside the triangle, but point q is not. 2. Point q is inside the triangle, but point p is not. 3. Both points p and q are inside the triangle. 4. Neither point p nor point q is inside the triangle. Note: A triangle is considered non-degenerate if it meets the following conditions, where |ab| denotes the length of the line segment between points a and b: · |ab| + |bc| > |ac| · |bc| + |ac| > |ab| · |ab| + |ac| > |bc| Example 1 = a(x1, y1) : (2, 2) 2 = b(x2, y2) : (7, 2) 3 = c(x3, y3) : (5, 4)

Asked in: MEESHO

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def pointsBelong(x1, y1, x2, y2, x3, y3, xp, yp, xq, yq):
    # Write your code here
    a = [x1,y1]
    b = [x2,y2]
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, you need to proceed in two major phases: first verifying if the three given points form a valid, non-degenerate triangle, and second, checking whether two other points lie inside or on the boundary of this triangle.

Phase 1: Verify the Triangle Validity
A non-degenerate triangle is one where the three points are not collinear, meaning they form an actual triangle with an area greater than zero. The problem provides conditions based on the triangle inequality theorem, which states that for any triangle, the sum of lengths of any two sides must be strictly greater than the length of the remaining side.

Step 1: Compute the distances between the three points a, b, and c.
Use the Euclidean distance formula to calculate:
|ab| = distance between a and b
|bc| = distance between b and c
|ac| = distance between a and c

Step 2: Check the triangle inequality conditions:
- |ab| + |bc| > |ac|
- |bc| + |ac| > |ab|
- |ab| + |ac| > |bc|

If any of these conditions fail, the points are collinear or degenerate, and the lines do not form a valid triangle. Return scenario 0 immediately in that case.

Alternatively, you can also check for collinearity by calculating the area of the triangle using the determinant or cross product method:
Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
If the area is zero, the points are collinear, meaning no valid triangle is formed.

Phase 2: Check if points p and q lie inside or on the triangle
Once you confirm that a non-degenerate triangle exists, the next task is to determine the position of points p and q relative to this triangle. The goal is to find if each point is inside or on the boundary of the triangle or outside it.

Step 1: Understand the concept of point-in-triangle tests.
There are multiple geometric methods to check if a point lies inside a triangle, including:
- Using barycentric coordinates
- Using area comparisons
- Using vector cross products with orientation checks

Step 2: Area-based method (intuitive and easy to understand)
The idea is that if point P lies inside or on the boundary of the triangle ABC, then the sum of the areas of triangles PAB, PBC, and PCA will be equal to the area of triangle ABC.

Steps for each point (p and q):
- Calculate the area of the original triangle ABC.
- Calculate the area of triangle PAB.
- Calculate the area of triangle PBC.
- Calculate the area of triangle PCA.
- Sum these three smaller areas and compare with the area of ABC.
- If they are equal (considering floating-point precision), then P lies inside or on the triangle; otherwise, it is outside.

Step 3: Barycentric coordinate method
This method uses the concept of expressing the point as a weighted average of the vertices of the triangle.
- Compute parameters (u, v, w) representing the weights associated with vertices a, b, and c such that: P = u*a + v*b + w*c, where u + v + w = 1 and all weights are non-negative.
- If all weights are between 0 and 1 inclusive, then point P lies inside or on the boundary of the triangle.

Step 4: Use either method based on comfort and clarity. Both are efficient and work well for this problem.

Phase 3: Combine the results to find the scenario number
Once you know the status of points p and q (inside/on or outside), you can decide the scenario:
- If the triangle is invalid, return 0.
- Else if p is inside/on and q is not, return 1.
- Else if q is inside/on and p is not, return 2.
- Else if both p and q are inside/on, return 3.
- Otherwise, return 4 (both outside).

Additional Considerations:
- Take care of floating-point precision errors when comparing areas or coordinates. You may use a small epsilon tolerance to handle approximate equality.
- Include the triangle's boundary as part of the "inside" for the points, meaning if the point lies exactly on an edge or vertex, it should be considered inside.

Example Walkthrough with provided points:
- Compute side lengths and check validity for points (2,2), (7,2), and (5,4).
- Calculate triangle area to confirm non-degenerate.
- Check points p and q similarly for inside/on the triangle using area or barycentric methods.
- Return scenario accordingly.

This systematic approach ensures the problem is solved accurately by first establishing a valid geometric shape and then analyzing point positions with respect to that shape.
```


Related Questions