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

Трофимов Никита #227

Open
wants to merge 4 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
25 changes: 25 additions & 0 deletions cs/CircularCloudLayouterTests/CircularCloudLayouterTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagsCloudVisualization\TagsCloudVisualization.csproj" />
</ItemGroup>

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

namespace TagsCloudVisualization
{

[TestFixture]
public class SpiralFunction_Should
{
[TestCase(0, 0, 1)]
[TestCase(2, 10, 2)]
public void CorrectParameters_ShouldNotGiveErrors(int x, int y, double step)
{
Action action = () => new SpiralFunction(new Point(x, y), step);
action.Should().NotThrow<ArgumentException>();
}
}


[TestFixture]
public class CircularCloudLayouter_Should
{
private Random _random = new(1);
private Point _center;
private static CircularCloudLayouter _layouter;
private List<Rectangle> _rectangles = new();


[SetUp]
public void SetUp()
{
_center = new Point(0, 0);
_layouter = new CircularCloudLayouter(_center);
}

[TestCase(0, 0)]
[TestCase(-1, -1)]
public void PutNextRectangle_ShouldThrowArgumentException_WhenIncorrectSize(int sizeX, int sizeY)
{
Action action = () => _layouter.PutNextRectangle(new Size(sizeX, sizeY));
action.Should().Throw<ArgumentException>().WithMessage("Width and Height Size should positive");
}

[TestCase(10, 10, 10)]
public void PutNextRectangle_AddRectangles(int sizeX, int sizeY, int count)
{
for (var i = 0; i < count; i++)
_layouter.PutNextRectangle(new Size(sizeX, sizeY));

_layouter._rectangles.Count.Should().Be(count);
}

[Test]
public void CreateEmptyRectangle()
{
_layouter = new CircularCloudLayouter(new Point());
_layouter._rectangles.Should().BeEmpty();
}

[TestCase(30)]
[TestCase(50)]
[TestCase(100)]
public void PlacedRectangles_ShouldTrue_WhenCorrectNotIntersects(int countRectangles)
{
var isIntersectsWith = false;

for (var i = 0; i < countRectangles; i++)
{
var size = new Size(_random.Next(50, 100), _random.Next(40, 80));
var rectangle = _layouter.PutNextRectangle(size);
if (rectangle.IsIntersectOthersRectangles(_rectangles))
{
isIntersectsWith = true;
}
_rectangles.Add(rectangle);
}
isIntersectsWith.Should().BeTrue();
}

[TearDown]
public void TearDown()
{
if (TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
var filename = TestContext.CurrentContext.Test.MethodName;
var path = Environment.CurrentDirectory;
var drawer = new DrawCloud(1500, 1500);
drawer.CreateImage(_rectangles, "ImageForTests");
Console.WriteLine($"Tag cloud visualization saved to file {path + "\\" + filename}");
}
}
}
}
1 change: 1 addition & 0 deletions cs/CircularCloudLayouterTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
64 changes: 64 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Drawing;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter
{
public List<Rectangle> _rectangles;
private readonly SpiralFunction spiralFunction;
private readonly Point _center;

public CircularCloudLayouter(Point center)
{
_center = center;
_rectangles = new List<Rectangle>();
spiralFunction = new SpiralFunction(center, 2);
}

public Rectangle PutNextRectangle(Size sizeRectangle)
{
if (sizeRectangle.Width < 0 || sizeRectangle.Height < 0 || sizeRectangle.IsEmpty)
throw new ArgumentException("Width and Height Size should positive");

Rectangle rectangle;

while(true)
{
rectangle = new Rectangle(spiralFunction.GetNextPoint(), sizeRectangle);
if(rectangle.IsIntersectOthersRectangles(_rectangles))
break;
}
rectangle = MoveRectangleToCenter(rectangle);
_rectangles.Add(rectangle);
return rectangle;
}


private Rectangle MoveRectangleToCenter(Rectangle rectangle)
{

Rectangle newRectangle;
newRectangle = MoveRectangleAxis(rectangle, rectangle.GetCenter().X, _center.X,
new Point(rectangle.GetCenter().X < _center.X ? 1 : -1, 0));
newRectangle = MoveRectangleAxis(newRectangle, newRectangle.GetCenter().Y, _center.Y,
new Point(0, rectangle.GetCenter().Y < _center.Y ? 1 : -1));
return newRectangle;
}

private Rectangle MoveRectangleAxis(Rectangle newRectangle, int currentPosition, int desiredPosition, Point stepPoint)
{
while (newRectangle.IsIntersectOthersRectangles(_rectangles) && desiredPosition != currentPosition)
{
currentPosition += currentPosition < desiredPosition ? 1 : -1;
newRectangle.Location = newRectangle.Location.DecreasingCoordinate(stepPoint);
}

if (!newRectangle.IsIntersectOthersRectangles(_rectangles))
{
newRectangle.Location = newRectangle.Location.IncreasingCoordinate(stepPoint);
}

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

namespace TagsCloudVisualization;

public class RectangleGenerator
{
public List<Rectangle> GenerateRandomRectangles(CircularCloudLayouter layouter, int count)
{
var rectangles = new List<Rectangle>();
var random = new Random(1);
for (var i = 0; i < count; i++)
{
var size = new Size(random.Next(40,100), random.Next(20, 60));
rectangles.Add(layouter.PutNextRectangle(size));
}

return rectangles;
}
}
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.
21 changes: 21 additions & 0 deletions cs/TagsCloudVisualization/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using TagsCloudVisualization;

namespace TagsCloudVisualization
{
internal class Program
{
public static void Main()
{
var center = new Point(0, 0);
var layouter = new CircularCloudLayouter(center);
var cloud = new RectangleGenerator();
var rectangles = cloud.GenerateRandomRectangles(layouter, 50);
var drawer = new DrawCloud( 1500, 1500);
drawer.CreateImage(rectangles, "RandomRectangles50.png");
}
}
}
12 changes: 12 additions & 0 deletions cs/TagsCloudVisualization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Примеры работ

1. Рандомные прямоугольники 300 штук
![First](Images/RandomRectangles300.png)


2. Рандомные прямоугольники 150 штук
![First](Images/RandomRectangles150.png)


3. Рандомные прямоугольники 50 штук
![First](Images/RandomRectangles50.png)
33 changes: 33 additions & 0 deletions cs/TagsCloudVisualization/RectangleDraw.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;

namespace TagsCloudVisualization;

public class DrawCloud
{
private Bitmap _bitmap;
private readonly Size shiftToBitmapCenter;
private List<Rectangle> _rectangles;
public DrawCloud(int width, int height)
{
_bitmap = new Bitmap(width, height);
shiftToBitmapCenter = new Size(_bitmap.Width / 2, _bitmap.Height / 2);
}
public void CreateImage(List<Rectangle> rectangles, string filename)
{
_rectangles = rectangles;
using (Graphics g = Graphics.FromImage(_bitmap))
{
g.Clear(Color.White);
foreach (var r in _rectangles)
{
var rectangle = new Rectangle(r.Location + shiftToBitmapCenter, r.Size);
g.DrawRectangle(new Pen(Color.BlueViolet), rectangle);
}
}
_bitmap.Save(filename, ImageFormat.Png);
}
}
19 changes: 19 additions & 0 deletions cs/TagsCloudVisualization/RectangleExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;

namespace TagsCloudVisualization;

public static class RectangleExtension
{
public static Point DecreasingCoordinate(this Point selfPoint, Point otherPoint) =>
new(selfPoint.X + otherPoint.X, selfPoint.Y + otherPoint.Y);

public static Point IncreasingCoordinate(this Point selfPoint, Point otherPoint) =>
new(selfPoint.X - otherPoint.X, selfPoint.Y - otherPoint.Y);

public static Point GetCenter(this Rectangle rectangle) =>
new(rectangle.Location.X + rectangle.Width / 2, rectangle.Location.Y + rectangle.Height / 2);

public static bool IsIntersectOthersRectangles(this Rectangle rectangle, List<Rectangle> rectangles) =>
rectangles.All(rect => !rectangle.IntersectsWith(rect));

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

namespace TagsCloudVisualization;

public class SpiralFunction
{
private double angle;
private readonly Point _pastPoint;
private readonly double _step;

public SpiralFunction(Point start, double step)
{
_pastPoint = start;
_step = step;
}

public Point GetNextPoint()
{
var newX = (int)(_pastPoint.X + _step * angle * Math.Cos(angle));
var newY = (int)(_pastPoint.Y + _step * angle * Math.Sin(angle));
angle += Math.PI / 50;

return new Point(newX, newY);
}
}
19 changes: 19 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateProgramFile>false</GenerateProgramFile>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions cs/tdd.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BowlingGame", "BowlingGame\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "Samples\Samples.csproj", "{B5108E20-2ACF-4ED9-84FE-2A718050FC94}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudVisualization", "TagsCloudVisualization\TagsCloudVisualization.csproj", "{FB185471-A937-44A3-BBEC-38CBFDA51C37}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CircularCloudLayouterTests", "CircularCloudLayouterTests\CircularCloudLayouterTests.csproj", "{872C4E88-D91B-4EF1-AC39-C08A86404AED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +25,14 @@ Global
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.Build.0 = Release|Any CPU
{FB185471-A937-44A3-BBEC-38CBFDA51C37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB185471-A937-44A3-BBEC-38CBFDA51C37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB185471-A937-44A3-BBEC-38CBFDA51C37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB185471-A937-44A3-BBEC-38CBFDA51C37}.Release|Any CPU.Build.0 = Release|Any CPU
{872C4E88-D91B-4EF1-AC39-C08A86404AED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{872C4E88-D91B-4EF1-AC39-C08A86404AED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{872C4E88-D91B-4EF1-AC39-C08A86404AED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{872C4E88-D91B-4EF1-AC39-C08A86404AED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down