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

Бессараб Дмитрий #259

Open
wants to merge 12 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
Binary file added 1.bmp
Binary file not shown.
Binary file added 2.bmp
Binary file not shown.
Binary file added 3.bmp
Binary file not shown.
92 changes: 92 additions & 0 deletions TagsCloudVisualization.Tests/CircularCloudLayouter_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;
using System.Reflection;
using System.Drawing;
using TagsCloudVisualization.Tests;
using System.IO;
using System.Runtime.InteropServices;
using FluentAssertions.Execution;
using NUnit.Framework.Interfaces;


namespace TagsCloudVisualization
{
[TestFixture]
public class CircularCloudLayouter_Should
{
private CircularCloudLayouter layout;
[TestCase(1, 2, TestName = "Odd coordinate value results in an even size value")]
[TestCase(2, 5, TestName = "Even coordinate value results in an odd size value")]
public void CircularCloudLayouter_MakeRightSizeLayout(int coordinateValue, int sizeValue)
{
var center = new Point(coordinateValue, coordinateValue);
var size = new Size(sizeValue, sizeValue);

layout = new CircularCloudLayouter(center, new ArchemedianSpiral());

layout.Size.Should().BeEquivalentTo(size);
}

[TestCase(-1, 1, TestName = "Negative X")]
[TestCase(1, -1, TestName = "Negative Y")]
[TestCase(0, 1, TestName = "Zero X")]
[TestCase(1, 0, TestName = "Zero Y")]
public void CircularCloudLayouter_GetsOnlyPositiveCenterCoordinates(int x, int y)
{
Action makeLayout = () => new CircularCloudLayouter(new Point(x, y), new ArchemedianSpiral());

makeLayout.Should().Throw<ArgumentException>()
.WithMessage("Center coordinates values have to be greater than Zero");
}

[Test]
public void PutNextRectangle_ShouldKeepEnteredSize()
{
layout = new CircularCloudLayouter(new Point(5, 5), new ArchemedianSpiral());
var enteredSize = new Size(3, 4);
var returnedSize = layout.PutNextRectangle(enteredSize).Size;

returnedSize.Should().BeEquivalentTo(enteredSize);
}

[Test]
public void PutNextRectangle_HasNotEnoughPoints()
{

layout = new CircularCloudLayouter(new Point(100, 100), new TestPointGenerator());
var rectangleSizes = new RandomSizeRectangle().GenerateRectangles(5);

var makeRectangles = () => {
foreach (var size in rectangleSizes)
layout.PutNextRectangle(size);
};

makeRectangles.Should().Throw<ArgumentException>()
.WithMessage("Not Enough Points Generated");
}

[TearDown]
public void TearDown()
{
if (TestContext.CurrentContext.Result.Outcome != ResultState.Failure)
return;

var image = new Bitmap(layout.Size.Width, layout.Size.Height);
foreach (var rectangle in layout.Rectangles)
Drawings.DrawRectangle(image, rectangle);

var fileName = string.Format("{0}.bmp", TestContext.CurrentContext.Test.Name);
var path = TestContext.CurrentContext.WorkDirectory;
var fullPath = Path.Combine(path, fileName);
var message = string.Format("Tag cloud visualization saved to file {0}", fullPath);

image.Save(fileName);
Console.WriteLine(message);
}
}
}
50 changes: 50 additions & 0 deletions TagsCloudVisualization.Tests/IPointGenerator_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;
using FluentAssertions.Execution;

