add Tree data structure & render actual Trees

This commit is contained in:
Colin McMillen 2020-07-16 15:18:30 -04:00
parent d6addc15e0
commit 013e4836fb

View File

@ -44,30 +44,67 @@ namespace SemiColinGames {
public Vector2 p1, p2, p3, p4; public Vector2 p1, p2, p3, p4;
} }
public class TreeNode {
public float Orientation; // relative to parent
public float Length;
public float InWidth;
public float OutWidth;
public readonly List<TreeNode> Children;
public Vector2 Position;
public TreeNode(float orientation, float length, float inWidth, float outWidth) :
this(orientation, length, inWidth, outWidth, new List<TreeNode>()) {}
public TreeNode(float orientation, float length, float inWidth, float outWidth, List<TreeNode> children) {
Orientation = orientation;
Length = length;
InWidth = inWidth;
OutWidth = outWidth;
Children = children;
Position = Vector2.Zero;
}
}
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) { public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
var skeleton = new List<Vector2> { var tree =
new Vector2(0, 0), new TreeNode(0.0f, 100, 10, 6, new List<TreeNode>() {
new Vector2(0, 100), new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
new Vector2(10, 200), new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
new Vector2(30, 300), new TreeNode(-0.3f, 100, 6, 4, new List<TreeNode>() {
new Vector2(60, 400), new TreeNode(-0.1f, 100, 6, 4, new List<TreeNode>() {
new Vector2(100, 500), new TreeNode(-0.3f, 150, 4, 2),
}; new TreeNode(0.2f, 200, 4, 2),
new TreeNode(0.5f, 100, 4, 2)
})}),
new TreeNode(0.5f, 100, 6, 4, new List<TreeNode>() {
new TreeNode(-0.1f, 100, 6, 4, new List<TreeNode>() {
new TreeNode(-0.1f, 150, 4, 2),
new TreeNode(0.2f, 200, 4, 2)
})}),
})})});
graphics.Clear(backgroundColor); graphics.Clear(backgroundColor);
var segments = new List<Trapezoid>(); var segments = new List<Trapezoid>();
for (int i = 0; i < skeleton.Count - 1; i++) { LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
Vector2 low = skeleton[i]; queue.AddLast(tree);
Vector2 high = skeleton[i + 1]; while (queue.Count > 0) {
Trapezoid t; TreeNode parent = queue.First.Value;
int width = 10 - i; queue.RemoveFirst();
t.p1 = new Vector2(low.X - width, low.Y); Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation);
t.p2 = new Vector2(low.X + width, low.Y); Vector2 outPosition = Vector2.Add(parent.Position, outVector);
t.p3 = new Vector2(high.X - width + 4, high.Y); Trapezoid t = new Trapezoid();
t.p4 = new Vector2(high.X + width - 4, high.Y); t.p1 = new Vector2(parent.Position.X - parent.InWidth, parent.Position.Y);
t.p2 = new Vector2(parent.Position.X + parent.InWidth, parent.Position.Y);
t.p3 = new Vector2(outPosition.X - parent.OutWidth, outPosition.Y);
t.p4 = new Vector2(outPosition.X + parent.OutWidth, outPosition.Y);
segments.Add(t); segments.Add(t);
foreach (TreeNode child in parent.Children) {
child.Position = outPosition;
child.Orientation += parent.Orientation;
queue.AddLast(child);
}
} }
Color color = Color.SaddleBrown; Color color = Color.SaddleBrown;