Coding tests have become a mandatory process for many companies these days. In particular, JavaScript is a programming language widely used in web development, and solving geometric problems greatly aids in understanding algorithms. In this article, we will introduce a geometric algorithm problem that can be solved with JavaScript and explain the solution process in detail.
Problem Description
The following is a problem of determining whether two points are inside a given rectangle.
Problem: Determine if Points are Inside a Rectangle
The coordinates of the bottom-left corner of the given rectangle are (x1, y1), and the coordinates of the top-right corner are (x2, y2). Additionally, there are two points A(xA, yA) and B(xB, yB). Write a function to determine whether these points are inside the rectangle.
Input
- Bottom-left corner of the rectangle: (x1, y1)
- Top-right corner of the rectangle: (x2, y2)
- Point A: (xA, yA)
- Point B: (xB, yB)
Output
Write a function that returns true if each point is inside the rectangle, and false otherwise. If both points are inside, return true; otherwise, return false.
Solution Process
Now let’s go through the steps to solve this problem.
Step 1: Understanding the Problem
We have the coordinates of the rectangle’s corners and need to verify the coordinates of each point. The conditions for being inside the rectangle are as follows:
- x1 < xA < x2
- y1 < yA < y2
- x1 < xB < x2
- y1 < yB < y2
Each point must satisfy all the above conditions to be considered inside the rectangle.
Step 2: Writing the Function
Here is the JavaScript code based on this logic.
function isPointInsideRectangle(x1, y1, x2, y2, xA, yA, xB, yB) {
const isAInside = (x1 < xA && xA < x2) && (y1 < yA && yA < y2);
const isBInside = (x1 < xB && xB < x2) && (y1 < yB && yB < y2);
return isAInside && isBInside;
}
// Test
const result = isPointInsideRectangle(0, 0, 10, 10, 5, 5, 5, 5); // true
console.log(result);
Step 3: Testing and Verification
Let’s test the function. We confirmed that when the bottom-left corner of the rectangle is (0, 0) and the top-right corner is (10, 10), the point (5, 5) is inside.
In the example above, both points are the same (5, 5), so the function returns true. However, it should return false if one point is outside the rectangle.
Step 4: Checking Various Cases
We can validate the function through various cases. Here are additional test examples.
// Case 1: Both points are inside
console.log(isPointInsideRectangle(0, 0, 10, 10, 1, 1, 2, 2)); // true
// Case 2: Only one point is inside
console.log(isPointInsideRectangle(0, 0, 10, 10, 1, 1, 11, 11)); // false
// Case 3: Both points are outside
console.log(isPointInsideRectangle(0, 0, 10, 10, -1, -1, 11, 11)); // false
// Case 4: Located on the edge of the rectangle
console.log(isPointInsideRectangle(0, 0, 10, 10, 0, 5, 5, 10)); // false
Conclusion
Now we have completed the function to determine whether the points are inside a given rectangle using JavaScript. Problems like these are great for developing algorithmic thinking and are frequently presented in coding tests.
I hope this article helped you understand the basics of geometric problems and how to apply them. Continue to challenge yourself with various algorithmic problems!