日期:2014-05-20 浏览次数:20785 次
/*
* Triangle.java
* @author Wenzee Yann, wenzee@gmail.com
* @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;
}