'/////////////////////////////////////////////////////////////////////////////////////////////
'// Soluciones de Diseņo Computacional :: Lima :: Peru :: Enero 2008 04.02.2008
'// Rhinoscripting Workshop. http://www.espaciosdigitales.org/lima/1/
'// Script developed by Andrea Jimenez :: Claudia Soria :: Julio Miguel Saquiray
'// Instructores: Kenfield Griffith, Daniel Cardoso, Pablo C. Herrera
'// Coordinacion General: Pablo C. Herrera pablo@espaciosdigitales.org
'/////////////////////////////////////////////////////////////////////////////////////////////

Call Main
dim globalSize

Sub Main 
	'call variables
	'Dim sun
	dim treeTrunk
	dim endPt
	dim Sponge
	
	'dim lineaAng
	
	'Obtener del usuario la posicion del sol
	'Rhino.MessageBox("Select position of the sun")
		'sun = Rhino.GetPoint ( "Select position of the sun")
	
	'Obtener del usuario tronco
	Rhino.MessageBox("Select the trunk")
		treeTrunk = Rhino.GetObject ("Select tree trunk",4)
	
	'Sponge = Rhino.GetObject("Select building", 16)
	
	'Get the EndPoint of the trunk
	endPt = Rhino.curveEndPoint(treetrunk)
	Rhino.AddPoint endPt
	Rhino.Print "X coord: " & endPt(0)
	
	'Draw line between points	
	'lineaAng = Rhino.AddLine (sun,endPt)

globalSize = Rhino.Distance(Rhino.CurveStartPoint(treeTrunk),Rhino.CurveEndPoint(treeTrunk)) 


'Callin next function same name	
Call Div1(treeTrunk)

'Call InterSectObjects(Sponge)

End Sub
'==========================================================
'Function ObtenerAngulo(treeTrunk,lineaAng)

'Obtiene el angulo entre el tronco y la primera rama

'Calling new variables
	'dim AAAng
	'dim treeStart, treeEnd
	'dim branchStart, branchEnd
	'dim AngMax
	
	'Getting tree Start and End point -Array1
	'treeStart = Rhino.CurveStartPoint(treeTrunk)
	'treeEnd = Rhino.CurveEndPoint(treeTrunk)
	
	'Getting tree Start and End point -Array2
	'branchStart = Rhino.CurveStartPoint(lineaAng)
	'branchEnd = Rhino.CurveEndPoint(lineaAng)


	'Getting the angle between tree and branch
	'AAAng = Rhino.Angle2 (Array(treeStart, treeEnd), Array(branchStart, branchEnd))
	
	' (0) = Angulo en grados
	'Rhino.Print AAAng(0)
	' (1) = Reflex Angulo en grados
	'Rhino.Print AAAng(1)
	'Resultado del reflex angle
	'AngMax = Rhino.Messagebox (" El angulo complementario es:") & AAAng(1) + 270

	
'Call Div1(AngMax,treeStart, treeEnd)
	
'End Function
'======================================================================

