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 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) {
var skeleton = new List<Vector2> {
new Vector2(0, 0),
new Vector2(0, 100),
new Vector2(10, 200),
new Vector2(30, 300),
new Vector2(60, 400),
new Vector2(100, 500),
};
var tree =
new TreeNode(0.0f, 100, 10, 6, new List<TreeNode>() {
new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
new TreeNode(-0.1f, 100, 10, 6, new List<TreeNode>() {
new TreeNode(-0.3f, 100, 6, 4, new List<TreeNode>() {
new TreeNode(-0.1f, 100, 6, 4, new List<TreeNode>() {
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);
var segments = new List<Trapezoid>();
for (int i = 0; i < skeleton.Count - 1; i++) {
Vector2 low = skeleton[i];
Vector2 high = skeleton[i + 1];
Trapezoid t;
int width = 10 - i;
t.p1 = new Vector2(low.X - width, low.Y);
t.p2 = new Vector2(low.X + width, low.Y);
t.p3 = new Vector2(high.X - width + 4, high.Y);
t.p4 = new Vector2(high.X + width - 4, high.Y);
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.AddLast(tree);
while (queue.Count > 0) {
TreeNode parent = queue.First.Value;
queue.RemoveFirst();
Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation);
Vector2 outPosition = Vector2.Add(parent.Position, outVector);
Trapezoid t = new Trapezoid();
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);
foreach (TreeNode child in parent.Children) {
child.Position = outPosition;
child.Orientation += parent.Orientation;
queue.AddLast(child);
}
}
Color color = Color.SaddleBrown;