- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to use above below primitive to test whether two lines intersect

Here is a C++ program to use above below primitive to test whether two lines intersect. It can be used to test whether a line intersects a line segment. It does if and only if one endpoint of the segment is to the left of the line and the other is to the right.

## Algorithm

Begin For generating equation of the first line, generate random numbers for coefficient of x and y by using rand at every time of compilation. For generating equation of the second line, generate random numbers for coefficient of x and y by using rand at every time of compilation. Find the segment of line 1 as Y1. if (Y1 < 0) Find the segment of line 2 if (Y2 >= 0) print they are intersecting. else if (Y2 < 0) print they are not intersecting. else if (Y1 >0) Find the segment of line 2 if (Y2 <= 0) print they are intersecting. else if (Y2 >0) print they are not intersecting. End.

## Example Code

#include<time.h> #include<stdlib.h> #include<iostream> #include<math.h> using namespace std; const int L = 2; const int H= 20; int main(int argc, char **argv) { time_t s; time(&s); srand((unsigned int) s); int x1, x2, y1, y2; x1 = rand() % (H - L+ 1) + L; x2 = rand() % (H - L+ 1) + L; y1 = rand() % (H- L+ 1) + L; y2 = rand() % (H - L + 1) + L; cout << "The Equation of the 1st line is : (" << (y2 - y1) << ")x+(" << (x1 - x2) << ")y+(" << (x2 * y1 - x1 * y2) << ") = 0\n"; int p1, p2, q1, q2; p1 = rand() % (H- L+ 1) + L; p2 = rand() % (H- L + 1) + L; q1 = rand() % (H - L + 1) + L; q2 = rand() % (H - L + 1) + L; cout << "The Equation of the 2nd line is : (" << (q2 - q1) << ")x+(" << (p1 - p2) << ")y+(" << (p2 * q1 - p1 * q2) << ") = 0\n"; int Y1 = (y2 - y1) * p1 + (x1 - x2) * q1 + (x2 * y1 - x1 * y2); //Y1 segment if (Y1 < 0) { int Y2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2); //Y2 segment if (Y2 >= 0) cout << "Lines are intersecting"; else if (Y2 < 0) cout << "Lines are not intersecting"; } else if (Y1 >0) { int Y2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2); if (Y2 <= 0) cout << "Lines are intersecting"; else if (Y2 >0) cout << "Lines are not intersecting"; } else cout << "The point lies on the line"; return 0; }

## Output

The Equation of the 1st line is : (-3)x+(2)y+(1) = 0 The Equation of the 2nd line is : (-5)x+(-5)y+(130) = 0 Lines are intersecting The Equation of the 1st line is : (-1)x+(7)y+(-15) = 0 The Equation of the 2nd line is : (-4)x+(4)y+(-8) = 0 Lines are not intersecting

- Related Questions & Answers
- Program to find how many lines intersect in Python
- C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
- Program to remove sublist to get same number of elements below and above k in C++
- C++ program to find whether only two parallel lines contain all coordinates points or not
- Intersect two lists in C#
- How to use INTERSECT operator in Android sqlite?
- Check if two line segments intersect
- Python - Intersect two dictionaries through keys
- Program to test the equality of two arrays - JavaScript
- How to count the number of elements in an array below/above a given number (JavaScript)
- C++ program to find two points from two lines who are not same
- Java Program to convert String to short primitive
- Java program to print prime numbers below 100
- Python Program to test whether the length of rows are in increasing order
- Python program to check whether two lists are circularly identical

Advertisements