This is a C Program to check whether two lines intersect to each other. The above-below primitive can be used to test whether a line intersects a line segment. It does iff one endpoint of the segment is to the left of the line and the other is to the right. Segment intersection is similar but more complicated, and we refer you to implementations described below. The decision whether two segments intersect if they share an endpoint depends upon your application and is representative of the problems of degeneracy.
Here is source code of the C Program to Use Above Below Primitive to Test Whether Two Lines Intersect. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
const int LOW = 0;
const int HIGH = 10;
int main(int argc, char **argv) {
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int x1, x2, y1, y2;
x1 = rand() % (HIGH - LOW + 1) + LOW;
x2 = rand() % (HIGH - LOW + 1) + LOW;
y1 = rand() % (HIGH - LOW + 1) + LOW;
y2 = rand() % (HIGH - LOW + 1) + LOW;
printf("The Equation of the line is: (%d)x+(%d)y+(%d) = 0\n", (y2 - y1),
(x1 - x2), (x2 * y1 - x1 * y2));
int p1, p2, q1, q2;
p1 = rand() % (HIGH - LOW + 1) + LOW;
p2 = rand() % (HIGH - LOW + 1) + LOW;
q1 = rand() % (HIGH - LOW + 1) + LOW;
q2 = rand() % (HIGH - LOW + 1) + LOW;
printf("The Equation of the line is: (%d)p+(%d)q+(%d) = 0\n", (q2 - q1),
(p1 - p2), (p2 * q1 - p1 * q2));
int s1 = (y2 - y1) * p1 + (x1 - x2) * q1 + (x2 * y1 - x1 * y2);
if (s1 < 0) {
int s2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2);
if (s2 >= 0)
printf("The lines intersect");
else if (s2 < 0)
printf("The lines doesn't intersect");
} else if (s1 > 0) {
int s2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2);
if (s2 <= 0)
printf("The lines intersect");
else if (s2 > 0)
printf("The lines doesn't intersect");
} else
printf("The point lies on the line");
return 0;
}
Output:
$ gcc LinearIntersection.c $ ./a.out The Equation of the line is: (-2)x+(-8)y+(66) = 0 The Equation of the line is: (7)p+(7)q+(-77) = 0 The lines intersect
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check C Books
- Check Computer Science Books
- Practice Computer Science MCQs