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

Овечкин Илья #220

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions cs/BowlingGame/BowlingGame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit.Console" Version="3.11.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions cs/Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit.Console" Version="3.11.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;

namespace TagsCloudVisualization
{
public sealed class CircularCloudLayouter
{
private readonly ICollection<Rectangle> rectangles;
private readonly RectanglePlacer rectanglePlacer;

public IReadOnlyCollection<Rectangle> Rectangles => (IReadOnlyCollection<Rectangle>)rectangles;

public CircularCloudLayouter(Point center)
{
if (center.X < 0 || center.Y < 0)
throw new ArgumentException("the coordinates of the center must be positive numbers");
rectanglePlacer = new RectanglePlacer(center);
rectangles = new List<Rectangle>();
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
throw new ArgumentException("the width and height of the rectangle must be positive numbers");

var nextRengtanle = rectanglePlacer.GetPossibleNextRectangle((IReadOnlyCollection<Rectangle>)rectangles, rectangleSize);
rectangles.Add(nextRengtanle);
return nextRengtanle;
}

}
}
14 changes: 14 additions & 0 deletions cs/TagsCloudVisualization/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Drawing;
using TagsCloudVisualization;


var center = new Point(612, 512);
var imageSize = new Size(1200, 1200);
var layouter = new CircularCloudLayouter(center);
var rand = new Random();
for (int i = 0; i < 150; i++)
layouter.PutNextRectangle(new Size(rand.Next(20,100), rand.Next(60,80)));

var cloudVisualizatior = new TagsCloudVisualizator(layouter, imageSize);
cloudVisualizatior.DrawAndSaveCloud("cloud.png");
8 changes: 8 additions & 0 deletions cs/TagsCloudVisualization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ������� ��������� � ������� �����������

## Rectangles = 50 imageSize = (1024, 1024) center = (512, 512)
![example1](https://github.com/magnat0/tdd/blob/3cb5f7c6d529fb802c57d059e50768adde58d0b2/cs/TagsCloudVisualization/images/image1.png)
## Rectangles = 100 imageSize = (1024, 1024) center = (512, 512)
![example2](https://github.com/magnat0/tdd/blob/3cb5f7c6d529fb802c57d059e50768adde58d0b2/cs/TagsCloudVisualization/images/image2.png)
## Rectangles = 150 imageSize = (1024, 1024) center = (512, 512)
![example3](https://github.com/magnat0/tdd/blob/3cb5f7c6d529fb802c57d059e50768adde58d0b2/cs/TagsCloudVisualization/images/image3.png)
57 changes: 57 additions & 0 deletions cs/TagsCloudVisualization/RectanglePlacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Reflection.Metadata.Ecma335;

namespace TagsCloudVisualization
{
public sealed class RectanglePlacer
{
private Point center;
private double radius;
private double angle;
private double deltaRadius;
private double deltaAngle;

public RectanglePlacer(Point center, double deltaRadius = 0.1, double deltaAngle = 0.1)
{
if (center.X < 0 || center.Y < 0)
throw new ArgumentException("the coordinates of the center must be positive numbers");
this.center = center;
this.deltaRadius = deltaRadius;
this.deltaAngle = deltaAngle;
radius = 0;
angle = 0;
}

public Rectangle GetPossibleNextRectangle(IReadOnlyCollection<Rectangle> cloudRectangles, Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
throw new ArgumentException("the width and height of the rectangle must be positive numbers");

return FindPossibleNextRectangle(cloudRectangles, rectangleSize);
}

private Rectangle FindPossibleNextRectangle(IReadOnlyCollection<Rectangle> cloudRectangles, Size rectangleSize)
{
while (true)
{
var point = new Point(
(int)(center.X + radius * Math.Cos(angle)),
(int)(center.Y + radius * Math.Sin(angle))
);
var possibleRectangle = new Rectangle(point, rectangleSize);

if (!cloudRectangles.Any(rectangle => rectangle.IntersectsWith(possibleRectangle)))
{
return possibleRectangle;
}

angle += deltaAngle;
radius += deltaRadius;
}
}

}
}
18 changes: 18 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit.Console" Version="3.11.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualizator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;

namespace TagsCloudVisualization
{
public class TagsCloudVisualizator
{
private CircularCloudLayouter layouter;
private Size imageSize;

public TagsCloudVisualizator(CircularCloudLayouter layouter, Size imageSize)
{
this.layouter = layouter;
this.imageSize = imageSize;
}

public void DrawAndSaveCloud(string fileName)
{
var image = new Bitmap(imageSize.Width, imageSize.Height);
var graphics = Graphics.FromImage(image);
graphics.Clear(Color.White);
foreach (var rect in layouter.Rectangles)
{
graphics.FillRectangle(Brushes.Red, rect);
graphics.DrawRectangle(Pens.Black, rect);
}
graphics.Save();
image.Save(fileName);

}
}
}
Binary file added cs/TagsCloudVisualization/images/image1.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 cs/TagsCloudVisualization/images/image2.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 cs/TagsCloudVisualization/images/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading