Пример #1
0
bool intersection_point(line l1, line l2, point2d *p)
{
	if (same_lineQ(l1, l2)) {
		p->x = p->y = 0.0;
		return true;
	}
	if (parallelQ(l1,l2) == true) {
		return false;
	}

	p->x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);
	if (fabs(l1.b) > EPSILON)	// test for vertical line
		p->y = - (l1.a * p->x + l1.c) / l1.b;
	else
		p->y = - (l2.a * p->x + l2.c) / l2.b;

	return true;
}
Пример #2
0
void intersection_point(line l1, line l2, point p) {
	if(same_lineQ(l1, l2)) {
		printf("Warning: Identical lines, all points intersect.\n");
		p[X] = p[Y] = 0.0;
		return;
	}

	if(parallelQ(l1, l2) == TRUE) {
		printf("Error: Distinct parallel lines do not intersect.\n");
		return;
	}

	p[X] = (l2.b*l1.c - l1.b*l2.c) / (l2.a*l1.b - l1.a*l2.b);

	if(fabs(l1.b) > EPSILON)	/* test for vertical line */
		p[Y] = - (l1.a * (p[X]) + l1.c) / l1.b;
	else
		p[Y] = - (l2.a * (p[X]) + l2.c) / l2.b;
}
Пример #3
0
bool segments_intersect(segment s1, segment s2) {
	line l1, l2;		/* lines containing the input segments */
	point p;		/* intersection point */

	points_to_line(s1.p1, s1.p2, &l1);
	points_to_line(s2.p1, s2.p2, &l2);

	if(same_lineQ(l1, l2)) 	/* overlapping or disjoint segments */
		return( point_in_box(s1.p1, s2.p1, s2.p2) ||
			point_in_box(s1.p2, s2.p1, s2.p2) ||
			point_in_box(s2.p1, s1.p1, s1.p2) ||
			point_in_box(s2.p2, s1.p1, s1.p2) );

	if(parallelQ(l1, l2)) return(FALSE);

	intersection_point(l1, l2, p);

	return( point_in_box(p, s1.p1, s1.p2) && point_in_box(p, s2.p1, s2.p2) );
}
Пример #4
0
bool segments_intersect(segment s1, segment s2)
{
	line l1, l2;

	points_to_line(s1.p1, s1.p2, &l1);
	points_to_line(s2.p1, s2.p2, &l2);

	if (same_lineQ(l1, l2))
		return (point_in_box(s1.p1, s2.p1, s2.p2) ||
				point_in_box(s1.p2, s2.p1, s2.p2) ||
				point_in_box(s2.p1, s1.p1, s1.p2) ||
				point_in_box(s2.p1, s1.p1, s1.p2) );
	
	if (parallelQ(l1, l2)) 
		return false;
	
	point2d p;		// intersection point
	intersection_point(l1, l2, p);
	return (point_in_box(p, s1.p1, s1.p2) && point_in_box(p, s2.p1, s2.p2) );
}
Пример #5
0
bool same_lineQ(line l1, line l2)
{
	return (parallelQ(l1, l2) && (fabs(l1.c - l2.c) <= EPSILON) );
}