Skip to content
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
106 changes: 106 additions & 0 deletions cs/TagCloud/TagsCloudVisualization/Layouter/CircularCloudLayouter.cs
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Что это за поле? Для чего оно?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Это поле нужно для адаптивного изменения плотности точек спирали: чем меньше прямоугольники, тем плотнее спираль; чем больше прямоугольники, тем реже точки.


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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 }
}
9 changes: 9 additions & 0 deletions cs/TagCloud/TagsCloudVisualization/Layouter/ILayouter.cs
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);

}
13 changes: 13 additions & 0 deletions cs/TagCloud/TagsCloudVisualization/Layouter/PolarMath.cs
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);
}
}
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);
}
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;
}
}
}
77 changes: 77 additions & 0 deletions cs/TagCloud/TagsCloudVisualization/Program.cs
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}");
}
}
}
10 changes: 10 additions & 0 deletions cs/TagCloud/TagsCloudVisualization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Примеры визуализации раскладки с прямоугольниками случайного размера и с разными настройками визуализации

----
![cloud_small.png](out/cloud_small.png)

----
![cloud_medium.png](out/cloud_medium.png)

----
![cloud_large.png](out/cloud_large.png)
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);
}
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);
}
}
}
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;
}
}
Loading