java求三角形周长 面积_用java如何求三角形的周长和面积?

java求三角形周长 面积_用java如何求三角形的周长和面积?_第1张图片

Java code

/* * Triangle.java * @author Wenzee Yann, [email protected] * @version 0.1000 * @since 2009.05.04 */ public class Triangle { // 正三角形 public Triangle(double side) { this.side1 = side; this.side2 = side; this.side3 = side; this.angle1 = Math.PI / 3; this.angle2 = Math.PI / 3; this.angle3 = Math.PI / 3; this.perimeter = 3 * side; // L = 3*a this.area = Math.pow(side1, 2) * Math.sin(Math.PI / 3); //s = a*a*sin(π/3) } // 已知两直角边的直角三角形 public Triangle(double side1, double side2) { this.side1 = side1; this.side2 = side2; this.side3 = Math.sqrt(side1 * side1 + side2 + side2); this.angle1 = Math.atan(side1 / side2); this.angle2 = Math.PI / 2 - this.angle1; this.angle3 = Math.PI / 2; this.perimeter = this.side1 + this.side2 + this.side3; this.area = this.side1 * this.side2; } /* 已知三边的任意三角形 * 余弦定理:a2=b2+c2-2bccosA, b2=a2+c2-2accosB, c2=a2+b2-2abcosC * 三角形一边的平方等于另两边的平方和再减去这两边与夹角余弦的乘积的2倍 */ public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; this.angle1 = Math.acos((this.side2 * this.side2 + this.side3 + this.side3 - this.side1 * this.side1) / 2* this.side2 * this.side3); //余弦定理 this.angle2 = Math.acos((this.side1 * this.side1 + this.side3 + this.side3 - this.side2 * this.side1) / 2* this.side1 * this.side3); //余弦定理 this.angle3 = Math.PI - this.side1 - this.side2; this.perimeter = this.side1 + this.side2 + this.side3; this.area = this.side1 * this.side2 * Math.cos(this.angle3); } /* 已知两相连两边及其夹角 * 正弦定理: a/sinA=b/sinB=c/sinC=2R, 其中R是三角形外接圆半径。 * 余弦定理:a2=b2+c2-2bccosA, b2=a2+c2-2accosB, c2=a2+b2-2abcosC */ public Triangle(double side1, double side2, float angle3) { this.side1 = side1; this.side2 = side2; this.side3 = Math.sqrt(this.side1 * this.side1 + this.side2 * this.side2 - 2 * side1 * side2 * Math.cos(angle3)); //余弦定理 this.angle3 = angle3; this.angle1 = Math.asin(this.side1 * Math.sin(this.angle3) / this.side3); //正弦定理 this.angle2 = Math.PI /2 - this.angle1 - this.angle2; this.perimeter = this.side1 + this.side2 + this.side3; this.area = this.side1 * this.side2 * Math.cos(this.angle3); } /* 已知两角及其夹边 * 正弦定理: a/sinA=b/sinB=c/sinC=2R, 其中R是三角形外接圆半径。 */ public Triangle(double side1, float angle2, float angle3) { this.angle1 = Math.PI - angle2 -angle3; this.angle2 = angle2; this.angle3 = angle3; this.side1 = side1; this.side2 = this.side1 * Math.sin(this.angle2) / Math.sin(this.angle1); //正弦定理 this.side3 = this.side1 * Math.sin(this.angle3) / Math.sin(this.angle1); //正弦定理 this.perimeter = this.side1 + this.side2 + this.side3; this.area = this.side1 * this.side2 * Math.cos(this.angle3); } public double getSide1() { return side1; } public double getSide2() { return side2; } public double getSide3() { return side3; } public double getAngle1() { return angle1; } public double getAngle2() { return angle2; } public double getAngle3() { return angle3; } public double getPerimeter() { return perimeter; } public double getArea() { return area; } private double side1; private double side2; private double side3; private double angle1; //side1对应角 private double angle2; //side2对应角 private double angle3; //side3对应角 private double perimeter; // 周长 private double area; // 面积 } // end class

◆◆

评论读取中....

请登录后再发表评论!

◆◆

修改失败,请稍后尝试

你可能感兴趣的:(java求三角形周长,面积)