HTML & JS
#include <stdio.h>
/* line intersection */

typedef struct point {
    int x, y;
} Point;

int max(int a, int b) {
    return a > b ? a : b;
}

int min(int a, int b) {
    return a < b ? a : b;
}

int orientation(Point a, Point b, Point c) {
    int orient = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
    return orient == 0 ? orient : (orient > 0 ? 1 : 2);
}

int on_segment(Point a, Point b, Point c) {
    return c.x >= min(a.x, b.x) && c.x <= max(a.x, b.x) && c.y >= min(a.y, b.y) && c.y <= max(a.y, b.y);
}

/**
 * return value 0 => collinear
 * 1, 2 => clockwise and counterclockwise 
 * */
int do_line_segments_intersect(Point p1, Point q1, Point p2, Point q2) {
    int o1, o2, o3, o4;
    o1 = orientation(p1, q1, p2);
    o2 = orientation(p1, q1, q2);
    o3 = orientation(p2, q2, p1);
    o4 = orientation(p2, q2, q1);
    if(o1 != o2 && o3 != o4) return 1;
    // collinear and other point lies on segment, then intersect
    if(o1 == 0 && on_segment(p1, q1, q2)) return 1;
    if(o2 == 0 && on_segment(p1, q1, p2)) return 1;
    if(o3 == 0 && on_segment(p2, q2, q1)) return 1;
    if(o4 == 0 && on_segment(p2, q2, p1)) return 1;
    return 0;
}

int main() {
	Point p1, q1, p2, q2;
	scanf("%d %d", &p1.x, &p1.y);
	scanf("%d %d", &q1.x, &q1.y);
	scanf("%d %d", &p2.x, &p2.y);
	scanf("%d %d", &q2.x, &q2.y);
	do_line_segments_intersect(p1, q1, p2, q2) ? printf("yes") : printf("no");
	return 0;
}

Generated URL:

Output:

yes
Report Bug