Given three integers P, Q, and R representing 3 non-collinear points on a 2D plane with their respective x and y-coordinates, the task is to find the orthocenter of the triangle.
The orthocenter of the triangle is usually denoted by H, which is the intersection point of three altitudes of a triangle.
Input: P = (6, 0), Q = (0, 0), R = (0, 8)
Output: (0.000, 0.000)
Input: P = (-3, 1), Q = (2, 2), R = (-3, -5)
Output: (-4.400, 2.000)
Approach: The orthocenter lies inside the triangle if and only if the triangle is acute. If one angle is a right angle, the orthocenter coincides with the vertex at the right angle. The problem can be solved by the property that the orthocenter, circumcenter, and centroid of a triangle lies on the same line and the orthocenter divides the line joining the centroid and circumcenter in the ratio 3:2 externally.
Follow the steps below to solve the problem:
- Find the circumcenter of the triangle and store it in a pair CC(x1, y1).
- Find the centroid of the triangle and store it in a pair CT(x2, y2).
- Use section formula to get coordinates of the orthocenter of the given triangle as X = (3*x2 – 2*x1) and Y = (3*y2 – 2*y1).
- Print the value of X and Y as the result.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.