Function Div1(treeTrunk)

	ReDim point1(2)
	ReDim point2(2)
	Redim point3(2)
	Redim point4(2)
	dim distTrunk
	dim branch1
	dim branch2
	dim branch3
	dim branch4
	dim AngRand
	dim AngRand2
	dim AngRand3
	dim AngRand4
	dim treeStart, treeEnd
	dim branchStart, branchEnd
	dim locZ
	dim botSize, topSize
	dim botSizeb1, topSizeb1
	dim botSizeb2, topSizeb2
	dim botSizeb3, topSizeb3
	dim botSizeb4, topSizeb4
	
	botSize = Rhino.CurveLength(treeTrunk)/10
	topSize = Rhino.CurveLength(treeTrunk)/16
	
	'Getting tree Start and End point -Array1
	treeStart = Rhino.CurveStartPoint(treeTrunk)
	treeEnd = Rhino.CurveEndPoint(treeTrunk)
	
	'pipe the tree
	Rhino.SelectObject treeTrunk
	'Rhino.Command "Pipe " & "0.25 " & "0.25 " & " "
	
	
	Rhino.Command "Pipe " & CStr(botSize) & " " & CStr(topSize) & " " & " "
	Rhino.UnSelectAllObjects()
	
	
	locZ = treeEnd(2)
	
	'Rhino.SelectObject Rhino.AddPoint(treeEnd)
	
	'Rhino.Sleep 1000
	
	'Longitud del tronco
	distTrunk=Rhino.Distance (treeStart, treeEnd)
	
	'-----------------------------------------------------
	
	'Angulo Random branch1
	AngRand= CInt((Rnd()*90))
	Rhino.Print AngRand
	
	'Punto Random Branch1
	Rhino.Print "Cosine: " & cos(AngRand)
	Rhino.Print "X Direction" & distTrunk*cos(AngRand)
	Rhino.Print "Y Direction" & distTrunk*sin(AngRand)
	
	
	point1(0)=treeEnd(0)+ abs((distTrunk/3)*cos(AngRand))
	point1(1)=treeEnd(1)+ abs((distTrunk/3)*(sin(AngRand)))
	point1(2) = Rnd() + locZ + distTrunk/3
	
	'point1(2)=(((Rnd()*(0.4))+0.5)*distTrunk)+distTrunk

	Rhino.Addpoint point1
	
	'Trazo de Branch1
	branch1=Rhino.Addline (treeEnd,point1)
	
	botSizeb1 = Rhino.CurveLength(branch1)/20
	topSizeb1 = Rhino.CurveLength(branch1)/40
		
	'piping branch
	Rhino.SelectObject branch1
	Rhino.Command "Pipe " & CStr(botSizeb1) & " " & CStr(topSizeb1) & " " & " "
	Rhino.UnSelectAllObjects()
	
	'add sphere
	Rhino.AddSphere point1, (botSizeb1*1.2)

	'-----------------------------------------------------
	
	'Angulo Random branch2
	AngRand2= Int((Rnd()*(90))+90)
	Rhino.Print AngRand2
	
	
	Rhino.Print "Cosine: " & cos(AngRand2)
	Rhino.Print "Sine: " & sin(AngRand2)
	Rhino.Print "X Direction" & distTrunk*cos(AngRand2)
	Rhino.Print "Y Direction" & distTrunk*sin(AngRand2)
	
	'Punto Random Branch2
	point2(0)=treeEnd(0)- abs(((distTrunk/2)*(-cos(AngRand2)))) 'X
	point2(1)=treeEnd(1) + abs((distTrunk/2)*(sin(AngRand2))) 'Y
	point2(2) = Rnd() + locZ + distTrunk/2
	
	'point2(2)=(((Rnd()*(0.4))+0.5)*distTrunk)+distTrunk

	
	Rhino.Addpoint point2
	
	'Trazo de Branch2
	branch2=Rhino.Addline (treeEnd,point2)
	
	botSizeb2 = Rhino.CurveLength(branch2)/20
	topSizeb2 = Rhino.CurveLength(branch2)/40
		
	'piping branch2
	Rhino.SelectObject branch2
	Rhino.Command "Pipe " & CStr(botSizeb2) & " " & CStr(topSizeb2) & " " & " "
	Rhino.UnSelectAllObjects()
	
	'add sphere
	Rhino.AddSphere point2, (botSizeb2*1.2)
	
	'-----------------------------------------------------
	
	'Angulo Random branch3
	AngRand3= Int((Rnd()*(90))+180)
	Rhino.Print AngRand3
	'Punto Random Branch3
	point3(0)=treeEnd(0)-(abs((distTrunk/2)*cos(AngRand3)))
	point3(1)=treeEnd(1)-(abs((distTrunk/2)*sin(AngRand3)))
	point3(2) = Rnd() + locZ + distTrunk/2
	'point3(2)=(((Rnd()*(0.4))+0.5)*distTrunk)+distTrunk
	
	Rhino.Addpoint point3
	
	'Trazo de Branch3
	branch3=Rhino.Addline (treeEnd,point3)
	
	botSizeb3 = Rhino.CurveLength(branch3)/20
	topSizeb3 = Rhino.CurveLength(branch3)/40

	'piping branch3
	Rhino.SelectObject branch3
	Rhino.Command "Pipe " & CStr(botSizeb3) & " " & CStr(topSizeb3) & " " & " "
	Rhino.UnSelectAllObjects()
	
	'add sphere
	Rhino.AddSphere point3, (botSizeb3*1.2)
	
	'----------------------------------------------------
	
	'Angulo Random branch4
	AngRand4= Int((Rnd()*(90))+270)
	Rhino.Print AngRand4
	'Punto Random Branch4
	point4(0)=treeEnd(0)+(abs((distTrunk/2)*cos(AngRand4)))
	point4(1)=treeEnd(1)-(abs((distTrunk/2)*sin(AngRand4)))
	point4(2) = Rnd() + locZ + distTrunk/2
	'point4(2)=(((Rnd()*(0.4))+0.5)*distTrunk)+distTrunk
	
	Rhino.Addpoint point4
	
	'Trazo de Branch4
	branch4=Rhino.Addline (treeEnd,point4)
	'Aca va la extrusion
	
	botSizeb4 = Rhino.CurveLength(branch4)/20
	topSizeb4 = Rhino.CurveLength(branch4)/40
	
	
	'piping branch4
	Rhino.SelectObject branch4
	Rhino.Command "Pipe " & CStr(botSizeb4) & " " & CStr(topSizeb4) & " " & " "
	Rhino.UnSelectAllObjects()	

	'add sphere
	Rhino.AddSphere point4, (botSizeb4*1.2)

	
	If (Rhino.Distance(Rhino.CurveEndPoint(branch1) , Rhino.CurveStartPoint(branch1 )) > (globalSize/4)) Then 
		CALL div1(branch1)
		Call div1(branch2)
		CALL div1(branch3)
		Call div1(branch4)
	
	else 
		rhino.Print "end"
	end if

End Function

'=========================================================================




	



	

