Created
March 10, 2018 23:00
-
-
Save sbrl/188def998fe72fdcfa41a6f2d7f94613 to your computer and use it in GitHub Desktop.
[Triangle.cs] Forked from Rectangle.cs, this is a class that represents, yep, you guessed it, a triangle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SBRL.Utilities | |
{ | |
/// <summary> | |
/// Represents an isosceles / equilateral Triangle in 2D space. | |
/// </summary> | |
/// <remarks> | |
/// Features the ability to return its bounding box as a Rectangle - the struct | |
/// this struct was forked from. | |
/// Although the option UpsideDown property seems odd, there's a perfectly good reason | |
/// it's there, I just can't mention it here now because it's a part of a sekret project :P | |
/// </remarks> | |
/// <version>v0.1</version> | |
/// <license>MPL-2.0</license> | |
/// <changelog> | |
/// v0.1 - 10th March 2018 | |
/// - Created this class by forking Rectangle | |
/// </changelog> | |
public struct Triangle | |
{ | |
/// <summary> | |
/// A Triangle with all it's properties initialised to zero. | |
/// </summary> | |
public static readonly Triangle Zero = new Triangle() { Position = Vector2.Zero, BaseWidth = 0, Height = 0, UpsideDown = false }; | |
#region Core Data | |
/// <summary> | |
/// The position of the Triangle. | |
/// </summary> | |
public Vector2 Position { get; set; } | |
/// <summary> | |
/// The width of this Triangle's base. | |
/// </summary> | |
public float BaseWidth { get; set; } | |
/// <summary> | |
/// The height of this Triangle. | |
/// </summary> | |
public float Height { get; set; } | |
/// <summary> | |
/// Whether this triangle is upside-down or not. | |
/// </summary> | |
public bool UpsideDown { get; set; } | |
#endregion | |
#region Corners | |
/// <summary> | |
/// The bottom-left corner of the triangle. | |
/// </summary> | |
public Vector2 BottomLeft { | |
get { | |
return Position.Subtract(new Vector2(BaseWidth / 2, 0)); | |
} | |
} | |
/// <summary> | |
/// The bottom-right corner or the triangle. | |
/// </summary> | |
public Vector2 BottomRight { | |
get { | |
return Position.Add(new Vector2(BaseWidth / 2, 0)); | |
} | |
} | |
/// <summary> | |
/// The peak of the triangle. | |
/// </summary> | |
public Vector2 Peak { | |
get { | |
return UpsideDown ? Position.Add(new Vector2(0, Height)) : Position.Subtract(new Vector2(0, Height)); | |
} | |
} | |
public Rectangle Area { | |
get { | |
return new Rectangle( | |
BottomLeft.X, | |
UpsideDown ? Position.Y : Position.Y - Height, | |
BaseWidth, | |
Height | |
); | |
} | |
} | |
#endregion | |
public Triangle(Vector2 position, float baseWidth, float height, bool upsideDown = false) | |
{ | |
Position = position; | |
BaseWidth = baseWidth; | |
Height = height; | |
UpsideDown = upsideDown; | |
} | |
public override string ToString() | |
{ | |
return string.Format( | |
"[Triangle @ {0} - {1}x{2}, UpsideDown={3}]", | |
Position, | |
BaseWidth, | |
Height, | |
UpsideDown | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment