U
U
Umid2017-01-18 12:50:09
JavaScript
Umid, 2017-01-18 12:50:09

How to check if triangles intersect?

Моё почтение.
Задача: Сгенерировать 2 рандомных треугольника в системе координат, и проверить, пересекаются ли они.
Решил всё делать через canvas(а как же иначе).
Сгенерировать 2 раза по 3 отрезка (по координатам точек), проверить, что треугольник может получиться из них.
Затем соединить все три отрезка, что бы получились треугольники.
Интересует, как именно можно проверить пересекаются ли они, или нет?
Необходима формула с пояснением, где что.
Благодарю.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
Кирилл, 2017-01-18
@argz

Просто ищите пересекающиеся стороны у треугольников.

R
Rsa97, 2017-01-18
@Rsa97

Options: do not intersect, touch outside, intersect, one touches the other from the inside, one is completely nested in the second.
Intersect - any side of one triangle intersects with some side of the second and the sides are not parallel, for each side of the first triangle we check each side of the second.
One is completely inside the other - all the vertices of triangle A are located on one side of the sides of the second triangle B, bypassed along the contour, that is, from the segments B 1 B 2 , B 2 B 3 , B 3 B 1 .
Touching - the vertex of one of the triangles lies on the side of another triangle or the side of one triangle is parallel to the side of the second and intersects with it.

V
Vitaliy Orlov, 2017-01-18
@orlov0562

forget about the triangles, check whether the lines intersect (of which the triangles consist). If at least one line intersects, then the figures intersect

E
evgeniy_lm, 2017-01-18
@evgeniy_lm

Check the intersection of the sides of the triangle. You need to find the intersection points of the lines passing through the sides and make sure that these points lie on the sides of the triangle

X
x67, 2017-01-18
@x67

1. Get the equations of a straight line for each of the sides of each triangle (y=kx+b and the corresponding equation x=(yb)/k)
justification of the solution: axiom 1. When crossing figures, at least one vertex of one of the figures will lie on the other figure .
Further reading, see Figure
2. Substituting each coordinate of each of the vertices into the resulting equations in turn, the red line shows the geometric interpretation of the X substitution of the blue dot (the supposed vertex of the triangle). In this case, we substitute the X of the blue dot only in the equations of those lines in which this X is also present. Geom. interpretation - brown lines. That is, X of the blue dot is outside the range of the right gray line along X, so we do not check this line.
Когда будем подставлять У, мы не будем проверять нижнюю линию по этой же причине.
Чтобы вершина, находилась внутри треугольника, вторая координата синей точки должна быть внутри интервала, полученного из уравнений (геом. интерпретация - зеленые линии - тот самый интервал, внутри которого должен быть Y синей точки).
Таким образом, если хоть одна из вершин проходит проверку и по Х и по У, она находится внутри или на поверхности другого треугольника и треугольники пересекаются.
Если не совсем понятно, спрашивайте. А вообще лучше бы вам найти готовый велосипед и не парить себе мозги.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question