Esempio n. 1
0
bool check_collision_line(Line line1, Line line2) {
        bool v1 = false, v2 = false; // vertical lines
        if (line1.x1 == line1.x2) {
                v1 = true;
        }
        if (line2.x1 == line2.x2) {
                v2 = true;
        }
        double m1 = 0.0, m2 = 0.0;
        double b1 = 0.0, b2 = 0.0;
        if (!v1) {
                m1 = (line1.y2-line1.y1) / (line1.x2-line1.x1); // Slope
                b1 = line1.y1 - m1*line1.x1; // y-intercept
        }
        if (!v2) {
                m2 = (line2.y2-line2.y1) / (line2.x2-line2.x1);
                b2 = line2.y1 - m2*line2.x1;
        }

        if ((v1 ^ v2) == 0) { // either both or none are vertical
                if (m1 == m2) {
                        if (b1 == b2) { // same line
                                if (v1) {
                                        return check_collision_aligned_line(line1, line2);
                                } else {
					return true;
				}
                        } else {
				return false;
			}
                }
        } else if (v1) {
                if (is_between(line1.x1, line2.x1, line2.x2)) {
			double cy = m2*line1.x1 + b2;
			if (is_between(cy, line1.y1, line1.y2)) {
				return true;
			}
                }
		return false;
        } else if (v2) {
		if (is_between(line2.x1, line1.x1, line1.x2)) {
			double cy = m1*line2.x1 + b1;
			if (is_between(cy, line2.y1, line2.y2)) {
				return true;
			}
                }
		return false;
        }

        double cx = (b2-b1) / (m1-m2); // Possible collision

        if (is_between(cx, line1.x1, line1.x2)) {
                if (is_between(cx, line2.x1, line2.x2)) {
                        return true;
                }
        }

        return false;
}
Esempio n. 2
0
/*
* check_collision_line() - Return whether the given lines intersect
* @line1: one of the lines
* @line2: the other line
*/
bool check_collision_line(const Line& line1, const Line& line2) {
	// Check whether either of the given lines are vertical
	bool v1 = false, v2 = false;
	if (line1.x1 == line1.x2) {
		v1 = true;
	}
	if (line2.x1 == line2.x2) {
		v2 = true;
	}

	// If a given line isn't vertical, then compute its slope and y-intercept
	double m1 = 0.0, m2 = 0.0; // Declare slopes
	double b1 = 0.0, b2 = 0.0; // Declare y-intercepts
	if (!v1) {
		m1 = (line1.y2-line1.y1) / (line1.x2-line1.x1);
		b1 = line1.y1 - m1*line1.x1;
	}
	if (!v2) {
		m2 = (line2.y2-line2.y1) / (line2.x2-line2.x1);
		b2 = line2.y1 - m2*line2.x1;
	}

	if ((v1 ^ v2) == 0) { // If either both or none of the given lines are vertical
		if (m1 == m2) { // If they have the same slope and the same y-intercept then they are the same line
			if (b1 == b2) {
				if (v1) { // If the lines are vertical then check the collision externally
					return check_collision_aligned_line(line1, line2);
				} else {
					return true; // Return true if the lines are the same line
				}
			} else {
				return false; // Return false if they have the same slope but different y-intercept
			}
		}
	} else if (v1) { // If only the first line is vertical then check whether their bounds line up
		if (is_between(line1.x1, line2.x1, line2.x2)) { // Check the vertical line's x-value
			double cy = m2*line1.x1 + b2;
			if (is_between(cy, line1.y1, line1.y2)) { // Check the non-vertical line's y-value
				return true;
			}
		}
		return false;
	} else if (v2) { // If only the first line is vertical then check whether their bounds line up
		if (is_between(line2.x1, line1.x1, line1.x2)) { // Check the vertical line's x-value
			double cy = m1*line2.x1 + b1;
			if (is_between(cy, line2.y1, line2.y2)) { // Check the non-vertical line's y-value
				return true;
			}
		}
		return false;
	}

	// If no lines are vertical then check for intercepts otherwise
	double cx = (b2-b1) / (m1-m2); // Declare a possible collision, see below math
	/*
		y1 = m1*x1 + b1
		y2 = m2*x2 + b2

		for y1 == y2
		m1*x1 + b1 = m2*x2 + b2
		m1*x1 - m2*x2 = b2 - b1

		for cx == x1 == x2
		cx*(m1-m2) = (b2-b1)
		cx = (b2-b1)/(m1-m2)
	*/

	if (is_between(cx, line1.x1, line1.x2)) { // Check whether the x-value is within the bounds of both lines
		if (is_between(cx, line2.x1, line2.x2)) {
			return true; // Return true if there is a collision
		}
	}

	return false; // Return false if there is no collision
}