12,623,027 members (30,090 online)
Tip/Trick
alternative version

12.4K views
1 bookmarked
Posted

# Area of a Triangle in the Cartesian Coordinate System

, 27 Oct 2012 CPOL
 Rate this:
For absolute beginners in Java

## Introduction

This is a simple Java class with a method to calculate the area, given the coordinates of the three nodes of the triangle, and a main method to invoke the calcArea method.

## Background

This is a just a class I've implemented in order to solve a problem in CodeCheff.com [ http://www.codechef.com/problems/NICEQUAD ]

since the other parts of the program are specific only to that problem, I thought to post this generic part -calculating the area of a triangle in a cartesian coordinate system- in Code Project. This is for absolute beginners to help themselves with:

• Getting input from the user
• Working with 2D arrays
• Using methods in Math class
• Rounding off a decimal number
• and anything new according to your level as a programmer...

## Using the code

Nothing complex here. Just compile and run as usual.

```/* Author: @TharieHimself */
import java.util.Scanner;
public class Triangle{

public void calcArea(){
Scanner scan = new Scanner(System.in);

int[][] coordinates = new int[3][2];
double[] sides = new double[3];
int count = 0;

for(int r=0; r<3; r++)
{
for(int c=0; c<2;c++){
count++;
System.out.print("Enter Coordinate "+count+": ");
coordinates[r][c] = scan.nextInt();
}
}

for(int i = 0; i<3; i++)
{
sides[i] = Math.sqrt((Math.pow((coordinates[i][0]- coordinates[(i + 1) % 3][0]), 2)) + Math.pow((coordinates[i][1] - coordinates[(i + 1) % 3][1]), 2));
}

double s = (sides[0]+ sides[1]+ sides[2])/2;
double area = Math.sqrt(s*(s-sides[0])*(s-sides[1])*(s-sides[2]));

/* THIS WILL ROUND OFF THE AREA TO THREE DECIMAL PLACES */
double roundOff = Math.round(area * 1000.0) / 1000.0;

if(area <= 0)
System.out.println("\nYour Triangle does not exist!");
else
System.out.println("\nArea of your Triangle is: "+roundOff);

}

}
```

## Points of Interest

A formula for calculating the area of a triangle when all sides are known is attributed to two famous mathematicians; Heron of Alexandria and Archimedes!

## About the Author

 Sri Lanka
No Biography provided

## Comments and Discussions

 First Prev Next
 No need to use Pow or Sqrt Andreas Gieriet27-Oct-12 5:39 Andreas Gieriet 27-Oct-12 5:39
 You may calculate the bounding box of your triangle and subtract the three outer triangle areas. These three areas are half of the rectangle areas the affected vertecies span. I.e. area = (max(x0, x1, x1) - min(x0, x1, x1)) * (max(y0, y1, y1) - min(y0, y1, y1)) - abs((x0-x1) * (y0-y1)) / 2 - abs((x1-x2) * (y1-y2)) / 2 - abs((x2-x0) * (y2-y0)) / 2 If you have the constraint of integer coordinates and integer area, then you may do the following and reject the solution if the result is odd (ends on 1, 3, 5, 7, or 9): double area = 2 * (max(x0, x1, x1) - min(x0, x1, x1)) * (max(y0, y1, y1) - min(y0, y1, y1)) - abs((x0-x1) * (y0-y1)) - abs((x1-x2) * (y1-y2)) - abs((x2-x0) * (y2-y0)) Cheers Andimodified 28-Oct-12 7:28am.
 It can be done. rhuiden26-Oct-12 12:03 rhuiden 26-Oct-12 12:03
 Re: It can be done. Tharindu Wijeratna26-Oct-12 16:41 Tharindu Wijeratna 26-Oct-12 16:41
 Keep it simple, sir Daniele Alberto Galliano26-Oct-12 3:39 Daniele Alberto Galliano 26-Oct-12 3:39
 Last Visit: 31-Dec-99 19:00     Last Update: 3-Dec-16 21:56 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Web02 | 2.8.161128.1 | Last Updated 27 Oct 2012
Article Copyright 2012 by Tharindu Wijeratna