The Wayback Machine - https://web.archive.org/web/20240928070511/https://www.geeksforgeeks.org/find-simple-closed-path-for-a-given-set-of-points/
Open In App

Find Simple Closed Path for a given set of points

Last Updated : 04 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a set of points, connect the dots without crossing. 
 

Simple Closed Path for a given set of points 1Simple Closed Path for a given set of points 2

Example: 

Input: points[] = {(0, 3), (1, 1), (2, 2), (4, 4),
                   (0, 0), (1, 2), (3, 1}, {3, 3}};

Output: Connecting points in following order would
        not cause any crossing
       {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
        (4, 4), (1, 2), (0, 3)}

We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use sorting. 

  • Find the bottom-most point by comparing y coordinate of all points. If there are two points with same y value, then the point with smaller x coordinate value is considered. Put the bottom-most point at first position. 
     

find the bottom-most point by comparing y coordinate of all points

  • Consider the remaining n-1 points and sort them by polar angle in counterclockwise order around points[0]. If polar angle of two points is same, then put the nearest point first.
  • Traversing the sorted array (sorted in increasing order of angle) yields simple closed path. 
     

traversing the sorted array

How to compute angles? 
One solution is to use trigonometric functions. 
Observation: We don’t care about the actual values of the angles. We just want to sort by angle. 
Idea: Use the orientation to compare angles without actually computing them!

Below is C++ implementation of above idea.  

C++




// A C++ program to find simple closed path for n points
// for explanation of orientation()
#include <bits/stdc++.h>
using namespace std;
 
struct Point
{
    int x, y;
};
 
// A global point needed for  sorting points with reference
// to the first point. Used in compare function of qsort()
Point p0;
 
// A utility function to swap two points
int swap(Point &p1;, Point &p2;)
{
    Point temp = p1;
    p1 = p2;
    p2 = temp;
}
 
// A utility function to return square of distance between
// p1 and p2
int dist(Point p1, Point p2)
{
    return (p1.x - p2.x)*(p1.x - p2.x) +
           (p1.y - p2.y)*(p1.y - p2.y);
}
 
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are collinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p, Point q, Point r)
{
    int val = (q.y - p.y) * (r.x - q.x) -
              (q.x - p.x) * (r.y - q.y);
 
    if (val == 0) return 0;  // collinear
    return (val > 0)? 1: 2; // clockwise or counterclock wise
}
 
// A function used by library function qsort() to sort
//  an array of points with respect to the first point
int compare(const void *vp1, const void *vp2)
{
   Point *p1 = (Point *)vp1;
   Point *p2 = (Point *)vp2;
 
   // Find orientation
   int o = orientation(p0, *p1, *p2);
   if (o == 0)
     return (dist(p0, *p2) >= dist(p0, *p1))? -1 : 1;
 
   return (o == 2)? -1: 1;
}
 
// Prints simple closed path for a set of n points.
void printClosedPath(Point points[], int n)
{
   // Find the bottommost point
   int ymin = points[0].y, min = 0;
   for (int i = 1; i < n; i++)
   {
     int y = points[i].y;
 
     // Pick the bottom-most. In case of tie, choose the
     // left most point
     if ((y < ymin) || (ymin == y &&
         points[i].x < points[min].x))
        ymin = points[i].y, min = i;
   }
 
   // Place the bottom-most point at first position
   swap(points[0], points[min]);
 
   // Sort n-1 points with respect to the first point.
   // A point p1 comes before p2 in sorted output if p2
   // has larger polar angle (in counterclockwise
   // direction) than p1
   p0 = points[0];
   qsort(&points;[1], n-1, sizeof(Point), compare);
 
   // Now stack has the output points, print contents
   // of stack
   for (int i=0; i<n; i++)
       cout << "(" << points[i].x << ", "
            << points[i].y <<"), ";
}
 
// Driver program to test above functions
int main()
{
    Point points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4},
                       {0, 0}, {1, 2}, {3, 1}, {3, 3}};
    int n = sizeof(points)/sizeof(points[0]);
    printClosedPath(points, n);
    return 0;
}


