玲珑杯-射击气球-点到线段的距离
生活随笔
收集整理的這篇文章主要介紹了
玲珑杯-射击气球-点到线段的距离
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
玲瓏杯-射擊氣球-點(diǎn)到線段的距離
求點(diǎn)到線段的距離
由于網(wǎng)頁顯示吃tab鍵,縮進(jìn)無法正常顯示
對(duì)于空間V-xyz三維幾何中點(diǎn)到水平線段的距離,可以投影到xy平面(歐式幾何)上。distance = √(?h ^2 + d ^2);
其中d為投影xy平面上的點(diǎn)到線段的距離。
線段端點(diǎn)a(x1, y1), b(x2, y2),任一點(diǎn)vxt, yt)。
若兩端點(diǎn)a,b是否共點(diǎn)
d = 兩點(diǎn)間距離
否則
若兩端點(diǎn)和任意點(diǎn)v共線(用點(diǎn)積結(jié)果判斷)
若 v 在線段ab上
d = 0;
否則 d = min(|va|, |vb|);
否則?
若三點(diǎn)組成鈍角三角形(用余弦定理判斷)
d = min(|va|, |vb|);
否則 d = 三角形abv的以ab為底的高(用點(diǎn)乘求得)
今天玲瓏杯的A題,題不難,但過的不多,用了一些高中數(shù)學(xué)知識(shí),大概這就是為啥高中生虐了大學(xué)生的部分原因吧。
#include <iostream> #include <cstdio> #include <queue> #include <stack> #include <string> #include <cstring> #include <map> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> using namespace std; const int maxn = 1e4 + 7;int main() {ios::sync_with_stdio(false);int t;cin >> t;while(t--) {int x1, y1, H, x2, y2;cin >> H >> x1 >> y1 >> x2 >> y2;int x0, y0, h0, x, y, h;cin >> x0 >> y0 >> h0 >> x >> y >> h;int n;double line = sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));cin >> n;while(n--) {int t;cin >> t;int xt = x0 + t* x;int yt = y0 + t * y;int ht = h0 + t * h;double d;if (x1 == x2 && y1 == y2) { //重合 d = sqrt(pow(xt - x1, 2.0) + pow(yt - y1, 2.0));} else {if ((x1 - xt) * (y2 - yt) == (x2 - xt) * (y1 - yt)) { //共線 if(xt >= min(x1, x2) && yt <= max(y1, y2)) {d = 0.0;} else {d = min(sqrt(pow(x1 - xt, 2.0) + pow(y1 - yt, 2.0)),sqrt(pow(x2 - xt, 2.0) + pow(y2 - yt, 2.0)));}}else {if (line * line + pow(xt - x1, 2.0) + pow(yt - y1, 2.0) < pow(xt - x2, 2.0) + pow(yt - y2, 2.0) ||line * line + pow(xt - x2, 2.0) + pow(yt - y2, 2.0) < pow(xt - x1, 2.0) + pow(yt - y1, 2.0))d = min(sqrt(pow(x1 - xt, 2.0) + pow(y1 - yt, 2.0)),sqrt(pow(x2 - xt, 2.0) + pow(y2 - yt, 2.0)));else d = fabs((x1 - xt) * (y2 - yt) - (x2 - xt) * (y1 - yt)) / line; }}double ans = sqrt(pow(ht - H, 2.0) + d * d);printf("%.2lf\n", ans);}}return 0; }
總結(jié)
以上是生活随笔為你收集整理的玲珑杯-射击气球-点到线段的距离的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用IPv6实现公网访问远程桌面
- 下一篇: Mac版本降级教程