Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
100 changes: 100 additions & 0 deletions TagCloudGenerator/Algorithms/BasicTagCloudAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TagCloudGenerator.Core.Interfaces;

namespace TagCloudGenerator.Algorithms
{
public class BasicTagCloudAlgorithm : ITagCloudAlgorithm
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Только сейчас обратил внимание на имя класса. Я бы предложил в названии как-то сослаться на спиральную раскладку или что-то вроде того.

{
private Point center;

private readonly Random random = new Random();

private readonly List<Rectangle> rectangles = new List<Rectangle>();

private double currentAngle = 0;
private double currentRadius = 0;

private int countToGenerate = 10;

private (int min, int max) width = new(20, 21);
private (int min, int max) height = new(20, 21);
Comment on lines +10 to +20
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Часть полей не используются


private double angleStep = 0.1;
private double radiusStep = 0.5;

public BasicTagCloudAlgorithm(Point center)
{
if (center.X <= 0 || center.Y <= 0) throw new ArgumentException("Center coordinates must be positives");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Здесь и в других местах. Лучше размещать инструкции на разных строках. Так их легче читать и воспринимать

this.center = center;
}

public BasicTagCloudAlgorithm()
{
this.center = new Point(0, 0);
}
Comment on lines +31 to +34
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Чтобы не дублировать логику по присваиванию можно вызывать конструктор выше, используя BasicTagCloudAlgorithm : this(...


public Rectangle PutNextRectangle(Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) throw new ArgumentException("Rectangle sizes must be positives");

if (rectangles.Count == 0)
{
return PutFirstRectangle(rectangleSize);
}

return PlaceNextRectange(rectangleSize);
}

private Rectangle PlaceNextRectange(Size rectangleSize)
{
while (true)
{
double x = center.X + currentRadius * Math.Cos(currentAngle);
double y = center.Y + currentRadius * Math.Sin(currentAngle);

var potentialCenter = new Point((int)(x - rectangleSize.Width / 2), (int)(y - rectangleSize.Height / 2));
var potentialRectangle = new Rectangle(potentialCenter, rectangleSize);

if (!IntersectWithAny(potentialRectangle))
{
rectangles.Add(potentialRectangle);
return potentialRectangle;
}

currentAngle += angleStep;
if (currentAngle >= 2 * Math.PI)
{
currentAngle = 0;
currentRadius += radiusStep;
}
}
}

private bool IntersectWithAny(Rectangle potentialRectangle)
{
return rectangles.Any(rect => rect.IntersectsWith(potentialRectangle));
}

private Rectangle PutFirstRectangle(Size rectangleSize)
{
var firstCenter = new Point((int)(center.X - rectangleSize.Width / 2), (int)(center.Y - rectangleSize.Width / 2));
var first = new Rectangle(firstCenter, rectangleSize);
currentRadius = rectangleSize.Height / 2;
rectangles.Add(first);
return first;
}

public BasicTagCloudAlgorithm WithCenterAt(Point point)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не используется

{
if (point.X <= 0 || point.Y <= 0) throw new ArgumentException("Center coordinates must be positives");
center = point;
return this;
}

}
}
Loading