Minimum lines to cover all points

Given N points in 2-dimensional space, we need to print the count of the minimum number of lines which traverse through all these N points and which go through a specific (xO, yO) point also.

Examples:

```If given points are (-1, 3), (4, 3), (2, 1), (-1, -2),
(3, -3) and (xO, yO) point is (1, 0) i.e. every line
must go through this point.
Then we have to draw at least two lines to cover all
these points going through (xO, yO) as shown in below
diagram.

```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We can solve this problem by considering the slope of all points with (xO, yO). If two distinct points have the same slope with (xO, yO) then they can be covered with same line only so we can track slope of each point and whenever we get a new slope we will increase our line count by one.
In below code slope is stored as a pair of integer to get rid of the precision problem and a set is used to keep track of occurred slopes.
Please see below code for better understanding.

 `// C++ program to get minimum lines to cover ` `// all the points ` `#include ` `using` `namespace` `std; ` ` `  `//  Utility method to get gcd of a and b ` `int` `gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == 0) ` `        ``return` `a; ` `    ``return` `gcd(b, a % b); ` `} ` ` `  `//  method returns reduced form of dy/dx as a pair ` `pair<``int``, ``int``> getReducedForm(``int` `dy, ``int` `dx) ` `{ ` `    ``int` `g = gcd(``abs``(dy), ``abs``(dx)); ` ` `  `    ``//  get sign of result ` `    ``bool` `sign = (dy < 0) ^ (dx < 0); ` ` `  `    ``if` `(sign) ` `        ``return` `make_pair(-``abs``(dy) / g, ``abs``(dx) / g); ` `    ``else` `        ``return` `make_pair(``abs``(dy) / g, ``abs``(dx) / g); ` `} ` ` `  `/*  method returns minimum number of lines to ` `    ``cover all points where all lines goes ` `    ``through (xO, yO) */` `int` `minLinesToCoverPoints(``int` `points[][2], ``int` `N, ` `                                   ``int` `xO, ``int` `yO) ` `{ ` `    ``//  set to store slope as a pair ` `    ``set< pair<``int``, ``int``> > st; ` `    ``pair<``int``, ``int``> temp; ` `    ``int` `minLines = 0; ` ` `  `    ``//  loop over all points once ` `    ``for` `(``int` `i = 0; i < N; i++) ` `    ``{ ` `        ``//  get x and y co-ordinate of current point ` `        ``int` `curX = points[i][0]; ` `        ``int` `curY = points[i][1]; ` ` `  `        ``temp = getReducedForm(curY - yO, curX - xO); ` ` `  `        ``// if this slope is not there in set, ` `        ``// increase ans by 1 and insert in set ` `        ``if` `(st.find(temp) == st.end()) ` `        ``{ ` `            ``st.insert(temp); ` `            ``minLines++; ` `        ``} ` `    ``} ` ` `  `    ``return` `minLines; ` `} ` ` `  `// Driver code to test above methods ` `int` `main() ` `{ ` `    ``int` `xO, yO; ` `    ``xO = 1; ` `    ``yO = 0; ` ` `  `    ``int` `points[][2] = ` `    ``{ ` `        ``{-1, 3}, ` `        ``{4, 3}, ` `        ``{2, 1}, ` `        ``{-1, -2}, ` `        ``{3, -3} ` `    ``}; ` ` `  `    ``int` `N = ``sizeof``(points) / ``sizeof``(points[0]); ` `    ``cout << minLinesToCoverPoints(points, N, xO, yO); ` `    ``return` `0; ` `} `

Output:

```2
```

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

tags:

Geometric Geometric
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://tutorialspoint.dev/algorithm/geometric-algorithms/minimum-lines-cover-points", "fetch_time": 1638582015000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2021-49/segments/1637964362923.11/warc/CC-MAIN-20211204003045-20211204033045-00072.warc.gz", "warc_record_offset": 646699174, "warc_record_length": 8000, "token_count": 1197, "char_count": 3410, "score": 3.703125, "int_score": 4, "crawl": "CC-MAIN-2021-49", "snapshot_type": "latest", "language": "en", "language_score": 0.8181950449943542, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00022-of-00064.parquet"}