Java




import java.util.*;
 
class Point {
    int x, y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
 
class ConvexHull {
    static Point p0;
 
    static void swap(Point p1, Point p2) {
        Point temp = p1;
        p1 = p2;
        p2 = temp;
    }
 
    static int dist(Point p1, Point p2) {
        return (int)Math.pow(p1.x - p2.x, 2) +
               (int)Math.pow(p1.y - p2.y, 2);
    }
 
    static int orientation(Point p, Point q, Point r) {
        int val = (q.y - p.y) * (r.x - q.x) -
                  (q.x - p.x) * (r.y - q.y);
 
        if (val == 0) return 0; // collinear
        return (val > 0)? 1: 2;
    }
 
    static int compare(Point p1, Point p2) {
        int o = orientation(p0, p1, p2);
        if (o == 0)
            return (dist(p0, p2) >= dist(p0, p1))? -1 : 1;
        return (o == 2)? -1: 1;
    }
 
    static void printClosedPath(Point points[], int n) {
        int ymin = points[0].y, min = 0;
        for (int i = 1; i < n; i++) {
            int y = points[i].y;
            if ((y < ymin) || (ymin == y &&
                points[i].x < points[min].x))
                ymin = points[i].y;
                min = i;
        }
        swap(points[0], points[min]);
        p0 = points[0];
        Arrays.sort(points, 1, n, (p1, p2) -> compare(p1, p2));
        for (int i=0; i<n; i++)
            System.out.println("(" + points[i].x + ", " + points[i].y + "), ");
    }
 
    public static void main(String[] args) {
        Point[] points = {new Point(0, 3), new Point(1, 1), new Point(2, 2), new Point(4, 4),
                          new Point(0, 0), new Point(1, 2), new Point(3, 1), new Point(3, 3)};
        int n = points.length;
        printClosedPath(points, n);
    }
}


Python3




from functools import cmp_to_key
# A Python program to find simple closed path for n points
# for explanation of orientation()
 
# A global point needed for  sorting points with reference
# to the first point. Used in compare function of qsort()
p0 = None
 
# A utility function to return square of distance between
# p1 and p2
def dist(p1, p2):
    return (p1[0] - p2[0])*(p1[0] - p2[0]) + (p1[1] - p2[1])*(p1[1] - p2[1])
 
# To find orientation of ordered triplet (p, q, r).
# The function returns following values
# 0 --> p, q and r are collinear
# 1 --> Clockwise
# 2 --> Counterclockwise
def orientation(p, q, r):
    val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
 
    if val == 0: return 0  # collinear
    return 1 if val > 0 else 2 # clockwise or counterclock wise
 
# A function used by library function qsort() to sort
#  an array of points with respect to the first point
def compare(vp1, vp2):
    p1 = vp1
    p2 = vp2
 
    # Find orientation
    o = orientation(p0, p1, p2)
    if o == 0:
        return -1 if dist(p0, p2) >= dist(p0, p1) else 1
 
    return -1 if o == 2 else 1
 
# Prints simple closed path for a set of n points.
def printClosedPath(points, n):
    global p0
    # Find the bottommost point
    ymin = points[0][1]
    min = 0
    for i in range(1,n):
        y = points[i][1]
 
        # Pick the bottom-most. In case of tie, choose the
        # left most point
        if (y < ymin) or (ymin == y and points[i][0] < points[min][0]):
            ymin = points[i][1]
            min = i
 
    # Place the bottom-most point at first position
    temp = points[0]
    points[0] = points[min]
    points[min] = temp
 
    # Sort n-1 points with respect to the first point.
    # A point p1 comes before p2 in sorted output if p2
    # has larger polar angle (in counterclockwise
    # direction) than p1
    p0 = points[0]
    points.sort(key=cmp_to_key(compare))
 
