Skip to content
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

Шмаков Данил #207

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions cs/TagCloud/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Drawing;

namespace TagCloud;

public class CircularCloudLayouter
{
private List<Rectangle> rectangles;
private Point center;
private ICloudShaper shaper;

public IEnumerable<Rectangle> Rectangles => rectangles;

public CircularCloudLayouter(Point center)
{
this.center = center;
rectangles = new List<Rectangle>();
shaper = SpiralCloudShaper.Create(this.center);
Copy link

@gisinka gisinka Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем делать интерфейс и жестко привязываться к реализации в конструкторе? Через конструктор можно принимать ICloudShaper

}

public Rectangle PutNextRectangle(Size size)
{
if (size.Width <= 0)
throw new ArgumentException("Size width must be positive number");
if (size.Height <= 0)
throw new ArgumentException("Size height must be positive number");

Rectangle rectangle = Rectangle.Empty;
foreach (var point in shaper.GetPossiblePoints())
{
rectangle = new Rectangle(point, size);
if (!Rectangles.Any(rect => rect.IntersectsWith(rectangle)))
break;
}

rectangles.Add(rectangle);
return rectangle;
}
}
8 changes: 8 additions & 0 deletions cs/TagCloud/CloudDrawers/CloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagCloudTests;

public interface ICloudDrawer
{
void Draw(IEnumerable<Rectangle> rectangle, string name);
}
52 changes: 52 additions & 0 deletions cs/TagCloud/CloudDrawers/TagCloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Drawing;
using System.Dynamic;
using TagCloudTests;

namespace TagCloud;

public class TagCloudDrawer : ICloudDrawer
{
private readonly string path;
private readonly IColorSelector selector;

private TagCloudDrawer(string path, IColorSelector selector)
{
this.path = path;
this.selector = selector;
}

public void Draw(IEnumerable<Rectangle> rectangles, string name)
{
if (rectangles.Count() == 0)
throw new ArgumentException("Empty rectangles list");

var minX = rectangles.Min(rect => rect.X);
var maxX = rectangles.Max(rect => rect.Right);
var minY = rectangles.Min(rect => rect.Top);
var maxY = rectangles.Max(rect => rect.Bottom);

using var bitmap = new Bitmap(maxX - minX + 2, maxY - minY + 2);
using var graphics = Graphics.FromImage(bitmap);
graphics.DrawRectangles(
selector,
rectangles
.Select(rect => rect with { X = -minX + rect.X, Y = -minY + rect.Y })
.ToArray()
);
SaveToFile(bitmap, name);
}

private void SaveToFile(Bitmap bitmap, string name)
{
var pathToFile = @$"{path}\{name}.jpg";
bitmap.Save(pathToFile);
Console.WriteLine($"Tag cloud visualization saved to file {path}");
}

public static TagCloudDrawer Create(string path, IColorSelector selector)
{
if (!Directory.Exists(path))
throw new ArgumentException("Directory does not exist");
return new TagCloudDrawer(path, selector);
}
}
15 changes: 15 additions & 0 deletions cs/TagCloud/ColorSelectors/ConstantColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Drawing;
using TagCloudTests;

namespace TagCloud;

public class ConstantColorSelector : IColorSelector
{
private Color color;
public ConstantColorSelector(Color color)
{
this.color = color;
}

public Color PickColor() => color;
}
8 changes: 8 additions & 0 deletions cs/TagCloud/ColorSelectors/IColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagCloudTests;

public interface IColorSelector
{
Color PickColor();
}
22 changes: 22 additions & 0 deletions cs/TagCloud/ColorSelectors/RandomColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;

namespace TagCloudTests;

public class RandomColorSelector : IColorSelector
{
private readonly Random random;

public RandomColorSelector()
{
random = new Random(DateTime.Now.Microsecond);
}

public Color PickColor()
{
return Color.FromArgb(255,
random.Next(0, 255),
random.Next(0, 255),
random.Next(0, 255)
);
}
}
17 changes: 17 additions & 0 deletions cs/TagCloud/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Drawing;
using TagCloudTests;

namespace TagCloud;

public static class Extensions
{
public static void DrawRectangles(this Graphics graphics, IColorSelector selector, Rectangle[] rectangles, int lineWidth = 1)
{
var pen = new Pen(selector.PickColor(), lineWidth);
foreach (var rectangle in rectangles)
{
graphics.DrawRectangle(pen, rectangle);
pen.Color = selector.PickColor();
}
}
}
8 changes: 8 additions & 0 deletions cs/TagCloud/ICloudShaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagCloud;

public interface ICloudShaper

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сделал

{
IEnumerable<Point> GetPossiblePoints();
}
53 changes: 53 additions & 0 deletions cs/TagCloud/SpiralCloudShaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Drawing;

namespace TagCloud;

