-
Notifications
You must be signed in to change notification settings - Fork 321
Копытов Михаил #266
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
Open
Mihail3141
wants to merge
13
commits into
kontur-courses:master
Choose a base branch
from
Mihail3141: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
Копытов Михаил #266
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9a0d66a
Сделал генератор точек, идущих по спирали
Mihail3141 c036e09
Сделал генерацию прямоугольников
Mihail3141 c6b65dd
Сделал визуализацию облока
Mihail3141 aa7daee
Добавил интерфейс для PointGenerator
Mihail3141 9628372
Сделал генерацию точек потенциально бесконечной
Mihail3141 78ca294
Вынес GetCircularCloudRectangles в отедльный класс
Mihail3141 2a715f6
Разместил классы в соответствующих папках
Mihail3141 7885a4a
Добавил тест на плотность
Mihail3141 00d9f22
Добавил [TearDown]
Mihail3141 cc95652
Добавил тест на ненахождение подходящей точки
Mihail3141 cdd8d79
Вынес проверку на наличие пересечений прямоугольинков в отдельный метод
Mihail3141 dbca6ef
Внёс правки в CircularCloudLayouter
Mihail3141 accc25a
Внёс правки
Mihail3141 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
56 changes: 56 additions & 0 deletions
56
cs/TagsCloudVisualization/CircularCloudLayouter/CircularCloudLayouter.cs
This file contains hidden or 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,56 @@ | ||
| using System.Drawing; | ||
| using TagsCloudVisualization.PointGenerator; | ||
|
|
||
|
|
||
| namespace TagsCloudVisualization.CircularCloudLayouter; | ||
|
|
||
| public class CircularCloudLayouter : ICloudLayouter | ||
| { | ||
| private readonly List<Rectangle> rectangles = []; | ||
|
|
||
| private readonly IPointGenerator pointGenerator; | ||
|
|
||
| private readonly int maxPointsPerRectangle; | ||
|
|
||
| public CircularCloudLayouter(Point center, int maxPointsPerRectangle, IPointGenerator pointGenerator) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(pointGenerator); | ||
|
|
||
| if (center.X <= 0 || center.Y <= 0) | ||
| throw new ArgumentException("Center should be greater than 0"); | ||
|
|
||
| if (maxPointsPerRectangle <= 0) | ||
| throw new ArgumentException("maxPointsPerRectangle must be greater than 0"); | ||
|
|
||
| this.maxPointsPerRectangle = maxPointsPerRectangle; | ||
| this.pointGenerator = pointGenerator; | ||
| } | ||
|
|
||
|
|
||
| public Rectangle PutNextRectangle(Size rectangleSize) | ||
| { | ||
| if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
| throw new ArgumentException("Rectangle size must be positive"); | ||
|
|
||
| var points = pointGenerator | ||
| .GetPoints() | ||
| .Take(maxPointsPerRectangle); | ||
|
|
||
|
|
||
| foreach (var point in points) | ||
| { | ||
| var rectangle = new Rectangle(point, rectangleSize); | ||
| for (var i = rectangles.Count - 1; i >= 0; i--) | ||
| { | ||
| if (rectangles[i].IntersectsWith(rectangle)) | ||
| goto NextPoint; | ||
| } | ||
|
|
||
| rectangles.Add(rectangle); | ||
| return rectangle; | ||
| NextPoint: ; | ||
| } | ||
|
|
||
| throw new ArgumentException($"Failed to find place for the {rectangles.Count} rectangle"); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
cs/TagsCloudVisualization/CircularCloudLayouter/ICloudLayouter.cs
This file contains hidden or 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,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization.CircularCloudLayouter; | ||
|
|
||
| public interface ICloudLayouter | ||
| { | ||
| Rectangle PutNextRectangle(Size rectangleSize); | ||
| } |
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.
This file contains hidden or 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,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public interface IImageSaver | ||
| { | ||
| void Save(Bitmap bitmap, string fileName); | ||
| } |
This file contains hidden or 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,26 @@ | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class ImageSaver : IImageSaver | ||
| { | ||
| public void Save(Bitmap bitmap, string fileName) | ||
| { | ||
| var projectDir = GetProjectDirectory(); | ||
| var imagesDir = Path.Combine(projectDir, "Image"); | ||
| Directory.CreateDirectory(imagesDir); | ||
| var path = Path.Combine(imagesDir, fileName); | ||
| bitmap.Save(path, ImageFormat.Png); | ||
| } | ||
|
|
||
| private static string GetProjectDirectory() | ||
| { | ||
| var dir = new DirectoryInfo(Environment.CurrentDirectory); | ||
| while (dir != null && !dir.GetFiles("*.csproj").Any()) | ||
| { | ||
| dir = dir.Parent; | ||
| } | ||
| return dir?.FullName ?? Environment.CurrentDirectory; | ||
| } | ||
| } |
This file contains hidden or 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,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization.PointGenerator; | ||
|
|
||
| public interface IPointGenerator | ||
| { | ||
| public IEnumerable<Point> GetPoints(); | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
cs/TagsCloudVisualization/PointGenerator/SpiralPointGenerator.cs
This file contains hidden or 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,34 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization.PointGenerator; | ||
|
|
||
| public class SpiralPointGenerator : IPointGenerator | ||
| { | ||
| private readonly Point center; | ||
| private readonly double radius; | ||
| private readonly double angle; | ||
|
|
||
| public SpiralPointGenerator(Point center, double radius = 2, double angle = double.Pi/40) | ||
| { | ||
| if (center.X < 0 || center.Y < 0) | ||
| throw new ArgumentException("Center coordinates must be non-negative"); | ||
| if (radius <= 0) | ||
| throw new ArgumentException("Radius must be positive"); | ||
| this.radius = radius; | ||
| this.angle = angle; | ||
| this.center = center; | ||
| } | ||
|
|
||
| public IEnumerable<Point> GetPoints() | ||
| { | ||
| var currentAngle = 0.0; | ||
| while (true) | ||
| { | ||
| var vector = radius * currentAngle / (2 * Math.PI); | ||
| var x = center.X + (int)Math.Round(Math.Cos(currentAngle) * vector); | ||
| var y = center.Y + (int)Math.Round(Math.Sin(currentAngle) * vector); | ||
| yield return new Point(x, y); | ||
| currentAngle += angle; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,38 @@ | ||
| using System.Drawing; | ||
| using TagsCloudVisualization.PointGenerator; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public static class Program | ||
| { | ||
| private static void Main() | ||
| { | ||
| var imageWidth = 2500; | ||
| var imageHeight = 2500; | ||
| var imageCenter = new Point(imageWidth / 2, imageHeight / 2); | ||
| var rectangleSize = new Size(200, 80); | ||
| var imageSize = new Size(imageWidth, imageHeight); | ||
|
|
||
| var rectangles = CreateRandomSizeRectangles(150, imageCenter, rectangleSize); | ||
| var cloudRenderer = new TagCloudRenderer(imageSize); | ||
| var imageSaver = new ImageSaver(); | ||
|
|
||
| var image = cloudRenderer.CreateRectangleCloud(rectangles); | ||
| imageSaver.Save(image, "cloud.png"); | ||
| } | ||
|
|
||
| private static IEnumerable<Rectangle> CreateRandomSizeRectangles(int count, Point center, Size rectangleSize, | ||
| double minScaleFactor = 0.4, int maxPointsPerRectangle = 100000) | ||
| { | ||
| var pointGenerator = new SpiralPointGenerator(center); | ||
| var layouter = new CircularCloudLayouter.CircularCloudLayouter(center, maxPointsPerRectangle, pointGenerator); | ||
| var random = new Random(10); | ||
| for (var i = 0; i < count; i++) | ||
| { | ||
| var rnd = random.NextDouble() + minScaleFactor; | ||
| var rndWidth = (int)Math.Round(rectangleSize.Width * rnd); | ||
| var rndHeight = (int)Math.Round(rectangleSize.Height * rnd); | ||
| yield return layouter.PutNextRectangle(new Size(rndWidth, rndHeight)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,2 @@ | ||
|  | ||
|  |
This file contains hidden or 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,8 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public interface ICloudRenderer | ||
| { | ||
| public Bitmap CreateRectangleCloud(IEnumerable<Rectangle> rectangles); | ||
|
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. Через интерфейс нужно работать
Author
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. Не совсем понял, что требуется |
||
| } | ||
This file contains hidden or 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,19 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class TagCloudRenderer(Size imageSize) : ICloudRenderer | ||
| { | ||
| public Bitmap CreateRectangleCloud(IEnumerable<Rectangle> rectangles) | ||
| { | ||
| var bitmap = new Bitmap(imageSize.Width, imageSize.Height); | ||
|
|
||
| using var graphics = Graphics.FromImage(bitmap); | ||
| graphics.Clear(Color.FromArgb(0, 34, 43)); | ||
| var pen = new Pen(Color.FromArgb(212, 85, 0), 2); | ||
| foreach (var rect in rectangles) | ||
| graphics.DrawRectangle(pen, rect); | ||
|
|
||
| return bitmap; | ||
| } | ||
| } |
This file contains hidden or 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,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="System.Drawing.Common" Version="10.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
3 changes: 3 additions & 0 deletions
3
cs/TagsCloudVisualization/TagsCloudVisualization.csproj.DotSettings
This file contains hidden or 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,3 @@ | ||
| <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
| <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=imagesaver/@EntryIndexedValue">True</s:Boolean> | ||
| <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=render/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
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.
Интерфейс не используется