dukevin
dukevin

Reputation: 23178

Java: calculating area of a triangle

import java.lang.Math;
import java.awt.*
public class Triangle implements Shape
{
    java.awt.Point a;
    java.awt.Point b;
    java.awt.Point c;

    public Triangle(java.awt.Point a, java.awt.Point b, java.awt.Point c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }    
public double getArea( )
    {
       double area;
       return area = Math.abs((a-c)*(b-a)-(a-b)*(c-a));
    } ...

http://upload.wikimedia.org/math/f/e/5/fe56529cdaaaa9bb2f71c1ad8a1a454f.png <--area formula

I am trying to calculate the area of a triangle from 3 points (x,y) from a 2D Cartesian coordinate system. I'm assuming that my above formula correctly yields the area of a triangle (if not, please correct me) but my compiler says "operator - cannot be applied to java.awt.Point,java.awt.Point". I'm assuming it's saying this because you cannot subtract points from each other, but each value in the formula is either an x or y value, not a point. How can I fix my code so this would work? Thanks!

Upvotes: 4

Views: 15091

Answers (6)

Andreas Dolk
Andreas Dolk

Reputation: 114757

The underlying problem: In Java, operators like '+' and '-' are only allowed for primitive types (like byte, int, long) but not for objects (in general) and arrays.

Other languages allow for operator overloading, so in c++ you could define a '+' operation for Point objects and there your initial idea would compile and run. But that is not possible in Java.

The only exceptions are String (it's allowed to 'add' String objects) and the primitive wrappers like Integer and Double in Java 1.5+ (autoboxing converts them back to primitives)

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59451

Update: I didn't notice that OP had linked to a formula, that's why I looked up this one and coded it. You should use the other formula as this one involves more calculations (including 4 calls to sqrt, I think that would be heavy).


Using Heron's formula

double distance(Point a, Point b)
{
  double dx = a.x - b.x; 
  double dy = a.y - b.y;
  return Math.sqrt(dx * dx + dy * dy);
}
double getArea()
{
  double ab = distance(a, b);
  double bc = distance(c, b);
  double ca = distance(a, c);
  double s = (ab + bc + ca) / 2;
  return Math.sqrt(s * (s - ab) * (s - bc) * (s - ca))
}

Upvotes: 2

user256717
user256717

Reputation:

compiler is telling you the exact right thing.

Math.abs((a-c)*(b-a)-(a-b)*(c-a)

you forgot .x in a.x .y in b.y etc. that is (a.x - c.x)* ...

Upvotes: 3

Adam Matan
Adam Matan

Reputation: 136141

According to Wikipedia, you formula is correct. The article contains lots of useful and clear data.
According to the java.awt.point documentation, you should use the getX() and getY() methods, which return the coordinate value of a point.

That is,

alt text

Should be expressed as:

Math.abs((a.getX()-c.getX())*(b.getY()-a.getY())-
         (a.getX()-b.getX())*(c.getY()-a.getY()))*0.5;

It is probably not such a good practice to use point.x, because you shouldn't access an object's variable if you have a getter method that does that. This is the one aspect of separation between interface and implementation: the data point.x might be stored in many forms, not just int; The interface method assures that you'll get an int every time you use it.

Upvotes: 12

nekman
nekman

Reputation: 1919

Use a.x - c.x etc.

Just read the Javadoc: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Point.html

Upvotes: 0

Kai Huppmann
Kai Huppmann

Reputation: 10775

As the linked formula says, don't calculate with the points but with their x- and y-values. I'll leave it to you (it's homework!) to do that in java.

And don't forget to divide by 2.

Upvotes: 0

Related Questions