Calculate the area of ​​a triangle taking into account 3 user points -Beginner

I can get my code to compile, but it does not create the desired area. I do not know where I stumbled.

They want the user to enter 6 coordinates (x and y value) for the three points of the triangle and get the area. My code is as follows:

import java.util.Scanner; public class AreaTriangle { // find the area of a triangle public static void main (String [] args) { double side1 = 0; double side2 = 0; double side3 = 0; Scanner input = new Scanner(System.in); //obtain three points for a triangle System.out.print("Enter three points for a triangle (x and y intercept): "); double side1x = input.nextDouble(); double side1y = input.nextDouble(); double side2x = input.nextDouble(); double side2y = input.nextDouble(); double side3x = input.nextDouble(); double side3y = input.nextDouble(); //find length of sides of triangle side1 = Math.pow(Math.pow((side2x - side1x), 2) + Math.pow((side2y - side1y), 2) * .05, side1); side2 = Math.pow(Math.pow((side3x - side2x), 2) + Math.pow((side3y - side2y), 2) * .05, side2); side3 = Math.pow(Math.pow((side1x - side3x), 2) + Math.pow((side1y - side3y), 2) * .05, side3); double s = (side1 + side2 + side3) / 2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3)) * 0.5; System.out.println("area" + area); } } 
+4
source share
3 answers

You should try to implement this equation. http://www.mathopenref.com/coordtrianglearea.html

+2
source

@ Michael's suggestion is good. Following your code, I would use the Pythagorean theorem as follows:

 side1 = Math.sqrt( Math.pow((side2x - side1x), 2) + Math.pow((side2y - side1y), 2)); 

In your code:

 side1 = Math.pow( Math.pow((side2x - side1x), 2) + Math.pow((side2y - side1y), 2) * .05 , side1); 

side1 0 before calculation, and almost all power 0 is 1. Therefore side1 ends as 1 regardless of the points.

+2
source

Another way I discovered is that you can use a cross-product to find the area of ​​the triangle. This may be a little easier for you, since you already have points. You can turn three points into two vectors and take the cross product.

change Oops, forgotten to add to the area of ​​the triangle, will be half the cross product, since the cross product will give you the area of ​​the parallelogram formed by two vectors (and the triangle is half).

0
source

All Articles