-
Notifications
You must be signed in to change notification settings - Fork 321
Шипицын Павел #275
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
PavelMartinelli
wants to merge
11
commits into
kontur-courses:master
Choose a base branch
from
PavelMartinelli:Tag_Cloud_1HW
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
Шипицын Павел #275
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
561988a
Made Tag_Cloud1HW
153657f
fix CircularCloudLayouter
192fe8e
fix TagCloudGenerator
9344533
fix TagCloudVisualizer
3c78770
fix Configs
751999b
fix Tests and Paths
f16f87c
Add TagCloudVisualizerTests
799c871
Small fixes
ba358f4
fix Project structure
876024a
fix Visualizer & ImageSaver.cs
eca933a
fix CircularCloudLayouter & Tests
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
106 changes: 106 additions & 0 deletions
106
cs/TagCloud/TagsCloudVisualization/Layouter/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,106 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class CircularCloudLayouter : ILayouter | ||
| { | ||
| private readonly Point _center; | ||
| private readonly List<Rectangle> _placedRectangles; | ||
| private readonly ISpiralPointsProvider _pointsProvider; | ||
| private int _minDimension; | ||
|
|
||
| public CircularCloudLayouter(Point center, ISpiralPointsProvider pointsProvider) | ||
| { | ||
| _center = center; | ||
| _placedRectangles = []; | ||
| _pointsProvider = pointsProvider; | ||
| _minDimension = int.MaxValue; | ||
| } | ||
|
|
||
| public Rectangle PutNextRectangle(Size rectangleSize) | ||
| { | ||
| if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
| throw new ArgumentException("Rectangle size must have positive dimensions"); | ||
|
|
||
| UpdateMinDimension(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. Зачем это? |
||
|
|
||
| foreach (var point in _pointsProvider.GetSpiralPoints(_minDimension)) | ||
| { | ||
| var candidateRectangle = CreateRectangleAtPoint(rectangleSize, point); | ||
|
|
||
| if (IntersectsWithAny(candidateRectangle)) | ||
| continue; | ||
|
|
||
| candidateRectangle = CompactRectangle(candidateRectangle); | ||
| _placedRectangles.Add(candidateRectangle); | ||
| return candidateRectangle; | ||
| } | ||
|
|
||
| throw new InvalidOperationException("Failed to find position for rectangle"); | ||
| } | ||
|
|
||
| private void UpdateMinDimension(Size size) | ||
| { | ||
| _minDimension = Math.Min(_minDimension, Math.Min(size.Width, size.Height)); | ||
| } | ||
|
|
||
| private Rectangle CreateRectangleAtPoint(Size size, Point point) | ||
| { | ||
| return new Rectangle( | ||
| point.X - size.Width / 2, | ||
| point.Y - size.Height / 2, | ||
| size.Width, | ||
| size.Height); | ||
| } | ||
|
|
||
| private Rectangle CompactRectangle(Rectangle rectangle) | ||
| { | ||
| var canMove = true; | ||
|
|
||
| while (canMove && !IntersectsWithAny(rectangle)) | ||
| { | ||
| var movedX = TryMoveTowardsCenter(ref rectangle, Axis.X); | ||
| var movedY = TryMoveTowardsCenter(ref rectangle, Axis.Y); | ||
| canMove = movedX || movedY; | ||
| } | ||
|
|
||
| return rectangle; | ||
| } | ||
|
|
||
| private bool TryMoveTowardsCenter(ref Rectangle rectangle, Axis axis) | ||
| { | ||
| var centerCoord = axis == Axis.X | ||
| ? rectangle.X + rectangle.Width / 2 | ||
| : rectangle.Y + rectangle.Height / 2; | ||
|
|
||
| var targetCoord = axis == Axis.X ? _center.X : _center.Y; | ||
| var direction = Math.Sign(targetCoord - centerCoord); | ||
|
|
||
| if (direction == 0) | ||
| return false; | ||
|
|
||
| var movedRectangle = rectangle; | ||
| if (axis == Axis.X) | ||
| movedRectangle.X += direction; | ||
| else | ||
| movedRectangle.Y += direction; | ||
|
|
||
| if (IntersectsWithAny(movedRectangle)) | ||
| return false; | ||
|
|
||
| rectangle = movedRectangle; | ||
| return true; | ||
| } | ||
|
|
||
| private bool IntersectsWithAny(Rectangle rectangle) | ||
| { | ||
| for (var i = _placedRectangles.Count - 1; i >= 0; i--) | ||
| { | ||
| if (_placedRectangles[i].IntersectsWith(rectangle)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private enum Axis { X, Y } | ||
| } | ||
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,9 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public interface ILayouter | ||
| { | ||
| Rectangle PutNextRectangle(Size rectangleSize); | ||
|
|
||
| } |
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,13 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public static class PolarMath | ||
| { | ||
| public static Point PolarToCartesian(double radius, double angle, Point center) | ||
| { | ||
| var x = center.X + (int)(radius * Math.Cos(angle)); | ||
| var y = center.Y + (int)(radius * Math.Sin(angle)); | ||
| return new Point(x, y); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
cs/TagCloud/TagsCloudVisualization/Layouter/SpiralPointsProvider/ISpiralPointsProvider.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; | ||
|
|
||
| public interface ISpiralPointsProvider | ||
| { | ||
| IEnumerable<Point> GetSpiralPoints(int minDimension); | ||
| } |
35 changes: 35 additions & 0 deletions
35
cs/TagCloud/TagsCloudVisualization/Layouter/SpiralPointsProvider/SpiralPointsProvider.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,35 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class SpiralPointsProvider : ISpiralPointsProvider | ||
| { | ||
| private readonly Point _center; | ||
| private readonly double _angleStep; | ||
| private readonly double _radiusStepFactor; | ||
|
|
||
| private double _currentRadius; | ||
| private double _currentAngle; | ||
|
|
||
| public SpiralPointsProvider(Point center, double angleStep = 0.05, double radiusStepFactor = 0.05) | ||
| { | ||
| _center = center; | ||
| _angleStep = angleStep; | ||
| _radiusStepFactor = radiusStepFactor; | ||
| _currentRadius = 0; | ||
| _currentAngle = 0; | ||
| } | ||
|
|
||
| public IEnumerable<Point> GetSpiralPoints(int minDimension) | ||
| { | ||
| while (true) | ||
| { | ||
| var point = PolarMath.PolarToCartesian(_currentRadius, _currentAngle, _center); | ||
|
|
||
| _currentAngle += _angleStep; | ||
| _currentRadius += minDimension * _radiusStepFactor; | ||
|
|
||
| yield return point; | ||
| } | ||
| } | ||
| } |
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,77 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization.ConsoleApp; | ||
|
|
||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| var imageSaver = new ImageSaver(); | ||
| var visualizer = new TagCloudVisualizer(imageSaver); | ||
| var sizeProvider = new RandomRectangleSizeProvider(); | ||
|
|
||
| var layouterFactory = (Point center) => | ||
| new CircularCloudLayouter(center, new SpiralPointsProvider(center)); | ||
|
|
||
| var tagCloudGenerator = new TagCloudGenerator( | ||
| visualizer, | ||
| layouterFactory, | ||
| sizeProvider); | ||
|
|
||
| Console.WriteLine("Generating tag cloud visualizations..."); | ||
|
|
||
| List<TagCloudGenerationConfig> generationConfigs = | ||
| [ | ||
| new TagCloudGenerationConfig( | ||
| center: new Point(400, 300), | ||
| rectangleCount: 30, | ||
| minSize: new Size(20, 10), | ||
| maxSize: new Size(50, 30) | ||
| ), | ||
| new TagCloudGenerationConfig( | ||
| center: new Point(500, 400), | ||
| rectangleCount: 100, | ||
| minSize: new Size(30, 15), | ||
| maxSize: new Size(80, 40) | ||
| ), | ||
| new TagCloudGenerationConfig( | ||
| center: new Point(600, 450), | ||
| rectangleCount: 150, | ||
| minSize: new Size(25, 12), | ||
| maxSize: new Size(120, 60) | ||
| ) | ||
| ]; | ||
|
|
||
| List<TagCloudVisualizationConfig> visualizationConfigs = | ||
| [ | ||
| new TagCloudVisualizationConfig( | ||
| outputFileName: "cloud_small.png", | ||
| imageSize: new Size(800, 600), | ||
| backgroundColor: Color.White, | ||
| rectangleColor: Color.Blue, | ||
| centerColor: Color.Red | ||
| ), | ||
| new TagCloudVisualizationConfig( | ||
| outputFileName: "cloud_medium.png", | ||
| imageSize: new Size(1000, 800), | ||
| backgroundColor: Color.White, | ||
| rectangleColor: Color.Green, | ||
| centerColor: Color.Red | ||
| ), | ||
| new TagCloudVisualizationConfig( | ||
| outputFileName: "cloud_large.png", | ||
| imageSize: new Size(1200, 900), | ||
| backgroundColor: Color.Black, | ||
| rectangleColor: Color.Yellow, | ||
| centerColor: Color.Red | ||
| ) | ||
| ]; | ||
|
|
||
| var results = tagCloudGenerator.GenerateMultiple(generationConfigs, visualizationConfigs); | ||
|
|
||
| foreach (var result in results) | ||
| { | ||
| Console.WriteLine(result.StartsWith("ERROR") ? $" - {result}" : $"- Created cloud at {result}"); | ||
| } | ||
| } | ||
| } |
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,10 @@ | ||
| # Примеры визуализации раскладки с прямоугольниками случайного размера и с разными настройками визуализации | ||
|
|
||
| ---- | ||
|  | ||
|
|
||
| ---- | ||
|  | ||
|
|
||
| ---- | ||
|  |
8 changes: 8 additions & 0 deletions
8
.../TagsCloudVisualization/TagCloudGenerator/RectangleSizeProvider/IRectangleSizeProvider.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; | ||
|
|
||
| public interface IRectangleSizeProvider | ||
| { | ||
| IEnumerable<Size> GetSizes(int count, Size minSize, Size maxSize); | ||
| } |
32 changes: 32 additions & 0 deletions
32
...CloudVisualization/TagCloudGenerator/RectangleSizeProvider/RandomRectangleSizeProvider.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,32 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class RandomRectangleSizeProvider : IRectangleSizeProvider | ||
| { | ||
| private readonly Random _random; | ||
|
|
||
| public RandomRectangleSizeProvider(Random? random = null) | ||
| { | ||
| _random = random ?? new Random(); | ||
| } | ||
|
|
||
| public IEnumerable<Size> GetSizes(int count, Size minSize, Size maxSize) | ||
| { | ||
| if (count <= 0) | ||
| throw new ArgumentException("Count must be positive", nameof(count)); | ||
|
|
||
| if (minSize.Width <= 0 || minSize.Height <= 0) | ||
| throw new ArgumentException("Min size must have positive dimensions", nameof(minSize)); | ||
|
|
||
| if (maxSize.Width < minSize.Width || maxSize.Height < minSize.Height) | ||
| throw new ArgumentException("Max size must be greater than or equal to min size", nameof(maxSize)); | ||
|
|
||
| for (var i = 0; i < count; i++) | ||
| { | ||
| var width = _random.Next(minSize.Width, maxSize.Width + 1); | ||
| var height = _random.Next(minSize.Height, maxSize.Height + 1); | ||
| yield return new Size(width, height); | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
cs/TagCloud/TagsCloudVisualization/TagCloudGenerator/TagCloudGenerationConfig.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,23 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization; | ||
|
|
||
| public class TagCloudGenerationConfig | ||
| { | ||
| public Point Center { get; set; } | ||
| public int RectangleCount { get; set; } | ||
| public Size MinSize { get; set; } | ||
| public Size MaxSize { get; set; } | ||
|
|
||
| public TagCloudGenerationConfig( | ||
| Point center, | ||
| int rectangleCount, | ||
| Size minSize, | ||
| Size maxSize) | ||
| { | ||
| Center = center; | ||
| RectangleCount = rectangleCount; | ||
| MinSize = minSize; | ||
| MaxSize = maxSize; | ||
| } | ||
| } |
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.
Что это за поле? Для чего оно?
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.
Это поле нужно для адаптивного изменения плотности точек спирали: чем меньше прямоугольники, тем плотнее спираль; чем больше прямоугольники, тем реже точки.