Number of horizontal or vertical line segments to connect 3 points


Suppose you are given three different points (or coordinates) and you want to find out number of horizontal or vertical line segments which can be made by connecting those three points. Such line segments together are also known as polyline. You need concepts of computational geometry in order to solve this problem. In this article, we will discuss various approaches in C++ to tackle this problem.

Input Output Scenarios

Suppose c1, c2 and c3 are coordinates of 3 points in a cartesian plane. The number of horizontal or vertical line segments to connect these 3 points will be given as follows.

Input: c1 = (-1, -1), c2 = (-2, 3), c3 = (4, 3)
Output: 1
Input: c1 = (1, -1), c2 = (1, 3), c3 = (4, 3)
Output: 2
Input: c1 = (1, 1), c2 = (2, 6), c3 = (5, 2)
Output: 3

Note − The horizontal and vertical line segments must be aligned to the coordinate axis.

Using If Statements

We can use if statements to check whether horizontal or vertical lines exists between the three points.

  • Create a function which checks if any two points have similar x-coordinate by comparing c1.x with c2.x, c1.x with c3.x and c2.x with c3.x. If any of the conditions are satisfied, it means a horizontal line segment does exist and the count is incremented.

  • Similarly, the function checks if any two points have similar y-coordinate by comparing c1.y with c2.y, c1.y with c3.y and c2.y with c3.y. If any of the conditions are satisfied, it means a vertical line segment does exists. Again, the count is incremented.

Example

#include <iostream>
using namespace std;

struct Coordinate {
   int x;
   int y;
};
int countLineSegments(Coordinate c1, Coordinate c2, Coordinate c3) {
   int count = 0;
   // Check for the horizontal segment
   if (c1.x == c2.x || c1.x == c3.x || c2.x == c3.x)
      count++; 
   // Check for the vertical segment
   if (c1.y == c2.y || c1.y == c3.y || c2.y == c3.y)
      count++; 
   return count;
}

int main() {
   Coordinate c1, c2, c3;
   c1.x = -1; c1.y = -5;
   c2.x = -2; c2.y = 3;
   c3.x = 4; c3.y = 3;

   int numSegments = countLineSegments(c1, c2, c3);

   std::cout << "Number of horizontal or vertical line segments: " << numSegments << std::endl;

   return 0;
}

Output

Number of horizontal or vertical line segments: 1

Note  If all the three points are on the same axis of the cartesian plane that is, X-axis or Y-axis, then the number of line segments required to connect them is 1. If the points form L-shape, then the result will be 2. Otherwise, the result will be 3.

Using Helper Functions

  • Here, we can count the line segments by using the helper functions (horizontalLine and verticalLine).

  • We use the fact that in a cartesian system, a horizontal line will have all its points on the same y-coordinate. The horizontalLine function checks if two points can form a horizontal segment by comparing their y coordinates. If the y-coordinate are same, then horizontal line exists.

  • Similarly, a vertical line will have all its points on the same x-coordinate. The verticalLine function checks if two points can form a vertical segment by comparing their x coordinates. If the x-coordinate are same, then vertical line exists.

  • Next, we have the countLineSegments function which counts the number of horizontal and vertical line segments. If horizontal or vertical line exists, the count is incremented after each iteration.

Example

#include <iostream>
using namespace std;

struct Coordinate {
   int x;
   int y;
};

// Helper functions
bool horizontalLine(Coordinate c1, Coordinate c2)  {
   return c1.y == c2.y;
}

bool verticalLine(Coordinate c1, Coordinate c2)  {
   return c1.x == c2.x;
}

int countLineSegments(Coordinate c1, Coordinate c2, Coordinate c3)  {
   int count = 0;
   // Check for horizontal segment
   if (horizontalLine(c1, c2) || horizontalLine(c1, c3) || horizontalLine(c2, c3))
      count++; 
   // Check for vertical segment
   if (verticalLine(c1, c2) || verticalLine(c1, c3) || verticalLine(c2, c3))
      count++; 
   return count;
}

int main() {
   Coordinate c1, c2, c3;
   c1.x = -1; c1.y = -5;
   c2.x = -2; c2.y = 3;
   c3.x = 4; c3.y = 3;

   int numSegments = countLineSegments(c1, c2, c3);

   std::cout << "Number of horizontal or vertical line segments: " << numSegments << std::endl;

   return 0;
}

Output

Number of horizontal or vertical line segments: 1

Conclusion

In this article, we have explored various approaches using C++ to find out the number of horizontal and vertical lines which can connect 3 different points in a cartesian plane. We have discussed the if statement approach for solving the problem. However, since the number of iterations is more, the time complexity increases. We can efficiently solve this problem by using the helper functions which reduces the number of iterations resulting in decreased time complexity.

Updated on: 12-Jul-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements