//	I have included the hyperbolic routine in this script as it works better this way.
//
//	The size of the hyperbolic circle is somewhat irrelevant and is set as a default to
//	a radius of 250.
//
//	Make sure you GROUP together ALL your previous lines and center them on the artboard.
//	Then UNGROUP them and run the script with ONLY the lines selected, NOT your bounding
//	circle. The script will then translate the angles, make new hyperbolic lines.


var HyperbolicDisk = function(Origin, Radius, bool){
			this.Origin 		= 		Origin
			this.Radius 		= 		Radius
			if(bool) this.Disk 	= 		new Path.Circle(this.Origin, this.Radius)
			this.Lines 			= 		[]
}

HyperbolicDisk.prototype.ArcPoint = function(angle1, angle2, bool){
			var point1			= 		this.Origin + new Point(Math.cos(angle1.toRadians()), Math.sin(-angle1.toRadians())) * this.Radius
			var point2			= 		this.Origin + new Point(Math.cos(angle2.toRadians()), Math.sin(-angle2.toRadians())) * this.Radius	
			var theta			= 		((this.Origin - point1).angle + (this.Origin - point2).angle) / 2
			var dTheta			= 		Math.abs((this.Origin - point1).angle - (this.Origin - point2).angle) / 2
			if(dTheta == 90)			return [point1, point2]
			if(dTheta == 0)				return false
			var r				= 		this.Radius * Math.tan(dTheta.toRadians())
			var R				= 		this.Radius / Math.cos(dTheta.toRadians())
			var arcPoint 		= 		this.Origin - new Point({
												length: bool ? R - r : R + r,
												angle: (R - r < 0) ? theta + 180 : theta
										})
			return 						[point1, arcPoint, point2]
}

HyperbolicDisk.prototype.Line = function(angle1, angle2, bool){
			var points 			= 		this.ArcPoint(angle1, angle2, bool)
			if(!points || points.length < 2 || points.length > 3) return
			this.Lines.push(points.length < 3 ? new Path.Line(points[0], points[1]) : new Path.Arc(points[0], points[1], points[2]))
}




//	Hyperbolic Disk is drawn in the middle of the artboard
//	with a radius of 250pt.
var circle = new HyperbolicDisk(document.bounds.center, 250, true)

//	Create new layer in which to put the new hyperbolic lines.
var hyperbolicLayer = new Layer()
hyperbolicLayer.name = 'Hyperbolic Layer'
hyperbolicLayer.appendTop(circle.Disk)

//	Select the straight lines.
var selectedPaths = document.getItems({ type: Path, selected: true })



print('Number of straight lines: ' + selectedPaths.length + '\nCONVERTING LINES...')

while(selectedPaths.length >= 1){

			var item = selectedPaths[0]

			if(item.segments.length > 1){
					//	Get the endpoints of the line
					var point_1 = item.segments[0].point
					var point_2 = item.segments[item.segments.length -1].point
		
					//	Calculate the angles of the endpoints.
					var angle_1 = (point_1 - circle.Origin).angle * -1
					var angle_2 = (point_2 - circle.Origin).angle * -1

					//	Draw a hyperbolic line
					circle.Line(angle_1, angle_2, true)
					hyperbolicLayer.appendTop(circle.Lines[circle.Lines.length -1])
			}

			selectedPaths.shift()
	
			//	Include the line below if the original lines should be deleted after
			//	they have been processed by the script.
//			item.remove()
}
print('Number of hyperbolic lines: ' + (hyperbolicLayer.children.length -1))



