poj3819 Coverage (求直線與圓的交占直線的百分比 ),poj3819coverage

來源:互聯網
上載者:User

poj3819 Coverage (求直線與圓的交占直線的百分比 ),poj3819coverage

    題意:給你一條直線和若干個圓,求圓與直線相交的長度占整條直線的比例

   解題思路:通過定比分點的方法求出圓與直線的交占圓的比例。

    第一步:(確定投影的方向是x軸還是y軸) 

   (1)當直線的line.s(x, y), line.e(x, y)的line.s.x與line.e.x不同時,這條直線可以等同於起點為line.s.x, line.e.x;

   (2)不滿足(1)時(即line.s.x==line.e.x時),當直線的line.s(x, y), line.e(x, y)的line.s.y與line.e.y不同時,這條直線可以等同於起點為line.s.x, line.e.x;

   (3)當不滿足(1)以及(2)時(即line.s==line.e),這時候直線為一個點,任何的圓都與它沒有交,圓占整條直線的比例為0;

    第二步:(將圓投影到第一步得到的直線上)

    求出圓在直線上的投影的範圍;

    第三步:

    求出所有圓的並,將圓的併除以線段的長度,求圓與線段的交佔線段的百分比;


   

#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cmath>using namespace  std;const int MAX = 300;struct Node{double x, y;};struct Line{Node s, e;};Line line;Node p[MAX];double A, B, C, delta;double x, y, r;double x11, y11, dx, dy;Node tmp, cir;double sqr(double x){return x * x;}int circle_cross_line(Node s, Node e, Node O, double r)//判斷圓與直線是否有交點{double x0 = O.x, y0=O.y;x11 = s.x, y11 = s.y;double x2 = e.x, y2 = e.y;dx = x2 - x11, dy = y2 - y11;A = dx * dx + dy*dy;B = 2 * dx * (x11 - x0) + 2 * dy * (y11 - y0);C = sqr(x11-x0) + sqr(y11-y0) - sqr(r);delta = sqr(B) - 4 * A * C;return delta > 0;}int cmp(Node a, Node b){if (a.x < b.x)return 1;return 0;}int main(){int n, i, cnt;int flag, flagnum;double leng;while (scanf("%d", &n) && n){flagnum = 0;scanf("%lf%lf%lf%lf", &line.s.x, &line.s.y, &line.e.x, &line.e.y);if (line.s.x!=line.e.x){if (line.s.x < line.e.x){tmp.x = line.s.x;tmp.y = line.e.x;}else{tmp.x = line.e.x;tmp.y = line.s.x;}flag = 0;leng = fabs(line.e.x - line.s.x);}else if (line.s.x==line.e.x && line.s.y!=line.e.y){if (line.s.y < line.e.y){tmp.x = line.s.y;tmp.y = line.e.y;}else{tmp.x = line.e.y;tmp.y = line.s.y;}flag = 1;leng = fabs(line.e.y - line.s.y);}elseflagnum = 1;cnt = 0;for (i=0; i<n; i++){scanf("%lf%lf%lf", &cir.x, &cir.y, &r);if (flagnum)continue;if (circle_cross_line(line.s, line.e, cir, r)){p[cnt].x = (-B-sqrt(delta))/(2.0*A);p[cnt].y = (-B+sqrt(delta))/(2.0*A);if (flag==0){p[cnt].x = p[cnt].x * dx + x11;p[cnt].y = p[cnt].y * dx + x11;}else{p[cnt].x = p[cnt].x * dy + y11;p[cnt].y = p[cnt].y * dy + y11;}if (p[cnt].x>p[cnt].y){double t = p[cnt].x;p[cnt].x = p[cnt].y;p[cnt].y = t;}if (p[cnt].x>tmp.y || p[cnt].y<tmp.x)continue;if (p[cnt].x<tmp.x)p[cnt].x = tmp.x;if (p[cnt].y>tmp.y)p[cnt].y = tmp.y; cnt++;}}if (flagnum || cnt==0)printf("0.00\n");else{sort(p, p+cnt, cmp);double sum = 0;tmp = p[0];for (i=1; i<cnt; i++){if (p[i].x < tmp.y){if (p[i].y > tmp.y)tmp.y = p[i].y;}else{sum += tmp.y - tmp.x;tmp = p[i];}}sum += tmp.y - tmp.x;printf("%.2f\n", sum/leng*100.0);}}return 0;}

  





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.