-
Notifications
You must be signed in to change notification settings - Fork 307
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
Большаков Николай #256
Open
stupidnessplusplus
wants to merge
25
commits into
kontur-courses:master
Choose a base branch
from
stupidnessplusplus: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
Большаков Николай #256
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
66f225d
Create homework project
stupidnessplusplus c0293fc
Add ICircularCloudLayouter.cs
stupidnessplusplus 7183fc6
Add spiral layouter
stupidnessplusplus 344d04e
Add tests
stupidnessplusplus aab0770
Add RectanglesVisualizer.cs
stupidnessplusplus 1b644f3
Add pictures
stupidnessplusplus 04dc69c
Add SizeParser.cs
stupidnessplusplus 9f9d08e
Add Program.cs with Main method
stupidnessplusplus 9e00853
Change RectanglesVisualizer
stupidnessplusplus a516105
Add failed tests visualization
stupidnessplusplus 8ab4c3d
Fix exception when creating empty image
stupidnessplusplus f9d9483
Add homework projects to tdd solution
stupidnessplusplus eb47d4f
Delete CircularCloudLayouter_Constructor_Tests.cs
stupidnessplusplus 9dffe35
Rename tests class
stupidnessplusplus 1cc941b
Remove public Center property
stupidnessplusplus 16f8ea4
Style changes
stupidnessplusplus 82eefab
Rewrite test using linq
stupidnessplusplus e19896c
Remove Debug.Assert
stupidnessplusplus 9ade8c9
Add DirectionOperations.cs
stupidnessplusplus c493b78
Store direction to previous rectangle
stupidnessplusplus 47fc9fa
Change to iteration by directions
stupidnessplusplus 7fa5911
Rewrite using SortedList
stupidnessplusplus ca73e6c
Add ability to run with random sizes
stupidnessplusplus b1b1fc2
Add example sizes file
stupidnessplusplus 74e492e
Remove method for second rectangle
stupidnessplusplus 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
This file contains 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
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.
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 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 @@ | ||
![](Pictures/spiral_random_100.png) | ||
![](Pictures/spiral_random_250.png) | ||
![](Pictures/spiral_random_25000.png) | ||
|
||
Получается ромб, если прямоугольники одного размера | ||
![](Pictures/spiral_same_size.png) | ||
|
||
Сплющивается, если все прямоугольники вытянуты | ||
![](Pictures/spiral_width.png) |
This file contains 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 @@ | ||
namespace TagsCloud; | ||
|
||
public enum Direction | ||
{ | ||
None, | ||
Left, | ||
Right, | ||
Up, | ||
Down, | ||
} |
This file contains 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 TagsCloud; | ||
|
||
public interface ICircularCloudLayouter | ||
{ | ||
public Rectangle PutNextRectangle( | ||
Size rectangleSize); | ||
} |
This file contains 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,118 @@ | ||
using System.Diagnostics; | ||
using System.Drawing; | ||
|
||
namespace TagsCloud; | ||
|
||
/// <summary> | ||
/// Вспомогательная структура для хранения прямоугольников, отсортированных по координатам сторон, | ||
/// с возможностью получения прямоугольника по индексу в отсортированном списке. | ||
/// </summary> | ||
public class SortedRectanglesList | ||
{ | ||
private static readonly Dictionary<Direction, Comparer<Rectangle>> _comparers; | ||
|
||
private readonly Dictionary<Direction, List<Rectangle>> _sortedRectangles; | ||
|
||
static SortedRectanglesList() | ||
{ | ||
var leftComparer = Comparer<Rectangle>.Create((x1, x2) => x1.Left.CompareTo(x2.Left)); | ||
var rightComparer = Comparer<Rectangle>.Create((x1, x2) => -x1.Right.CompareTo(x2.Right)); | ||
var topComparer = Comparer<Rectangle>.Create((x1, x2) => x1.Top.CompareTo(x2.Top)); | ||
var bottomComparer = Comparer<Rectangle>.Create((x1, x2) => -x1.Bottom.CompareTo(x2.Bottom)); | ||
|
||
_comparers = new Dictionary<Direction, Comparer<Rectangle>>(4) | ||
{ | ||
{ Direction.Left, rightComparer }, | ||
{ Direction.Right, leftComparer }, | ||
{ Direction.Up, bottomComparer }, | ||
{ Direction.Down, topComparer }, | ||
}; | ||
} | ||
|
||
public SortedRectanglesList() | ||
{ | ||
_sortedRectangles = new Dictionary<Direction, List<Rectangle>>(4) | ||
{ | ||
{ Direction.Left, [] }, | ||
{ Direction.Right, [] }, | ||
{ Direction.Up, [] }, | ||
{ Direction.Down, [] }, | ||
}; | ||
} | ||
|
||
public int Count { get; private set; } | ||
|
||
public void Add( | ||
Rectangle rectangle) | ||
{ | ||
Insert(_sortedRectangles[Direction.Left], rectangle, _comparers[Direction.Left]); | ||
Insert(_sortedRectangles[Direction.Right], rectangle, _comparers[Direction.Right]); | ||
Insert(_sortedRectangles[Direction.Up], rectangle, _comparers[Direction.Up]); | ||
Insert(_sortedRectangles[Direction.Down], rectangle, _comparers[Direction.Down]); | ||
|
||
Count++; | ||
} | ||
|
||
public Rectangle Get( | ||
Direction sortingDirection, | ||
int index) | ||
{ | ||
if (!_sortedRectangles.TryGetValue(sortingDirection, out var rectangles)) | ||
{ | ||
throw new ArgumentException($"Unsupported sorting direction: {sortingDirection}."); | ||
} | ||
|
||
if (index < 0 || index >= Count) | ||
{ | ||
throw new IndexOutOfRangeException($"Index was out of range: {index}."); | ||
} | ||
|
||
return rectangles[index]; | ||
} | ||
|
||
public bool HasIntersection( | ||
Rectangle rectangle, | ||
Direction sortingDirection, | ||
int startIndex, | ||
out int intersectedRectangleIndex) | ||
{ | ||
if (!_sortedRectangles.TryGetValue(sortingDirection, out var rectangles)) | ||
{ | ||
throw new ArgumentException($"Unsupported sorting direction: {sortingDirection}."); | ||
} | ||
|
||
if (startIndex < 0) | ||
{ | ||
throw new IndexOutOfRangeException($"Index was out of range: {startIndex}."); | ||
} | ||
|
||
for (var i = startIndex; i < rectangles.Count; i++) | ||
{ | ||
if (rectangle.IntersectsWith(rectangles[i])) | ||
{ | ||
intersectedRectangleIndex = i; | ||
return true; | ||
} | ||
} | ||
|
||
intersectedRectangleIndex = -1; | ||
return false; | ||
} | ||
|
||
private static void Insert( | ||
List<Rectangle> rectangles, | ||
Rectangle rectangle, | ||
Comparer<Rectangle>? comparer) | ||
{ | ||
Debug.Assert(rectangles != null); | ||
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. давай уберем дебаг ассерт из кода |
||
|
||
var index = rectangles.BinarySearch(rectangle, comparer); | ||
|
||
if (index < 0) | ||
{ | ||
index = -index - 1; | ||
} | ||
|
||
rectangles.Insert(index, rectangle); | ||
} | ||
} |
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.
1 - давай перепишем с использованием sortedList
2 - не совсем поняла задумку: почему один и тот же прямоугольник храним четырежды? кажется все взаимодействие с этим классом сводится к проверке пересечения прямоугольников (но это можно проверить и без дублирования листов)
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.
Я когда это решение писал, думал про SortedList, но почему-то подумал, что в нем взятие по индексу происходит за O(log(n)), что мне не подходило. Оказывается, нет.
Эта структура нужна только для оптимизации, чтобы избавиться от постоянной проверки пересечения со всеми прямоугольниками сразу. Она хранит прямоугольники, отсортированные по каждой из сторон, и при проверке на пересечение возвращает еще и индекс пересечения, чтобы потом, когда сдвинем прямоугольник, не проверять все прямоугольники сначала, а с этого индекса.