第二章第十九题(几何:三角形的面积)(Geometry: area of a triangle)

*2.19(几何:三角形的面积)编写程序,提示用户输入三角形的三个点(x1,y1)、(x2,y2)和(x3,y3),然后显示它的面积。计算三角形面积的公式是:

s = (side1 + side2 + side3) / 2

area = \sqrt{s(s-side1)(s-side2)(s-side3))}

下面是一个运行示例:

Enter the coordinates of three points separated by spaces\nlike x1 y1 x2 y2 x3 y3 : 1.5 -3.4 4.6 5 9.5 -3.4

The area of the triangle is 33.6

 

*2.19(Geometry: area of a triangle) Write a program that prompts the user to enter three

points, (x1, y1), (x2, y2), and (x3, y3), of a triangle then displays its area.

The formula for computing the area of a triangle is

s = (side1 + side2 + side3) / 2

area = \sqrt{s(s-side1)(s-side2)(s-side3))}

Here is a simple run:

Enter the coordinates of three points separated by spaces\nlike x1 y1 x2 y2 x3 y3 : 1.5 -3.4 4.6 5 9.5 -3.4

The area of the triangle is 33.6

 

下面是参考答案代码:

import java.util.*;


public class AreaTriangleQuestion19 {
	public static void main(String[] args) {
		double x1, y1;
		double x2, y2;
		double x3, y3;
		//distance between two points
		double LengthSide1, LengthSide2, LengthSide3;
		//HalfSumLengthSide represent that half of summation of all of sides
		double HalfSumLengthSide, AreaTriangle;

		System.out.print("Enter the coordinates of three points separated"
						+ " by spaces\nlike x1 y1 x2 y2 x3 y3 : ");
		Scanner PointsInput = new Scanner(System.in);
		x1 = PointsInput.nextDouble(); y1 = PointsInput.nextDouble();
		x2 = PointsInput.nextDouble(); y2 = PointsInput.nextDouble();
		x3 = PointsInput.nextDouble(); y3 = PointsInput.nextDouble();
		
		LengthSide1 = Math.pow(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2), 0.5);
		LengthSide2 = Math.pow(Math.pow(x3-x1, 2) + Math.pow(y3-y1, 2), 0.5);
		LengthSide3 = Math.pow(Math.pow(x3-x2, 2) + Math.pow(y3-y2, 2), 0.5);

		HalfSumLengthSide = (LengthSide1 + LengthSide2 + LengthSide3) / 2;
		AreaTriangle = Math.pow(HalfSumLengthSide
								*(HalfSumLengthSide - LengthSide1)
								*(HalfSumLengthSide - LengthSide2)
								*(HalfSumLengthSide - LengthSide3), 0.5);
		System.out.println("The area of the triangle is " + AreaTriangle);
		
		PointsInput.close();
	}
}

运行效果:

 

注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
open3d::geometry::TriangleMesh::CreateFromPointCloudAlphaShape 函数是Open3D库中用来根据给定的点云数据创建一个三角网格表达的函数。但当调用这个函数时如果失败,可能由以下几个原因导致: 1. 点云数据为空:函数需要至少存在一个点云数据才能创建三角网格,如果没有输入点云数据,函数将无法执行。 2. Alpha参数设置错误:这个函数需要一个Alpha参数来定义点云数据中点之间的连接程度。如果Alpha参数的值设置得过小,可能导致连接的面过少,从而无法成功创建三角网格。相反,如果Alpha参数的值设置得过大,可能导致连接的面过多,从而导致函数执行时间过长甚至内存溢出。 3. 输入的点云数据不适用于三角网格表示:这个函数是基于Alpha形状重建方法的,对于某些形状的点云数据可能无法成功创建三角网格。例如,如果点云数据中的点分布非常稀疏或是非常集中,都可能导致函数执行失败。 为了解决上述问,我们可以检查点云数据是否为空、调整Alpha参数的值或者尝试其他适用于形状重建的方法,如Marching Cubes,Poisson Reconstruction等。此外,我们还可以对点云数据进行预处理,例如进行滤波、重采样等操作,以提高函数的成功率。最后,如果上述方法都无法解决问,可能需要考虑点云数据本身的质量或者使用其他库或方法来处理点云数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值