Jump to content

Java Help

Featured Replies

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

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

...*0.5

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.