###### 算法提高 12-1三角形

为二维空间中的点设计一个结构体，在此基础上为三角形设计一个结构体。分别设计独立的函数计算三角形的周长、面积、中心和重心。输入三个点，输出这三个点构成的三角形的周长、面积、外心和重心。结果保留小数点后2位数字。

0 0
1 0
0 1

3.41
0.50
0.50 0.50
0.33 0.33

(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))；

    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三角形

2017-09-27 18:00:01

#### 12-1

2018-03-07 09:38:09

#### 蓝桥杯 算法提高 12-1三角形 （Java解题）

2017-11-28 21:34:12

#### 蓝桥杯 算法提高 12-1三角形 【计算几何_三角形】【全是板子】

2018-02-16 23:12:53

#### 算法提高 12-1三角形

2018-02-10 23:25:30

#### 蓝桥 ADV-230 算法提高 12-1三角形 【数学公式】

2017-04-06 08:29:25

#### 蓝桥杯算法提高 12-1三角形

2017-04-07 16:02:08

2017-09-20 23:33:06

#### 蓝桥杯 练习系统 算法提高Java

2015年05月25日 189KB 下载

2017-03-06 16:28:22