231 lines
5.5 KiB
C#
231 lines
5.5 KiB
C#
using System;
|
|
|
|
namespace Quik
|
|
{
|
|
/// <summary>
|
|
/// A 2 dimensional Vector.
|
|
/// </summary>
|
|
public struct QuikVec2
|
|
{
|
|
public float X;
|
|
public float Y;
|
|
|
|
public float Length() => MathF.Sqrt(X * X + Y * Y);
|
|
|
|
public QuikVec2 Normalize() => this * (1.0f / this.Length());
|
|
|
|
public float Atan2() => MathF.Atan2(Y, X);
|
|
public static QuikVec2 operator +(QuikVec2 a, QuikVec2 b)
|
|
{
|
|
return new QuikVec2()
|
|
{
|
|
X = a.X + b.X,
|
|
Y = a.Y + b.Y
|
|
};
|
|
}
|
|
|
|
public static QuikVec2 operator -(QuikVec2 a)
|
|
{
|
|
return new QuikVec2()
|
|
{
|
|
X = -a.X,
|
|
Y = -a.Y
|
|
};
|
|
}
|
|
|
|
public static QuikVec2 operator -(QuikVec2 a, QuikVec2 b)
|
|
{
|
|
return new QuikVec2()
|
|
{
|
|
X = a.X - b.X,
|
|
Y = a.Y - b.Y
|
|
};
|
|
}
|
|
|
|
public static QuikVec2 operator *(float a, QuikVec2 b)
|
|
{
|
|
return new QuikVec2()
|
|
{
|
|
X = a * b.X,
|
|
Y = a * b.Y
|
|
};
|
|
}
|
|
|
|
public static QuikVec2 operator *(QuikVec2 a, float b) => b * a;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A RGBA color value.
|
|
/// </summary>
|
|
public struct QuikColor
|
|
{
|
|
/// <summary>
|
|
/// Red channel.
|
|
/// </summary>
|
|
public byte R;
|
|
/// <summary>
|
|
/// Green channel.
|
|
/// </summary>
|
|
public byte G;
|
|
/// <summary>
|
|
/// Blue channel.
|
|
/// </summary>
|
|
public byte B;
|
|
/// <summary>
|
|
/// Alpha channel.
|
|
/// </summary>
|
|
public byte A;
|
|
|
|
public QuikColor(byte r, byte g, byte b, byte a)
|
|
{
|
|
R = r;
|
|
G = g;
|
|
B = b;
|
|
A = a;
|
|
}
|
|
|
|
public QuikColor(byte r, byte g, byte b) : this(r, g, b, 1) { }
|
|
|
|
public QuikColor(int hexCode)
|
|
{
|
|
R = (byte)((hexCode >> 24) & 0xFF);
|
|
G = (byte)((hexCode >> 16) & 0xFF);
|
|
B = (byte)((hexCode >> 8 ) & 0xFF);
|
|
A = (byte)((hexCode >> 0 ) & 0xFF);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A bezier curve segment.
|
|
/// </summary>
|
|
public struct QuikBezier
|
|
{
|
|
/// <summary>
|
|
/// Segment start point.
|
|
/// </summary>
|
|
public QuikVec2 Start;
|
|
|
|
/// <summary>
|
|
/// Start point control point.
|
|
/// </summary>
|
|
public QuikVec2 ControlA;
|
|
|
|
/// <summary>
|
|
/// End point control point.
|
|
/// </summary>
|
|
public QuikVec2 ControlB;
|
|
|
|
/// <summary>
|
|
/// Segment end point.
|
|
/// </summary>
|
|
public QuikVec2 End;
|
|
|
|
/// <summary>
|
|
/// Get a point in the curve segment.
|
|
/// </summary>
|
|
/// <param name="t">Control parameter (between 0 and 1)</param>
|
|
/// <returns>The point on the curve.</returns>
|
|
public QuikVec2 GetBezierPoint(float t)
|
|
{
|
|
return
|
|
(1 - t) * (1 - t) * (1 - t) * Start +
|
|
(1 - t) * (1 - t) * t * ControlA +
|
|
(1 - t) * t * t * ControlB +
|
|
t * t * t * End;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the tangent on the curve.
|
|
/// </summary>
|
|
/// <param name="t">Control parameter (between 0 and 1)</param>
|
|
/// <returns>The tangent curve.</returns>
|
|
public QuikVec2 GetBezierTangent(float t)
|
|
{
|
|
return
|
|
3 * (1 - t) * (1 - t) * (ControlA - Start) +
|
|
6 * (1 - t) * (ControlB - ControlA) +
|
|
3 * t * t * (End - ControlB);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A line segment.
|
|
/// </summary>
|
|
public struct QuikLine
|
|
{
|
|
/// <summary>
|
|
/// Start point.
|
|
/// </summary>
|
|
public QuikVec2 Start;
|
|
|
|
/// <summary>
|
|
/// End point.
|
|
/// </summary>
|
|
public QuikVec2 End;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A rectangle.
|
|
/// </summary>
|
|
public struct QuikRectangle
|
|
{
|
|
/// <summary>
|
|
/// Rectangle minimum point.
|
|
/// </summary>
|
|
public QuikVec2 Min;
|
|
|
|
/// <summary>
|
|
/// Rectangle maximum point.
|
|
/// </summary>
|
|
public QuikVec2 Max;
|
|
|
|
public QuikRectangle(float l, float t, float r, float b)
|
|
{
|
|
Min = new QuikVec2() {X = r, Y = b};
|
|
Max = new QuikVec2() {X = l, Y = t};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// An ellipse.
|
|
/// </summary>
|
|
/// <remarks>It is undefined to have an ellipse with non-orthogonal axes.</remarks>
|
|
public struct QuikEllipse
|
|
{
|
|
/// <summary>
|
|
/// Ellipse center point.
|
|
/// </summary>
|
|
public QuikVec2 Center;
|
|
|
|
/// <summary>
|
|
/// First ellipse axis.
|
|
/// </summary>
|
|
public QuikVec2 AxisA;
|
|
|
|
/// <summary>
|
|
/// Second ellipse axis.
|
|
/// </summary>
|
|
public QuikVec2 AxisB;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A triangle.
|
|
/// </summary>
|
|
public struct QuikTriangle
|
|
{
|
|
/// <summary>
|
|
/// First vertex.
|
|
/// </summary>
|
|
public QuikVec2 A;
|
|
|
|
/// <summary>
|
|
/// Second vertex.
|
|
/// </summary>
|
|
public QuikVec2 B;
|
|
|
|
/// <summary>
|
|
/// Third vertex.
|
|
/// </summary>
|
|
public QuikVec2 C;
|
|
}
|
|
} |