Jump to content

Recommended Posts

Posted (edited)

Given the corner points of a triangle (x1, y1), (x2, y2), (x3, y3), compute the area.



Hint: The area of the triangle with corner points (0, 0), (x1, y1), and (x2, y2) is |x1 · y2 - x2 · y1| / 2.



Complete the following code:



public class Geometry

{

/**

A method to return the smaller of two integers

@param a, the first integer

@param b, the second integer

@return small, the smaller of the two

*/

public static double triangleArea(double x1, double y1,

double x2, double y2, double x3, double y3)

{

...

}

}

 

What's the answer? Having a tough time.

Edited by cyberproxy
Posted (edited)

To help you find the area of a triangle given three vertices you will need to use this formula:

 

[latex]area = \left | \frac{x_{1}\left ( y_{2}-y_{3} \right )+x_{2}\left ( y_{3}-y_{1} \right )+x_{3}\left ( y_{1}-y_{2} \right )}{2} \right |[/latex]

 

Since I'm nice, I wrote this line that you will need to use and remember to return the area if you are using a method.

 

 

double area = Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))*0.5);
Edited by Spart
Posted

...*0.5

In Java, is there a precision reason of using *0.5 instead of /2.0? Or is that just your personal preference?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.