给定一三角形面积S,如何求平面内三点坐标,并输出,确保不超时,使用c++实现
时间: 2024-05-07 16:17:51 浏览: 78
假设三角形三个顶点坐标为A(x1,y1)、B(x2,y2)、C(x3,y3),我们可以利用向量法求出三角形面积S。
首先,我们可以通过向量AB和向量AC求出向量AB与向量AC的叉积,即
```
AB = (x2 - x1, y2 - y1)
AC = (x3 - x1, y3 - y1)
ABxAC = AB.x * AC.y - AB.y * AC.x
```
则三角形面积S等于向量AB与向量AC的叉积的一半,即
```
S = 0.5 * |ABxAC|
```
接下来,我们可以通过向量AB和向量AC的叉积求出向量AD的长度,其中向量AD与向量AB垂直,即
```
AD = 2 * S / |AB|
```
同样地,我们可以通过向量AC和向量AB的叉积求出向量AE的长度,其中向量AE与向量AC垂直,即
```
AE = 2 * S / |AC|
```
知道了向量AD和向量AE的长度,我们就可以求出点D和点E的坐标,即
```
D(x4, y4) = A(x1, y1) + (AD / |AB|) * AB
E(x5, y5) = A(x1, y1) + (AE / |AC|) * AC
```
最后,我们可以通过点D和点E的坐标求出点F的坐标,即
```
F(x6, y6) = D(x4, y4) + (E(x5, y5) - D(x4, y4)) * cosθ
```
其中θ为角A的角度,即
```
cosθ = (AB·AC) / (|AB|·|AC|)
```
代码如下:
```c
#include <stdio.h>
#include <math.h>
#define EPS 1e-6
struct point {
double x, y;
};
double cross(struct point A, struct point B, struct point C) {
return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}
double dist(struct point A, struct point B) {
return sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
}
struct point get_point_on_line(struct point A, struct point B, double k) {
struct point P;
P.x = A.x + (B.x - A.x) * k;
P.y = A.y + (B.y - A.y) * k;
return P;
}
int main() {
struct point A, B, C, D, E, F;
double AB, AC, AD, AE, cosA, sinA;
double S;
printf("请输入三角形三个顶点坐标:\n");
scanf("%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
S = fabs(cross(A, B, C)) / 2;
AB = dist(A, B);
AC = dist(A, C);
AD = 2 * S / AB;
AE = 2 * S / AC;
cosA = (B.x - A.x) * (C.x - A.x) + (B.y - A.y) * (C.y - A.y);
cosA /= AB * AC;
sinA = sqrt(1 - cosA * cosA);
D = get_point_on_line(A, B, AD / AB);
E = get_point_on_line(A, C, AE / AC);
F.x = D.x + (E.x - D.x) * cosA - (E.y - D.y) * sinA;
F.y = D.y + (E.y - D.y) * cosA + (E.x - D.x) * sinA;
printf("三角形面积为%lf,三点坐标分别为(%lf, %lf), (%lf, %lf), (%lf, %lf)\n", S, D.x, D.y, E.x, E.y, F.x, F.y);
return 0;
}
```
输入三角形三个顶点坐标后,程序将输出三角形面积和三点坐标。
阅读全文