Logo by KRAFTWERK - Contribute your own Logo!

END OF AN ERA, FRACTALFORUMS.COM IS CONTINUED ON FRACTALFORUMS.ORG

it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Follow us on Twitter
 
*
Welcome, Guest. Please login or register. March 02, 2021, 08:07:58 AM


Login with username, password and session length


The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!


Pages: [1]   Go Down
  Print  
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Best way to make these tree iterations? Arrays? location marikers? nested etc  (Read 1439 times)
0 Members and 1 Guest are viewing this topic.
Mrz00m
Fractal Lover
**
Posts: 204


« on: October 01, 2012, 03:39:05 PM »

Hi there, i am learning fractals in javascript and Unity3d.

I am stuck on this task:
rotate a root object 3 times,
add 10 cubes in a line to the root object,
mark the 10th cube as a new root object,
rotate the new root object 3 times,
add 30 cubes to teh new root object same as before.

I am very stuck on this task:
how to i mark the 3 new cubes at the end of each branch as a sub root, seeing as they wont have existed before the rotate function was made in the first place?
Should i make an array with "newroot [1,2,3]"
where do i place the nested elements of the function?

here is the code for the 3 branches.:
Code:

#pragma strict

@script RequireComponent(AudioSource)

var cubePrefab : GameObject ;//set cube for model
var startObj : GameObject ;
startObj = gameObject ;//set root object of function
private var rot : Quaternion; //set empty rotation var
rot.eulerAngles = Vector3(0, 0, 0);


function Start()//running this triggers all sub functions
{
Rotate (startObj,rot);
}

function Rotate (ting:GameObject,trot:Quaternion)//rotate the root object 3 times
{   
for (var x = 0; x < 3; x++)
{
trot.eulerAngles = Vector3(0,120,0);
ting.transform.rotation = ting.transform.rotation * trot;//take base point and add TROT degrees every loop
Lines( ting, 1.0  , 10.0);//add 10 cubes in a line to the root object
}
}

function Lines( iter:GameObject, ratio:float , depth:float)//add line of cubes

{

var cube = Instantiate(cubePrefab,Vector3(0, 0, 0),rot);
var turnAxis= Vector3(0, 4, 3);
rot.eulerAngles = turnAxis;

   
cube.transform.position = iter.transform.TransformPoint(Vector3(1, 0, 0));//places new cube on top of last one's relative position
cube.transform.localScale = Vector3(2,2,2);
cube.transform.localScale *= ratio;
//cube.transform.localScale *=.90;//sets size of old cube to new one
cube.transform.rotation = iter.transform.rotation * rot;
//print (iter.transform.rotation);
if(depth > 0)
{
Lines( cube, ratio *.98 , depth -1 );
}    
}

where do i place the rotate function?


* 3tree.jpg (37.74 KB, 924x580 - viewed 393 times.)
Logged
cKleinhuis
Administrator
Fractal Senior
*******
Posts: 7044


formerly known as 'Trifox'


WWW
« Reply #1 on: October 01, 2012, 04:54:54 PM »

think easy, you have one basic rule which would be simple recursive ?

func(obj,angle,depth){
obj.rotate as you like via angle
newelements[]=create new objects -> e.g. depth*10 -> return as array list
call func(newelements[lastelement],angle,depth+1) -> recursive call
}

if you want a 3 branch to be created by this, you could call the function three times in each step, for example:
call func(newelements[lastelement],angle,depth+1)
call func(newelements[lastelement-1],angle,depth+1)
call func(newelements[lastelement-2],angle,depth+1)

« Last Edit: October 01, 2012, 11:16:20 PM by cKleinhuis » Logged

---

divide and conquer - iterate and rule - chaos is No random!
Mrz00m
Fractal Lover
**
Posts: 204


« Reply #2 on: October 01, 2012, 10:56:48 PM »

Thanks so much!

I was convinced that recursion meant that i could program a loop within a loop and magically use the right words so it would call back on itself in the intended form. actually i have to at least do the over routine and prior to figuring out how it would work in an ad infinitum loop.

in 2 minutes of reading your answer it was rolling.


* recurse ok.jpg (80.75 KB, 1156x769 - viewed 396 times.)
Logged
cKleinhuis
Administrator
Fractal Senior
*******
Posts: 7044


formerly known as 'Trifox'


WWW
« Reply #3 on: October 01, 2012, 11:03:21 PM »

great that it worked, glad to help wink

this is a fantastic starter into branching visualisation, you can even make the branches count random, that whould produce wild structures have fun with it!
Logged

---

divide and conquer - iterate and rule - chaos is No random!
Pages: [1]   Go Down
  Print  
 
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.128 seconds with 27 queries. (Pretty URLs adds 0.008s, 2q)