StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit 846f38d20589459cc09a38f93fbc5f54feb0195d
parent ef94ee47124a1cbe0bd136620ba4e7ce236a504e
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Sat, 29 Jun 2019 18:40:13 +0100

Added matricies, updated vector struct, and started adding more interactivity.

Diffstat:
MStarSim/StarSim/Program.cs | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
MStarSim/StarSimLib/Constants.cs | 15++++++++++-----
MStarSim/StarSimLib/Graphics/Drawer.cs | 42++++++++++++++++++++++++++++++++++++++++--
AStarSim/StarSimLib/Matrix4x4.cs | 181+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MStarSim/StarSimLib/Physics/Body.cs | 26+++++++++++++-------------
MStarSim/StarSimLib/Physics/BodyGenerator.cs | 46++++++++++++++++++++++++++++++++++------------
MStarSim/StarSimLib/Physics/BodyUpdater.cs | 2+-
MStarSim/StarSimLib/Physics/OrbitGenerator.cs | 10+++++-----
MStarSim/StarSimLib/StarSimLib.xml | 406++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
DStarSim/StarSimLib/Vector3d.cs | 376-------------------------------------------------------------------------------
AStarSim/StarSimLib/Vector4d.cs | 462+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 1086 insertions(+), 549 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using SFML.Graphics; +using SFML.System; using SFML.Window; using StarSimLib; using StarSimLib.Graphics; @@ -53,7 +54,11 @@ namespace StarSim window = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulator", Styles.Default, new ContextSettings()); window.SetVisible(false); - (bodies, bodyShapeMap) = BodyGenerator.GenerateBodies(Constants.BodyCount, true); + View windowView = new View(new Vector2f(window.Size.X / 2f, window.Size.Y / 2f), new Vector2f(window.Size.X, window.Size.Y)); + window.SetView(windowView); + + bodies = BodyGenerator.GenerateBodies(Constants.BodyCount, true); + bodyShapeMap = BodyGenerator.GenerateShapes(bodies); #if DEBUG bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; #else @@ -64,6 +69,11 @@ namespace StarSim } /// <summary> + /// Whether the simulation is paused at any given time. + /// </summary> + private static bool IsSimulationPaused { get; set; } + + /// <summary> /// Handles key presses. /// </summary> /// <param name="sender">The <see cref="Window"/> that sent the event.</param> @@ -73,11 +83,28 @@ namespace StarSim switch (eventArgs.Code) { case Keyboard.Key.Space: - bodyPositionUpdater(bodies, Constants.TimeStep); + // toggle the paused state + IsSimulationPaused = !IsSimulationPaused; + break; + + case Keyboard.Key.W: + case Keyboard.Key.Up: + // rotate the view north + break; + + case Keyboard.Key.A: + case Keyboard.Key.Left: + // rotate the view west break; - case Keyboard.Key.G: - (bodies, bodyShapeMap) = BodyGenerator.GenerateBodies(Constants.BodyCount, true); + case Keyboard.Key.S: + case Keyboard.Key.Down: + // rotate the view south + break; + + case Keyboard.Key.D: + case Keyboard.Key.Right: + // rotate the view east break; default: @@ -92,6 +119,7 @@ namespace StarSim /// <param name="eventArgs">The <see cref="MouseMoveEventArgs"/> associated with the key press.</param> private static void HandleMouseMoved(object sender, MouseMoveEventArgs eventArgs) { + Console.WriteLine($"Mouse moved: {eventArgs.X} {eventArgs.Y}"); } /// <summary> @@ -101,6 +129,7 @@ namespace StarSim /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the key press.</param> private static void HandleMousePressed(object sender, MouseButtonEventArgs eventArgs) { + Console.WriteLine($"Mouse pressed: {eventArgs.X} {eventArgs.Y}, {eventArgs.Button}"); } /// <summary> @@ -110,6 +139,24 @@ namespace StarSim /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the key press.</param> private static void HandleMouseReleased(object sender, MouseButtonEventArgs eventArgs) { + Console.WriteLine($"Mouse released: {eventArgs.X} {eventArgs.Y}, {eventArgs.Button}"); + } + + /// <summary> + /// Handles scrolling of the mouse wheel. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="MouseWheelScrollEventArgs"/> associated with the key press.</param> + private static void HandleMouseScrolled(object sender, MouseWheelScrollEventArgs eventArgs) + { + if (eventArgs.Delta > 0) + { + bodyDrawer.Scale(1.1f); + } + else if (eventArgs.Delta < 0) + { + bodyDrawer.Scale(0.9f); + } } /// <summary> @@ -118,8 +165,9 @@ namespace StarSim /// <param name="args">Any command line arguments passed to the program.</param> private static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Console.WriteLine("Hello, World!"); Console.WriteLine("Press 'enter' to continue..."); + Console.ReadLine(); // we reveal the window so that the user may interact with our program @@ -133,7 +181,8 @@ namespace StarSim window.KeyPressed += HandleKeyPressed; window.MouseButtonPressed += HandleMousePressed; window.MouseButtonReleased += HandleMouseReleased; - window.MouseMoved += HandleMouseMoved; + //window.MouseMoved += HandleMouseMoved; + window.MouseWheelScrolled += HandleMouseScrolled; bodyDrawer.DrawBodies(); @@ -142,13 +191,17 @@ namespace StarSim window.Clear(); window.DispatchEvents(); - bodyPositionUpdater(bodies, Constants.TimeStep); + if (!IsSimulationPaused) + { + bodyPositionUpdater(bodies, Constants.TimeStep); + } + bodyDrawer.DrawBodies(); window.Display(); } - Console.WriteLine("Goodbye World!"); + Console.WriteLine("Goodbye, World!"); Console.WriteLine("Press 'enter' to quit..."); Console.ReadLine(); } diff --git a/StarSim/StarSimLib/Constants.cs b/StarSim/StarSimLib/Constants.cs @@ -19,7 +19,7 @@ namespace StarSimLib public const double CentralBodyMass = SolarMass * 1e6; /// <summary> - /// The frame rate limit for the simulation. + /// The frame rate limit for the program. /// </summary> public const uint FrameRate = 60; @@ -29,9 +29,14 @@ namespace StarSimLib public const double G = 6.673e-11f; /// <summary> - /// The number of seconds that each frame represents. + /// The number of seconds that each simulation tick represents. /// </summary> - public const double SecondsPerFrame = 1e8f; + public const double SecondsPerTick = 1e8f; + + /// <summary> + /// The tick rate limit for the simulation. + /// </summary> + public const uint SimulationRate = 60; /// <summary> /// Softens the force between <see cref="Body"/>s to avoid infinities. @@ -49,9 +54,9 @@ namespace StarSimLib public const double SolarMass = 1.98892e30f; /// <summary> - /// The default time step for the simulation. + /// The time step for the simulation. /// </summary> - public const double TimeStep = SecondsPerFrame * FrameRate; + public const double TimeStep = SecondsPerTick * (SimulationRate / (float)FrameRate) * SimulationRate; /// <summary> /// Factor by which to multiply the position of <see cref="Body"/>s to scale them to the screen for displaying. diff --git a/StarSim/StarSimLib/Graphics/Drawer.cs b/StarSim/StarSimLib/Graphics/Drawer.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using SFML.Graphics; using SFML.System; using StarSimLib.Physics; @@ -6,11 +7,22 @@ using StarSimLib.Physics; namespace StarSimLib.Graphics { /// <summary> - /// Represents a drawer capable of rendering bodies to a screen. + /// Represents a drawer capable of rendering bodies to a <see cref="RenderTarget"/>. /// </summary> public class Drawer { /// <summary> + /// The aspect ratio of the render target. Allows for normalisation of the <see cref="RenderTarget"/> space + /// into a normal plane ([-1, -1] = top left, [1, 1] = bottom right). + /// </summary> + private readonly float aspectRatio; + + /// <summary> + /// The field of view for the drawer. + /// </summary> + private readonly float fieldOfView; + + /// <summary> /// Holds all the <see cref="Body"/> instances that should be simulated. /// </summary> private readonly Body[] managedBodies; @@ -44,6 +56,7 @@ namespace StarSimLib.Graphics public Drawer(RenderTarget target, ref Body[] bodies, ref Dictionary<Body, CircleShape> bodyShapeMap) { renderTarget = target; + aspectRatio = renderTarget.Size.Y / (float)renderTarget.Size.X; originOffset = target.Size / 2; managedBodies = bodies; @@ -51,6 +64,11 @@ namespace StarSimLib.Graphics managedBodyShapeMap = bodyShapeMap; } + private double InverseScaleFactor + { + get { return 1 / Math.Tan(fieldOfView / 2); } + } + /// <summary> /// Draws each <see cref="Body"/> instance that is managed by this drawer to the <see cref="RenderTarget"/> specified /// in the constructor. @@ -68,8 +86,28 @@ namespace StarSimLib.Graphics (float)(body.Position.Y * Constants.UniverseScalingFactor + originOffset.Y) ); + /* + shape.Position = new Vector2f( + (float)(body.Position.X - shape.Radius / 2 + originOffset.X), + (float)(body.Position.Y - shape.Radius / 2 + originOffset.Y) + ); + */ + shape.Draw(renderTarget, RenderStates.Default); } } + + /// <summary> + /// Scales this instances view by the given amount. + /// </summary> + /// <param name="scaleMultiplier">The multiplier by which to scale the viewport of this instances render target.</param> + public void Scale(float scaleMultiplier) + { + View renderView = renderTarget.GetView(); + + renderView.Zoom(scaleMultiplier); + + renderTarget.SetView(renderView); + } } } \ No newline at end of file diff --git a/StarSim/StarSimLib/Matrix4x4.cs b/StarSim/StarSimLib/Matrix4x4.cs @@ -0,0 +1,180 @@ +using System; + +namespace StarSimLib +{ + /// <summary> + /// A 4-by-4 matrix of <see cref="double"/> values. + /// </summary> + public struct Matrix4x4 + { + /// <summary> + /// The internal matrix representation. + /// </summary> + private readonly double[][] matrix; + + /// <summary> + /// The default <see cref="MatrixValueGeneratorDelegate"/> to use when constructing a <see cref="Matrix4x4"/> + /// from a given 2D <see cref="double"/> array. Simply copies over the values from the source matrix to the + /// output matrix. Should the source matrix contain null fields, a 0 will be placed at the relevant field in + /// the target matrix. + /// </summary> + public static readonly MatrixValueGeneratorDelegate CopyMatrixValuesGeneratorDelegate = + (sourceMatrix, i, j) => sourceMatrix?[i]?[j] ?? 0; + + /// <summary> + /// Initialises a new instance of the <see cref="Matrix4x4"/> struct. + /// </summary> + /// <param name="defaultValue">The value to which to initialise all fields in the matrix.</param> + public Matrix4x4(double defaultValue) : this(null, (sourceMatrix, i, j) => defaultValue) + { + } + + /// <summary> + /// Initialises a new instance of the <see cref="Matrix4x4"/> struct. + /// </summary> + /// <param name="sourceMatrix">The source matrix from which to copy over values.</param> + public Matrix4x4(double[][] sourceMatrix) : this(sourceMatrix, CopyMatrixValuesGeneratorDelegate) + { + } + + /// <summary> + /// Initialises a new instance of the <see cref="Matrix4x4"/> struct. + /// </summary> + /// <param name="sourceMatrix">The source matrix from which to copy over values.</param> + public Matrix4x4(Matrix4x4 sourceMatrix) : this(sourceMatrix, CopyMatrixValuesGeneratorDelegate) + { + } + + /// <summary> + /// Initialises a new instance of the <see cref="Matrix4x4"/> struct. + /// </summary> + /// <param name="sourceMatrix">The source matrix.</param> + /// <param name="valueGenerator"> + /// The <see cref="MatrixValueGeneratorDelegate"/> to use to generate new matrix values. Can use the source matrix + /// or can ignore it. + /// </param> + public Matrix4x4(double[][] sourceMatrix, MatrixValueGeneratorDelegate valueGenerator) + { + // construct the columns + matrix = new double[4][]; + + try + { + // for each column in the target matrix + for (int i = 0; i < 4; i++) + { + // construct the rows + matrix[i] = new double[4]; + + // for each row in the target matrix + for (int j = 0; j < 4; j++) + { + // set the value at [i, j] according to the given generator function + this[i, j] = valueGenerator(sourceMatrix, i, j); + } + } + } + catch (IndexOutOfRangeException ex) + { + throw new ArgumentException("Source array is not a minimum of 4x4 in size.", nameof(sourceMatrix), ex); + } + } + + /// <summary> + /// Initialises a new instance of the <see cref="Matrix4x4"/> struct. + /// </summary> + /// <param name="sourceMatrix">The source matrix.</param> + /// <param name="valueGenerator"> + /// The <see cref="MatrixValueGeneratorDelegate"/> to use to generate new matrix values. Can use the source matrix + /// or can ignore it. + /// </param> + public Matrix4x4(Matrix4x4 sourceMatrix, MatrixValueGeneratorDelegate valueGenerator) : + this((double[][])sourceMatrix, valueGenerator) + { + } + + /// <summary> + /// Generates a value to place in the target matrix (at [i, j]), using a source matrix and i and j coordinates. + /// </summary> + /// <param name="sourceMatrix">The source matrix to use.</param> + /// <param name="i">The column index.</param> + /// <param name="j">The row index</param> + /// <returns>The value to place at the (i, j) position in the new matrix.</returns> + public delegate double MatrixValueGeneratorDelegate(double[][] sourceMatrix, int i, int j); + + /// <summary> + /// Indexes the matrix as a 2D array. + /// </summary> + /// <param name="i">The column index.</param> + /// <param name="j">The row index.</param> + /// <returns>The value at the specified field.</returns> + public double this[int i, int j] + { + get { return matrix[i][j]; } + set { matrix[i][j] = value; } + } + + /// <summary> + /// Implicitly converts a <see cref="Matrix4x4"/> to a 2D <see cref="double"/> array. + /// </summary> + /// <param name="matrix4x4">The matrix to convert.</param> + public static implicit operator double[][](Matrix4x4 matrix4x4) + { + return matrix4x4.matrix; + } + + /// <summary> + /// Implements the multiplication operator for a <see cref="Matrix4x4"/> and a <see cref="Vector4d"/>. This is + /// the 'dot' operation for a vector and a matrix. + /// </summary> + /// <param name="matrix4x4">The matrix.</param> + /// <param name="vector">The 3D vector.</param> + /// <returns>The dot product of the <see cref="Matrix4x4"/> and the <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(Matrix4x4 matrix4x4, Vector4d vector) + { + return new Vector4d + { + X = vector.X * matrix4x4[0, 0] + + vector.Y * matrix4x4[1, 0] + + vector.Z * matrix4x4[2, 0] + + vector.W * matrix4x4[3, 0], + Y = vector.X * matrix4x4[0, 1] + + vector.Y * matrix4x4[1, 1] + + vector.Z * matrix4x4[2, 1] + + vector.W * matrix4x4[3, 1], + Z = vector.X * matrix4x4[0, 2] + + vector.Y * matrix4x4[1, 2] + + vector.Z * matrix4x4[2, 2] + + vector.W * matrix4x4[3, 2], + W = vector.X * matrix4x4[0, 3] + + vector.Y * matrix4x4[1, 3] + + vector.Z * matrix4x4[2, 3] + + vector.W * matrix4x4[3, 3] + }; + } + + /// <summary> + /// Implements the multiplication operator for a <see cref="Matrix4x4"/> and a <see cref="Vector4d"/>. This is + /// the 'dot' operation for a vector and a matrix. + /// </summary> + /// <param name="vector">The 3D vector.</param> + /// <param name="matrix4x4">The matrix.</param> + /// <returns>The dot product of the <see cref="Matrix4x4"/> and the <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(Vector4d vector, Matrix4x4 matrix4x4) => matrix4x4 * vector; + + #region Overrides of ValueType + + /// <inheritdoc /> + public override string ToString() + { + return $"[" + + $"[{this[0, 0]}, {this[1, 0]}, {this[2, 0]}, {this[3, 0]}]," + + $"[{this[0, 1]}, {this[1, 1]}, {this[2, 1]}, {this[3, 1]}]," + + $"[{this[0, 2]}, {this[1, 2]}, {this[2, 2]}, {this[3, 2]}]," + + $"[{this[0, 3]}, {this[1, 3]}, {this[2, 3]}, {this[3, 3]}]" + + $"]"; + } + + #endregion Overrides of ValueType + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/Physics/Body.cs b/StarSim/StarSimLib/Physics/Body.cs @@ -16,7 +16,7 @@ namespace StarSimLib.Physics /// <summary> /// The backing field for the <see cref="Force"/> property. /// </summary> - private Vector3d force; + private Vector4d force; /// <summary> /// Backing field for the <see cref="Mass"/> property. @@ -26,12 +26,12 @@ namespace StarSimLib.Physics /// <summary> /// Backing field for the <see cref="Position"/> property. /// </summary> - private Vector3d position; + private Vector4d position; /// <summary> /// Backing field for the <see cref="Velocity"/> property. /// </summary> - private Vector3d velocity; + private Vector4d velocity; /// <summary> /// The generation that this body belongs to. @@ -51,7 +51,7 @@ namespace StarSimLib.Physics /// <param name="mass">The starting mass of the <see cref="Body"/>.</param> /// <param name="generation">The generation that this <see cref="Body"/> belongs to.</param> /// <param name="id">The unique id for this body.</param> - public Body(Vector3d position, Vector3d velocity, double mass, uint generation = 1, uint id = 1) + public Body(Vector4d position, Vector4d velocity, double mass, uint generation = 1, uint id = 1) { Generation = generation; Id = id; @@ -60,13 +60,13 @@ namespace StarSimLib.Physics this.velocity = velocity; this.mass = mass; - force = new Vector3d(); + force = new Vector4d(); } /// <summary> /// The current force on the <see cref="Body"/> in 3D space. /// </summary> - public Vector3d Force + public Vector4d Force { get { return force; } } @@ -82,7 +82,7 @@ namespace StarSimLib.Physics /// <summary> /// The current position of the <see cref="Body"/> in 3D space. /// </summary> - public Vector3d Position + public Vector4d Position { get { return position; } } @@ -90,7 +90,7 @@ namespace StarSimLib.Physics /// <summary> /// The current velocity of the <see cref="Body"/> in 3D space. /// </summary> - public Vector3d Velocity + public Vector4d Velocity { get { return velocity; } } @@ -101,7 +101,7 @@ namespace StarSimLib.Physics /// <param name="a">The first <see cref="Body"/> instance.</param> /// <param name="b">The second <see cref="Body"/> instance.</param> /// <returns></returns> - public static Vector3d GetForceBetween(Body a, Body b) + public static Vector4d GetForceBetween(Body a, Body b) { // Inlines the Body.DistanceTo(Body) as the position deltas need to be cached for later, // as well as to gain a small performance increase @@ -120,7 +120,7 @@ namespace StarSimLib.Physics // with a softening factor, we get the attraction force vector between the 2 bodies double force = numerator / denominator; - return new Vector3d(force * dx / distance, force * dy / distance, force * dz / distance); + return new Vector4d(force * dx / distance, force * dy / distance, force * dz / distance); } /// <summary> @@ -150,13 +150,13 @@ namespace StarSimLib.Physics /// <param name="body">The other <see cref="Body"/> instance to which to calculate the distance.</param> /// <param name="displacement">The vector showing the displacement of the current body from the given one.</param> /// <returns>The distance to the other <see cref="Body"/> as a <see cref="float"/>.</returns> - public double DistanceTo(Body body, out Vector3d displacement) + public double DistanceTo(Body body, out Vector4d displacement) { double dpx = body.Position.X - Position.X, dpy = body.Position.Y - Position.Y, dpz = body.Position.Z - Position.Z; - displacement = new Vector3d(dpx, dpy, dpz); + displacement = new Vector4d(dpx, dpy, dpz); return Math.Sqrt(dpx * dpx + dpy * dpy + dpz * dpz); } @@ -186,7 +186,7 @@ namespace StarSimLib.Physics /// </summary> /// <param name="forceVector">The sum vector of all forces due to other <see cref="Body"/>s.</param> /// <param name="deltaTime">The time step.</param> - public void Update(Vector3d forceVector, double deltaTime) + public void Update(Vector4d forceVector, double deltaTime) { velocity += deltaTime * forceVector / mass; position += deltaTime * velocity; diff --git a/StarSim/StarSimLib/Physics/BodyGenerator.cs b/StarSim/StarSimLib/Physics/BodyGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using SFML.Graphics; +using SFML.System; namespace StarSimLib.Physics { @@ -20,41 +21,62 @@ namespace StarSimLib.Physics public static uint CurrentGeneration { get; private set; } /// <summary> - /// Generates an array of <see cref="Body"/> instances, and generates <see cref="CircleShape"/> instances for - /// each generated body. Returns the generated array and dictionary as a tuple. + /// Generates an array of <see cref="Body"/> instances, possibly including a central attractor. /// </summary> /// <param name="bodyCount">The number of <see cref="Body"/> instances to generate.</param> /// <param name="centralAttractor">Whether to have a central massive attractor.</param> - /// <returns>The generated <see cref="Body"/> array, and <see cref="Body"/> to <see cref="CircleShape"/> map.</returns> - public static (Body[] generatedBodies, Dictionary<Body, CircleShape> bodyCircleShapeMap) GenerateBodies(int bodyCount = 2, bool centralAttractor = false) + /// <returns>The generated <see cref="Body"/> array.</returns> + public static Body[] GenerateBodies(int bodyCount = 2, bool centralAttractor = false) { CurrentGeneration++; uint id = 0; Body[] generatedBodies = new Body[bodyCount]; - Dictionary<Body, CircleShape> bodyCircleShapeMap = new Dictionary<Body, CircleShape>(); for (int i = 0; i < generatedBodies.Length; i++) { float mass = (float)(Rng.NextDouble() * Constants.SolarMass); - Vector3d position = OrbitGenerator.RandomPosition(); - Vector3d velocity = OrbitGenerator.RandomOrbit(position); + Vector4d position = OrbitGenerator.RandomPosition(); + Vector4d velocity = OrbitGenerator.RandomOrbit(position); generatedBodies[i] = new Body(position, velocity, mass, CurrentGeneration, id); - bodyCircleShapeMap.Add(generatedBodies[i], new CircleShape(1) { FillColor = Color.White }); id++; } if (centralAttractor && bodyCount >= 2) { - bodyCircleShapeMap.Remove(generatedBodies[0]); - generatedBodies[0] = new Body(new Vector3d(), new Vector3d(), Constants.CentralBodyMass, CurrentGeneration, 0); - bodyCircleShapeMap.Add(generatedBodies[0], new CircleShape(4) { FillColor = Color.Red }); + generatedBodies[0] = new Body(new Vector4d(), new Vector4d(), Constants.CentralBodyMass, CurrentGeneration, 0); + } + + return generatedBodies; + } + + /// <summary> + /// Generates a <see cref="Dictionary{TKey,TValue}"/> mapping each given <see cref="Body"/> to a + /// <see cref="CircleShape"/> that represents the body instance in 2D space. + /// </summary> + /// <param name="bodies">The <see cref="Body"/> instances for which to generate the shapes.</param> + /// <returns> + /// A <see cref="Dictionary{TKey,TValue}"/> mapping the given <see cref="Body"/> instances to their <see cref="CircleShape"/> instances. + /// </returns> + public static Dictionary<Body, CircleShape> GenerateShapes(IEnumerable<Body> bodies) + { + Dictionary<Body, CircleShape> bodyCircleShapeMap = new Dictionary<Body, CircleShape>(); + + foreach (Body body in bodies) + { + CircleShape shape = body.Mass >= Constants.CentralBodyMass + ? new CircleShape(4) { FillColor = Color.Red } + : new CircleShape(1) { FillColor = Color.White }; + + shape.Origin = new Vector2f(shape.Radius, shape.Radius); + + bodyCircleShapeMap.Add(body, shape); } - return (generatedBodies, bodyCircleShapeMap); + return bodyCircleShapeMap; } } } \ No newline at end of file diff --git a/StarSim/StarSimLib/Physics/BodyUpdater.cs b/StarSim/StarSimLib/Physics/BodyUpdater.cs @@ -23,7 +23,7 @@ namespace StarSimLib.Physics public static void UpdateBodiesBruteForce(IEnumerable<Body> bodies, double deltaTime) { IEnumerable<Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray(); - Vector3d forceVector = new Vector3d(); + Vector4d forceVector = new Vector4d(); foreach (Body body in bodyEnumerable) { diff --git a/StarSim/StarSimLib/Physics/OrbitGenerator.cs b/StarSim/StarSimLib/Physics/OrbitGenerator.cs @@ -17,9 +17,9 @@ namespace StarSimLib.Physics /// </summary> /// <param name="positionVector">The position vector for the body for which to generate the orbit.</param> /// <returns>The magnitude of the velocity of the orbit.</returns> - public static Vector3d RandomOrbit(Vector3d positionVector) + public static Vector4d RandomOrbit(Vector4d positionVector) { - Vector3d position = positionVector; + Vector4d position = positionVector; // F = G m1 - m2 / distance double distanceToCentralBody = position.Magnitude(); @@ -42,21 +42,21 @@ namespace StarSimLib.Physics vz = 0; // Randomly orient the orbit - return !(Rng.NextDouble() <= 0.5f) ? new Vector3d(vx, vy, vz) : new Vector3d(-vx, -vy, -vz); + return !(Rng.NextDouble() <= 0.5f) ? new Vector4d(vx, vy, vz) : new Vector4d(-vx, -vy, -vz); } /// <summary> /// Returns a random position within the universe sphere (see <see cref="Constants.UniverseSize"/>). /// </summary> /// <returns>A 3D position vector.</returns> - public static Vector3d RandomPosition() + public static Vector4d RandomPosition() { double RandomPos() { return Constants.UniverseSize * Math.Exp(-1.8) * (.5 - Rng.NextDouble()); } - return new Vector3d(RandomPos(), RandomPos(), RandomPos()); + return new Vector4d(RandomPos(), RandomPos(), RandomPos()); } } } \ No newline at end of file diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -21,7 +21,7 @@ </member> <member name="F:StarSimLib.Constants.FrameRate"> <summary> - The frame rate limit for the simulation. + The frame rate limit for the program. </summary> </member> <member name="F:StarSimLib.Constants.G"> @@ -29,9 +29,14 @@ The gravitational constant (m^3 kg^-1 s^-2). </summary> </member> - <member name="F:StarSimLib.Constants.SecondsPerFrame"> + <member name="F:StarSimLib.Constants.SecondsPerTick"> <summary> - The number of seconds that each frame represents. + The number of seconds that each simulation tick represents. + </summary> + </member> + <member name="F:StarSimLib.Constants.SimulationRate"> + <summary> + The tick rate limit for the simulation. </summary> </member> <member name="F:StarSimLib.Constants.SofteningFactor"> @@ -51,7 +56,7 @@ </member> <member name="F:StarSimLib.Constants.TimeStep"> <summary> - The default time step for the simulation. + The time step for the simulation. </summary> </member> <member name="F:StarSimLib.Constants.UniverseScalingFactor"> @@ -105,7 +110,18 @@ </member> <member name="T:StarSimLib.Graphics.Drawer"> <summary> - Represents a drawer capable of rendering bodies to a screen. + Represents a drawer capable of rendering bodies to a <see cref="T:SFML.Graphics.RenderTarget"/>. + </summary> + </member> + <member name="F:StarSimLib.Graphics.Drawer.aspectRatio"> + <summary> + The aspect ratio of the render target. Allows for normalisation of the <see cref="T:SFML.Graphics.RenderTarget"/> space + into a normal plane ([-1, -1] = top left, [1, 1] = bottom right). + </summary> + </member> + <member name="F:StarSimLib.Graphics.Drawer.fieldOfView"> + <summary> + The field of view for the drawer. </summary> </member> <member name="F:StarSimLib.Graphics.Drawer.managedBodies"> @@ -147,6 +163,112 @@ in the constructor. </summary> </member> + <member name="M:StarSimLib.Graphics.Drawer.Scale(System.Single)"> + <summary> + Scales this instances view by the given amount. + </summary> + <param name="scaleMultiplier">The multiplier by which to scale the viewport of this instances render target.</param> + </member> + <member name="T:StarSimLib.Matrix4x4"> + <summary> + A 4-by-4 matrix of <see cref="T:System.Double"/> values. + </summary> + </member> + <member name="F:StarSimLib.Matrix4x4.matrix"> + <summary> + The internal matrix representation. + </summary> + </member> + <member name="F:StarSimLib.Matrix4x4.CopyMatrixValuesGeneratorDelegate"> + <summary> + The default <see cref="T:StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate"/> to use when constructing a <see cref="T:StarSimLib.Matrix4x4"/> + from a given 2D <see cref="T:System.Double"/> array. Simply copies over the values from the source matrix to the + output matrix. Should the source matrix contain null fields, a 0 will be placed at the relevant field in + the target matrix. + </summary> + </member> + <member name="M:StarSimLib.Matrix4x4.#ctor(System.Double)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Matrix4x4"/> struct. + </summary> + <param name="defaultValue">The value to which to initialise all fields in the matrix.</param> + </member> + <member name="M:StarSimLib.Matrix4x4.#ctor(System.Double[][])"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Matrix4x4"/> struct. + </summary> + <param name="sourceMatrix">The source matrix from which to copy over values.</param> + </member> + <member name="M:StarSimLib.Matrix4x4.#ctor(StarSimLib.Matrix4x4)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Matrix4x4"/> struct. + </summary> + <param name="sourceMatrix">The source matrix from which to copy over values.</param> + </member> + <member name="M:StarSimLib.Matrix4x4.#ctor(System.Double[][],StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Matrix4x4"/> struct. + </summary> + <param name="sourceMatrix">The source matrix.</param> + <param name="valueGenerator"> + The <see cref="T:StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate"/> to use to generate new matrix values. Can use the source matrix + or can ignore it. + </param> + </member> + <member name="M:StarSimLib.Matrix4x4.#ctor(StarSimLib.Matrix4x4,StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.Matrix4x4"/> struct. + </summary> + <param name="sourceMatrix">The source matrix.</param> + <param name="valueGenerator"> + The <see cref="T:StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate"/> to use to generate new matrix values. Can use the source matrix + or can ignore it. + </param> + </member> + <member name="T:StarSimLib.Matrix4x4.MatrixValueGeneratorDelegate"> + <summary> + Generates a value to place in the target matrix (at [i, j]), using a source matrix and i and j coordinates. + </summary> + <param name="sourceMatrix">The source matrix to use.</param> + <param name="i">The column index.</param> + <param name="j">The row index</param> + <returns>The value to place at the (i, j) position in the new matrix.</returns> + </member> + <member name="P:StarSimLib.Matrix4x4.Item(System.Int32,System.Int32)"> + <summary> + Indexes the matrix as a 2D array. + </summary> + <param name="i">The column index.</param> + <param name="j">The row index.</param> + <returns>The value at the specified field.</returns> + </member> + <member name="M:StarSimLib.Matrix4x4.op_Implicit(StarSimLib.Matrix4x4)~System.Double[][]"> + <summary> + Implicitly converts a <see cref="T:StarSimLib.Matrix4x4"/> to a 2D <see cref="T:System.Double"/> array. + </summary> + <param name="matrix4x4">The matrix to convert.</param> + </member> + <member name="M:StarSimLib.Matrix4x4.op_Multiply(StarSimLib.Matrix4x4,StarSimLib.Vector4d)"> + <summary> + Implements the multiplication operator for a <see cref="T:StarSimLib.Matrix4x4"/> and a <see cref="T:StarSimLib.Vector4d"/>. This is + the 'dot' operation for a vector and a matrix. + </summary> + <param name="matrix4x4">The matrix.</param> + <param name="vector">The 3D vector.</param> + <returns>The dot product of the <see cref="T:StarSimLib.Matrix4x4"/> and the <see cref="T:StarSimLib.Vector4d"/>.</returns> + </member> + <member name="M:StarSimLib.Matrix4x4.op_Multiply(StarSimLib.Vector4d,StarSimLib.Matrix4x4)"> + <summary> + Implements the multiplication operator for a <see cref="T:StarSimLib.Matrix4x4"/> and a <see cref="T:StarSimLib.Vector4d"/>. This is + the 'dot' operation for a vector and a matrix. + </summary> + <param name="vector">The 3D vector.</param> + <param name="matrix4x4">The matrix.</param> + <returns>The dot product of the <see cref="T:StarSimLib.Matrix4x4"/> and the <see cref="T:StarSimLib.Vector4d"/>.</returns> + </member> + <member name="M:StarSimLib.Matrix4x4.ToString"> + <inheritdoc /> + </member> <member name="T:StarSimLib.Physics.Body"> <summary> Represents a stellar body. @@ -187,7 +309,7 @@ The unique id for this body. </summary> </member> - <member name="M:StarSimLib.Physics.Body.#ctor(StarSimLib.Vector3d,StarSimLib.Vector3d,System.Double,System.UInt32,System.UInt32)"> + <member name="M:StarSimLib.Physics.Body.#ctor(StarSimLib.Vector4d,StarSimLib.Vector4d,System.Double,System.UInt32,System.UInt32)"> <summary> Initialises a new instance of the <see cref="T:StarSimLib.Physics.Body"/> class. </summary> @@ -238,7 +360,7 @@ </summary> <param name="otherBody">The other instance with which to collide.</param> </member> - <member name="M:StarSimLib.Physics.Body.DistanceTo(StarSimLib.Physics.Body,StarSimLib.Vector3d@)"> + <member name="M:StarSimLib.Physics.Body.DistanceTo(StarSimLib.Physics.Body,StarSimLib.Vector4d@)"> <summary> Finds and returns the distance between the current <see cref="T:StarSimLib.Physics.Body"/> instance and the given <see cref="T:StarSimLib.Physics.Body"/>. In other words, it finds the magnitude of the translation vector between the two <see cref="T:StarSimLib.Physics.Body"/> instances. @@ -258,7 +380,7 @@ </summary> <param name="deltaTime">The time step.</param> </member> - <member name="M:StarSimLib.Physics.Body.Update(StarSimLib.Vector3d,System.Double)"> + <member name="M:StarSimLib.Physics.Body.Update(StarSimLib.Vector4d,System.Double)"> <summary> Updates the <see cref="P:StarSimLib.Physics.Body.Position"/> of the current body using the given force vector and time step. </summary> @@ -285,12 +407,21 @@ </member> <member name="M:StarSimLib.Physics.BodyGenerator.GenerateBodies(System.Int32,System.Boolean)"> <summary> - Generates an array of <see cref="T:StarSimLib.Physics.Body"/> instances, and generates <see cref="T:SFML.Graphics.CircleShape"/> instances for - each generated body. Returns the generated array and dictionary as a tuple. + Generates an array of <see cref="T:StarSimLib.Physics.Body"/> instances, possibly including a central attractor. </summary> <param name="bodyCount">The number of <see cref="T:StarSimLib.Physics.Body"/> instances to generate.</param> <param name="centralAttractor">Whether to have a central massive attractor.</param> - <returns>The generated <see cref="T:StarSimLib.Physics.Body"/> array, and <see cref="T:StarSimLib.Physics.Body"/> to <see cref="T:SFML.Graphics.CircleShape"/> map.</returns> + <returns>The generated <see cref="T:StarSimLib.Physics.Body"/> array.</returns> + </member> + <member name="M:StarSimLib.Physics.BodyGenerator.GenerateShapes(System.Collections.Generic.IEnumerable{StarSimLib.Physics.Body})"> + <summary> + Generates a <see cref="T:System.Collections.Generic.Dictionary`2"/> mapping each given <see cref="T:StarSimLib.Physics.Body"/> to a + <see cref="T:SFML.Graphics.CircleShape"/> that represents the body instance in 2D space. + </summary> + <param name="bodies">The <see cref="T:StarSimLib.Physics.Body"/> instances for which to generate the shapes.</param> + <returns> + A <see cref="T:System.Collections.Generic.Dictionary`2"/> mapping the given <see cref="T:StarSimLib.Physics.Body"/> instances to their <see cref="T:SFML.Graphics.CircleShape"/> instances. + </returns> </member> <member name="T:StarSimLib.Physics.UpdateDelegate"> <summary> @@ -321,7 +452,7 @@ Random number generator. </summary> </member> - <member name="M:StarSimLib.Physics.OrbitGenerator.RandomOrbit(StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Physics.OrbitGenerator.RandomOrbit(StarSimLib.Vector4d)"> <summary> Returns a randomised orbit around a central, heavy mass. </summary> @@ -334,249 +465,270 @@ </summary> <returns>A 3D position vector.</returns> </member> - <member name="T:StarSimLib.Vector3d"> + <member name="T:StarSimLib.Vector4d"> <summary> - A 3D vector with <see cref="T:System.Double"/> components. + A 4D vector with <see cref="T:System.Double"/> components. </summary> </member> - <member name="F:StarSimLib.Vector3d.X"> + <member name="F:StarSimLib.Vector4d.W"> + <summary> + The w component of the vector. + </summary> + </member> + <member name="F:StarSimLib.Vector4d.X"> <summary> The x component of the vector. </summary> </member> - <member name="F:StarSimLib.Vector3d.Y"> + <member name="F:StarSimLib.Vector4d.Y"> <summary> The y component of the vector. </summary> </member> - <member name="F:StarSimLib.Vector3d.Z"> + <member name="F:StarSimLib.Vector4d.Z"> <summary> The z component of the vector. </summary> </member> - <member name="M:StarSimLib.Vector3d.#ctor(System.Double,System.Double)"> + <member name="M:StarSimLib.Vector4d.#ctor(System.Double,System.Double)"> <summary> - Initialises a new instance of the <see cref="T:StarSimLib.Vector3d"/> struct. Sets Z to 0. + Initialises a new instance of the <see cref="T:StarSimLib.Vector4d"/> struct. Sets Z and W to 0. </summary> <param name="x">The x component.</param> <param name="y">The y component.</param> </member> - <member name="M:StarSimLib.Vector3d.#ctor(System.Double,System.Double,System.Double)"> + <member name="M:StarSimLib.Vector4d.#ctor(System.Double,System.Double,System.Double)"> <summary> - Initialises a new instance of the <see cref="T:StarSimLib.Vector3d"/> struct. + Initialises a new instance of the <see cref="T:StarSimLib.Vector4d"/> struct. Sets W to 0. </summary> <param name="x">The x component.</param> <param name="y">The y component.</param> <param name="z">The z component.</param> </member> - <member name="M:StarSimLib.Vector3d.op_Subtraction(StarSimLib.Vector3d,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.#ctor(System.Double,System.Double,System.Double,System.Double)"> <summary> - Implements the subtraction operator for 2 <see cref="T:StarSimLib.Vector3d"/>s. + Initialises a new instance of the <see cref="T:StarSimLib.Vector4d"/> struct. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The other <see cref="T:StarSimLib.Vector3d"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="x">The x component.</param> + <param name="y">The y component.</param> + <param name="z">The z component.</param> + <param name="w">The w component.</param> + </member> + <member name="M:StarSimLib.Vector4d.op_Subtraction(StarSimLib.Vector4d,StarSimLib.Vector4d)"> + <summary> + Implements the subtraction operator for 2 <see cref="T:StarSimLib.Vector4d"/>s. + </summary> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The other <see cref="T:StarSimLib.Vector4d"/>.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Subtraction(StarSimLib.Vector3d,System.ValueTuple{System.Double,System.Double,System.Double})"> + <member name="M:StarSimLib.Vector4d.op_Subtraction(StarSimLib.Vector4d,System.ValueTuple{System.Double,System.Double,System.Double,System.Double})"> <summary> - Implements the subtraction operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the subtraction operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Subtraction(System.ValueTuple{System.Double,System.Double,System.Double},StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Subtraction(System.ValueTuple{System.Double,System.Double,System.Double,System.Double},StarSimLib.Vector4d)"> <summary> - Implements the subtraction operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the subtraction operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Subtraction(StarSimLib.Vector3d,System.Double)"> + <member name="M:StarSimLib.Vector4d.op_Subtraction(StarSimLib.Vector4d,System.Double)"> <summary> - Implements the subtraction operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the subtraction operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Subtraction(System.Double,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Subtraction(System.Double,StarSimLib.Vector4d)"> <summary> - Implements the subtraction operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the subtraction operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Multiply(StarSimLib.Vector3d,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Multiply(StarSimLib.Vector4d,StarSimLib.Vector4d)"> <summary> - Implements the multiplication operator for 2 <see cref="T:StarSimLib.Vector3d"/>s. + Implements the multiplication operator for 2 <see cref="T:StarSimLib.Vector4d"/>s. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The other <see cref="T:StarSimLib.Vector3d"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The other <see cref="T:StarSimLib.Vector4d"/>.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Multiply(StarSimLib.Vector3d,System.ValueTuple{System.Double,System.Double,System.Double})"> + <member name="M:StarSimLib.Vector4d.op_Multiply(StarSimLib.Vector4d,System.ValueTuple{System.Double,System.Double,System.Double,System.Double})"> <summary> - Implements the multiplication operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the multiplication operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Multiply(System.ValueTuple{System.Double,System.Double,System.Double},StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Multiply(System.ValueTuple{System.Double,System.Double,System.Double,System.Double},StarSimLib.Vector4d)"> <summary> - Implements the multiplication operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the multiplication operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Multiply(StarSimLib.Vector3d,System.Double)"> + <member name="M:StarSimLib.Vector4d.op_Multiply(StarSimLib.Vector4d,System.Double)"> <summary> - Implements the multiplication operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the multiplication operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Multiply(System.Double,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Multiply(System.Double,StarSimLib.Vector4d)"> <summary> - Implements the multiplication operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the multiplication operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Division(StarSimLib.Vector3d,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Division(StarSimLib.Vector4d,StarSimLib.Vector4d)"> <summary> - Implements the division operator for 2 <see cref="T:StarSimLib.Vector3d"/>s. + Implements the division operator for 2 <see cref="T:StarSimLib.Vector4d"/>s. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The other <see cref="T:StarSimLib.Vector3d"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The other <see cref="T:StarSimLib.Vector4d"/>.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Division(StarSimLib.Vector3d,System.ValueTuple{System.Double,System.Double,System.Double})"> + <member name="M:StarSimLib.Vector4d.op_Division(StarSimLib.Vector4d,System.ValueTuple{System.Double,System.Double,System.Double,System.Double})"> <summary> - Implements the division operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the division operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Division(System.ValueTuple{System.Double,System.Double,System.Double},StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Division(System.ValueTuple{System.Double,System.Double,System.Double,System.Double},StarSimLib.Vector4d)"> <summary> - Implements the division operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the division operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Division(StarSimLib.Vector3d,System.Double)"> + <member name="M:StarSimLib.Vector4d.op_Division(StarSimLib.Vector4d,System.Double)"> <summary> - Implements the division operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the division operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Division(System.Double,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Division(System.Double,StarSimLib.Vector4d)"> <summary> - Implements the division operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the division operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Addition(StarSimLib.Vector3d,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Addition(StarSimLib.Vector4d,StarSimLib.Vector4d)"> <summary> - Implements the addition operator for 2 <see cref="T:StarSimLib.Vector3d"/>s. + Implements the addition operator for 2 <see cref="T:StarSimLib.Vector4d"/>s. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The other <see cref="T:StarSimLib.Vector3d"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The other <see cref="T:StarSimLib.Vector4d"/>.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Addition(StarSimLib.Vector3d,System.ValueTuple{System.Double,System.Double,System.Double})"> + <member name="M:StarSimLib.Vector4d.op_Addition(StarSimLib.Vector4d,System.ValueTuple{System.Double,System.Double,System.Double,System.Double})"> <summary> - Implements the addition operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the addition operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Addition(System.ValueTuple{System.Double,System.Double,System.Double},StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Addition(System.ValueTuple{System.Double,System.Double,System.Double,System.Double},StarSimLib.Vector4d)"> <summary> - Implements the addition operator for a <see cref="T:StarSimLib.Vector3d"/> and a (X, Y, Z) tuple. + Implements the addition operator for a <see cref="T:StarSimLib.Vector4d"/> and a (X, Y, Z, W) tuple. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> - <param name="vector2">The 3D tuple.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> + <param name="vector2">The 4D tuple.</param> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Addition(StarSimLib.Vector3d,System.Double)"> + <member name="M:StarSimLib.Vector4d.op_Addition(StarSimLib.Vector4d,System.Double)"> <summary> - Implements the addition operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the addition operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.op_Addition(System.Double,StarSimLib.Vector3d)"> + <member name="M:StarSimLib.Vector4d.op_Addition(System.Double,StarSimLib.Vector4d)"> <summary> - Implements the addition operator for a <see cref="T:StarSimLib.Vector3d"/> and a scalar <see cref="T:System.Double"/> value. + Implements the addition operator for a <see cref="T:StarSimLib.Vector4d"/> and a scalar <see cref="T:System.Double"/> value. </summary> - <param name="vector">The original <see cref="T:StarSimLib.Vector3d"/>.</param> + <param name="vector">The original <see cref="T:StarSimLib.Vector4d"/>.</param> <param name="scalar">The scalar <see cref="T:System.Double"/>.</param> - <returns>The new <see cref="T:StarSimLib.Vector3d"/>.</returns> + <returns>The new <see cref="T:StarSimLib.Vector4d"/>.</returns> + </member> + <member name="P:StarSimLib.Vector4d.Item(System.Int32)"> + <summary> + Indexes the vector as a 1D array. + </summary> + <param name="i">The column index.</param> + <returns>The value at the specified field.</returns> </member> - <member name="M:StarSimLib.Vector3d.Abs"> + <member name="M:StarSimLib.Vector4d.Abs"> <summary> - Returns the absolute value of this <see cref="T:StarSimLib.Vector3d"/>. + Returns the absolute value of this <see cref="T:StarSimLib.Vector4d"/>. </summary> - <returns>The absolute value (magnitude) of this <see cref="T:StarSimLib.Vector3d"/>, as a <see cref="T:System.Double"/>.</returns> + <returns>The absolute value (magnitude) of this <see cref="T:StarSimLib.Vector4d"/>, as a <see cref="T:System.Double"/>.</returns> </member> - <member name="M:StarSimLib.Vector3d.Magnitude"> + <member name="M:StarSimLib.Vector4d.Magnitude"> <summary> - Returns the magnitude of this <see cref="T:StarSimLib.Vector3d"/>. + Returns the magnitude of this <see cref="T:StarSimLib.Vector4d"/>. </summary> - <returns>The magnitude (absolute value) of this <see cref="T:StarSimLib.Vector3d"/>, as a <see cref="T:System.Double"/>.</returns> + <returns>The magnitude (absolute value) of this <see cref="T:StarSimLib.Vector4d"/>, as a <see cref="T:System.Double"/>.</returns> </member> - <member name="T:StarSimLib.Vector3d.UnitVectors"> + <member name="T:StarSimLib.Vector4d.UnitVectors"> <summary> Holds unit vectors. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Backwards"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Backwards"> <summary> The negative unit vector for the Z axis. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Down"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Down"> <summary> The negative unit vector for the Y axis. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Forwards"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Forwards"> <summary> The positive unit vector for the Z axis. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Left"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Left"> <summary> The positive unit vector for the X axis. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Right"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Right"> <summary> The negative unit vector for the X axis. </summary> </member> - <member name="F:StarSimLib.Vector3d.UnitVectors.Up"> + <member name="F:StarSimLib.Vector4d.UnitVectors.Up"> <summary> The positive unit vector for the Y axis. </summary> </member> - <member name="M:StarSimLib.Vector3d.ToString"> + <member name="M:StarSimLib.Vector4d.ToString"> <inheritdoc /> </member> </members> diff --git a/StarSim/StarSimLib/Vector3d.cs b/StarSim/StarSimLib/Vector3d.cs @@ -1,375 +0,0 @@ -using System; -using System.Data.Common; -using System.Diagnostics.Contracts; -using System.Transactions; - -namespace StarSimLib -{ - /// <summary> - /// A 3D vector with <see cref="double"/> components. - /// </summary> - public struct Vector3d - { - /// <summary> - /// The x component of the vector. - /// </summary> - public double X; - - /// <summary> - /// The y component of the vector. - /// </summary> - public double Y; - - /// <summary> - /// The z component of the vector. - /// </summary> - public double Z; - - /// <summary> - /// Initialises a new instance of the <see cref="Vector3d"/> struct. Sets Z to 0. - /// </summary> - /// <param name="x">The x component.</param> - /// <param name="y">The y component.</param> - public Vector3d(double x, double y) - { - X = x; - Y = y; - Z = 0; - } - - /// <summary> - /// Initialises a new instance of the <see cref="Vector3d"/> struct. - /// </summary> - /// <param name="x">The x component.</param> - /// <param name="y">The y component.</param> - /// <param name="z">The z component.</param> - public Vector3d(double x, double y, double z) - { - X = x; - Y = y; - Z = z; - } - - #region Operator Overloads - - /// <summary> - /// Implements the subtraction operator for 2 <see cref="Vector3d"/>s. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The other <see cref="Vector3d"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator -(Vector3d vector, Vector3d vector2) - { - vector.X -= vector2.X; - vector.Y -= vector2.Y; - vector.Z -= vector2.Z; - - return vector; - } - - /// <summary> - /// Implements the subtraction operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator -(Vector3d vector, (double X, double Y, double Z) vector2) - { - (double x, double y, double z) = vector2; - - vector.X -= x; - vector.Y -= y; - vector.Z -= z; - - return vector; - } - - /// <summary> - /// Implements the subtraction operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator -((double X, double Y, double Z) vector2, Vector3d vector) => vector - vector2; - - /// <summary> - /// Implements the subtraction operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator -(Vector3d vector, double scalar) - { - vector.X -= scalar; - vector.Y -= scalar; - vector.Z -= scalar; - - return vector; - } - - /// <summary> - /// Implements the subtraction operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator -(double scalar, Vector3d vector) => vector - scalar; - - /// <summary> - /// Implements the multiplication operator for 2 <see cref="Vector3d"/>s. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The other <see cref="Vector3d"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator *(Vector3d vector, Vector3d vector2) - { - vector.X *= vector2.X; - vector.Y *= vector2.Y; - vector.Z *= vector2.Z; - - return vector; - } - - /// <summary> - /// Implements the multiplication operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator *(Vector3d vector, (double X, double Y, double Z) vector2) - { - (double x, double y, double z) = vector2; - - vector.X *= x; - vector.Y *= y; - vector.Z *= z; - - return vector; - } - - /// <summary> - /// Implements the multiplication operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator *((double X, double Y, double Z) vector2, Vector3d vector) => vector * vector2; - - /// <summary> - /// Implements the multiplication operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator *(Vector3d vector, double scalar) - { - vector.X *= scalar; - vector.Y *= scalar; - vector.Z *= scalar; - - return vector; - } - - /// <summary> - /// Implements the multiplication operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator *(double scalar, Vector3d vector) => vector * scalar; - - /// <summary> - /// Implements the division operator for 2 <see cref="Vector3d"/>s. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The other <see cref="Vector3d"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator /(Vector3d vector, Vector3d vector2) - { - vector.X /= vector2.X; - vector.Y /= vector2.Y; - vector.Z /= vector2.Z; - - return vector; - } - - /// <summary> - /// Implements the division operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator /(Vector3d vector, (double X, double Y, double Z) vector2) - { - (double x, double y, double z) = vector2; - - vector.X /= x; - vector.Y /= y; - vector.Z /= z; - - return vector; - } - - /// <summary> - /// Implements the division operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator /((double X, double Y, double Z) vector2, Vector3d vector) => vector / vector2; - - /// <summary> - /// Implements the division operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator /(Vector3d vector, double scalar) - { - vector.X /= scalar; - vector.Y /= scalar; - vector.Z /= scalar; - - return vector; - } - - /// <summary> - /// Implements the division operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator /(double scalar, Vector3d vector) => vector / scalar; - - /// <summary> - /// Implements the addition operator for 2 <see cref="Vector3d"/>s. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The other <see cref="Vector3d"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator +(Vector3d vector, Vector3d vector2) - { - vector.X += vector2.X; - vector.Y += vector2.Y; - vector.Z += vector2.Z; - - return vector; - } - - /// <summary> - /// Implements the addition operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator +(Vector3d vector, (double X, double Y, double Z) vector2) - { - (double x, double y, double z) = vector2; - - vector.X += x; - vector.Y += y; - vector.Z += z; - - return vector; - } - - /// <summary> - /// Implements the addition operator for a <see cref="Vector3d"/> and a (X, Y, Z) tuple. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="vector2">The 3D tuple.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator +((double X, double Y, double Z) vector2, Vector3d vector) => vector + vector2; - - /// <summary> - /// Implements the addition operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator +(Vector3d vector, double scalar) - { - vector.X += scalar; - vector.Y += scalar; - vector.Z += scalar; - - return vector; - } - - /// <summary> - /// Implements the addition operator for a <see cref="Vector3d"/> and a scalar <see cref="double"/> value. - /// </summary> - /// <param name="vector">The original <see cref="Vector3d"/>.</param> - /// <param name="scalar">The scalar <see cref="double"/>.</param> - /// <returns>The new <see cref="Vector3d"/>.</returns> - public static Vector3d operator +(double scalar, Vector3d vector) => vector + scalar; - - #endregion Operator Overloads - - /// <summary> - /// Returns the absolute value of this <see cref="Vector3d"/>. - /// </summary> - /// <returns>The absolute value (magnitude) of this <see cref="Vector3d"/>, as a <see cref="double"/>.</returns> - [Pure] - public double Abs() - { - return Math.Sqrt(X * X + Y * Y + Z * Z); - } - - /// <summary> - /// Returns the magnitude of this <see cref="Vector3d"/>. - /// </summary> - /// <returns>The magnitude (absolute value) of this <see cref="Vector3d"/>, as a <see cref="double"/>.</returns> - [Pure] - public double Magnitude() - { - return Abs(); - } - - /// <summary> - /// Holds unit vectors. - /// </summary> - public static class UnitVectors - { - /// <summary> - /// The negative unit vector for the Z axis. - /// </summary> - public static readonly Vector3d Backwards = new Vector3d(0, 0, -1); - - /// <summary> - /// The negative unit vector for the Y axis. - /// </summary> - public static readonly Vector3d Down = new Vector3d(0, -1, 0); - - /// <summary> - /// The positive unit vector for the Z axis. - /// </summary> - public static readonly Vector3d Forwards = new Vector3d(0, 0, 1); - - /// <summary> - /// The positive unit vector for the X axis. - /// </summary> - public static readonly Vector3d Left = new Vector3d(1, 0, 0); - - /// <summary> - /// The negative unit vector for the X axis. - /// </summary> - public static readonly Vector3d Right = new Vector3d(-1, 0, 0); - - /// <summary> - /// The positive unit vector for the Y axis. - /// </summary> - public static readonly Vector3d Up = new Vector3d(0, 1, 0); - } - - #region Overrides of ValueType - - /// <inheritdoc /> - public override string ToString() - { - return $"({X}, {Y}, {Z}"; - } - - #endregion Overrides of ValueType - } -} -\ No newline at end of file diff --git a/StarSim/StarSimLib/Vector4d.cs b/StarSim/StarSimLib/Vector4d.cs @@ -0,0 +1,461 @@ +using System; +using System.Diagnostics.Contracts; + +namespace StarSimLib +{ + /// <summary> + /// A 4D vector with <see cref="double"/> components. + /// </summary> + public struct Vector4d + { + /// <summary> + /// The w component of the vector. + /// </summary> + public double W; + + /// <summary> + /// The x component of the vector. + /// </summary> + public double X; + + /// <summary> + /// The y component of the vector. + /// </summary> + public double Y; + + /// <summary> + /// The z component of the vector. + /// </summary> + public double Z; + + /// <summary> + /// Initialises a new instance of the <see cref="Vector4d"/> struct. Sets Z and W to 0. + /// </summary> + /// <param name="x">The x component.</param> + /// <param name="y">The y component.</param> + public Vector4d(double x, double y) + { + X = x; + Y = y; + Z = 0; + W = 0; + } + + /// <summary> + /// Initialises a new instance of the <see cref="Vector4d"/> struct. Sets W to 0. + /// </summary> + /// <param name="x">The x component.</param> + /// <param name="y">The y component.</param> + /// <param name="z">The z component.</param> + public Vector4d(double x, double y, double z) + { + X = x; + Y = y; + Z = z; + W = 0; + } + + /// <summary> + /// Initialises a new instance of the <see cref="Vector4d"/> struct. + /// </summary> + /// <param name="x">The x component.</param> + /// <param name="y">The y component.</param> + /// <param name="z">The z component.</param> + /// <param name="w">The w component.</param> + public Vector4d(double x, double y, double z, double w) + { + X = x; + Y = y; + Z = z; + W = w; + } + + #region Operator Overloads + + /// <summary> + /// Implements the subtraction operator for 2 <see cref="Vector4d"/>s. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The other <see cref="Vector4d"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator -(Vector4d vector, Vector4d vector2) + { + vector.X -= vector2.X; + vector.Y -= vector2.Y; + vector.Z -= vector2.Z; + vector.W -= vector2.W; + + return vector; + } + + /// <summary> + /// Implements the subtraction operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator -(Vector4d vector, (double X, double Y, double Z, double W) vector2) + { + (double x, double y, double z, double w) = vector2; + + vector.X -= x; + vector.Y -= y; + vector.Z -= z; + vector.W -= w; + + return vector; + } + + /// <summary> + /// Implements the subtraction operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator -((double X, double Y, double Z, double W) vector2, Vector4d vector) => vector - vector2; + + /// <summary> + /// Implements the subtraction operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator -(Vector4d vector, double scalar) + { + vector.X -= scalar; + vector.Y -= scalar; + vector.Z -= scalar; + vector.W -= scalar; + + return vector; + } + + /// <summary> + /// Implements the subtraction operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator -(double scalar, Vector4d vector) => vector - scalar; + + /// <summary> + /// Implements the multiplication operator for 2 <see cref="Vector4d"/>s. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The other <see cref="Vector4d"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(Vector4d vector, Vector4d vector2) + { + vector.X *= vector2.X; + vector.Y *= vector2.Y; + vector.Z *= vector2.Z; + vector.W *= vector2.W; + + return vector; + } + + /// <summary> + /// Implements the multiplication operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(Vector4d vector, (double X, double Y, double Z, double W) vector2) + { + (double x, double y, double z, double w) = vector2; + + vector.X *= x; + vector.Y *= y; + vector.Z *= z; + vector.W *= w; + + return vector; + } + + /// <summary> + /// Implements the multiplication operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator *((double X, double Y, double Z, double W) vector2, Vector4d vector) => vector * vector2; + + /// <summary> + /// Implements the multiplication operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(Vector4d vector, double scalar) + { + vector.X *= scalar; + vector.Y *= scalar; + vector.Z *= scalar; + vector.W *= scalar; + + return vector; + } + + /// <summary> + /// Implements the multiplication operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator *(double scalar, Vector4d vector) => vector * scalar; + + /// <summary> + /// Implements the division operator for 2 <see cref="Vector4d"/>s. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The other <see cref="Vector4d"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator /(Vector4d vector, Vector4d vector2) + { + vector.X /= vector2.X; + vector.Y /= vector2.Y; + vector.Z /= vector2.Z; + vector.W /= vector2.W; + + return vector; + } + + /// <summary> + /// Implements the division operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator /(Vector4d vector, (double X, double Y, double Z, double W) vector2) + { + (double x, double y, double z, double w) = vector2; + + vector.X /= x; + vector.Y /= y; + vector.Z /= z; + vector.W /= w; + + return vector; + } + + /// <summary> + /// Implements the division operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator /((double X, double Y, double Z, double W) vector2, Vector4d vector) => vector / vector2; + + /// <summary> + /// Implements the division operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator /(Vector4d vector, double scalar) + { + vector.X /= scalar; + vector.Y /= scalar; + vector.Z /= scalar; + vector.W /= scalar; + + return vector; + } + + /// <summary> + /// Implements the division operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator /(double scalar, Vector4d vector) => vector / scalar; + + /// <summary> + /// Implements the addition operator for 2 <see cref="Vector4d"/>s. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The other <see cref="Vector4d"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator +(Vector4d vector, Vector4d vector2) + { + vector.X += vector2.X; + vector.Y += vector2.Y; + vector.Z += vector2.Z; + vector.W += vector2.W; + + return vector; + } + + /// <summary> + /// Implements the addition operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator +(Vector4d vector, (double X, double Y, double Z, double W) vector2) + { + (double x, double y, double z, double w) = vector2; + + vector.X += x; + vector.Y += y; + vector.Z += z; + vector.W += w; + + return vector; + } + + /// <summary> + /// Implements the addition operator for a <see cref="Vector4d"/> and a (X, Y, Z, W) tuple. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="vector2">The 4D tuple.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator +((double X, double Y, double Z, double W) vector2, Vector4d vector) => vector + vector2; + + /// <summary> + /// Implements the addition operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator +(Vector4d vector, double scalar) + { + vector.X += scalar; + vector.Y += scalar; + vector.Z += scalar; + vector.W += scalar; + + return vector; + } + + /// <summary> + /// Implements the addition operator for a <see cref="Vector4d"/> and a scalar <see cref="double"/> value. + /// </summary> + /// <param name="vector">The original <see cref="Vector4d"/>.</param> + /// <param name="scalar">The scalar <see cref="double"/>.</param> + /// <returns>The new <see cref="Vector4d"/>.</returns> + public static Vector4d operator +(double scalar, Vector4d vector) => vector + scalar; + + #endregion Operator Overloads + + /// <summary> + /// Indexes the vector as a 1D array. + /// </summary> + /// <param name="i">The column index.</param> + /// <returns>The value at the specified field.</returns> + public double this[int i] + { + get + { + switch (i) + { + case 0: + return X; + + case 1: + return Y; + + case 2: + return Z; + + case 3: + return W; + + default: + return 0; + } + } + + set + { + switch (i) + { + case 0: + X = value; + break; + + case 1: + Y = value; + break; + + case 2: + Z = value; + break; + + case 3: + W = value; + break; + + default: + break; + } + } + } + + /// <summary> + /// Returns the absolute value of this <see cref="Vector4d"/>. + /// </summary> + /// <returns>The absolute value (magnitude) of this <see cref="Vector4d"/>, as a <see cref="double"/>.</returns> + [Pure] + public double Abs() + { + return Math.Sqrt(X * X + Y * Y + Z * Z + W * W); + } + + /// <summary> + /// Returns the magnitude of this <see cref="Vector4d"/>. + /// </summary> + /// <returns>The magnitude (absolute value) of this <see cref="Vector4d"/>, as a <see cref="double"/>.</returns> + [Pure] + public double Magnitude() + { + return Abs(); + } + + /// <summary> + /// Holds unit vectors. + /// </summary> + public static class UnitVectors + { + /// <summary> + /// The negative unit vector for the Z axis. + /// </summary> + public static readonly Vector4d Backwards = new Vector4d(0, 0, -1); + + /// <summary> + /// The negative unit vector for the Y axis. + /// </summary> + public static readonly Vector4d Down = new Vector4d(0, -1, 0); + + /// <summary> + /// The positive unit vector for the Z axis. + /// </summary> + public static readonly Vector4d Forwards = new Vector4d(0, 0, 1); + + /// <summary> + /// The positive unit vector for the X axis. + /// </summary> + public static readonly Vector4d Left = new Vector4d(1, 0, 0); + + /// <summary> + /// The negative unit vector for the X axis. + /// </summary> + public static readonly Vector4d Right = new Vector4d(-1, 0, 0); + + /// <summary> + /// The positive unit vector for the Y axis. + /// </summary> + public static readonly Vector4d Up = new Vector4d(0, 1, 0); + } + + #region Overrides of ValueType + + /// <inheritdoc /> + public override string ToString() + { + return $"[{X}, {Y}, {Z}, {W}]"; + } + + #endregion Overrides of ValueType + } +} +\ No newline at end of file