
Humaira A.
asked 12/10/20How do you write a Java method for how to find the orthocenter of a triangle?
I need to know the steps on how to create an orthocenter method since whatever I try doesn't seem to work. I tried (a(x) * tan(a) and so on for the other three points but that doesn't work either. What should I do?
1 Expert Answer

Patrick B. answered 12/10/20
Math and computer tutor/teacher
//**** Point.java *****************//
class Point
{
protected double x;
protected double y;
public Point( double X, double Y)
{
x = X; y=Y;
}
public double GetX() { return(x); }
public double GetY() { return(y); }
public Point( Point p)
{
x = p.GetX(); y=p.GetY();
}
}
//**** Line.java ********************//
class Line
{
protected double slopeM;
protected double interceptB;
Line ( double m, double b)
{
slopeM = m;
interceptB = b;
}
double GetSlope() { return(slopeM); }
double GetIntercept() { return(interceptB); }
Line ( Line l)
{
slopeM = l.GetSlope();
interceptB = l.GetIntercept();
}
}
//**** TraingleOrthocenter.java *****//
class TriangleOrthocenter
{
private Point A;
private Point B;
private Point C;
private Point orthocenter;
private Line heightLineAB; //passes through C and perpendicular to AB
private Line heightLineBC; //passes through A and perpendicular to BC
private Line heightLineAC; //passes through B and perpendicular to AC
TriangleOrthocenter( Point pointA, Point pointB, Point pointC)
{
A = new Point(pointA);
B = new Point(pointB);
C = new Point(pointC);
//calculates the slopes of each side
double Mab = (A.GetY()-B.GetY())/(A.GetX() - B.GetX());
double Mac = (A.GetY()-C.GetY())/(A.GetX()- C.GetX());
double Mbc = (B.GetY() - C.GetY())/(B.GetX() - C.GetX());
double heightSlopeMab = (-1/Mab);
double heightSlopeMac = (-1/Mac);
double heightSlopeMbc = (-1/Mbc);
//calculates the intercepts of each side as INTX = y - mx
double INTXab = C.GetY() - heightSlopeMab * C.GetX();
double INTXac = B.GetY() - heightSlopeMac * B.GetX();
double INTXbc = A.GetY() - heightSlopeMbc* A.GetX();
heightLineAB = new Line(heightSlopeMab,INTXab);
heightLineAC = new Line(heightSlopeMac,INTXac);
heightLineBC = new Line(heightSlopeMbc,INTXbc);
double x = (heightLineAB.GetIntercept() - heightLineAC.GetIntercept())/ (heightLineAC.GetSlope() - heightLineAB.GetSlope());
double y = heightLineAB.GetSlope()*x + heightLineAB.GetIntercept();
orthocenter = new Point(x,y);
}
public Point GetOrthocenter() { return new Point(orthocenter); }
}
//*** MAIN *****//
class OrthoMain
{
public static void main(String args[])
{
Point A = new Point(1,2);
Point B = new Point(2,6);
Point C = new Point(3,-4);
TriangleOrthocenter x = new TriangleOrthocenter(A,B,C);
Point pointOrthocenter = x.GetOrthocenter();
System.out.println("[" + pointOrthocenter.GetX() + "," + pointOrthocenter.GetY() +"]");
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESROUCES section under this link12/10/20