namespace TagsCloudVisualization
{
[TestFixture]
public class IPointGenerator_Should
{
[TestCaseSource(nameof(TestCases))]
public void GeneratePoints_MovingAwayFromTheStartFor(IPointGenerator pointGenerator)
{
var start = new Point(0, 0);
var points = pointGenerator.GeneratePoints(start);
var nearPoint = points.ElementAt(100);
var farPoint = points.ElementAt(1000);

DistanceBetween(start, nearPoint).Should().BeLessThan(DistanceBetween(start, farPoint));
}

[TestCaseSource(nameof(TestCases))]
public void GeneratePoints_ReturnsStartAsFirstPointFor(IPointGenerator pointGenerator)
{
var start = new Point(100, 100);
var firstReturned = pointGenerator.GeneratePoints(start)
.First();

firstReturned.Should().BeEquivalentTo(start);
}

public static IEnumerable<IPointGenerator> TestCases()
{
yield return new ArchemedianSpiral();
yield return new HeartShaped();
yield return new DeltaSHaped();
}

public int DistanceBetween(Point start, Point destination)
{
return (int)(Math.Sqrt((start.X - destination.X) * (start.X - destination.X) +
(start.Y - destination.Y) * (start.Y - destination.Y)));
}
}
}
25 changes: 25 additions & 0 deletions TagsCloudVisualization.Tests/TagsCloudVisualization.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
</ItemGroup>

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

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions TagsCloudVisualization.Tests/TestPointGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization.Tests
{
internal class TestPointGenerator : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var points = new[] { new Point(0, 0), new Point(1, 1), new Point(2, 2) };
foreach (var point in points)
yield return point;
}
}
}
27 changes: 27 additions & 0 deletions TagsCloudVisualization/ArchemedianSpiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class ArchemedianSpiral : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 1;
var spiralStep = 0.0;
yield return start;
while (true)
{
spiralStep += Math.PI / 180;
var x = start.X + (int)(zoom * spiralStep * Math.Cos(spiralStep));
var y = start.Y + (int)(zoom * spiralStep * Math.Sin(spiralStep));
var next = new Point(x, y);
yield return next;
}
}
}
}
55 changes: 55 additions & 0 deletions TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter
{
public readonly Point Center;
public readonly Size Size;
private readonly IEnumerable<Point> _points;
public List<Rectangle> Rectangles { get; set; }


public CircularCloudLayouter(Point center, IPointGenerator pointGenerator)
{
if (center.X <=0 || center.Y <=0)
throw new ArgumentException("Center coordinates values have to be greater than Zero");
Center = center;
Size = CountSize(center);
Rectangles = [];
_points = pointGenerator.GeneratePoints(Center);
}


private Size CountSize(Point center)
{
var width = (center.X % 2 == 0) ? center.X * 2 + 1 : Center.X * 2;
var height = (center.Y % 2 == 0) ? center.Y * 2 + 1 : center.Y * 2;
return new Size(width, height);
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
foreach (var point in _points)
{
var supposed = new Rectangle(new Point(point.X - rectangleSize.Width / 2, point.Y - rectangleSize.Height / 2),
rectangleSize);
if (IntersectsWithAnyOther(supposed, Rectangles))
continue;
Rectangles.Add(supposed);
return supposed;
}
throw new ArgumentException("Not Enough Points Generated");
}

public static bool IntersectsWithAnyOther(Rectangle supposed, List<Rectangle> others)
{
return others.Any(x => x.IntersectsWith(supposed));
}
}
}
39 changes: 39 additions & 0 deletions TagsCloudVisualization/DeltaShaped.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class DeltaSHaped : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 5;
yield return start;
while (true)
{
foreach (var pair in Heart())
{
var x = start.X + (int)(zoom * pair.Item1);
var y = start.Y + (int)(zoom * pair.Item2);
var next = new Point(x, y);
yield return next;
}
zoom += 2;
}
}

public IEnumerable<(double, double)> Heart()
{
for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180)
{
var x = 2 * Math.Cos(t) + Math.Cos(2*t);
var y = 2 * Math.Sin(t) - Math.Sin(2 * t);
yield return (x, y);
}
}
}
}
42 changes: 42 additions & 0 deletions TagsCloudVisualization/Drawings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class Drawings
{
public static void DrawPicture(IRectangleGenerator rectangleGenerator, int quantity,
IPointGenerator pointGenerator, Point startPoint, string filename)
{
var rectangleSizes = rectangleGenerator.
GenerateRectangles(quantity).ToArray();
var layout = new CircularCloudLayouter(startPoint, pointGenerator);
var image = new Bitmap(layout.Size.Width, layout.Size.Height);
foreach (var size in rectangleSizes)
{
DrawRectangle(image, layout.PutNextRectangle(size));
}
image.Save(filename);
}

public static void DrawRectangle(Bitmap image, Rectangle rectangle)
{
var brush = new SolidBrush(GetRandomColor());
var formGraphics = Graphics.FromImage(image);
formGraphics.FillRectangle(brush, rectangle);
brush.Dispose();
formGraphics.Dispose();
}

private static Color GetRandomColor()
{
var random = new Random();
return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
}
}
39 changes: 39 additions & 0 deletions TagsCloudVisualization/HeartShaped.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class HeartShaped : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 1;
yield return start;
while (true)
{
foreach (var pair in Heart())
{
var x = start.X + (int)(zoom * pair.Item1);
var y = start.Y + (int)(zoom * pair.Item2);
var next = new Point(x, y);
yield return next;
}
zoom += 1;
}
}

public IEnumerable<(double, double)> Heart()
{
for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180)
{
var x = 16 * Math.Sin(t) * Math.Sin(t) * Math.Sin(t);
var y = -13 * Math.Cos(t) + 5 * Math.Cos(2 * t) + 2 * Math.Cos(3 * t) + Math.Cos(4 * t);
yield return (x, y);
}
}
}
}
Loading