Last active
November 25, 2017 23:45
-
-
Save pkuderov/3c1b2982f67e8d43157892189caf8c1f to your computer and use it in GitHub Desktop.
Compare Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xunit; | |
namespace BoringVector.Tests | |
{ | |
public class VectorTests | |
{ | |
[Theory] | |
[InlineData(3, 4, 25)] | |
[InlineData(3, -4, 25)] | |
public void Test_SquareLength_ReturnsCorrectValue(double x, double y, double expected) | |
{ | |
Assert.Equal(new Vector(x, y).SquareLength(), expected); | |
} | |
[Theory] | |
[InlineData(1, 2, 3, 4, 4, 6)] | |
[InlineData(-1, -2, 3, 4, 2, 2)] | |
public void Test_Add_ReturnsCorrectValue(double xV, double yV, double xU, double yU, double xExpected, double yExpected) | |
{ | |
Assert.Equal(new Vector(xV, yV) + new Vector(xU, yU), new Vector(xExpected, yExpected)); | |
} | |
[Theory] | |
[InlineData(1, 1, 0, 0, 0)] | |
[InlineData(2, -2, -1, -2, 2)] | |
[InlineData(-2, 2, 1.3, -2.6, 2.6)] | |
public void Test_Scale_ReturnsCorrectValue(double x, double y, double k, double xExpected, double yExpected) | |
{ | |
Assert.Equal(new Vector(x, y).Scale(k), new Vector(xExpected, yExpected)); | |
} | |
[Theory] | |
[InlineData(1, 0, 1, 0, 1)] | |
[InlineData(1, -2, 3, -4, 11)] | |
public void Test_DotProduct_ReturnsCorrectValue(double xV, double yV, double xU, double yU, double expected) | |
{ | |
Assert.Equal(new Vector(xV, yV).DotProduct(new Vector(xU, yU)), expected); | |
} | |
[Theory] | |
[InlineData(1, -1, -1, 1, 0)] | |
[InlineData(1, 2, 3, 4, -2)] | |
public void Test_CrossProduct_ReturnsCorrectValue(double xV, double yV, double xU, double yU, double expected) | |
{ | |
Assert.Equal(new Vector(xV, yV).CrossProduct(new Vector(xU, yU)), expected); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace BoringVector | |
{ | |
/* | |
Здесь тебе нужно написать класс с методами-расширениями структуры Vector: | |
- IsZero: проверяет, является ли вектор нулевым, т.е. его координаты близки к нулю (в эпсилон окрестности). За эпсилон здесь и далее берем 1e-6. | |
- Normalize: нормализует вектор | |
- GetAngleBetween: возвращает угол между двумя векторами в радианах. Примечание: нулевой вектор сонаправлен любому другому. | |
- GetRelation: возвращает значение перечесления VectorRelation(General, Parallel, Orthogonal) - отношение между двумя векторами("общий случай", параллельны, перпендикулярны). Перечисление задавать тоже тебе) | |
*/ | |
public enum VectorRelation | |
{ | |
General = 0, | |
Parallel, | |
Orthogonal | |
}; | |
/// <summary> | |
/// Класс с методами расширяющий структуру Vector. | |
/// </summary> | |
internal static class VectorExtensions | |
{ | |
/// <summary> | |
/// Проверяет, является ли вектор нулевым. | |
/// </summary> | |
/// <param name="v">Проверяемый экземпляр вектора <see cref="Vector"/>.</param> | |
/// <returns>Результат<see cref="bool"/> вектора.</returns> | |
public static bool IsZero(this Vector v) | |
{ | |
return (Math.Abs(v.x) < 1e-6 && Math.Abs(v.y) < 1e-6); | |
} | |
/// <summary> | |
/// Нормализует вектор. | |
/// </summary> | |
/// <param name="v">Нормализируемый экземпляр вектора <see cref="Vector"/>.</param> | |
/// <returns>Нормализованный вектор <see cref="Vector"/>.</returns> | |
public static Vector Normalize(this Vector v) | |
{ | |
return v / Math.Sqrt(v.SquareLength()); | |
} | |
/// <summary> | |
/// Возвращает угол между двумя векторами в радианах. | |
/// </summary> | |
/// <param name="v1">Первый вектор <see cref="Vector"/></param> | |
/// <param name="v2">Второй вектор <see cref="Vector"/></param> | |
/// <returns>Угол между векторами в радианах <see cref="double"/>.</returns> | |
public static double GetAngleBetween(this Vector v1, Vector v2) | |
{ | |
if (!IsZero(v1) && !IsZero(v2)) | |
{ | |
return Math.Acos(v2.DotProduct(v1) / (Math.Sqrt(v1.SquareLength()) * Math.Sqrt(v2.SquareLength()))); | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
/// <summary> | |
/// Возвращает отношение между двумя векторами("общий случай", параллельны, перпендикулярны). | |
/// </summary> | |
/// <param name="v1">Первый вектор <see cref="Vector"/></param> | |
/// <param name="v2">Второй вектор <see cref="Vector"/></param> | |
/// <returns>Отношение между векторами <see cref="VectorRelation"/>.</returns> | |
public static VectorRelation GetRelation(this Vector v1, Vector v2) | |
{ | |
double angle = v1.GetAngleBetween(v2); | |
if (v1.CrossProduct(v2) < 1e-6) | |
{ | |
return VectorRelation.Parallel; | |
} | |
else if (v1.DotProduct(v2) < 1e-6) | |
{ | |
return VectorRelation.Orthogonal; | |
} | |
else | |
{ | |
return VectorRelation.General; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment