信息学奥赛一本通 1034:计算三角形面积 | OpenJudge NOI 1.3 17

【题目链接】

ybt 1034:计算三角形面积
OpenJudge NOI 1.3 17:计算三角形面积

【题目考点】

1. 已知三点求三角形面积公式

已知三点分别为 ( x 1 , y 1 ) , ( x 2 , y 2 ) , ( x 3 , y 3 ) (x_1,y_1), (x_2,y_2), (x_3,y_3) (x1,y1),(x2,y2),(x3,y3)
先给出结论:这三个点围成的三角形的面积为:
S = 1 2 ∣ x 1 y 2 + x 2 y 3 + x 3 y 1 − x 1 y 3 − x 2 y 1 − x 3 y 2 ∣ S=\frac{1}{2}|x_1y_2+x_2y_3+x_3y_1-x_1y_3-x_2y_1-x_3y_2| S=21x1y2+x2y3+x3y1x1y3x2y1x3y2

推导过程:
  • a ⃗ = ( x 2 − x 1 , y 2 − y 1 ) , b ⃗ = ( x 3 − x 1 , y 3 − y 1 ) \vec{a} = (x_2-x_1,y_2-y_1), \vec{b} = (x_3-x_1,y_3-y_1) a =(x2x1,y2y1),b =(x3x1,y3y1)
  • 已知向量点积公式 a ⃗ ⋅ b ⃗ = ∣ a ⃗ ∣ ∣ b ⃗ ∣ c o s θ \vec{a} \cdot \vec{b} = |\vec{a}||\vec{b}|cos\theta a b =a b cosθ,其中 θ \theta θ是向量 a ⃗ , b ⃗ \vec{a}, \vec{b} a ,b 的夹角。
    c o s θ = a ⃗ ⋅ b ⃗ ∣ a ⃗ ∣ ∣ b ⃗ ∣ cos\theta = \frac{\vec{a} \cdot \vec{b}}{|\vec{a}||\vec{b}|} cosθ=a b a b
  • 已知 s i n 2 θ + c o s 2 θ = 1 sin^2\theta + cos^2\theta = 1 sin2θ+cos2θ=1,且 θ < π \theta<\pi θ<π
    s i n θ = 1 − c o n s 2 θ = ( ∣ a ⃗ ∣ ∣ b ⃗ ∣ ) 2 − ( a ⃗ ⋅ b ⃗ ) 2 ∣ a ⃗ ∣ ∣ b ⃗ ∣ sin\theta = \sqrt{1-cons^2\theta} = \frac{\sqrt{(|\vec{a}||\vec{b}|)^2 - (\vec{a} \cdot \vec{b})^2}}{|\vec{a}||\vec{b}|} sinθ=1cons2θ =a b (a b )2(a b )2
  • 已知三角形面积公式: S = 1 2 ∣ a ⃗ ∣ ∣ b ⃗ ∣ s i n θ S = \frac{1}{2}|\vec{a}||\vec{b}|sin\theta S=21a b sinθ
    S = 1 2 ∣ a ⃗ ∣ ∣ b ⃗ ∣ ( ∣ a ⃗ ∣ ∣ b ⃗ ∣ ) 2 − ( a ⃗ ⋅ b ⃗ ) 2 ∣ a ⃗ ∣ ∣ b ⃗ ∣ = 1 2 ( ∣ a ⃗ ∣ ∣ b ⃗ ∣ ) 2 − ( a ⃗ ⋅ b ⃗ ) 2 \begin{aligned} S &= \frac{1}{2}|\vec{a}||\vec{b}|\frac{\sqrt{(|\vec{a}||\vec{b}|)^2 - (\vec{a} \cdot \vec{b})^2}}{|\vec{a}||\vec{b}|}\\ &= \frac{1}{2}\sqrt{(|\vec{a}||\vec{b}|)^2 - (\vec{a} \cdot \vec{b})^2}\\ \end{aligned} S=21a b a b (a b )2(a b )2 =21(a b )2(a b )2
  • ∣ a ⃗ ∣ = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 |\vec{a}| = \sqrt{(x_2-x_1)^2+(y_2-y_1)^2} a =(x2x1)2+(y2y1)2
    ∣ b ⃗ ∣ = ( x 3 − x 1 ) 2 + ( y 3 − y 1 ) 2 |\vec{b}| = \sqrt{(x_3-x_1)^2+(y_3-y_1)^2} b =(x3x1)2+(y3y1)2
    a ⃗ ⋅ b ⃗ = ( x 2 − x 1 ) ( x 3 − x 1 ) + ( y 2 − y 1 ) ( y 3 − y 1 ) \vec{a} \cdot \vec{b} = (x_2-x_1)(x_3-x_1)+(y_2-y_1)(y_3-y_1) a b =(x2x1)(x3x1)+(y2y1)(y3y1)
  • 根据以上公式: S = 1 2 ( ∣ a ⃗ ∣ ∣ b ⃗ ∣ ) 2 − ( a ⃗ ⋅ b ⃗ ) 2 S = \frac{1}{2}\sqrt{(|\vec{a}||\vec{b}|)^2 - (\vec{a} \cdot \vec{b})^2} S=21(a b )2(a b )2
    已经可以做到输入 x 1 , y 1 , x 2 , y 2 , x 3 , y 3 x_1,y_1, x_2,y_2, x_3,y_3 x1,y1,x2,y2,x3,y3,求S。
    继续化归,可以得到公式
    S = 1 2 ∣ x 1 y 2 + x 2 y 3 + x 3 y 1 − x 1 y 3 − x 2 y 1 − x 3 y 2 ∣ S=\frac{1}{2}|x_1y_2+x_2y_3+x_3y_1-x_1y_3-x_2y_1-x_3y_2| S=21x1y2+x2y3+x3y1x1y3x2y1x3y2

2. 海伦公式:已知三条边的长度求三角形面积

a = ∣ a ⃗ ∣ , b = ∣ b ⃗ ∣ , c = ∣ c ⃗ ∣ a = |\vec{a}|, b = |\vec{b}|, c = |\vec{c}| a=a ,b=b ,c=c
已知 a ⃗ = ( x 2 − x 1 , y 2 − y 1 ) , b ⃗ = ( x 3 − x 1 , y 3 − y 1 ) , c ⃗ = ( x 3 − x 2 , y 3 − y 2 ) \vec{a} = (x_2-x_1,y_2-y_1), \vec{b} = (x_3-x_1,y_3-y_1), \vec{c} = (x_3-x_2,y_3-y_2) a =(x2x1,y2y1),b =(x3x1,y3y1),c =(x3x2,y3y2)
有:

  • a = ∣ a ⃗ ∣ = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 a = |\vec{a}| = \sqrt{(x_2-x_1)^2+(y_2-y_1)^2} a=a =(x2x1)2+(y2y1)2
    b = ∣ b ⃗ ∣ = ( x 3 − x 1 ) 2 + ( y 3 − y 1 ) 2 b = |\vec{b}| = \sqrt{(x_3-x_1)^2+(y_3-y_1)^2} b=b =(x3x1)2+(y3y1)2
    c = ∣ c ⃗ ∣ = ( x 3 − x 2 ) 2 + ( y 3 − y 2 ) 2 c = |\vec{c}| = \sqrt{(x_3-x_2)^2+(y_3-y_2)^2} c=c =(x3x2)2+(y3y2)2
  • 半周长 p = 1 2 ( a + b + c ) p = \frac{1}{2}(a+b+c) p=21(a+b+c)
  • 海伦公式,三角形面积 S = p ( p − a ) ( p − b ) ( p − c ) S = \sqrt{p(p-a)(p-b)(p-c)} S=p(pa)(pb)(pc)

3. 求一个数的绝对值

  • 求整数的绝对值:int abs(int a);
  • 求浮点型数的绝对值:double fabs(double a);

【题解代码】

解法1:用公式 S = 1 2 ( ∣ a ⃗ ∣ ∣ b ⃗ ∣ ) 2 − ( a ⃗ ⋅ b ⃗ ) 2 S = \frac{1}{2}\sqrt{(|\vec{a}||\vec{b}|)^2 - (\vec{a} \cdot \vec{b})^2} S=21(a b )2(a b )2

#include <bits/stdc++.h>
using namespace std;
int main()
{
	double x1,y1,x2,y2,x3,y3,a_m,b_m,adotb,res;
	cin>>x1>>y1>>x2>>y2>>x3>>y3;
	a_m = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));//|a|
	b_m = sqrt((x3-x1)*(x3-x1) + (y3-y1)*(y3-y1));//|b|
	adotb = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1);//a点乘b 
	res = 0.5 * sqrt(a_m*b_m*a_m*b_m-adotb*adotb);//结果 
	cout<<fixed<<setprecision(2)<<res<<endl;
	return 0;
}

解法2:用公式 S = 1 2 ∣ x 1 y 2 + x 2 y 3 + x 3 y 1 − x 1 y 3 − x 2 y 1 − x 3 y 2 ∣ S=\frac{1}{2}|x_1y_2+x_2y_3+x_3y_1-x_1y_3-x_2y_1-x_3y_2| S=21x1y2+x2y3+x3y1x1y3x2y1x3y2

#include <bits/stdc++.h>
using namespace std;
int main()
{
	double x1,y1,x2,y2,x3,y3;
	cin>>x1>>y1>>x2>>y2>>x3>>y3;
	cout<<fixed<<setprecision(2)<<fabs((x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2))/2;
	return 0;
}

解法3:用海伦公式 S = p ( p − a ) ( p − b ) ( p − c ) S = \sqrt{p(p-a)(p-b)(p-c)} S=p(pa)(pb)(pc)

#include <bits/stdc++.h>
using namespace std;
int main()
{
	double x1,y1,x2,y2,x3,y3,a,b,c,p;
	cin>>x1>>y1>>x2>>y2>>x3>>y3;
	a = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));//|a|
	b = sqrt((x3-x1)*(x3-x1) + (y3-y1)*(y3-y1));//|b|
	c = sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));//|c|
	p = (a + b + c)/2;//p半周长 
	cout<<fixed<<setprecision(2)<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页