    # Now stack has the output points, print contents
    # of stack
    for i in range(n):
        print("(",points[i][0],",",points[i][1],"), ", end="")
 
# Driver program to test above functions
points = [[0, 3], [1, 1], [2, 2], [4, 4], [0, 0], [1, 2], [3, 1], [3, 3]]
n = len(points)
 
printClosedPath(points, n)


C#




using System;
using System.Collections.Generic;
 
public class Point {
    public int x, y;
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
public class ClosestPath {
    static Point p0;
 
    static int dist(Point p1, Point p2)
    {
        return (p1.x - p2.x) * (p1.x - p2.x)
            + (p1.y - p2.y) * (p1.y - p2.y);
    }
 
    static int orientation(Point p, Point q, Point r)
    {
        int val = (q.y - p.y) * (r.x - q.x)
                  - (q.x - p.x) * (r.y - q.y);
        if (val == 0)
            return 0; // collinear
        return (val > 0)
            ? 1
            : 2; // clockwise or counterclockwise
    }
 
    static int compare(Point p1, Point p2)
    {
        int o = orientation(p0, p1, p2);
        if (o == 0)
            return (dist(p0, p2) >= dist(p0, p1)) ? -1 : 1;
        return (o == 2) ? -1 : 1;
    }
 
    static void printClosedPath(List<Point> points, int n)
    {
        // Find the bottommost point
        int ymin = points[0].y;
        int min = 0;
        for (int i = 1; i < n; i++) {
            int y = points[i].y;
            if ((y < ymin)
                || (ymin == y
                    && points[i].x < points[min].x)) {
                ymin = points[i].y;
                min = i;
            }
        }
 
        // Place the bottom-most point at first position
        Point temp = points[0];
        points[0] = points[min];
        points[min] = temp;
 
        // Sort n-1 points with respect to the first point.
        // A point p1 comes before p2 in sorted output if p2
        // has larger polar angle (in counterclockwise
        // direction) than p1
        p0 = points[0];
        points.Sort(compare);
 
        // Now stack has the output points, print contents
        // of stack
        for (int i = 0; i < n; i++) {
            Console.Write("(" + points[i].x + ", "
                          + points[i].y + "), ");
        }
    }
 
