The Wayback Machine - https://web.archive.org/web/20240823160310/https://www.geeksforgeeks.org/check-whether-the-point-x-y-lies-on-a-given-line/
Open In App

Check whether the point (x, y) lies on a given line

Last Updated : 07 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
24.51%
47.4K

Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line.

Examples: 

Input: m = 3, c = 2, x = 1, y = 5 
Output: Yes 
m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y 
Hence, the given point satisfies the line’s equation

Input: m = 5, c = 2, x = 2, y = 5 
Output: No 
 

Approach: In order for the given point to lie on the line, it must satisfy the equation of the line. Check whether y = (m * x) + c holds true.

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that return true if
// the given point lies on the given line
bool pointIsOnLine(int m, int c, int x, int y)
{
    // If (x, y) satisfies the equation of the line
    if (y == ((m * x) + c))
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int m = 3, c = 2;
    int x = 1, y = 5;
 
    if (pointIsOnLine(m, c, x, y))
        cout << "Yes";
    else
        cout << "No";
}


Java




// Java implementation of the approach
 
class GFG
{
 
// Function that return true if
// the given point lies on the given line
static boolean pointIsOnLine(int m, int c,
                        int x, int y)
{
    // If (x, y) satisfies the equation
    // of the line
    if (y == ((m * x) + c))
        return true;
 
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int m = 3, c = 2;
    int x = 1, y = 5;
 
    if (pointIsOnLine(m, c, x, y))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code has been contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
 
# Function that return true if the
# given point lies on the given line
def pointIsOnLine(m, c, x, y):
     
    # If (x, y) satisfies the
    # equation of the line
    if (y == ((m * x) + c)):
        return True;
 
    return False;
 
# Driver code
m = 3; c = 2;
x = 1; y = 5;
 
if (pointIsOnLine(m, c, x, y)):
    print("Yes");
else:
    print("No");
     
# This code is contributed by mits


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function that return true if
// the given point lies on the given line
static bool pointIsOnLine(int m, int c,
                          int x, int y)
{
    // If (x, y) satisfies the equation
    // of the line
    if (y == ((m * x) + c))
        return true;
 
    return false;
}
 
// Driver code
public static void Main()
{
    int m = 3, c = 2;
    int x = 1, y = 5;
 
    if (pointIsOnLine(m, c, x, y))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Akanksha Rai


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function that return true if
// the given point lies on the given line
function pointIsOnLine(m, c, x, y)
{
     
    // If (x, y) satisfies the equation
    // of the line
    if (y == ((m * x) + c))
        return true;
 
    return false;
}
 
// Driver code
var m = 3, c = 2;
var x = 1, y = 5;
 
if (pointIsOnLine(m, c, x, y))
    document.write("Yes");
else
    document.write("No");
 
// This code is contributed by Rajput-Ji
 
</script>


PHP




<?php
// PHP implementation of the approach
 
// Function that return true if the
// given point lies on the given line
function pointIsOnLine($m, $c, $x, $y)
{
     
    // If (x, y) satisfies the equation
    // of the line
    if ($y == (($m * $x) + $c))
        return true;
 
    return false;
}
 
// Driver code
$m = 3; $c = 2;
$x = 1; $y = 5;
 
if (pointIsOnLine($m, $c, $x, $y))
    echo "Yes";
else
    echo "No";
     
// This code is contributed by Ryuga
?>


Output

Yes

Time Complexity: O(1)

Auxiliary Space: O(1)



Similar Reads

Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
Check if given point lies in range of any of the given towers
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not. Examples: Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15,
7 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Equation of a straight line passing through a point and making a given angle with a given line
Given four integers a, b, c representing coefficients of a straight line with equation (ax + by + c = 0), the task is to find the equations of the two straight lines passing through a given point [Tex](x1, y1) [/Tex]and making an angle ? with the given straight line. Examples: Input: a = 2, b = 3, c = -7, x1 = 4, y1 = 9, ? = 30Output: y = -0.49x +1
15+ min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Check whether the given floating point number is a palindrome
Given a floating-point number N, the task is to check whether it is palindrome or not. Input: N = 123.321 Output: YesInput: N = 122.1 Output: No Approach: First, convert the given floating-point number into a character array.Initialize the low to first index and high to the last index.While low &lt; high: If the character at low is not equal to the
4 min read
Check whether given floating point number is even or odd
Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number but its last digit is divisible by 2. Examples : I
6 min read
Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
Given three integers a, b and c which represents coefficients of the equation of a line a * x + b * y - c = 0. Given two integer points (x1, y1) and (x2, y2). The task is to determine whether the points (x1, y1) and (x2, y2) lie on the same side of the given line or not.Examples: Input : a = 1, b = 1, c = 1, x1 = 1, y1 = 1, x2 = 1, y2 = 2 Output :
6 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Check if vertex X lies in subgraph of vertex Y for the given Graph
Given an undirected graph and two vertices X and Y, our task is to check whether the vertex X lies in the subgraph of the vertex Y. Examples: Input: X = 2, Y = 3 Output: No Explanation: Subgraph of a vertex Y = 3 is set of all the vertex which lies below Y and are reachable by Y. Here subgraph of 3 contains {6} not 2. Input: X = 6, Y = 1 Output: Ye
10 min read
Check whether a point exists in circle sector or not.
We have a circle centered at origin (0, 0). As input we are given with starting angle of the circle sector and the size of the circle sector in percentage. Examples: Input : Radius = 8 StartAngle = 0 Percentage = 12 x = 3 y = 4 Output : Point (3, 4) exists in the circle sector Input : Radius = 12 Startangle = 45 Percentage = 25 x = 3 y = 4 Output :
5 min read
Rearrange two given arrays such that sum of same indexed elements lies within given range
Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print "Yes". Otherwise, print "No". Examples: Input: arr
6 min read
Count pairs from a given array whose product lies in a given range
Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i &lt; j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: Valid pairs are {4, 1}, {1, 5} and {4, 2}. Input: a
13 min read
Find time required to reach point N from point 0 according to given rules
Given two positive integers X and Y and N number of points arranged in a line, the task is to find the time required to reach point N from point 0 according to the following rules: Every point has one barrier that closes after every Y minutes and remains closed for the next Y minutes.On reaching a point if the barrier is closed, then the person nee
7 min read
Check whether a straight line can be formed using N co-ordinate points
Given an array arr[] of N co-ordinate points, the task is to check whether a straight line can be formed using these co-ordinate points. Input: arr[] = {{0, 0}, {1, 1}, {2, 2}} Output: Yes Explanation: Slope of every two points is same. That is 1. Therefore, a straight line can be formed using these points. Input: arr[] = {{0, 1}, {2, 0}} Output: Y
6 min read
Queries to check if a number lies in N ranges of L-R
Given N ranges and Q queries consisting of numbers. Every range consists of L and R. The task is to check if the given number lies in any of the given ranges or not for every query.Note: There is no overlapping range.Examples: Input: range[] = { {5, 6}, {1, 3}, {8, 10} Q = 4 1st query: 2 2nd query: 3 3rd query: 4 4th query: 7Output: Yes Yes No No1s
8 min read
Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
Find the other end point of a line with given one end and mid
Given a midpoint of line(m1, m2) and one coordinate of a line (x1, y1), find the other end point(x2, y2) of a line. Examples: Input : x1 = –1, y1 = 2, and m1 = 3, m2 = –6 Output : x2 = 7, y2 = 10 Input : x1 = 6.4, y1 = 3 and m1 = –10.7, m2 = 4 Output : x2 = 3, y2 = 4 The Midpoint Formula: The midpoint of two points, (x1, y2) and (x2, y2) is the poi
4 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 an Integer point on a line segment with given two ends
Given two points pointU and pointV in XY-space, we need to find a point which has integer coordinates and lies on a line going through points pointU and pointV. Examples: If pointU = (1, -1 and pointV = (-4, 1) then equation of line which goes through these two points is, 2X + 5Y = -3 One point with integer co-ordinate which satisfies above equatio
8 min read
Section formula (Point that divides a line in given ratio)
Given two coordinates (x1, y1) and (x2, y2), and m and n, find the co-ordinates that divides the line joining (x1, y1) and (x2, y2) in the ratio m : n  Examples:   Input : x1 = 1, y1 = 0, x2 = 2 y2 = 5, m = 1, n = 1Output : (1.5, 2.5)Explanation: co-ordinates (1.5, 2.5)divides the line in ratio 1 : 1Input : x1 = 2, y1 = 4, x2 = 4, y2 = 6, m = 2, n
5 min read
Article Tags :
Practice Tags :