-
Notifications
You must be signed in to change notification settings - Fork 306
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
crycrash
wants to merge
9
commits into
kontur-courses:master
Choose a base branch
from
crycrash:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Зиновьева Милана #244
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
47e27fe
Added basic tests for initialization
crycrash 80c6f63
Added spiral and some tests for spiral
crycrash 627f537
A spiral has been built into the addition of rectangles and addition …
crycrash 0b4df01
Added tag cloud visualization and examples
crycrash 85cbeba
Fixed README
crycrash 8f0befd
Fixed issues and the project is divided into subprojects
crycrash 76bfcc8
The rectangle layout algorithm has been changed
crycrash d4fb1cd
Added drawing when the test falls
crycrash 4a2c28a
Small changes
crycrash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Drawing; | ||
using TagsCloudVisualization; | ||
|
||
namespace DrawingTagsCloudVisualization; | ||
|
||
public class DrawingExamples | ||
{ | ||
static void Main() | ||
{ | ||
DrawImage_DecreasingRectangles120(); | ||
DrawImage_MixedRectangles320(); | ||
DrawImage_EqualsRectangles250(); | ||
} | ||
|
||
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.CenterCloud.X * 2, tempLayouter.CenterCloud.Y * 2), tempLayouter.GetRectangles); | ||
drawingTagsCloud.SaveToFile("EqualsRectangles250.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.CenterCloud.X * 2, tempLayouter.CenterCloud.Y * 2), tempLayouter.GetRectangles); | ||
drawingTagsCloud.SaveToFile("MixedRectangles320.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.CenterCloud.X * 2, tempLayouter.CenterCloud.Y * 2), tempLayouter.GetRectangles); | ||
drawingTagsCloud.SaveToFile("DecreasingRectangles120.png"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.Maui.Graphics; | ||
using Microsoft.Maui.Graphics.Skia; | ||
using System.Drawing; | ||
|
||
namespace DrawingTagsCloudVisualization; | ||
|
||
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; | ||
|
||
foreach (var rect in rectangles) | ||
{ | ||
canvas.FillRectangle(rect.X, rect.Y, rect.Width, rect.Height); | ||
} | ||
return canvas; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
cs/DrawingTagsCloudVisualization/DrawingTagsCloudVisualization.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>false</IsTestProject> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Maui.Graphics" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Maui.Graphics.Skia" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../TagsCloudVisualization/TagsCloudVisualization.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudVisualization; | ||
|
||
public class ArchimedeanSpiral | ||
{ | ||
private readonly Point startPoint; | ||
private readonly double radiusStep; | ||
private readonly double angleStep; | ||
private double currentAngle; | ||
|
||
public ArchimedeanSpiral(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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudVisualization; | ||
|
||
public class CircularCloudLayouter | ||
{ | ||
private ArchimedeanSpiral spiral; | ||
private Point centerСloud; | ||
|
||
private List<Rectangle> rectangles; | ||
|
||
public Point CenterCloud => centerСloud; | ||
|
||
public List<Rectangle> GetRectangles => rectangles; | ||
|
||
public CircularCloudLayouter(Point center) | ||
{ | ||
this.centerСloud = center; | ||
this.spiral = new ArchimedeanSpiral(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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. так же можно было использовать with expression |
||
} | ||
while (IsRectangleIntersect(tempRectangle)); | ||
if (rectangles.Count > 1) | ||
tempRectangle = TryToMoveRectangleNearToCenter(tempRectangle); | ||
rectangles.Add(tempRectangle); | ||
return tempRectangle; | ||
} | ||
|
||
private Rectangle TryToMoveRectangleNearToCenter(Rectangle rectangle) | ||
{ | ||
while (true) | ||
{ | ||
var tempRectangle = rectangle; | ||
if (rectangle.X != centerСloud.X) | ||
{ | ||
var directionX = rectangle.X < centerСloud.X ? 1 : -1; | ||
rectangle = MovingRectangleIfPossible(rectangle, true, directionX); | ||
} | ||
if (rectangle.Y != centerСloud.Y) | ||
{ | ||
var directionY = rectangle.Y < centerСloud.Y ? 1 : -1; | ||
rectangle = MovingRectangleIfPossible(rectangle, false, directionY); | ||
} | ||
|
||
if (tempRectangle.Equals(rectangle)) | ||
break; | ||
} | ||
return rectangle; | ||
} | ||
|
||
private Rectangle MovingRectangleIfPossible(Rectangle rectangle, bool isX, int direction) | ||
{ | ||
var shiftPoint = isX ? new Point(direction, 0) : new Point(0, direction); | ||
var movedRectangle = rectangle with | ||
{ | ||
Location = new Point(rectangle.X + shiftPoint.X, rectangle.Y + shiftPoint.Y) | ||
}; | ||
|
||
if (!IsRectangleIntersect(movedRectangle)) | ||
{ | ||
rectangle = movedRectangle; | ||
} | ||
return rectangle; | ||
} | ||
|
||
private bool IsRectangleIntersect(Rectangle rectangleChecked) => | ||
rectangles.Any(rectangleChecked.IntersectsWith); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>false</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
<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> | ||
Comment on lines
+12
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Неиспользуемые пакеты стоит удалять из проекта |
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using NUnit.Framework; | ||
using FluentAssertions; | ||
using System.Drawing; | ||
using TagsCloudVisualization; | ||
|
||
namespace TagsCloudVisualizationTests; | ||
|
||
public class TestsSpiral | ||
{ | ||
|
||
ArchimedeanSpiral currentSpiral; | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
currentSpiral = new ArchimedeanSpiral(new Point(0, 0)); | ||
} | ||
|
||
[Test] | ||
public void Spiral_ThrowingWhenRadiusNonPositive() | ||
{ | ||
Action action = new Action(() => new ArchimedeanSpiral(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)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Указание типа точки можно сделать короче