Java中多边形面积的计算
我有一个名为SimplePolygon的类,它使用用户提供的坐标创建多边形。我试图定义一种方法来计算多边形的面积。这是一项作业,课程讲师希望我们使用以下公式来计算面积 我可以用任何一个公式。我选对了 我的代码给了我错误的区域。我不知道怎么了Java中多边形面积的计算,java,Java,我有一个名为SimplePolygon的类,它使用用户提供的坐标创建多边形。我试图定义一种方法来计算多边形的面积。这是一项作业,课程讲师希望我们使用以下公式来计算面积 我可以用任何一个公式。我选对了 我的代码给了我错误的区域。我不知道怎么了 public class SimplePolygon implements Polygon { protected int n; // number of vertices of the polygon protected Point2D.Doub
public class SimplePolygon implements Polygon {
protected int n; // number of vertices of the polygon
protected Point2D.Double[] vertices; // vertices[0..n-1] around the polygon
public double area() throws NonSimplePolygonException {
try
{
if(isSimple()==false)
throw new NonSimplePolygonException();
else
{
double sum = 0;
for(int i = 0; i < vertices.length - 1; i++)
if(i == 0)
sum += vertices[i].x * (vertices[i+1].y - vertices[vertices.length - 1].y);
else
sum += vertices[i].x * (vertices[i+1].y - vertices[i-1].y);
double area = 0.5 * Math.abs(sum);
return area;
}
}
catch(NonSimplePolygonException e)
{
System.out.println("The Polygon is not simple.");
}
return 0.0;
}
现在,您已经修复了琐碎的边界情况,您丢失了另一个边界,并且您的循环是错误的。已更正调试代码:
public double area()
{
double sum = 0;
for (int i = 0; i < vertices.length ; i++)
{
if (i == 0)
{
System.out.println(vertices[i].x + "x" + (vertices[i + 1].y + "-" + vertices[vertices.length - 1].y));
sum += vertices[i].x * (vertices[i + 1].y - vertices[vertices.length - 1].y);
}
else if (i == vertices.length - 1)
{
System.out.println(vertices[i].x + "x" + (vertices[0].y + "-" + vertices[i - 1].y));
sum += vertices[i].x * (vertices[0].y - vertices[i - 1].y);
}
else
{
System.out.println(vertices[i].x + "x" + (vertices[i + 1].y + "-" + vertices[i - 1].y));
sum += vertices[i].x * (vertices[i + 1].y - vertices[i - 1].y);
}
}
double area = 0.5 * Math.abs(sum);
return area;
}
总和中缺少一个项:顶点[n-1]。x*顶点[0]。y-顶点[n-2]。y 在编辑问题之前,第一学期也有问题: 此外,如果i==0,则术语应为顶点[i].x*顶点[i+1].y-顶点[n-1].y 假设n等于顶点.length 编写循环代码的最简单方法可能是:
n = vertices.length;
sum =0;
for (int i = 0; i < n; i++) {
sum += vertices[i].x * (vertices[(i + 1) % n].y - vertices[(i + n - 1) % n].y);
}
我找到了另一种方法
将第一个元素再次添加到多边形数组中
因此,我们可以避免越界情况以及许多If条件
以下是我的解决方案:
public class PolygonArea {
public static void main(String[] args) {
PolygonArea p = new PolygonArea();
System.out.println(p.calculateArea());
}
Point[] points = new Point[5];
public double calculateArea() {
points[0] = new Point("A", 4, 10);
points[1] = new Point("B", 9, 7);
points[2] = new Point("C", 11, 2);
points[3] = new Point("D", 2, 2);
/** Add first entry again to polygon */
points[4] = new Point("A", 4, 10);
double sum = 0.0;
for (int i = 0; i < points.length - 1; ++i) {
sum += (points[i].X * points[i + 1].Y) - (points[i + 1].X * points[i].Y);
}
return Math.abs(sum / 2);
}
class Point {
final String _ID;
final int X;
final int Y;
public Point(String id, int x, int y) {
_ID = id;
X = x;
Y = y;
}
}
}
请向我们提供样本数据和预期输出。isSimple方法来自哪里?你说得对。但是我在4个顶点的多边形上使用了这种方法。因此,在这种情况下,3与顶点相同。长度-1。我要说的是,请看@Henry的答案-对于另一个边情况,您似乎缺少了一个术语。@Fedora-您的循环也错了。我们被复杂的事情缠住了,错过了简单的事情。明白了。谢谢你,埃文。你从哪里得到的:顶点[n-1].x*顶点[0].y-顶点[n-2].y?公式没有这么说,这是另一种边界情况,其中i=n-1,因此i+1大于总顶点数。我已经用正确的代码更新了我的答案。这是一个术语,其中I=n-1,你把它放在循环中。这个在索引上很聪明。
public class PolygonArea {
public static void main(String[] args) {
PolygonArea p = new PolygonArea();
System.out.println(p.calculateArea());
}
Point[] points = new Point[5];
public double calculateArea() {
points[0] = new Point("A", 4, 10);
points[1] = new Point("B", 9, 7);
points[2] = new Point("C", 11, 2);
points[3] = new Point("D", 2, 2);
/** Add first entry again to polygon */
points[4] = new Point("A", 4, 10);
double sum = 0.0;
for (int i = 0; i < points.length - 1; ++i) {
sum += (points[i].X * points[i + 1].Y) - (points[i + 1].X * points[i].Y);
}
return Math.abs(sum / 2);
}
class Point {
final String _ID;
final int X;
final int Y;
public Point(String id, int x, int y) {
_ID = id;
X = x;
Y = y;
}
}
}