java求三角形周长面积及重心外心内心

废话不多说,直接看代码,注释已经很清楚了

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.println("请分别输入三点坐标");
		int a1=in.nextInt(),a2=in.nextInt();
		int b1=in.nextInt(),b2=in.nextInt();
		int c1=in.nextInt(),c2=in.nextInt();
		double zc=zhouchang(a1,a2,b1,b2,c1,c2);//周长
		
		System.out.println("周长:"+String.format("%.2f", zc));//输出周长
		System.out.println("面积:"+String.format("%.2f", mianji(a1, a2, b1, b2, c1, c2)));//输出面积
		System.out.println(zhongxin(a1, a2, b1, b2, c1, c2));//输出重心坐标
		System.out.println(waixin(a1,a2,b1,b2,c1,c2));//输出外心坐标
		System.out.println(neixin(a1,a2,b1,b2,c1,c2));//输出内心坐标
	}
	private static double distance(int a1, int a2, int b1, int b2) {//求两点距离
		double l=Math.sqrt((a1-b1)*(a1-b1)+(a2-b2)*(a2-b2));
		return l;
	}
	private static double zhouchang(int a1, int a2, int b1, int b2, int c1, int c2) {//求周长
		double l1=distance(a1, a2, b1, b2);
		double l2=distance(a1, a2, c1, c2);
		double l3=distance(b1, b2, c1, c2);
		return l1+l2+l3;
	}
	private static double mianji(int a1, int a2, int b1, int b2, int c1, int c2) {//求面积
		return Math.abs((a1*b2-a1*c2+b1*c2-b1*a2+c1*a2-c1*b2)/2.0);
	}
	private static String zhongxin(int a1, int a2, int b1, int b2, int c1, int c2) {//求重心
		return "重心坐标:"+String.format("%.2f", (a1+b1+c1)/3.0)+" "+String.format("%.2f", (a2+b2+c2)/3.0);
	}
	private static String waixin(int a1, int a2, int b1, int b2, int c1, int c2) {//求外心
		double a=b1-a1;
	    double b=b2-a2;
	    double c=(b1*b1+b2*b2-a1*a1-a2*a2)/2.0;
	    
	    double d=c1-b1;
	    double e=c2-b2;
	    double f=(c1*c1+c2*c2-b1*b1-b2*b2)/2.0;
	    
	    double x=(e*c-b*f)/(a*e-d*b);
	    double y=(c*d-a*f)/(b*d-a*e);
		return "外心坐标:"+String.format("%.2f", x)+" "+String.format("%.2f", y);
	}
	private static String neixin(int a1, int a2, int b1, int b2, int c1, int c2) {//求内心
		double a=distance(b1, b2, c1, c2);
		double b=distance(a1, a2, c1, c2);
		double c=distance(a1, a2, b1, b2);
		double x=(a*a1+b*b1+c*c1)/(a+b+c);
		double y=(a*a2+b*b2+c*c2)/(a+b+c);
		return "内心坐标:"+String.format("%.2f", x)+" "+String.format("%.2f", y);
	}
}

运行结果:
在这里插入图片描述

以下是基于C++的三维三角形外心的代码: ```c++ #include <iostream> #include <cmath> using namespace std; // 三维向量结构体 struct Vector3D { double x, y, z; }; // 三角形结构体 struct Triangle { Vector3D vertex[3]; }; // 计算两个向量的叉积 Vector3D crossProduct(Vector3D v1, Vector3D v2) { Vector3D result; result.x = v1.y * v2.z - v1.z * v2.y; result.y = v1.z * v2.x - v1.x * v2.z; result.z = v1.x * v2.y - v1.y * v2.x; return result; } // 计算两个向量的点积 double dotProduct(Vector3D v1, Vector3D v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } // 计算向量的模长 double magnitude(Vector3D v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); } // 计算三角形外心 Vector3D circumcenter(Triangle t) { Vector3D a = t.vertex[0]; Vector3D b = t.vertex[1]; Vector3D c = t.vertex[2]; Vector3D ab = { b.x - a.x, b.y - a.y, b.z - a.z }; Vector3D ac = { c.x - a.x, c.y - a.y, c.z - a.z }; Vector3D bc = { c.x - b.x, c.y - b.y, c.z - b.z }; double ab2 = dotProduct(ab, ab); double ac2 = dotProduct(ac, ac); double bc2 = dotProduct(bc, bc); Vector3D abXac = crossProduct(ab, ac); Vector3D abXbc = crossProduct(ab, bc); Vector3D acXbc = crossProduct(ac, bc); Vector3D circumcenter = { (ab2 * acXbc.x + ac2 * abXbc.x + bc2 * abXac.x) / (2 * magnitude(abXac)), (ab2 * acXbc.y + ac2 * abXbc.y + bc2 * abXac.y) / (2 * magnitude(abXac)), (ab2 * acXbc.z + ac2 * abXbc.z + bc2 * abXac.z) / (2 * magnitude(abXac)) }; circumcenter.x += a.x; circumcenter.y += a.y; circumcenter.z += a.z; return circumcenter; } int main() { Triangle t; cout << "Enter the coordinates of the first vertex: "; cin >> t.vertex[0].x >> t.vertex[0].y >> t.vertex[0].z; cout << "Enter the coordinates of the second vertex: "; cin >> t.vertex[1].x >> t.vertex[1].y >> t.vertex[1].z; cout << "Enter the coordinates of the third vertex: "; cin >> t.vertex[2].x >> t.vertex[2].y >> t.vertex[2].z; Vector3D circumcenter = circumcenter(t); cout << "The coordinates of the circumcenter are: "; cout << circumcenter.x << " " << circumcenter.y << " " << circumcenter.z << endl; return 0; } ``` 该代码首先定义了三维向量结构体和三角形结构体,并实现了计算两个向量的叉积、点积和模长的函数。然后实现了计算三角形外心的 `circumcenter` 函数,该函数根据公式计算三角形外心的坐标。最后在 `main` 函数中读入三角形的三个顶点坐标,调用 `circumcenter` 函数计算外心坐标,最终输出结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值