Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Sources/Everglow.Core/DataStructures/UnionFind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Everglow.Commons.DataStructures;

public class UnionFind
{
private int[] parent;
private int[] rank;

public UnionFind(int size)
{
parent = new int[size];
rank = new int[size];

for (int i = 0; i < size; i++)
{
parent[i] = i;
rank[i] = 0;
}
}

public int Find(int x)
{
if (parent[x] != x)
{
parent[x] = Find(parent[x]);
}
return parent[x];
}

public void Union(int x, int y)
{
int rootX = Find(x);
int rootY = Find(y);

if (rootX == rootY)
{
return;
}

if (rank[rootX] < rank[rootY])
{
parent[rootX] = rootY;
}
else if (rank[rootX] > rank[rootY])
{
parent[rootY] = rootX;
}
else
{
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
11 changes: 11 additions & 0 deletions Sources/Everglow.Core/Everglow.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
</PropertyGroup>
<ItemGroup>
<Reference Remove="$(tMLPath)" />
<AdditionalFiles Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Compile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<EmbeddedResource Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<None Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Reference Remove="$(tMLLibraryPath)\**\tModPorter.dll" />
<Reference Remove="$(tMLLibraryPath)\**\TerrariaHooks.dll" />
<Reference Remove="$(tMLLibraryPath)\**\ReLogic.dll" />
<Solaestas-EffectFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-HjsonFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-ImageFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-JsonFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-MusicFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-ResourceFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
<Solaestas-TextFile Remove="Physics\Everglow.Commons.Physics.PBEngine\**" />
</ItemGroup>
<ItemGroup>
<Reference Include="$(MSBuildThisFileDirectory)\..\..\Libraries\*.dll" />
Expand Down
210 changes: 210 additions & 0 deletions Sources/Everglow.Core/Utilities/Matrix2x2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Everglow.Commons.Utilities
{
/// <summary>
/// 2x2 矩阵类
/// </summary>
public class Matrix2x2
{
public static Matrix2x2 Identity = new Matrix2x2(
new double[2, 2]
{
{ 1, 0}, { 0, 1 },
});
public static Matrix2x2 Zero = new Matrix2x2(
new double[2, 2]
{
{ 0, 0 }, { 0, 0 },
});

private double[,] _matrix;

public Matrix2x2()
{
_matrix = new double[2, 2];
}

public Matrix2x2(double[,] initialMatrix)
{
if (initialMatrix.GetLength(0) != 2 || initialMatrix.GetLength(1) != 2)
throw new ArgumentException("Initial matrix must be 2x2");

_matrix = initialMatrix;
}

public double this[int row, int col]
{
get
{
return _matrix[row, col];
}
set
{
_matrix[row, col] = value;
}
}

public double Trace()
{
return _matrix[0, 0] + _matrix[1, 1];
}

public Matrix2x2 Inverse()
{
double det = Determinant();
if (det == 0)
throw new InvalidOperationException("Matrix is not invertible.");

var result = new Matrix2x2
{
[0, 0] = _matrix[1, 1] / det,
[0, 1] = -_matrix[0, 1] / det,
[1, 0] = -_matrix[1, 0] / det,
[1, 1] = _matrix[0, 0] / det
};

return result;
}

public Matrix2x2 Multiply(Matrix2x2 other)
{
var result = new Matrix2x2
{
[0, 0] = this[0, 0] * other[0, 0] + this[0, 1] * other[1, 0],
[0, 1] = this[0, 0] * other[0, 1] + this[0, 1] * other[1, 1],
[1, 0] = this[1, 0] * other[0, 0] + this[1, 1] * other[1, 0],
[1, 1] = this[1, 0] * other[0, 1] + this[1, 1] * other[1, 1]
};

return result;
}

public Vector2 Multiply(Vector2 other)
{
return new Vector2((float)(this[0, 0] * other.X + this[0, 1] * other.Y),
(float)(this[1, 0] * other.X + this[1, 1] * other.Y));
}

public double Determinant()
{
return _matrix[0, 0] * _matrix[1, 1] - _matrix[0, 1] * _matrix[1, 0];
}

public Matrix2x2 Adjoint()
{
return new Matrix2x2
{
[0, 0] = _matrix[1, 1],
[0, 1] = -_matrix[0, 1],
[1, 0] = -_matrix[1, 0],
[1, 1] = _matrix[0, 0],
};
}

public static Matrix2x2 operator *(Matrix2x2 a, double b)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] * b,
[0, 1] = a[0, 1] * b,
[1, 0] = a[1, 0] * b,
[1, 1] = a[1, 1] * b
};

return result;
}

public static Matrix2x2 operator *(double b, Matrix2x2 a)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] * b,
[0, 1] = a[0, 1] * b,
[1, 0] = a[1, 0] * b,
[1, 1] = a[1, 1] * b
};

return result;
}

public static Matrix2x2 operator *(Matrix2x2 a, Matrix2x2 b)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] * b[0, 0] + a[0, 1] * b[1, 0],
[0, 1] = a[0, 0] * b[0, 1] + a[0, 1] * b[1, 1],
[1, 0] = a[1, 0] * b[0, 0] + a[1, 1] * b[1, 0],
[1, 1] = a[1, 0] * b[0, 1] + a[1, 1] * b[1, 1]
};

return result;
}

public static Matrix2x2 operator +(Matrix2x2 a, Matrix2x2 b)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] + b[0, 0],
[0, 1] = a[0, 1] + b[0, 1],
[1, 0] = a[1, 0] + b[1, 0],
[1, 1] = a[1, 1] + b[1, 1]
};

return result;
}

public static Matrix2x2 operator -(Matrix2x2 a, Matrix2x2 b)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] - b[0, 0],
[0, 1] = a[0, 1] - b[0, 1],
[1, 0] = a[1, 0] - b[1, 0],
[1, 1] = a[1, 1] - b[1, 1]
};

return result;
}

public static Matrix2x2 operator /(Matrix2x2 a, Matrix2x2 b)
{
var result = new Matrix2x2
{
[0, 0] = a[0, 0] / b[0, 0],
[0, 1] = a[0, 1] / b[0, 1],
[1, 0] = a[1, 0] / b[1, 0],
[1, 1] = a[1, 1] / b[1, 1]
};

return result;
}

public Matrix2x2 Transpose()
{
return new Matrix2x2
{
[0, 0] = _matrix[0, 0],
[0, 1] = _matrix[1, 0],
[1, 0] = _matrix[0, 1],
[1, 1] = _matrix[1, 1],
};
}


public static Matrix2x2 CreateRotationMatrix(float r)
{
return new Matrix2x2
{
[0, 0] = Math.Cos(r),
[0, 1] = -Math.Sin(r),
[1, 0] = Math.Sin(r),
[1, 1] = Math.Cos(r),
};
}
}
}
Loading
Loading