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

Зиновьева Милана #244

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
8 changes: 4 additions & 4 deletions cs/Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</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="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

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

namespace TagsCloudLayouter;

class CircularCloudLayouter
{
private Spiral spiral;
private Point centercloud;

private List<Rectangle> rectangles;

public Point GetCenter => centercloud;
Copy link

Choose a reason for hiding this comment

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

опечатка


public List<Rectangle> GetRectangles => rectangles;

public CircularCloudLayouter(Point center)
{
this.centercloud = center;
this.spiral = new Spiral(center);
this.rectangles = new List<Rectangle>();
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
if (rectangleSize.IsEmpty)
{
throw new ArgumentNullException("rectangle is empty");
}
if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0)
{
throw new ArgumentOutOfRangeException("side less or equal zero");
}
Rectangle tempRectangle;
do
{
Point nextPoint = spiral.GetNextPoint();
tempRectangle = new Rectangle(new Point(nextPoint.X, nextPoint.Y), rectangleSize);
Copy link

Choose a reason for hiding this comment

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

так же можно было использовать with expression

}
while (IsRectangleIntersect(tempRectangle));
rectangles.Add(tempRectangle);
return tempRectangle;
}

private bool IsRectangleIntersect(Rectangle rectangleChecked) =>
rectangles.Any(rectangleChecked.IntersectsWith);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions cs/TagsCloudVisualization/DrawingExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Drawing;

namespace TagsCloudLayouter;

public class DrawingExamples
{
public static void DrawImage_EqualsRectangles250()
{
var tempLayouter = new CircularCloudLayouter(new Point(400, 400));
for (int i = 0; i < 250; i++)
tempLayouter.PutNextRectangle(new Size(10, 5));
DrawingTagsCloud drawingTagsCloud = new DrawingTagsCloud(new Point(tempLayouter.GetCenter.X * 2, tempLayouter.GetCenter.Y * 2), tempLayouter.GetRectangles);
drawingTagsCloud.SaveToFile("EqualsRectangles250temp.png");
}

public static void DrawImage_MixedRectangles320()
{
var tempLayouter = new CircularCloudLayouter(new Point(400, 400));
var rectanglesSizes = new List<Size>
{
new Size(10, 5),
new Size(8, 8),
new Size(12, 3),
new Size(6, 10)
};
for (int i = 0; i < 80; i++)
{
foreach (var size in rectanglesSizes)
tempLayouter.PutNextRectangle(size);
}
DrawingTagsCloud drawingTagsCloud = new DrawingTagsCloud(new Point(tempLayouter.GetCenter.X * 2, tempLayouter.GetCenter.Y * 2), tempLayouter.GetRectangles);
drawingTagsCloud.SaveToFile("MixedRectangles320temp.png");
}

public static void DrawImage_DecreasingRectangles120()
{
var tempLayouter = new CircularCloudLayouter(new Point(400, 400));
tempLayouter.PutNextRectangle(new Size(160, 180));
for (int i = 0; i < 80; i++)
{
tempLayouter.PutNextRectangle(new Size(60, 40));
}
for (int i = 0; i < 39; i++)
{
tempLayouter.PutNextRectangle(new Size(20, 25));
}
DrawingTagsCloud drawingTagsCloud = new DrawingTagsCloud(new Point(tempLayouter.GetCenter.X * 2, tempLayouter.GetCenter.Y * 2), tempLayouter.GetRectangles);
drawingTagsCloud.SaveToFile("DecreasingRectangles120temp.png");
}
}
44 changes: 44 additions & 0 deletions cs/TagsCloudVisualization/DrawingTagsCloud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Skia;
using System.Drawing;

namespace TagsCloudLayouter;
public class DrawingTagsCloud
{
private readonly System.Drawing.Point centercloud;
private readonly List<Rectangle> rectangles;

public DrawingTagsCloud(System.Drawing.Point center, List<Rectangle> rectanglesInput)
{
this.centercloud = center;
this.rectangles = rectanglesInput;
}

public void SaveToFile(string filePath)
{
using var bitmapContext = new SkiaBitmapExportContext(800, 800, 2.0f);

var canvas = bitmapContext.Canvas;
canvas.FontColor = Colors.Black;
canvas = Draw(canvas);
using var image = bitmapContext.Image;
using var stream = File.OpenWrite(filePath);
image.Save(stream);
}

private ICanvas Draw(ICanvas canvas)
{

canvas.FillColor = Colors.Blue;
canvas.FontColor = Colors.Black;

foreach (var rect in rectangles)
{
canvas.FillRectangle(rect.X, rect.Y,
rect.Width,
rect.Height);
Copy link

Choose a reason for hiding this comment

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

При разбиении на строки, стоит либо на каждый аргумент выделять строку, либо в каждую строку помещать элементы вплоть до стандартной границы. Тут всё влезет на одну строку


}
return canvas;
}
}
Binary file added cs/TagsCloudVisualization/EqualsRectangles250.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/MixedRectangles320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions cs/TagsCloudVisualization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Увеличивающися по размеру прямоугольники
![alt text](DecreasingRectangles120.png)


Одинаковые прямоугольники
![alt text](EqualsRectangles250.png)


Прямоугольники разных размеров
![alt text](MixedRectangles320.png)
32 changes: 32 additions & 0 deletions cs/TagsCloudVisualization/Spiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Drawing;

namespace TagsCloudLayouter;

class Spiral
Copy link

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.

Стоит поправить

{
private readonly Point startPoint;
private readonly double radiusStep;
private readonly double angleStep;
private double currentAngle;

public Spiral(Point startPoint, double radiusStep = 1)
{
if (radiusStep <= 0) throw new ArgumentOutOfRangeException(nameof(radiusStep), "radius step must be positive");

this.angleStep = Math.PI / 180;
this.startPoint = startPoint;
this.radiusStep = radiusStep;
this.currentAngle = 0;
}

public Point GetNextPoint()
{
var radius = radiusStep * currentAngle;

var x = (int)(startPoint.X + radius * Math.Cos(currentAngle));
var y = (int)(startPoint.Y + radius * Math.Sin(currentAngle));
currentAngle += angleStep;

return new Point(x, y);
}
}
32 changes: 32 additions & 0 deletions cs/TagsCloudVisualization/TagsCloudVisualization.csproj
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>
Copy link

Choose a reason for hiding this comment

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

Выдели тесты в отдельный проект

</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="9.0.0" />
<PackageReference Include="Microsoft.Maui.Graphics.Skia" Version="9.0.0" />
<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="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

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

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

namespace TagsCloudLayouter;

public class TestsSpiral
{

Spiral currentSpiral;
[SetUp]
public void SetUp()
{
currentSpiral = new Spiral(new Point(0, 0));
}

[Test]
public void Spiral_ThrowingWhenRadiusNonPositive()
{
Action action = new Action(() => new Spiral(new Point(0, 0), -9));
action.Should().Throw<ArgumentOutOfRangeException>().Which.Message.Should().Contain("radius step must be positive");
}

[Test]
public void GetNextPoint_CenterPointSetting()
{
currentSpiral.GetNextPoint().Should().Be(new Point(0, 0));
}

[Test]
public void GetNextPoint_SeveralPointSetting()
{
for (int i = 0; i < 180; i++)
{
currentSpiral.GetNextPoint();
}
currentSpiral.GetNextPoint().Should().Be(new Point((int)(-Math.PI), 0));
}

}
Loading