public class TrianglePosition {
public static void main(String[] args) {
// 三角形顶点和中垂线点的坐标
Point a = new Point(0, 0);
Point d = new Point(2, 4);
Point b = new Point(d.x * 2, 0);
Point c = new Point(-d.x, 0);
System.out.println("点 B 的坐标为:" + b);
System.out.println("点 C 的坐标为:" + c);
}
// 计算两个点之间的距离
private static double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
// 判断两个 double 值是否相等
private static boolean doubleEquals(double d1, double d2) {
return Math.abs(d1 - d2) < 1e-6;
}
// 计算三角形中垂线的长度
private static double getBD(Point a, Point d) {
return distance(a, d);
}
// 判断一个点是否在一条直线上
private static boolean isPointInLine(Point p, Point p1, Point p2) {
if (doubleEquals(p1.x, p2.x)) {
return doubleEquals(p.x, p1.x);
}
double k = (p2.y - p1.y) / (p2.x - p1.x);
double b = p1.y - k * p1.x;
return doubleEquals(p.y, k * p.x + b);
}
// 计算等腰三角形的顶点和另外两个点的位置
private static void getTrianglePosition(Point a, Point d, Point b, Point c) {
double bd = getBD(a, d);
double ac = bd * 2;
b.y = d.y;
c.y = d.y;
double ab = distance(a, b);
double bc = distance(b, c);
if (doubleEquals(ab, bc)) {
// 三角形的底边与 y 轴平行
b.x = d.x + ac / 2;
c.x = d.x - ac / 2;
} else {
// 三角形的底边与 y 轴不平行
b.x = d.x + (ab * 2 - ac) / 2;
c.x = d.x - (ab * 2 - ac) / 2;
}
// 如果 a、b、c 三点共线,则根据等腰三角形的性质,将 c 点往左移动一个长度单位
while (isPointInLine(c, a, b)) {
c.x -= 1;
}
}
// 定义一个二维坐标点类
private static class Point {
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
java
public class TrianglePosition {
public static void main(String[] args) {
// 三角形顶点和中垂线点的坐标,以及底边 BD 的长度
Point a = new Point(0, 0);
Point d = new Point(2, 4);
double bd = 5;
Point b = new Point();
Point c = new Point();
getTrianglePosition(a, d, bd, b ,c);
System.out.println("点 B 的坐标为:" + b);
System.out.println("点 C 的坐标为:" + c);
}
// 计算两个点之间的距离
private static double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
// 根据角度和距离计算出坐标
private static Point getPoint(double angle, double distance) {
return new Point(distance * Math.cos(angle), distance * Math.sin(angle));
}
// 计算等腰三角形的顶点和另外两个点的位置
private static void getTrianglePosition(Point a, Point d, double bd, Point b, Point c) {
// AD 的长度
double ad = distance(a, d);
// BD 的一半
double bdm = bd / 2;
// 线段 BD 的中点坐标
Point m = new Point((b.x + c.x) / 2, (b.y + c.y) / 2);
m.x = d.x - bdm;
m.y = d.y;
// 求出 BM 和 MC 的长度
double bm = Math.sqrt(ad * ad - bdm * bdm);
double cm = bd - bm;
// 求出角 BDA 的正弦值
double sinBDA = bdm / ad;
// 求出角 BDA 的弧度值
double angleBDA = Math.asin(sinBDA);
// 求出角 BDC 的弧度值
double angleBDC = Math.PI - angleBDA;
// 计算出点 B 和点 C 的坐标
b.x = d.x - bm * Math.sin(angleBDA);
b.y = d.y - bm * Math.cos(angleBDA);
c.x = d.x + cm * Math.sin(angleBDC);
c.y = d.y - cm * Math.cos(angleBDC);
}
// 定义一个二维坐标点类
private static class Point {
public double x;
public double y;
public Point() {
this(0, 0);
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
public class IsoscelesTriangleView extends View {
private Paint mPaint;
// 三角形的顶点和中垂线点的坐标
private Point mA;
private Point mD;
// 三角形的底边长度
private float mBD;
// 构造方法
public IsoscelesTriangleView(Context context) {
super(context);
init();
}
// 初始化方法,用于设置画笔的属性
private void init() {
mPaint = new Paint();
// 设置画笔颜色
mPaint.setColor(Color.RED);
// 设置画笔样式为描边
mPaint.setStyle(Paint.Style.STROKE);
// 设置画笔的宽度
mPaint.setStrokeWidth(2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 计算等腰三角形的三个顶点的位置
Point b = new Point();
Point c = new Point();
getTrianglePosition(mA, mD, mBD, b ,c);
// 将坐标系原点移动到画布中心
canvas.translate(getWidth() / 2f, getHeight() / 2f);
// y 轴取反,以匹配 Android 的绘图坐标系
canvas.scale(1, -1);
// 绘制三角形
Path path = new Path();
path.moveTo(mA.x, mA.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.close();
canvas.drawPath(path, mPaint);
}
// 设置三角形的顶点和中垂线点的坐标以及底边长度
public void setTriangleParams(Point a, Point d, float bd) {
mA = a;
mD = d;
mBD = bd;
invalidate();
}
// 计算两个点之间的距离
private static double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1