intersect
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kaigouthro

//@version=5

// @description Find Line Intersecction X/Y
library("intersect")

// @function line intersection coordinates
// @returns [x,y]  with x as int if bool is used
_isect(l1,l2)=>
	x1    = line.get_x1(l1), y1 =line.get_y1(l1), x2 = line.get_x2(l1), y2 =line.get_y2(l1)
	x3    = line.get_x1(l2), y3 =line.get_y1(l2), x4 = line.get_x2(l2), y4 =line.get_y2(l2)
	x     = ((x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)) 
	y     = ((x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
	ylow  = math.min( math.max ( y1, y2 ) , math.max ( y3, y4 ) )
	yhigh = math.max( math.min ( y1, y2 ) , math.min ( y3, y4 ) )
	xlow  = math.min( math.max ( x1, x2 ) , math.max ( x3, x4 ) )
	xhigh = math.max( math.min ( x1, x2 ) , math.min ( x3, x4 ) )
	if (x > xlow or x < xhigh) or (y> ylow or y < yhigh)
		x := float(na)
		y := float(na)
	[x,y]

// @function line intersection coordinates
// @returns [x,y]  with x as int if bool is used
_isect(l1,l2,round) =>
	[x,y] = _isect(l1,l2)
	int x1 = round ? math.round(x) : int(x)
	[int(x1),float(y)]

// @function line intersection coordinates
// @param   l1 (line)  first  line
// @param   l2 (line)  second line
// @param   _round  True to makee an INT for plotting
//			if not used, will not round ( overload loophole)
// @returns [x,y]  with x as int if bool is used
export get(line l1, line l2, bool _round) =>
	_isect(l1,l2,_round)
// @function line intersection coordinates
// @returns [x,y]  with x as int if bool is used
export get(line l1, line l2 ) =>
	_isect(l1,l2,true)


// //////////////////////////////////////////////////////////////////
// /// demo 
// import kaigouthro/hsvColor/6 as h

// // helpers
// li ( ) => last_bar_index
// bi ( ) => bar_index
// lh (i) => li() - i
// h  (i) => bi() - i

// var l1 = line(na)
// var l2 = line(na)
// var n1 = line(na)
// var n2 = line(na)

// if barstate.isconfirmed and bar_index % 10 == 0
// 	_c = color.new(h.hsv(bar_index% 60*6,1,.8,1),0)
// 	l1 := line.new(h(int(math.random(0,100))) , math.random(0, 100) , h(int(math.random(0,100))), math.random(0,100),  color = _c,  style = line.style_arrow_left)
// 	l2 := line.new(h(int(math.random(0,100))) , math.random(0, 100) , h(int(math.random(0,100))), math.random(0,100),  color = _c,  style = line.style_arrow_left)

// 	// find getion and draw new line to indicate rounded for plot
// 	[x,y] = get(l1,l2,true)

// 	label.new(x,y,'Cross', style=label.style_cross, textcolor = color.white, color = color.red)

// 	// truncate the right/second half of line to getion
// 	n1 := line.copy(l1)
// 	n2 := line.copy(l2)
// 	line.set_x2(l1,x)
// 	line.set_x2(l2,x)
// 	line.set_y2(l1,y)
// 	line.set_y2(l2,y)
// 	if na(x)
// 		line.delete(l1)
// 		line.delete(l2)
// 		line.delete(n1)
// 		line.delete(n2)
// 	line.set_color(n1,#a0a0a0a0)
// 	line.set_color(n2,#a0a0a0a0)
// 	line.set_style(n1,line.style_dotted)
// 	line.set_style(n2,line.style_dotted)

