examples/raycasting/main.go
func intersection(l1, l2 line) (float64, float64, bool) {
	// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line
	denom := (l1.X1-l1.X2)*(l2.Y1-l2.Y2) - (l1.Y1-l1.Y2)*(l2.X1-l2.X2)
	tNum := (l1.X1-l2.X1)*(l2.Y1-l2.Y2) - (l1.Y1-l2.Y1)*(l2.X1-l2.X2)
	uNum := -((l1.X1-l1.X2)*(l1.Y1-l2.Y1) - (l1.Y1-l1.Y2)*(l1.X1-l2.X1))

	if denom == 0 {
		return 0, 0, false
	}

	t := tNum / denom
	if t > 1 || t < 0 {
		return 0, 0, false
	}

	u := uNum / denom
	if u > 1 || u < 0 {
		return 0, 0, false
	}

	x := l1.X1 + t*(l1.X2-l1.X1)
	y := l1.Y1 + t*(l1.Y2-l1.Y1)
	return x, y, true
}
func intersection(l1, l2 line) (float64, float64, bool) {
	// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line
	denom := (l1.X1-l1.X2)*(l2.Y1-l2.Y2) - (l1.Y1-l1.Y2)*(l2.X1-l2.X2)
	tNum := (l1.X1-l2.X1)*(l2.Y1-l2.Y2) - (l1.Y1-l2.Y1)*(l2.X1-l2.X2)
	uNum := -((l1.X1-l1.X2)*(l1.Y1-l2.Y1) - (l1.Y1-l1.Y2)*(l1.X1-l2.X1))

	if denom == 0 {
		return 0, 0, false
	}

	t := tNum / denom
	if t > 1 || t < 0 {
		return 0, 0, false
	}

	u := uNum / denom
	if u > 1 || u < 0 {
		return 0, 0, false
	}

	x := l1.X1 + t*(l1.X2-l1.X1)
	y := l1.Y1 + t*(l1.Y2-l1.Y1)
	return x, y, true
}
func intersection ( l1 , l2 line ) ( float64 , float64 , bool ) { // https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line denom := ( l1 . X1 - l1 . X2 ) * ( l2 . Y1 - l2 . Y2 ) - ( l1 . Y1 - l1 . Y2 ) * ( l2 . X1 - l2 . X2 ) tNum := ( l1 . X1 - l2 . X1 ) * ( l2 . Y1 - l2 . Y2 ) - ( l1 . Y1 - l2 . Y1 ) * ( l2 . X1 - l2 . X2 ) uNum := - ( ( l1 . X1 - l1 . X2 ) * ( l1 . Y1 - l2 . Y1 ) - ( l1 . Y1 - l1 . Y2 ) * ( l1 . X1 - l2 . X1 ) ) if denom == 0 { return 0 , 0 , false } t := tNum / denom if t > 1 || t < 0 { return 0 , 0 , false } u := uNum / denom if u > 1 || u < 0 { return 0 , 0 , false } x := l1 . X1 + t * ( l1 . X2 - l1 . X1 ) y := l1 . Y1 + t * ( l1 . Y2 - l1 . Y1 ) return x , y , true }
// intersection calculates the intersection of given two lines.
intersection calculates the intersection of given two lines .