Program to check if the points are parallel to X axis or Y axis
Given n points, we need to check if these n points are parallel to X axis or Y axis or to No axis.
Examples:
Input : x[] = {0, 0, 0, 0, 0|
y[] = {9, 2, 1, 3, 4}
Output : Parallel to Y Axis
Input : x[] = {1, 2, 3|
y[] = {9, 2, 1}
Output : Not Parallel to X or Y Axis
Approach
To find the points parallel to X or Y axis just check if the points are same for any axis or not. If all the points on X axis are same then the line is parallel to Y axis. If all the points on Y axis are same so the line is parallel to X axis. If none of the cases is true, then it is not parallel to any axis.
Input the value N. And then Input the value of points in a
C++
#include <bits/stdc++.h>
using namespace std;
void parallel( int n, int a[][2])
{
bool x = true , y = true ;
for ( int i = 0; i < n - 1; i++) {
if (a[i][0] != a[i + 1][0])
x = false ;
if (a[i][1] != a[i + 1][1])
y = false ;
}
if (x)
cout << "parallel to Y Axis" << endl;
else if (y)
cout << "parallel to X Axis" << endl;
else
cout << "Not parallel to X"
<< " and Y Axis" << endl;
}
int main()
{
int a[][2] = { { 1, 2 },
{ 1, 4 },
{ 1, 6 },
{ 1, 0 } };
int n = sizeof (a) / sizeof (a[0]);
parallel(n, a);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void parallel( int a[][])
{
boolean x = true , y = true ;
for ( int i = 0 ; i < a.length - 1 ; i++) {
if (a[i][ 0 ] != a[i + 1 ][ 0 ])
x = false ;
if (a[i][ 1 ] != a[i + 1 ][ 1 ])
y = false ;
}
if (x)
System.out.println( "Parallel to Y Axis" );
else if (y)
System.out.println( "Parallel to X Axis" );
else
System.out.println( "Not parallel to X"
+ " and Y axis" );
}
public static void main(String[] args)
{
int a[][] = { { 1 , 2 },
{ 1 , 4 },
{ 1 , 6 },
{ 1 , 0 } };
parallel(a);
}
}
|
Python3
def parallel(n, a):
x = True ;
y = True ;
for i in range (n - 1 ):
if (a[i][ 0 ] ! = a[i + 1 ][ 0 ]):
x = False ;
if (a[i][ 1 ] ! = a[i + 1 ][ 1 ]):
y = False ;
if (x):
print ( "Parallel to Y Axis" );
elif (y):
print ( "Parallel to X Axis" );
else :
print ( "Not Parallel to X and Y Axis" );
a = [[ 1 , 2 ], [ 1 , 4 ],
[ 1 , 6 ], [ 1 , 0 ]];
n = len (a);
parallel(n, a);
|
C#
class GFG {
static void parallel( int [, ] a)
{
bool x = true , y = true ;
for ( int i = 0; i < a.Rank - 1; i++) {
if (a[i, 0] != a[i + 1, 0])
x = false ;
if (a[i, 1] != a[i + 1, 1])
y = false ;
}
if (x)
System.Console.WriteLine( "Parallel to Y Axis" );
else if (y)
System.Console.WriteLine( "Parallel to X Axis" );
else
System.Console.WriteLine( "Not parallel to X"
+ " and Y axis" );
}
public static void Main()
{
int [, ] a = { { 1, 2 },
{ 1, 4 },
{ 1, 6 },
{ 1, 0 } };
parallel(a);
}
}
|
PHP
<?php
function parallel( $n , $a )
{
$x = true; $y = true;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $a [ $i ][0] != $a [ $i + 1][0])
$x = false;
if ( $a [ $i ][1] != $a [ $i + 1][1])
$y = false;
}
if ( $x )
echo "parallel to Y Axis" ;
else if (y)
echo "parallel to X Axis" ;
else
echo "Not parallel to X" , " and Y Axis" ;
}
$a = array ( array (1, 2),
array (1, 4),
array (1, 6),
array (1, 0));
$n = count ( $a );
parallel( $n , $a );
?>
|
Javascript
<script>
function parallel(n, a)
{
let x = true , y = true ;
for (let i = 0; i < n - 1; i++)
{
if (a[i][0] != a[i + 1][0])
x = false ;
if (a[i][1] != a[i + 1][1])
y = false ;
}
if (x)
document.write( "parallel to Y Axis" + "</br>" );
else if (y)
document.write( "parallel to X Axis" + "</br>" );
else
document.write( "Not parallel to X" +
" and Y Axis" + "</br>" );
}
let a = [ [ 1, 2 ],
[ 1, 4 ],
[ 1, 6 ],
[ 1, 0 ] ];
let n = a.length;
parallel(n, a);
</script>
|
Output:
Parallel to Y Axis
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...