Java:计算三角形的面积
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2145571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: calculating area of a triangle
提问by lemon
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
http://upload.wikimedia.org/math/f/e/5/fe56529cdaaaa9bb2f71c1ad8a1a454f.png<--面积公式
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!
我正在尝试从 2D 笛卡尔坐标系中的 3 个点 (x,y) 计算三角形的面积。我假设我上面的公式正确地产生了三角形的面积(如果没有,请纠正我)但我的编译器说“运算符 - 不能应用于 java.awt.Point,java.awt.Point”。我假设它是这样说的,因为你不能相互减去点,但公式中的每个值都是 x 或 y 值,而不是一个点。我怎样才能修复我的代码,这样才能正常工作?谢谢!
回答by Adam Matan
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.
根据维基百科,你的公式是正确的。这篇文章包含许多有用且清晰的数据。
根据java.awt.point 文档,您应该使用getX()和getY()方法,它们返回点的坐标值。
That is,
那是,


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.xmight be stored in many forms, not just int; The interface method assures that you'll get an int every time you use it.
使用 可能不是一个好习惯point.x,因为如果您有一个 getter 方法,则不应访问对象的变量。这是接口和实现分离的一方面:数据point.x可能以多种形式存储,而不仅仅是int; interface 方法确保您每次使用它时都会得到一个 int。
回答by Fakrudeen
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)* ...
你忘记了 .x in ax .y in by 等,即 (ax - cx)* ...
回答by Amarghosh
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).
更新:我没有注意到 OP 已经链接到一个公式,这就是我查找这个并对其进行编码的原因。您应该使用另一个公式,因为这个公式涉及更多计算(包括 4 次调用sqrt,我认为这会很繁重)。
Using Heron'sformula
使用海伦公式
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))
}
回答by Andreas Dolk
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.
潜在问题:在 Java 中,像“+”和“-”这样的运算符只允许用于原始类型(例如 byte、int、long),而不允许用于对象(通常)和数组。
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.
其他语言允许运算符重载,因此在 C++ 中,您可以为 Point 对象定义一个“+”操作,并且您最初的想法将在那里编译和运行。但这在 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)
唯一的例外是 String(允许“添加”String 对象)和原始包装器,如 Java 1.5+ 中的 Integer 和 Double(自动装箱将它们转换回原始类型)
回答by nekman
Use a.x - c.xetc.
使用a.x - c.x等。
Just read the Javadoc: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Point.html
只需阅读 Javadoc:http: //java.sun.com/j2se/1.5.0/docs/api/java/awt/Point.html
回答by Kai Huppmann
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.
正如链接公式所说,不要用点计算,而是用它们的 x 和 y 值。我会把它留给你(这是家庭作业!)在java中完成。
And don't forget to divide by 2.
并且不要忘记除以 2。

