Add Trapezoid.Rotate() & Translate().

Use them to rotate tree segments appropriately.
This commit is contained in:
Colin McMillen 2020-07-21 11:03:02 -04:00
parent 9ac15e6d35
commit 1db526e82d

View File

@ -42,6 +42,20 @@ namespace SemiColinGames {
public struct Trapezoid { public struct Trapezoid {
public Vector2 p1, p2, p3, p4; public Vector2 p1, p2, p3, p4;
public void Rotate(float angle) {
p1 = p1.Rotate(angle);
p2 = p2.Rotate(angle);
p3 = p3.Rotate(angle);
p4 = p4.Rotate(angle);
}
public void Translate(Vector2 position) {
p1 = Vector2.Add(p1, position);
p2 = Vector2.Add(p2, position);
p3 = Vector2.Add(p3, position);
p4 = Vector2.Add(p4, position);
}
} }
public class TreeNode { public class TreeNode {
@ -104,11 +118,25 @@ namespace SemiColinGames {
queue.RemoveFirst(); queue.RemoveFirst();
Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation); Vector2 outVector = new Vector2(0, parent.Length).Rotate(parent.Orientation);
Vector2 outPosition = Vector2.Add(parent.Position, outVector); Vector2 outPosition = Vector2.Add(parent.Position, outVector);
// We want a trapezoid with 4 points. A is the in position, B is the out position.
// The TreeNode.Length is the distance from A to B.
// We come up with the points relative to A being the origin, then rotate the trapezoid
// by its orientation, and translate the result to A's actual position.
//
// 3---B---4 <-- length = outWidth
// / | \
// / | \
// 1------A------2 <-- length = inWidth
Trapezoid t = new Trapezoid(); Trapezoid t = new Trapezoid();
t.p1 = new Vector2(parent.Position.X - parent.InWidth, parent.Position.Y); t.p1 = new Vector2(-parent.InWidth, 0);
t.p2 = new Vector2(parent.Position.X + parent.InWidth, parent.Position.Y); t.p2 = new Vector2(parent.InWidth, 0);
t.p3 = new Vector2(outPosition.X - parent.OutWidth, outPosition.Y); t.p3 = new Vector2(-parent.OutWidth, parent.Length);
t.p4 = new Vector2(outPosition.X + parent.OutWidth, outPosition.Y); t.p4 = new Vector2(parent.OutWidth, parent.Length);
t.Rotate(parent.Orientation);
t.Translate(parent.Position);
segments.Add(t); segments.Add(t);
foreach (TreeNode child in parent.Children) { foreach (TreeNode child in parent.Children) {
child.Position = outPosition; child.Position = outPosition;