Java: the computational domain 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((ac)*(ba)-(ab)*(ca)); } ... 

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

I am trying to calculate the area of ​​a triangle of three points (x, y) from a 2D Cartesian coordinate system. I assume that my above formula correctly gives the area of ​​the triangle (if not, please correct me), but my compiler says that "the operator cannot be applied to java.awt.Point, java.awt.Point". I suppose this talks about this because you cannot subtract points from each other, but each value in the formula is either an x ​​or a y value, not a point. How can I fix my code so that it works? Thanks!

+4
source share
6 answers

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

I.e

alt text

Must 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 that good to use point.x because you should not access the object variable if you have a getter method that does this. This is one aspect of the separation between interface and implementation: point.x data can be stored in many forms, not just int ; The interface method ensures that you get an int every time you use it.

+12
source
Compiler

tells you the exact right thing.

 Math.abs((ac)*(ba)-(ab)*(ca) 

you forgot .x in axy in by, etc., i.e. (ax - cx) * ...

+3
source

Update: I did not notice that OP is associated with the formula, so I looked at it and encoded it. You should use a different formula, as this has to do with a lot of calculations (including 4 sqrt calls, I think it will be hard).


Using Heron Formula

 double distance(Point a, Point b) { double dx = ax - bx; double dy = ay - by; 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)) } 
+2
source

As indicated in the related formula, do not calculate with dots, but with their x and y values. I will leave it to you (this is homework!) To do it in java.

And do not forget to divide by 2.

0
source
0
source

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

Other languages ​​allow operator overloading, so in C ++ you can define the "+" operation for Point objects, and your original idea will compile and execute. But this is not possible in Java.

The only exceptions are String (this allows you to add String objects) and primitive wrappers such as Integer and Double in Java 1.5+ (autoboxing converts them back to primitives)

0
source

Source: https://habr.com/ru/post/1299519/


All Articles