    public static void Main()
    {
        List<Point> points = new List<Point>() {
            new Point(0, 3), new Point(1, 1),
                new Point(2, 2), new Point(4, 4),
                new Point(0, 0), new Point(1, 2),
                new Point(3, 1), new Point(3, 3)
        };
        int n = points.Count;
 
        printClosedPath(points, n);
    }
}
// This code is contributed by user_dtewbxkn77n


Javascript




// A javascript program to find simple closed path for n points
// for explanation of orientation()
 
// A global point needed for  sorting points with reference
// to the first point. Used in compare function of qsort()
let p0;
 
// A utility function to return square of distance between
// p1 and p2
function dist(p1, p2)
{
    return (p1[0] - p2[0])*(p1[0] - p2[0]) +
           (p1[1] - p2[1])*(p1[1] - p2[1]);
}
 
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are collinear
// 1 --> Clockwise
// 2 --> Counterclockwise
function orientation(p, q, r)
{
    let val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
 
    if (val == 0) return 0;  // collinear
    return (val > 0)? 1: 2; // clockwise or counterclock wise
}
 
// A function used by library function qsort() to sort
//  an array of points with respect to the first point
function compare(vp1, vp2)
{
   let p1 = vp1;
   let p2 = vp2;
 
   // Find orientation
   let o = orientation(p0, p1, p2);
   if (o == 0)
     return (dist(p0, p2) >= dist(p0, p1))? -1 : 1;
 
   return (o == 2)? -1: 1;
}
 
// Prints simple closed path for a set of n points.
function printClosedPath(points, n)
{
   // Find the bottommost point
   let ymin = points[0][1];
   let min = 0;
   for (let i = 1; i < n; i++)
   {
     let y = points[i][1];
      
     // Pick the bottom-most. In case of tie, choose the
     // left most point
     if ((y < ymin) || (ymin == y && points[i][0] < points[min][0])){
        ymin = points[i][1];
        min = i;
     }
 
   }
 
   // Place the bottom-most point at first position
   let temp = points[0];
    points[0] = points[min];
    points[min] = temp;
 
   // Sort n-1 points with respect to the first point.
   // A point p1 comes before p2 in sorted output if p2
   // has larger polar angle (in counterclockwise
   // direction) than p1
   p0 = points[0];
   points.sort(compare);
 
   // Now stack has the output points, print contents
   // of stack
   for (let i=0; i<n; i++)
       console.log("(" + points[i][0] + "," + points[i][1] + "), ");
}
 
// Driver program to test above functions
let points = [[0, 3], [1, 1], [2, 2], [4, 4], [0, 0], [1, 2], [3, 1], [3, 3]];
let n = points.length;
 
printClosedPath(points, n);
 
// The code is contributed by Nidhi goel.


Output: 

(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
(4, 4), (1, 2), (0, 3), 

Time complexity of above solution is O(n Log n) if we use a O(nLogn) sorting algorithm for sorting points.
Auxiliary Space: O(1), since no extra space has been taken.

Source: 
http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf

 



Previous Article
Next Article

Similar Reads

Find the point on X-axis from given N points having least Sum of Distances from all other points
Given an array arr[] consisting of N integers, denoting N points lying on the X-axis, the task is to find the point which has the least sum of distances from all other points. Example: Input: arr[] = {4, 1, 5, 10, 2} Output: (4, 0) Explanation: Distance of 4 from rest of the elements = |4 - 1| + |4 - 5| + |4 - 10| + |4 - 2| = 12 Distance of 1 from
11 min read
Find the centroid of a non-self-intersecting closed Polygon
Given N vertices of the polygon, the task is to find the centroid of the polygonExamples: Input: ar = {{0, 0}, {0, 8}, {8, 8}, {8, 0}} Output: {Cx, Cy} = {4, 4} Input: ar = {{1, 2}, {3, -4}, {6, -7}} Output: {Cx, Cy} = {3.33, -3} Approach: The centroid of a non-self-intersecting closed polygon defined by n vertices (x0, y0), (x1, y1), ..., (xn-1, y
5 min read
Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
A circle is given with k equidistant points on its circumference. 2 points A and B are given in the circle. Find the count of all obtuse angles (angles larger than 90 degree) formed from /_ACB, where C can be any point in circle other than A or B. Note : A and B are not equal. A < B. Points are between 1 and K(both inclusive). Examples : Input :
5 min read
Count points which are revisited while following the path specified by a given string
Given a string S representing a sequence of moves(L, R, U, and D) and two integers X and Y representing the starting coordinates, the task is to find the number of positions that are revisited while following the directions specified in the given string according to the following rules: If the current character is L, then decrement x-coordinate by
8 min read
Check if the door is open or closed
Given n doors and n persons. The doors are numbered 1 to n and persons are given id's numbered 1 to n. Each door can have only 2 status open and closed. Initially all the doors have status closed. Find the final status of all the doors if a person changes the current status of all the doors, i.e. if status open then change to status closed and vice
10 min read
Ways to choose three points with distance between the most distant points <= L
Given a set of n distinct points x1, x2, x3... xn all lying on the X-axis and an integer L, the task is to find the number of ways of selecting three points such that the distance between the most distant points is less than or equal to L Note: Order is not important i.e the points {3, 2, 1} and {1, 2, 3} represent the same set of three points Exam
15+ min read
Steps required to visit M points in order on a circular ring of N points
Given an integer 'n', consider a circular ring containing 'n' points numbered from '1' to 'n' such that you can move in the following way : 1 -> 2 -> 3 -> ..... -> n -> 1 -> 2 -> 3 -> ...... Also, given an array of integers (of size 'm'), the task is to find the number of steps it'll take to get to every point in the array i
5 min read
Minimize the maximum distance between adjacent points after adding K points anywhere in between
Given an array arr[] of N integers representing the position of N points along a straight line and an integer K, the task is to find the minimum value of the maximum distance between adjacent points after adding K points anywhere in between, not necessarily on an integer position. Examples: Input: arr[] = {2, 4, 8, 10}, K = 1Output: 2Explanation: A
9 min read
Prime points (Points that split a number into two primes)
Given a n-digit number. Prime point is the index of the digit whose left and right side numbers are prime. Print all the prime points of the number. If no prime point exists print -1. Examples: Input : 2317 Output : 1 2 Explanation : Left and right side numbers of index point 1 are 2 and 17 respectively and both are primes. Left and right side numb
10 min read
Minimum number of points to be removed to get remaining points on one side of axis
We are given n points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis. Examples : Input : 4 1 1 2 2 -1 -1 -2 2 Output : 1 Explanation : If we remove (-1, -1) then all the remaining points are above x-axis. Thus the answer is 1. Input : 3 1 10
6 min read
Number of Integral Points between Two Points
Given two points p (x1, y1) and q (x2, y2), calculate the number of integral points lying on the line joining them. Input: x1 = 2, y1 = 2, x2 = 5, y2 = 5 Output: 2 Explanation: There are only 2 integral points on the line joining (2,2) and (5,5). The points are (3,3) and (4,4). Input: x1 = 1, y1 = 9, x2 = 8, y2 = 16 Output: 6 Explanation: There are
8 min read
Find the path from root to a vertex such that all K vertices belongs to this path or 1 distance away
Given a tree consisting of N vertices numbered from 1 to N rooted at vertex 1. You are given Q queries. Each query consists of the set of K distinct vertices vi[1], vi[2],â€Ķ, vi[K]. The task is to determine if there is a path from the root to some vertex X such that each of the given K vertices either belongs to this path or has the distance 1 to so
10 min read
Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
Given a directed graph, which may contain cycles, where every edge has weight, the task is to find the minimum cost of any simple path from a given source vertex ‘s’ to a given destination vertex ‘t’. Simple Path is the path from one vertex to another such that no vertex is visited more than once. If there is no simple path possible then return INF
10 min read
Check a given sentence for a given set of simple grammar rules
A simple sentence if syntactically correct if it fulfills given rules. The following are given rules. Sentence must start with a Uppercase character (e.g. Noun/ I/ We/ He etc.) Then lowercase character follows. There must be spaces between words. Then the sentence must end with a full stop(.) after a word. Two continuous spaces are not allowed. Two
15+ min read
Count of Unique Direct Path Between N Points On a Plane
Given N points on a plane, where each point has a direct path connecting it to a different point, the task is to count the total number of unique direct paths between the points. Note: The value of N will always be greater than 2. Examples: Input: N = 4Output: 6Explanation: Think of 4 points as a 4 sided polygon. There will 4 direct paths (sides of
3 min read
Shortest path between two points in a Matrix with at most K obstacles
Given a 2-D array matrix[][] of size ROW * COL and an integer K, where each cell matrix[i][j] is either 0 (empty) or 1 (obstacle). A pointer can move up, down, left, or right from and to an empty cell in a single step. The task is to find the minimum number of steps required to go from the source (0, 0) to the destination (ROW-1, COL-1) with less t
13 min read
Find integral points with minimum distance from given set of integers using BFS
Given an integer array A[] of length N and an integer K. The task is to find K distinct integral points that are not present in the given array such that the sum of their distances from the nearest point in A[] is minimized. An integral point is defined as the point with both the coordinates as integers. Also, in the x-axis, we don't need to consid
9 min read
Find points at a given distance on a line of given slope
Given the co-ordinates of a 2-dimensional point p(x0, y0). Find the points at a distance L away from it, such that the line formed by joining these points has a slope of M. Examples: Input : p = (2, 1) L = sqrt(2) M = 1 Output :3, 2 1, 0 Explanation: The two points are sqrt(2) distance away from the source and have the required slope m = 1. Input :
8 min read
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all p
6 min read
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path
Given an N-ary tree consisting of N nodes numbered from 1 to N rooted at node 1, the task is to assign values to each node of the tree such that the sum of values from any root to the leaf path which contains at least two nodes is not divisible by the number of nodes along that path. Examples: Input: N = 11, edges[][] = {{1, 2}, {1, 3}, {1, 4}, {1,
11 min read
Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
Given a connected graph with N vertices and M edges. The task is to find the shortest path from source to the destination vertex such that the difference between adjacent edge weights in the shortest path change from positive to negative and vice versa ( Weight(E1) > Weight(E2) < Weight(E3) .... ). If no such path exists then print -1. Exampl
13 min read
Minimum cost such that path sum is at max K (Target Path Sum)
Given a rooted tree consisting of N vertices. The vertices are numbered from 0 to N-1, and the root is the vertex 0. You are also given a value array Arr: Arr0, Arr1,....., Arrn-1. You can do any of these moves any number of times: Pick any node that is not root cut the edge between it and its parent and connect an edge between this node to the roo
11 min read
Difference Between Hamiltonian Path and Eulerian Path
The Hamiltonian and Eulerian paths are two significant concepts used to the solve various problems related to the traversal and connectivity of the graphs. Understanding the differences between these two types of the paths is crucial for the designing efficient algorithms and solving the complex graph problems. What is a Hamiltonian Path?Hamiltonia
2 min read
Path with smallest difference between consecutive cells (Path with minimum effort)
Given a 2D array arr[] of size N x M, where arr[i][j] represents the value of cell (i, j). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (n-1, m-1). You can move up, down, left, or right, and you wish to find a path such that the maximum absolute difference in values between two consecutive cells of
14 min read
Program to find simple moving average | Set-2
Simple Moving Average is the average obtained from the data for some t period of time . In normal mean, its value get changed with the changing data but in this type of mean it also changes with the time interval. We get the mean for some period t and then we remove some previous data. Again we get new mean and this process continues. This is why i
5 min read
Find a point whose sum of distances from all given points on a line is K
Given a sorted array arr[] consisting of N integers, representing points on a line and an integer K, the task is to find any point P between the first and last point such that the sum of distances of all given points from P is equal to K. If no such point exists, then print "-1". Examples: Input: arr[] = {1, 3, 6, 7, 11}, K = 18Output: 8Explanation
14 min read
Find the maximum possible distance from origin using given points
Given N 2-Dimensional points. The task is to find the maximum possible distance from the origin using given points. Using the ith point (xi, yi) one can move from (a, b) to (a + xi, b + yi). Note: N lies between 1 to 1000 and each point can be used at most once.Examples: Input: arr[][] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}} Output: 14.14 The farthest p
6 min read
Find the equation of plane which passes through two points and parallel to a given axis
Given two points A(x1, y1, z1) and B(x2, y2, z2) and a set of points (a, b, c) which represent the axis (ai + bj + ck), the task is to find the equation of plane which passes through the given points A and B and parallel to the given axis. Examples: Input: x1 = 1, y1 = 2, z1 = 3, x2 = 3, y2 = 4, z2 = 5, a= 6, b = 7, c = 8 Output: 2x + 4y + 2z + 0 =
9 min read
Find the equation of the straight line passing through the given points
Given an array arr containing N coordinate points in a plane, the task is to check whether the coordinate points lie on a straight line or not. If they lie on a straight line then print Yes and also the equation of that line otherwise print No. Example: Input: arr[] = {{1, 1}, {2, 2}, {3, 3}} Output: Yes1x- 1y=0 Input: arr[] = {{0, 1}, {2, 0}}Outpu
6 min read
Find X and Y intercepts of a line passing through the given points
Given two points on a 2D plane, the task is to find the x - intercept and the y - intercept of a line passing through the given points.Examples: Input: points[][] = {{5, 2}, {2, 7}} Output: 6.2 10.333333333333334Input: points[][] = {{3, 2}, {2, 4}} Output: 4.0 8.0 Approach: Find the slope using the given points.Put the value of the slope in the exp
7 min read
Article Tags :
Practice Tags :