The shortest distance from point to segment

Source: Internet
Author: User

#include <bits/stdc++.h>using namespace Std;const int M = 1e5+10; Const double PI = ACOs ( -1.0); int n;d ouble sx, SY ;//source point double x[m], Y[m];d ouble minn = 1e18, MAXN = 1;d ouble dist (int id) {return sqrt ((X[ID]-SX) * (X[ID]-SX) + (y[id]- SY) * (Y[id]-sy));}    Double segdist (int id1, int id2) {//source point to polygon two points Id1,id2 shortest distance double A = Y[id2]-Y[ID1];    Double b = x[id1]-X[ID2];    Double C = x[id2]*y[id1]-y[id2]*x[id1]; if ((X[id1]-x[id2]) * (Sx-x[id2]) + (Y[id1]-y[id2]) * (Sy-y[id2]) < 0) return 1e18;//If the perpendicular falls outside the line segment, return Infinity if ((X[ID2]-X[ID1]) * (Sx-x[id1]) + (Y[ID2]-Y[ID1]) * (SY-Y[ID1]) < 0) return 1e18;//take advantage of the dot product obtuse <0 return Fabs (c+sy*b+sx*a)/sqrt (a*a+b*b);    Using cross product and area method}int main () {CIN >> n >> sx >> sy;    for (int i = 0; i < n; i + +) {scanf ("%lf%lf", &x[i],&y[i]);        } for (int i = 0; i < n; i + +) {MAXN = max (MAXN, Dist (i)); Minn = MIN (Minn, dist (i));//Perpendicular falls outside the segment, the shortest distance from the source to the segment Minn = MIN (Minn, segdiSt (I, (i+1)%n));    } cout.precision (12);    cout << pi* (maxn*maxn-minn*minn) << Endl; return 0;}

  

The shortest distance from point to segment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.