-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Брайденбихер Виктор Николаевич #238
Open
viteokB
wants to merge
9
commits into
kontur-courses:master
Choose a base branch
from
viteokB:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0f93d6
Выполненное домашнее задание
viteokB 45ebd90
Исправление недоработок
viteokB e3b1cfb
Откат обновления пакета Newtonsoft.Json
viteokB 7380895
Исправление недочетов CloudClasses.
viteokB 9d0430e
Рефакторинг тестов
viteokB 8500201
Изменение интерефейса ICloudLayouter и его реализация CircularCloudLa…
viteokB 0432d5d
Добавил возможность использовать состояние, для созд. изображения
viteokB c235046
Доработал CircularCloudLayouter
viteokB ee289c4
Поправил тестирование
viteokB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,4 +197,5 @@ FakesAssemblies/ | |
|
||
*Solved.cs | ||
|
||
.idea/ | ||
.idea/ | ||
/cs/tdd.sln.DotSettings |
43 changes: 43 additions & 0 deletions
43
cs/TagsCloudVisualization/CloudClasses/CircularCloudLayouter.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Drawing; | ||
using System.Runtime.CompilerServices; | ||
using TagsCloudVisualization.CloudClasses.Interfaces; | ||
|
||
namespace TagsCloudVisualization.CloudClasses | ||
{ | ||
public class CircularCloudLayouter : ICloudLayouter | ||
{ | ||
private readonly List<Rectangle> rectangles = new List<Rectangle>(); | ||
|
||
public readonly IRayMover RayMover; | ||
|
||
public List<Rectangle> Rectangles => rectangles; | ||
|
||
public CircularCloudLayouter(IRayMover rayMover) | ||
{ | ||
RayMover = rayMover; | ||
} | ||
|
||
public Rectangle PutNextRectangle(Size rectangleSize) | ||
{ | ||
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
throw new ArgumentException("The height and width of the Rectangle must be greater than 0"); | ||
|
||
foreach (var point in RayMover.MoveRay()) | ||
{ | ||
var location = new Point(point.X - rectangleSize.Width / 2, | ||
point.Y - rectangleSize.Height / 2); | ||
|
||
var rectangle = new Rectangle(location, rectangleSize); | ||
|
||
// Проверяем, пересекается ли новый прямоугольник с уже существующими | ||
if (!rectangles.Any(r => r.IntersectsWith(rectangle))) | ||
{ | ||
rectangles.Add(rectangle); | ||
return rectangle; | ||
} | ||
} | ||
|
||
throw new InvalidOperationException("No suitable location found for the rectangle."); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
cs/TagsCloudVisualization/CloudClasses/Interfaces/ICloudLayouter.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudVisualization.CloudClasses.Interfaces | ||
{ | ||
public interface ICloudLayouter | ||
{ | ||
public Rectangle PutNextRectangle(Size rectangleSize); | ||
|
||
public List<Rectangle> Rectangles { get; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
cs/TagsCloudVisualization/CloudClasses/Interfaces/IRayMover.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudVisualization.CloudClasses.Interfaces | ||
{ | ||
// Интерфейс для класса, который перемещает луч по спирали, генерируя последовательность точек. | ||
public interface IRayMover | ||
{ | ||
IEnumerable<Point> MoveRay(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudVisualization.CloudClasses | ||
{ | ||
public class Ray | ||
{ | ||
public double Radius { get; private set; } | ||
|
||
//В радианах | ||
public double Angle { get; private set; } | ||
|
||
public readonly Point StartPoint; | ||
|
||
public Ray(Point startPoint, int radius, int angle) | ||
{ | ||
StartPoint = startPoint; | ||
|
||
Radius = radius; | ||
|
||
Angle = angle; | ||
} | ||
|
||
public void Update(double deltaRadius, double deltaAngle) | ||
{ | ||
Radius += deltaRadius; | ||
Angle += deltaAngle; | ||
} | ||
|
||
public Point EndPoint => new Point | ||
{ | ||
X = StartPoint.X + (int)(Radius * Math.Cos(Angle)), | ||
Y = StartPoint.Y + (int)(Radius * Math.Sin(Angle)) | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Drawing; | ||
using TagsCloudVisualization.CloudClasses.Interfaces; | ||
|
||
namespace TagsCloudVisualization.CloudClasses | ||
{ | ||
public class SpiralRayMover : IRayMover | ||
{ | ||
private readonly Ray spiralRay; | ||
|
||
private readonly double radiusStep; | ||
|
||
//В радианах | ||
private readonly double angleStep; | ||
|
||
private const double OneRound = Math.PI * 2; | ||
|
||
public SpiralRayMover(Point center, double radiusStep = 1, double angleStep = 5, | ||
int startRadius = 0, int startAngle = 0) | ||
{ | ||
if (center.X <= 0 || center.Y <= 0) | ||
throw new ArgumentException("SpiralRayMover center Point should have positive X and Y"); | ||
|
||
if (radiusStep <= 0 || angleStep <= 0) | ||
throw new ArgumentException("radiusStep and angleStep should be positive"); | ||
|
||
spiralRay = new Ray(center, startRadius, startAngle); | ||
this.radiusStep = radiusStep; | ||
|
||
//Преобразование из градусов в радианы | ||
this.angleStep = angleStep * Math.PI / 180; | ||
} | ||
|
||
public IEnumerable<Point> MoveRay() | ||
{ | ||
while (true) | ||
{ | ||
yield return spiralRay.EndPoint; | ||
|
||
//Радиус увеличивается на 1 только после полного прохождения круга | ||
spiralRay.Update(radiusStep / OneRound * angleStep, angleStep); | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.23 KB
...ctangleNotIntersectsWithOtherRectangles63e7c14d-22d8-4823-8263-c51bf0ef4d6b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13 KB
...ctangleNotIntersectsWithOtherRectanglesb77a115f-bdc6-4b16-b196-1049273d785e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Drawing; | ||
using TagsCloudVisualization.CloudClasses; | ||
using TagsCloudVisualization.CloudClasses.Interfaces; | ||
|
||
namespace TagsCloudVisualization.Visualisation | ||
{ | ||
internal class Program | ||
{ | ||
private static void Main(string[] args) | ||
{ | ||
using var tagCloudGenerator = new TagCloudImageGenerator(new Visualiser(Color.Black, 1)); | ||
|
||
var center = new Point(500, 500); | ||
var bitmapSize = new Size(1000, 1000); | ||
|
||
var firstSizes = GenerateRectangleSizes(40, 60, 80, 60, 80); | ||
var secondSizes = GenerateRectangleSizes(100, 10, 40, 20, 30); | ||
var thirdSizes = GenerateRectangleSizes(30, 80, 130, 80, 130); | ||
|
||
var firstLayouter = SetupCloudLayout(center, firstSizes); | ||
var secondLayouter = SetupCloudLayout(center, secondSizes); | ||
var thirdLayouter = SetupCloudLayout(center, thirdSizes); | ||
|
||
using var bitmap1 = tagCloudGenerator.CreateNewBitmap(bitmapSize, firstLayouter.Rectangles); | ||
using var bitmap2 = tagCloudGenerator.CreateNewBitmap(bitmapSize, secondLayouter.Rectangles); | ||
using var bitmap3 = tagCloudGenerator.CreateNewBitmap(bitmapSize, thirdLayouter.Rectangles); | ||
|
||
BitmapSaver.SaveToCorrect(bitmap1, "bitmap_1.png"); | ||
BitmapSaver.SaveToCorrect(bitmap2, "bitmap_2.png"); | ||
BitmapSaver.SaveToCorrect(bitmap3, "bitmap_3.png"); | ||
} | ||
|
||
private static IEnumerable<Size> GenerateRectangleSizes(int count, int minWidth, int maxWidth, int minHeight, int maxHeight) | ||
{ | ||
var sizeBuilder = SizeBuilder.Configure() | ||
.SetCount(count) | ||
.SetWidth(minWidth, maxWidth) | ||
.SetHeight(minHeight, maxHeight) | ||
.Generate(); | ||
|
||
return sizeBuilder; | ||
} | ||
|
||
private static CircularCloudLayouter SetupCloudLayout(Point center, IEnumerable<Size> sizes) | ||
{ | ||
var layouter = new CircularCloudLayouter(new SpiralRayMover(center)); | ||
|
||
sizes.ToList() | ||
.ForEach(size => layouter.PutNextRectangle(size)); | ||
|
||
return layouter; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<StartupObject>TagsCloudVisualization.Visualisation.Program</StartupObject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
<PackageReference Include="System.Drawing.Common" Version="8.0.10" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="CorrectImages\" /> | ||
<Folder Include="FailImages\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
150 changes: 150 additions & 0 deletions
150
cs/TagsCloudVisualization/Tests/CircularCloudLayouterTests.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework.Interfaces; | ||
using System.Drawing; | ||
using TagsCloudVisualization.CloudClasses; | ||
using TagsCloudVisualization.Visualisation; | ||
|
||
namespace TagsCloudVisualization.Tests | ||
{ | ||
public class CircularCloudLayouterTests | ||
{ | ||
private const int WIDTH = 1000; | ||
|
||
private const int HEIGHT = 1000; | ||
|
||
private CircularCloudLayouter _layouter; | ||
|
||
private Point _center; | ||
|
||
private IVisualiser _visualiser; | ||
|
||
private TagCloudImageGenerator _tagCloudImageGenerator; | ||
|
||
private Size _errorImageSize; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_center = new Point(WIDTH / 2, HEIGHT / 2); | ||
|
||
var mover = new SpiralRayMover(_center); | ||
|
||
_layouter = new CircularCloudLayouter(mover); | ||
|
||
_visualiser = new Visualiser(Color.Black, 1); | ||
|
||
_tagCloudImageGenerator = new TagCloudImageGenerator(_visualiser); | ||
|
||
_errorImageSize = new Size(WIDTH, HEIGHT); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
if (TestContext.CurrentContext.Result.Outcome == ResultState.Failure) | ||
{ | ||
using var errorBitmap = _tagCloudImageGenerator.CreateNewBitmap(_errorImageSize, _layouter.Rectangles); | ||
|
||
var fileName = $"{TestContext.CurrentContext.Test.MethodName + Guid.NewGuid()}.png"; | ||
BitmapSaver.SaveToFail(errorBitmap, fileName); | ||
} | ||
|
||
_visualiser.Dispose(); | ||
_tagCloudImageGenerator.Dispose(); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_WhenCreated_RectanglesShoudBeEmpty() | ||
{ | ||
_layouter.Rectangles.Should().BeEmpty(); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_WhenCreated_FirstPointEqualsCenter() | ||
{ | ||
var firstPoint = _layouter.RayMover.MoveRay().First(); | ||
|
||
firstPoint.Should().BeEquivalentTo(_center); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_WhenAddFirstRectangle_ContainOneAndSameRectangle() | ||
{ | ||
var rectangle = _layouter.PutNextRectangle(new Size(20, 10)); | ||
|
||
_layouter.Rectangles | ||
.Select(r => r).Should().HaveCount(1).And.Contain(rectangle); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_WhenAddRectangle_ReturnsRectangleWithSameSize() | ||
{ | ||
var givenSize = new Size(20, 20); | ||
|
||
var rectangle = _layouter.PutNextRectangle(givenSize); | ||
|
||
rectangle.Size.Should().BeEquivalentTo(givenSize); | ||
} | ||
|
||
[TestCase(0, 0)] | ||
[TestCase(-2, 2)] | ||
[TestCase(2, -2)] | ||
[TestCase(-2, -2)] | ||
public void CircularCloudLayouter_WhenWrongSize_ThrowArgumentException(int width, int height) | ||
{ | ||
Assert.Throws<ArgumentException>(() => _layouter.PutNextRectangle(new Size(width, height)), | ||
"Not valid size should be positive"); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_WhenAddFew_ShouldHaveSameCount() | ||
{ | ||
var sizesList = GenerateSizes(5); | ||
AddRectanglesToLayouter(sizesList); | ||
|
||
_layouter.Rectangles.Should().HaveCount(5); | ||
} | ||
|
||
[TestCase(1, 2, 3)] | ||
public void CircularCloudLayouter_ShouldIncreaseRectangleCountCorrectly(int countBefore, int add, int countAfter) | ||
{ | ||
var countBeforeSizes = GenerateSizes(countBefore); | ||
AddRectanglesToLayouter(countBeforeSizes); | ||
|
||
var addSizes = GenerateSizes(add); | ||
AddRectanglesToLayouter(addSizes); | ||
|
||
_layouter.Rectangles.Should().HaveCount(countAfter); | ||
} | ||
|
||
[TestCase(2)] | ||
[TestCase(30)] | ||
public void CircularCloudLayouter_WhenAddFew_RectangleNotIntersectsWithOtherRectangles(int count) | ||
{ | ||
var listSizes = GenerateSizes(count); | ||
AddRectanglesToLayouter(listSizes); | ||
|
||
foreach (var rectangle in _layouter.Rectangles) | ||
{ | ||
_layouter.Rectangles | ||
.Any(r => r.IntersectsWith(rectangle) && rectangle != r) | ||
.Should().BeFalse(); | ||
} | ||
} | ||
|
||
private List<Size> GenerateSizes(int count) | ||
{ | ||
return SizeBuilder.Configure() | ||
.SetCount(count) | ||
.SetWidth(60, 80) | ||
.SetHeight(60, 80) | ||
.Generate() | ||
.ToList(); | ||
} | ||
|
||
private void AddRectanglesToLayouter(List<Size> sizes) | ||
{ | ||
sizes.ForEach(size => _layouter.PutNextRectangle(size)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы разделил на два проекта: текущий, где будет находится вся логика и проект где будут тесты на логику.
Это сделает код более понятным и удобным - логика остается чистой, а тесты находятся отдельно и не мешают.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Плюс артефакты из тестов, будут складываться рядом с тестами, а не посреди логики
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я первоначально так и хотел, но когда я прочитал формулировку задания в репозитории
"Сделай форк этого репозитория. Добавь проект TagsCloudVisualization. Выполняй задания в этом проекте."
Это заставило меня передумать и создать один проект, но я понимаю, что лучше было бы это разделить
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Понял, немного от формулировки задания, можно отходить если считаешь как можно сделать лучше