Click here to Skip to main content
Click here to Skip to main content

Tagged as

Area of a Triangle in the Cartesian Coordinate System

, 27 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
For absolute beginners in Java


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. 


This is a just a class I've implemented in order to solve a problem in [ ] 

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(;
	     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++){
	    		 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]));
	     double roundOff = Math.round(area * 1000.0) / 1000.0;
	     if(area <= 0)
	    	 System.out.println("\nYour Triangle does not exist!");
	     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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tharindu Wijeratna

Sri Lanka Sri Lanka
No Biography provided
Follow on   Twitter

Comments and Discussions

SuggestionNo need to use Pow or Sqrt [modified] PinmemberAndreas Gieriet27-Oct-12 5:39 
GeneralIt can be done. Pinmemberrhuiden26-Oct-12 12:03 
AnswerRe: It can be done. PinmemberTharindu Wijeratna26-Oct-12 16:41 
QuestionKeep it simple, sir PinmemberDaniele Alberto Galliano26-Oct-12 3:39 
I can't understand why you use !(area > 0) instead of area <= 0.
Nobody is so beginner that cannot understand it :)

Anyway, you used a formula that fits very well with distances, when working in the cartesian plan, that benefits much more from the nature of coordinates.
In order to evaluate the area of any polygon in the coordinates plan the easiest approach is compute the area of all the trapezoids built by the x axis, the y elevations of each vertex and the side of the polygon. And you can cycle, too.


last:=lastIndex (last valid index of vector Points)
  Area+=(Points[i].X-Points[last].X)*(Points[i].Y-Points[last].Y)/2  (this area has a sign!!!)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 27 Oct 2012
Article Copyright 2012 by Tharindu Wijeratna
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid