算法提高 12-1三角形
2017-02-11 15:30
281 查看
问题描述
为二维空间中的点设计一个结构体,在此基础上为三角形设计一个结构体。分别设计独立的函数计算三角形的周长、面积、中心和重心。输入三个点,输出这三个点构成的三角形的周长、面积、外心和重心。结果保留小数点后2位数字。
样例输入
例:
0 0
1 0
0 1
样例输出
例如:
3.41
0.50
0.50 0.50
0.33 0.33
解题思路:
给定三角形三个顶点的坐标,如何求三角形的外心的坐标呢?
例如 :给定a(x1,y1) b(x2,y2) c(x3,y3)求外接圆心坐标O(x,y)
首先,外接圆的圆心也就是三角形的外心是三角形三条边的垂直平分线的交点,我们根据圆心到顶点的距离相等,可以列出以下方程:
2.化简得到:
3.最后根据克拉默法则:
给定三角形三个顶点的坐标,如何求三角形的重心的坐标呢?
例如 :给定a(x1,y1) b(x2,y2) c(x3,y3)求外接圆心坐标O(x,y)
首先,重心是三角形三条边的中线的交点,任意一条中线被重心分成1:2的两条线段,可以列出以下方程:
代码如下:
为二维空间中的点设计一个结构体,在此基础上为三角形设计一个结构体。分别设计独立的函数计算三角形的周长、面积、中心和重心。输入三个点,输出这三个点构成的三角形的周长、面积、外心和重心。结果保留小数点后2位数字。
样例输入
例:
0 0
1 0
0 1
样例输出
例如:
3.41
0.50
0.50 0.50
0.33 0.33
解题思路:
给定三角形三个顶点的坐标,如何求三角形的外心的坐标呢?
例如 :给定a(x1,y1) b(x2,y2) c(x3,y3)求外接圆心坐标O(x,y)
首先,外接圆的圆心也就是三角形的外心是三角形三条边的垂直平分线的交点,我们根据圆心到顶点的距离相等,可以列出以下方程:
(x1-x)(x1-x)-(y1-y)(y1-y)=(x2-x)(x2-x)+(y2-y)(y2-y); (x2-x)(x2-x)+(y2-y)(y2-y)=(x3-x)(x3-x)+(y3-y)(y3-y);
2.化简得到:
2*(x2-x1)*x+2*(y2-y1)y=x2^2+y2^2-x1^2-y1^2; 2*(x3-x2)*x+2*(y3-y2)y=x3^2+y3^2-x2^2-y2^2; 令A1=2*(x2-x1); B1=2*(y2-y1); C1=x2^2+y2^2-x1^2-y1^2; A2=2*(x3-x2); B2=2*(y3-y2); C2=x3^2+y3^2-x2^2-y2^2; 即 A1*x+B1y=C1; A2*x+B2y=C2;
3.最后根据克拉默法则:
x=((C1*B2)-(C2*B1))/((A1*B2)-(A2*B1)); y=((A1*C2)-(A2*C1))/((A1*B2)-(A2*B1));
给定三角形三个顶点的坐标,如何求三角形的重心的坐标呢?
例如 :给定a(x1,y1) b(x2,y2) c(x3,y3)求外接圆心坐标O(x,y)
首先,重心是三角形三条边的中线的交点,任意一条中线被重心分成1:2的两条线段,可以列出以下方程:
2*x - x1 - x2 = x3 - x 2*y - y1 - y2 = y3 - y 得出: x = ( x1 + x2 + x3) / 3 y = ( y1 + y2 + y3) / 3
代码如下:
import java.util.Scanner; class Point{ private double x; private double y; public Point(double x, double y) { super(); this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void Print(){ System.out.printf("%.2f %.2f",this.x,this.y); } public double Length(){ return this.x*this.x + this.y*this.y; } } class Triangle{ private Point[] point; private double[] line; public Triangle() { super(); this.point = new Point[3]; this.line = new double[3]; Scanner in = new Scanner(System.in); for ( int i = 0 ; i < 3 ; i++){ double x = in.nextDouble(); double y = in.nextDouble(); point[i] = new Point(x, y); } for ( int i = 0 ; i < this.point.length ; i++ ){ if ( i == 0 ){ line[i] = Line_Length(this.point[i], this.point[this.point.length-1]); }else{ line[i] = Line_Length(this.point[i-1], this.point[i]); } } in.close(); } public double Line_Length(Point point1 , Point point2){ double len = 0.0; double len_x = (point1.getX()-point2.getX()) * (point1.getX()-point2.getX()); double len_y = (point1.getY()-point2.getY()) * (point1.getY()-point2.getY()); len = Math.sqrt(len_x+len_y); return len; } public double Circumference(){ double c = 0.0; for ( int i = 0 ; i < this.line.length; i++ ){ c += this.line[i]; } return c; } public double Square(){ double s = 0.0; double p = 0; for ( int i = 0 ; i < line.length ; i++){ p += this.line[i]; } p /= 2; s = Math.sqrt(p*(p-this.line[0])*(p-this.line[1])*(p-this.line[2])); return s; } //重心 public Point MedianPoint(){ double x = 0.0; double y = 0.0; for ( int i = 0 ; i < point.length; i++){ x += this.point[i].getX(); y += this.point[i].getY(); } x /= 3; y /= 3; return new Point(x, y); } //外心 public Point CircumcenterPoint(){ double A1 = 2*(this.point[1].getX()-this.point[0].getX()); double A2 = 2*(this.point[2].getX()-this.point[1].getX()); double B1 = 2*(this.point[1].getY()-this.point[0].getY()); double B2 = 2*(this.point[2].getY()-this.point[1].getY()); double C1 = this.point[1].Length() - this.point[0].Length(); double C2 = this.point[2].Length() - this.point[1].Length(); double x = (C1*B2-C2*B1)/(A1*B2-A2*B1); double y = (A1*C2-A2*C1)/(A1*B2-A2*B1); return new Point(x, y); } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Triangle triangle = new Triangle(); Point median = triangle.MedianPoint(); Point circumcenter = triangle.CircumcenterPoint(); double c = triangle.Circumference(); double s = triangle.Square(); System.out.printf("%.2f",c); System.out.println(); System.out.printf("%.2f",s); System.out.println(); circumcenter.Print(); System.out.println(); median.Print(); } }
相关文章推荐
- 算法提高 12-1三角形
- 蓝桥杯算法提高 12-1三角形
- 算法提高 12-1三角形
- 算法提高 12-1三角形
- 算法提高 ADV-230 12-1三角形
- 算法提高 12-1三角形
- 算法提高 12-1三角形
- 算法提高 12-1三角形
- 蓝桥杯 算法提高 12-1三角形 (Java解题)
- 算法提高 12-1三角形
- 蓝桥杯 算法提高 12-1三角形 【计算几何_三角形】【全是板子】
- 算法提高 12-1三角形 外心坐标
- 蓝桥 ADV-230 算法提高 12-1三角形 【数学公式】
- 算法提高 三角形面积
- 算法提高 12-2扑克排序
- 蓝桥杯 算法提高 12-2扑克排序
- 算法笔记_082:蓝桥杯练习 12-1三角形(Java)
- 算法提高 ADV-135 三角形面积
- 蓝桥杯 ADV-88 算法提高 输出正反三角形
- 算法提高 12-2扑克排序