0

I am new to Java and I want to ask about code for calculating for the centroid of a triangle (by using POINT 2D class). Code that allows user to input x-coordinates, y-coordinates of the 3 vertices.

1
  • 1.) Calculate the 3 lines 2.) Solve a System of equations to find where they meet. This is more of a mathematical question than programming. Jan 31, 2016 at 4:16

1 Answer 1

2

Calculating the centroid is as simple as finding the average of the coordinates. Below is some code that does just that.

final Point2D.Double p1 = ...;
final Point2D.Double p2 = ...;
final Point2D.Double p3 = ...;

final Point2D.Double centroid = new Point2D.Double((p1.getX() + p2.getX() + p3.getX()) / 3.0, (p1.getY() + p2.getY() + p3.getY()) / 3.0);

Here is a site that provides a neat little interactable that lets you see this in action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.