-
Notifications
You must be signed in to change notification settings - Fork 53
Савицких Антон #39
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
base: master
Are you sure you want to change the base?
Савицких Антон #39
Changes from 1 commit
a9492e4
dca463c
d79298a
5913db0
ebc9ca0
d68665a
3b47dff
38a64da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| { | ||
| 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
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. Часть полей не используются |
||
|
|
||
| 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"); | ||
|
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.center = center; | ||
| } | ||
|
|
||
| public BasicTagCloudAlgorithm() | ||
| { | ||
| this.center = new Point(0, 0); | ||
| } | ||
|
Comment on lines
+31
to
+34
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. Чтобы не дублировать логику по присваиванию можно вызывать конструктор выше, используя 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) | ||
|
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. Не используется |
||
| { | ||
| if (point.X <= 0 || point.Y <= 0) throw new ArgumentException("Center coordinates must be positives"); | ||
| center = point; | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
| } | ||
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.
Только сейчас обратил внимание на имя класса. Я бы предложил в названии как-то сослаться на спиральную раскладку или что-то вроде того.