search
HomeBackend DevelopmentC++The number of horizontal or vertical line segments required to connect 3 points

The number of horizontal or vertical line segments required to connect 3 points

Suppose that given three different points (or coordinates), you want to find the number of horizontal or vertical line segments that can be formed by connecting these three points. Such line segments are also called polylines. In order to solve this problem, you need the concept of computational geometry. In this article, we will discuss various ways to solve this problem in C.

Input and output scenarios

Assume that c1, c2 and c3 are the coordinates of 3 points on the Cartesian plane. The number of horizontal or vertical line segments connecting these 3 points will be as shown below.

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 − Horizontal and vertical line segments must be aligned with the coordinate axes.

Use If statement

We can use if statement to check if there is a horizontal or vertical line between these three points.

  • Create a function by combining c1.x with c2.x, c1.x with c3.x and c2.x and c3.x. If either condition is met, it means there is a horizontal line segment and the count is incremented.

  • Similarly, this function combines c1.y with c2.y, c1.y with c3.y and c2 .y and c3.y. If any of the conditions is met, the vertical line segment does exist. The count increases again.

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 three points are on the same axis of the Cartesian plane, that is, the X axis or the Y axis, the number of line segments required to connect them is 1. If the points form an L shape, the result is 2, otherwise the result is 3.

Use auxiliary functions

  • Here, we can use auxiliary functions (horizontalLine and verticalLine) to calculate line segments.

  • We exploit the fact that in the Cartesian system all points of a horizontal line lie on the same y-coordinate. horizontalLineThe function checks whether two points can form a horizontal line segment by comparing their y coordinates. If the y-coordinates are the same, there is a horizontal line.

  • Similarly, all points of a vertical line lie at the same x-coordinate. verticalLineThe function checks whether two points can form a vertical line segment by comparing their x-coordinates. If the x-coordinates are the same, there is a vertical line.

  • Next, we have the countLineSegments function, which is used to count the number of horizontal and vertical line segments. If there are horizontal or vertical line segments, 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

in conclusion

In this article, we explore various methods using C to find the number of horizontal and vertical lines that can connect 3 different points in the Cartesian plane. We have discussed the if statement approach to solving this problem. However, due to the large number of iterations, the time complexity also increases. We can effectively solve this problem by using auxiliary functions, which reduces the number of iterations and thereby reduces the time complexity.

The above is the detailed content of The number of horizontal or vertical line segments required to connect 3 points. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Debunking the Myths: Is C   Really a Dead Language?Debunking the Myths: Is C Really a Dead Language?May 05, 2025 am 12:11 AM

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

C# vs. C  : A Comparative Analysis of Programming LanguagesC# vs. C : A Comparative Analysis of Programming LanguagesMay 04, 2025 am 12:03 AM

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

Building XML Applications with C  : Practical ExamplesBuilding XML Applications with C : Practical ExamplesMay 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

XML in C  : Handling Complex Data StructuresXML in C : Handling Complex Data StructuresMay 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C   and Performance: Where It Still DominatesC and Performance: Where It Still DominatesMay 01, 2025 am 12:14 AM

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.

C   XML Frameworks: Choosing the Right One for YouC XML Frameworks: Choosing the Right One for YouApr 30, 2025 am 12:01 AM

The choice of C XML framework should be based on project requirements. 1) TinyXML is suitable for resource-constrained environments, 2) pugixml is suitable for high-performance requirements, 3) Xerces-C supports complex XMLSchema verification, and performance, ease of use and licenses must be considered when choosing.

C# vs. C  : Choosing the Right Language for Your ProjectC# vs. C : Choosing the Right Language for Your ProjectApr 29, 2025 am 12:51 AM

C# is suitable for projects that require development efficiency and type safety, while C is suitable for projects that require high performance and hardware control. 1) C# provides garbage collection and LINQ, suitable for enterprise applications and Windows development. 2)C is known for its high performance and underlying control, and is widely used in gaming and system programming.

How to optimize codeHow to optimize codeApr 28, 2025 pm 10:27 PM

C code optimization can be achieved through the following strategies: 1. Manually manage memory for optimization use; 2. Write code that complies with compiler optimization rules; 3. Select appropriate algorithms and data structures; 4. Use inline functions to reduce call overhead; 5. Apply template metaprogramming to optimize at compile time; 6. Avoid unnecessary copying, use moving semantics and reference parameters; 7. Use const correctly to help compiler optimization; 8. Select appropriate data structures, such as std::vector.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.