import java.awt.geom.Point2D;
/*
1. 垂心: 三角形三条边上的高相交于一点.这一点叫做三角形的垂心.
2. 重心: 三角形三条边上的中线交于一点.这一点叫做三角形的重心.
3. 外心: 三角形三边的中垂线交于一点.这一点为三角形外接圆的圆心.
4. 内心三角形三内角平分线交于一点.这一点为三角形内切圆的圆心.
重心: 三边上中线的交点
垂心: 三条高的交点
内心: 内接圆圆心, 三个角角平分线交点
外心: 外接圆圆心, 三条边的垂直平分线交点, 三角形三条边的垂直平分线的交点
锐角三角形的外心在三角形内
直角三角形的外心是斜边的中点
钝角三角形的外心在三角形外
a, b, c是三角形的三条边条
面积s: s*s=p(p-a)(p-b)(p-c), p=(a+b+c)/2
外圆半径 = abc / (4*面积)
内圆半径 = 2 * 面积 / 周长
内切圆心坐标(x,y): 三角形三个顶点的坐标:A(x1,y1),B(x2,y2),C(x3,y3)
x=(x1*BC+x2*CA+x3*AB)/(AB+BC+CA);
y=(y1*BC+y2*CA+y3*AB)/(AB+BC+CA).
*/
public class Triangle {
// 三角形的三个顶点
private Point2D.Double p1;
private Point2D.Double p2;
private Point2D.Double p3;
private double dis12;
private double dis23;
private double dis31;
public Triangle(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
dis12 = distenceOfPoints(p1, p2);
dis23 = distenceOfPoints(p2, p3);
dis31 = distenceOfPoints(p3, p1);
}
// 两点之间的距离
public static double distenceOfPoints(Point2D.Double p1, Point2D.Double p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
// 内切圆圆心
public Point2D.Double innerCenter() {
double p = perimeter();
double x = (p1.x * dis23 + p2.x * dis31 + p3.x * dis12) / p;
double y = (p1.y * dis23 + p2.y * dis31 + p3.y * dis12) / p;
return new Point2D.Double(x, y);
}
// 外切圆圆心
public Point2D.Double outerCenter() {
double x1 = p1.x;
double x2 = p2.x;
double x3 = p3.x;
double y1 = p1.y;
double y2 = p2.y;
double y3 = p3.y;
double x = ((y2 - y1) * (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) - (y3 - y1)
* (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1))
/ (2 * (x3 - x1) * (y2 - y1) - 2 * ((x2 - x1) * (y3 - y1)));
double y = ((x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1) - (x3 - x1)
* (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1))
/ (2 * (y3 - y1) * (x2 - x1) - 2 * ((y2 - y1) * (x3 - x1)));
return new Point2D.Double(x, y);
}
// 内切圆半径
public double innerRadius() {
return (2 * area()) / (dis12 + dis23 + dis31);
}
// 外接圆半径
public double outerRadius() {
return (dis12 * dis23 * dis31) / (4 * area());
}
// 三角形的面积
public double area() {
double p = 0.5 * perimeter();
return Math.sqrt(p * (p - dis12) * (p - dis23) * (p - dis31));
}
// 三角形的周长
public double perimeter() {
return dis12 + dis23 + dis31;
}
// 取得三角形的三个顶点
public Point2D.Double getP1() {
return (Point2D.Double) p1.clone();
}
public Point2D.Double getP2() {
return (Point2D.Double) p2.clone();
}
public Point2D.Double getP3() {
return (Point2D.Double) p3.clone();
}
}
================================= TriangleDrawer.java ==============================
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TriangleDrawer extends JPanel {
private static final long serialVersionUID = 1L;
private Triangle triangle;
public TriangleDrawer() {
triangle = new Triangle(new Point2D.Double(100, 100), new Point2D.Double(207, 130),
new Point2D.Double(220, 200));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 绘制三角形
drawLine(g, triangle.getP1(), triangle.getP2());
drawLine(g, triangle.getP2(), triangle.getP3());
drawLine(g, triangle.getP3(), triangle.getP1());
// 内接圆
double radius = triangle.innerRadius();
Point2D.Double center = triangle.innerCenter();
g.setColor(Color.BLUE);
drawCircle(g, center, radius);
// 外接圆
radius = triangle.outerRadius();
center = triangle.outerCenter();
g.setColor(Color.RED);
drawCircle(g, center, radius);
}
protected void drawLine(Graphics g, Point2D.Double startPoint, Point2D.Double endPoint) {
g.drawLine((int) startPoint.x, (int) startPoint.y, (int) endPoint.x, (int) endPoint.y);
}
protected void drawCircle(Graphics g, Point2D.Double center, double radius) {
g.drawOval((int) (center.x - radius), (int) (center.y - radius), (int) (2 * radius),
(int) (2 * radius));
}
private static void createGUIAndShow() {
JFrame frame = new JFrame("三角形");
JPanel contentPane = new TriangleDrawer();
frame.setContentPane(contentPane);
Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
int w = 500;
int h = 500;
int x = (ss.width - w) / 2;
int y = (ss.height - h) / 2;
x = x > 0 ? x : 0;
y = y > 0 ? y : 0;
frame.setBounds(x, y, w, h);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createGUIAndShow();
}
});
}
}