I am trying to build tree that you look like picture below with threejs and dotnet which has one main branch, can many branches attached to it the children branches can have many children or branches added to it, the branches are placed left or right. My code is working fine until I start adding more branches to the children. Any assistance shifting the X position properly after a new item is added to any children.
My code is as follow.
if (networkObjects != null)
{
var parentObjects = networkObjects.Where(a => a.Children.Count > 0).ToList();
var networkSources = networkObjects.Where(a => a.ItemType == NetworkItemType.Source || a.ItemType == NetworkItemType.Distribution && a.Children.Count > 0);
var xJump = 260;
var sameChildX = new List<SceneObject>();
var id = new List<(string,int)>();
foreach (var parentObject in parentObjects)
{
var parentSceneObject = NetworkSceneObjects.FirstOrDefault(a => a.id == parentObject?.SceneObjectId);
var yParent = parentSceneObject.location.Y + parentSceneObject.snappoints.Min(a => a.Y);
var test = networkObjects.Where(a => parentObject.Children.Contains(a.Uid)).Select(b => b.SceneObjectId).ToList();
var children = NetworkSceneObjects.Where(a => test.Contains(a.id))/*.OrderBy(a => a.Column)*/.ToList();
var childrenObject = networkObjects.Where(a => test.Contains(a.SceneObjectId))/*.OrderBy(a => a.Column)*/.ToList();
var isFirstparent = NetworkSceneObjects.FirstOrDefault();
var Tier = children.FirstOrDefault().Tier;
if (parentObject == null) continue;
var parentindex = parentObjects.FindIndex(a => a.SceneObjectId == parentObject.SceneObjectId);
if (parentindex > 0)
{
yParent += parentSceneObject.snappoints.Min(a => a.Y);
}
var lastChild = children.LastOrDefault();
var counter = 0;
var Column = 0;
var leftChildren = children.Where((a, i) => i % 2 == 0).ToList();
var rightChildren = children.Where((a, i) => i % 2 == 1).ToList();
foreach (var oa in children)
{
Column = oa.Column;
var childindex = children.FindIndex(a => a.id == oa.id);
var isLastChild = oa.id.Equals(lastChild.id);
var isFirst = isFirstparent.id == parentSceneObject.id;
var childrens = /*new List<string>();*/networkObjects.FirstOrDefault(a => a.SceneObjectId == oa.id).Children;
switch (parentObject.Children.Count % 2)
{
case 0: //even
if (!childrenObject.Any(a => a.Children.Count > 0))
{
if (childindex % 2 == 0) //even
{
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + xJump * oa.Column, yParent, oa.location.Z);
}
else //odd
{
if (parentindex > 0)
{
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + xJump * oa.Column, yParent, oa.location.Z);
}
else
oa.location = new System.Numerics.Vector3(xJump * oa.Column, yParent, oa.location.Z);
}
}
else
{
if (childindex % 2 == 0) //even
{
sameChildX = leftChildren.Where(a => a.location.X == oa.location.X && a.id != oa.id && a.Column < Column).ToList();
if (id.Any(a => a.Item1 == oa.id))
{
var leftSceneObject = NetworkSceneObjects.Where((a, i) => i % 2 == 0).LastOrDefault();
var diff = leftSceneObject.location.X - oa.location.X + oa.location.X;
if (diff == leftSceneObject.location.X)
Column = oa.Column = (int)(diff / 260);
}
if (childrens.Count > 0)
if (oa.Column > 0)
Column = oa.Column + (int)Math.Floor((double)childrens.Count / 2);
else
Column = oa.Column - (int)Math.Floor((double)childrens.Count / 2);
for (int j = childindex; j < sameChildX.Count; j++)
{
if (!id.Any(a => a.Item1 == oa.id))
{
id.Add((sameChildX[j].id, childrens.Count));
id.Add((oa.id, childrens.Count));
}
}
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + (xJump * Column), yParent, oa.location.Z);
}
else //odd
{
sameChildX = rightChildren.Where(a => a.location.X == oa.location.X && a.id != oa.id && a.Column > Column).ToList();
if (id.Any(a => a.Item1 == oa.id))
{
var rightSceneObject = NetworkSceneObjects.Where((a, i) => i % 2 == 1).LastOrDefault();
var diff = rightSceneObject.location.X + oa.location.X;
if (diff > rightSceneObject.location.X)
Column = oa.Column = (int)(diff / 260) + 1;
}
if (childrens.Count > 0)
if (oa.Column > 0)
Column = oa.Column + (int)Math.Floor((double)childrens.Count / 2);
else
Column = oa.Column - (int)Math.Floor((double)childrens.Count / 2);
for (int j = childindex; j <= sameChildX.Count; j++)
{
if (!id.Any(a => a.Item1 == oa.id))
{
id.Add((sameChildX[j - 1].id, childrens.Count));
id.Add((oa.id, childrens.Count));
}
}
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + (xJump * Column) , yParent, oa.location.Z);
}
}
break;
case 1: //odd
if (childrenObject.Any(a => a.Children.Count > 0))
{
if (childindex % 2 == 0) //even
{
if (childrens.Count > 0 && !isFirst)
Column = oa.Column - (int)Math.Floor((double)childrens.Count / 2) ;
}
else //odd
{
if (childrens.Count > 0)
Column = oa.Column + (int)Math.Floor((double)childrens.Count / 2);
}
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + (xJump * Column), yParent, oa.location.Z);
}
else
{
if (childindex % 2 == 0) //even
{
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + (xJump * Column), yParent, oa.location.Z);
var leftItems = NetworkSceneObjects.Where((a, i) => i % 2 == 0).ToList();
sameChildX = leftItems.Where(a => a.location.X == oa.location.X).ToList();
foreach (var item in leftItems)
{
}
}
else //odd
{
if (oa.Tier == -2)
oa.location = new System.Numerics.Vector3(xJump * (Column - 1), yParent, oa.location.Z);
else
oa.location = new System.Numerics.Vector3(parentSceneObject.location.X + (xJump * Column), yParent, oa.location.Z);
var rightItems = NetworkSceneObjects.Where((a, i) => i % 2 == 0).ToList();
var sameChildXIDs = rightItems.Where(a => a.location.X == oa.location.X).Select( b => b.ParentID).ToList();
var parentIDs = new List<SceneObject>();
}
}
break;
}
counter++;
}
}
}
Your assistance is highly appreciated.