package main type Segment struct P1 P2 Point func seg Segment Intersec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main
type Segment struct {
P1, P2 Point
}
func (seg Segment) Intersects(seg2 Segment) bool {
vec1 := seg.P2.Dif(seg.P1)
vec2 := seg2.P2.Dif(seg2.P1)
cross := vec1.Cross(vec2)
if cross == 0 {
return false
}
t1 := seg2.P1.Dif(seg.P1).Cross(vec2) / cross
t2 := seg2.P1.Dif(seg.P1).Cross(vec1) / cross
return 0 <= t1 && t1 <= 1 && 0 <= t2 && t2 <= 1
}