Home > Backend Development > C++ > C program to check if a point is parallel to the X-axis or Y-axis

C program to check if a point is parallel to the X-axis or Y-axis

WBOY
Release: 2023-09-02 22:49:05
forward
1180 people have browsed it

C program to check if a point is parallel to the X-axis or Y-axis

Given n points, we have to check whether the point is parallel to x-axis or y-axis or no axis based on the graph. A graph is a graph used to show the relationship between two variables, each measured along a right-angled axis. Parallel refers to identical straight lines with the same distance at all points, just like railroad tracks that are parallel to each other.

So we have to find out whether the points are parallel to the x-axis or the distance between the y-axis coordinate and the axis is the same at all points.

What are axes

A graph is an axis measured along two axes (x-axis and y-axis). The axis starts at the point value 0 and extends according to its specific variable value. The two axes combine to form a figure like a right triangle.

Let us understand it clearly with a simple diagram -

C program to check if a point is parallel to the X-axis or Y-axis

The method used below is as follows -

  • First we obtain the coordinates of the graphic in the form of (x, y) coordinates.
  • Then check if they are parallel to which axis.
  • If all y-coordinates are the same, the graph is parallel to the x-axis.
  • If the x-coordinates are the same, the graph is parallel to the y-axis.
  • Otherwise the graph is not parallel to any axis.

Algorithm

Start
In function void parallel (int n, int a[][2])
   Step 1-> Declare and initialize i and j
   Step 2-> Declare bool x = true, y = true
   Step 3-> Loop For i = 0 and i < n &ndash; 1 and i++
   Loop For j = 0 and j < 2 and j++
      If a[i][0] != a[i + 1][0] then,
         Set x as false
      If a[i][1] != a[i + 1][1] then,
         Set y as false
      End loop
   End loop
   Step 4-> If x then,
      Print "parallel to X Axis</p><p>"
   Step 5-> Else if y
      Print "parallel to Y Axis</p><p>"
   Step 6-> Else
      Print "parallel to X and Y Axis</p><p>"
In function int main()
   Step 1-> Declare an array &ldquo;a[][2]&rdquo;
   Step 2-> Declare and Initialize n as sizeof(a) / sizeof(a[0])
   Step 3-> Call function parallel(n, a)
Copy after login

Example

#include <stdio.h>
// To check the line is parellel or not

void parallel(int n, int a[][2]) {
   int i, j;
   bool x = true, y = true;
   // checking for parallel to X and Y
   // axis condition
   for (i = 0; i < n - 1; i++) {
      for (j = 0; j < 2; j++) {
         if (a[i][0] != a[i + 1][0])
            x = false;
         if (a[i][1] != a[i + 1][1])
            y = false;
      }
   }
   // To display the output
   if (x)
      printf("parallel to X Axis</p><p>" );
   else if (y)
      printf("parallel to Y Axis</p><p>" );
   else
      printf("parallel to X and Y Axis</p><p>" );
}
int main() {
   int a[][2] = { { 2, 1 },
   { 3, 1 },
   { 4, 1 },
   { 0, 1 } };
   int n = sizeof(a) / sizeof(a[0]);
   parallel(n, a);
   return 0;
}
Copy after login

Output

If you run the above code, it will generate the following output-

parallel to Y Axis
Copy after login

The above is the detailed content of C program to check if a point is parallel to the X-axis or Y-axis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template