[Careercup] 7.6 The lines Passes the most number of the Points go through the most points of the line

Source: Internet
Author: User

7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of points.

This problem gives us a lot of points, let's ask for a straight line that passes the most points. To the previous 7.5 A line Cut Two squares in half average split two square lines, all need to write out the point class and line class. In the Straight line class, I use the slope and intercept to represent the line, in order to deal with the slope does not exist, we also need a flag to mark whether it is a vertical line. In a straight line class, we have a function that determines whether two straight lines are equal. The method of judging equality is the same as that of the previous 7.3 line intersection, all need to use epsilon, as long as the absolute value of the difference of two numbers is less than epsilon, we will assume that is equal. For all the points given, each two points can form a straight line, our method is to traverse all the lines, all the same lines are stored in the hash table, key is the slope of the line, the mapping relationship is the slope and the map of the line set, then we just need to find the one that contains the most straight line, see the following code:

classPoint { Public:    Double_x, _y; Point (DoubleXDoubley): _x (x), _y (y) {};};classLine { Public:    StaticconstexprDouble_epsilon =0.0001; Double_slope, _intercept; BOOL_infi_slope =false; Line (point P, point Q) {if(Fabs (p._x-q._x) >_epsilon) {_slope= (p._y-q._y)/(P._x-q._x); _intercept= p._y-_slope *p._x; } Else{_infi_slope=true; _intercept=p._x; }    }    Static DoubleFloortonearestepsilon (Doubled) {intR = (int) (d/_epsilon); return((Double) r) *_epsilon; }    BOOLIsequivalent (DoubleADoubleb) {return(Fabs (a) <_epsilon); }    BOOLIsequivalent (line other) {if(Isequivalent (_slope, Other._slope) && isequivalent (_intercept, other._intercept) && (_infi_slope = =other._infi_slope)) {            return true; }        return false; }};classSolution { Public: Line findbestline (Vector<Point> &points) {Line res (points[0], points[1]); intbestcnt =0; Unordered_map<Double, Vector<line> >m;  for(inti =0; I < (int) Points.size (); ++i) { for(intj = i +1; J < (int) Points.size (); ++j) {Line line (Points[i], points[j]);                Insertline (M, line); intCNT =Countequivalentlines (M, line); if(CNT >bestcnt) {Res=Line ; Bestcnt=CNT; }            }        }        returnRes; }    voidInsertline (unordered_map<Double, vector<line> > &m, Line &Line ) {Vector<Line>lines; DoubleKey =Line::floortonearestepsilon (Line._slope); if(M.find (key)! =M.end ()) {Lines=M[key]; } Else{M[key]=lines;    } lines.push_back (line); }    intCountequivalentlines (unordered_map<Double, vector<line> > &m, Line &Line ) {        DoubleKey =Line::floortonearestepsilon (Line._slope); DoubleEPS =Line::_epsilon; returnCountequivalentlines (M[key], line) + Countequivalentlines (M[key-eps], line) + countequivalentlines (M[key +EPS], line); }    intCountequivalentlines (vector<line> &lines, Line &Line ) {        if(Lines.empty ())return 0; intres =0;  for(Auto &a:lines) {            if(line) + + (a.isequivalent)Res; }        returnRes; }};

[Careercup] 7.6 The lines Passes the most number of the Points go through the most points of the line

Related Article

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.