public class SpiralCloudShaper : ICloudShaper
{
private Point center;
private double coefficient;
private double deltaAngle;

private SpiralCloudShaper(Point center, double coefficient, double deltaAngle)
{
this.center = center;

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сделано

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Считаю, что конструктор должен быть максимально простым, никаких проверок внутри не должно быть, его задача - инициализировать объект, если хочешь сделать проверку на входные параметры, то лучше сделать статичный метод который это будет проверять, и потом сам вызовет тебе приватный конструктор,
Ну и декомпозиция, все дела
Да и тесты так как то приятнее писать когда вызываешь какой нибудь метод Create вместо new

Как вариант валидацию выносить в отдельный приватный метод

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сделал через Create

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так же это работает и для других классов

this.deltaAngle = deltaAngle;
this.coefficient = coefficient;
}

public IEnumerable<Point> GetPossiblePoints()
{
var currentAngle = 0D;
var position = center;
var previousPoint = position;
while(true)
{
while (position == previousPoint)
{
currentAngle += deltaAngle;
previousPoint = position;
position = CalculatePointByCurrentAngle(currentAngle);
}
yield return position;
previousPoint = position;
position = CalculatePointByCurrentAngle(currentAngle);
}
}

private Point CalculatePointByCurrentAngle(double angle)
{
return new Point(
center.X + (int)(coefficient * angle * Math.Cos(angle)),
center.Y + (int)(coefficient * angle * Math.Sin(angle))
);
}

public static SpiralCloudShaper Create(Point center, double coefficient = 0.1, double deltaAngle = 0.1)
{
if (coefficient <= 0)
throw new ArgumentException("Spiral coefficient must be positive number");
if (deltaAngle <= 0)
throw new ArgumentException("Spiral delta angle must be positive number");
return new SpiralCloudShaper(center, coefficient, deltaAngle);
}
}
13 changes: 13 additions & 0 deletions cs/TagCloud/TagCloud.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
119 changes: 119 additions & 0 deletions cs/TagCloudTests/CircularCloudLayouterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Drawing;
using FluentAssertions;
using NUnit.Framework.Interfaces;
using TagCloud;

namespace TagCloudTests;

public class CircularCloudLayouterTests
{
private const string RelativePathToFailDirectory = @"..\..\..\Fails";

private CircularCloudLayouter layouter;
private Point center;
private ICloudDrawer drawer;

[OneTimeSetUp]
public void OneTimeSetUp()
{
drawer = TagCloudDrawer.Create(Path.GetFullPath(RelativePathToFailDirectory), new RandomColorSelector());
}

[SetUp]
public void Setup()
{
center = new Point(0, 0);
layouter = new CircularCloudLayouter(center);
}

[TearDown]
public void Tear_Down()
{
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
drawer.Draw(layouter.Rectangles, TestContext.CurrentContext.Test.FullName);
}

[Test]
public void ReturnEmptyList_WhenCreated()
{
layouter.Rectangles.Should().BeEmpty();
}

[Test]
public void ReturnOneElementList_WhenAddOne()
{
layouter.PutNextRectangle(new Size(1, 1));
layouter.Rectangles.Count().Should().Be(1);
}

[Test]
public void ReturnTwoElementList_WhenAddTwo()
{
layouter.PutNextRectangle(new Size(1, 1));
layouter.PutNextRectangle(new Size(1, 1));
layouter.Rectangles.Count().Should().Be(2);
NotIntersectedAssert(layouter.Rectangles);
}

[TestCase(1, 1, 500, 0.77D, TestName = "WithSquareShape")]
[TestCase(20, 10, 500, 0.67D, TestName = "WithRectangleShape")]
public void AddManyNotIntersectedRectangles_WithConstantSize_ByCloudShape(int width, int height, int count, double accuracy)
{
var size = new Size(width, height);
for (int i = 0; i < count; i++)
layouter.PutNextRectangle(size);

layouter.Rectangles.Count().Should().Be(count);
NotIntersectedAssert(layouter.Rectangles);
CircleShapeAssertion(layouter.Rectangles, accuracy);
}

[Test]
public void AddManyNotIntersectedRectangles_WithVariableSize_ByCloudShape()
{
var rnd = new Random(DateTime.Now.Microsecond);

for (int i = 0; i < 200; i++)
{
var size = new Size(rnd.Next(20, 40), rnd.Next(20, 40));
layouter.PutNextRectangle(size);
}

layouter.Rectangles.Count().Should().Be(200);
NotIntersectedAssert(layouter.Rectangles);
CircleShapeAssertion(layouter.Rectangles, 0.6D);
}

[TestCase(-1, 1, TestName = "WithNegativeWidth")]
[TestCase(1, -1, TestName = "WithNegativeHeight")]
[TestCase(-1, -1, TestName = "WithNegativeWidthAndHeight")]
public void Throw_ThenTryPutRectangle(int width, int height)
{
Assert.Throws<ArgumentException>(() => layouter.PutNextRectangle(new Size(width, height)));
}

private void CircleShapeAssertion(IEnumerable<Rectangle> rectangles, double accuracy)
{
var circleRadius = rectangles
.SelectMany(rect => new[]
{
rect.Location,
rect.Location with { X = rect.Right },
rect.Location with { Y = rect.Bottom },
rect.Location with { X = rect.Right, Y = rect.Bottom }
})
.Max(point => point.GetDistanceTo(center));
Console.WriteLine(circleRadius);
var containingCircleRadius = Math.PI * circleRadius * circleRadius;
var rectanglesTotalArea = rectangles.Sum(rect => rect.Width * rect.Height);
Assert.GreaterOrEqual(rectanglesTotalArea / containingCircleRadius, accuracy);
}

public static void NotIntersectedAssert(IEnumerable<Rectangle> rectangles)
{
rectangles
.HasIntersectedRectangles()
.Should()
.BeFalse();
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чет шакалит, но лан

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading