URAL1348.GoatintheGarden2 [calculate the distance from a point to a line segment]

Source: Internet
Author: User
Link: acm. timus. ruproblem. aspx? The topic space1num1348 is to find the shortest distance and longest distance from a point to a line segment .. It is easier to get the longest distance, that is, to find the long distance between the two endpoints of a point and a line segment is ans. The shortest distance is interesting... The possible situation is the vertical foot of the point-to-line vertical line.

Question link: http://acm.timus.ru/problem.aspx? Space = 1num = 1348 the question is: Find the shortest distance and longest distance from a point to a line segment .. It is easier to get the longest distance, that is, to find the long distance between the two endpoints of a point and a line segment is ans. The shortest distance is interesting... The possible situation is the vertical foot of the point-to-line vertical line.

Question link: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1348

The question is: Find the shortest distance and longest distance from a point to a line segment ..

It is easier to get the longest distance, that is, to find the long distance between the two endpoints of a point and a line segment is ans.

The shortest distance is interesting...

It is possible that the point-to-line vertical line segment is inside the vertical line segment, and that is outside the vertical line segment...

In the online segment, the vertical line length, that is, the shortest distance, can be obtained by applying the cross product to calculate the area + the bottom surface length ..

If it is out of the online segment, the shortest distance is the minimum value of the two endpoints from the point to the line segment ..

Then the problem arises .. How can we determine whether the online segment is inside or outside the online segment ??

For details, see the code. --...

Code:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const double eps = 1e-8; const double pi = acos (-1); // POINT struct POINT {double x, y; POINT () {} POINT (double a, double B) {x = a; y = B ;}}; // line segment struct Seg {POINT a, B; Seg () {} Seg (POINT x, POINT y) {a = x; B = y ;}}; // straight Line struct Line {POINT a, B; Line () {} Line (POINT x, POINT y) {a = x; B = y ;}}; // cross (POINT o, POINT a, POINT B) {return (. x-o. x) * (B. y-o. y)-(B. x- O. x) * (. y-o. y);} // calculate the distance between two points double dis (POINT a, POINT B) {return sqrt (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y);} Seg s; POINT p; double L; // distance from a POINT to a straight line .. double PointToLine (POINT p, Line l) {return fabs (cross (p, l. a, l. b)/dis (l. a, l. b);} // The distance from a line segment to a straight line .. double PointToSeg (POINT p, Seg s) {POINT tmp = p; tmp. x + = s. a. y-s. b. y; tmp. y + = s. b. x-s. a. x; if (cross (s. a, p, tmp) * cross (s. b, p, tmp)> = 0) {return min (dis (p, s. a), dis (p, s. b);} return PointToLine (p, Line (s. a, s. b);} void solve () {double ans1 = PointToSeg (p, s), ans2 = max (dis (p, s. a), dis (p, s. b); printf ("%. 2lf \ n %. 2lf \ n ", ans1> L? Ans1-L: 0, ans2> L? Ans2-L: 0); return;} int main () {// freopen ("11.txt"," r ", stdin); while (~ Scanf ("% lf", & s. a. x, & s. a. y, & s. b. x, & s. b. y) {scanf ("% lf", & p. x, & p. y, & L); solve ();} return 0 ;}
    
   
  
 

--->

Well, you still need to study well...

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.