Skip to content
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
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
66f225d
Create homework project
stupidnessplusplus Nov 13, 2024
c0293fc
Add ICircularCloudLayouter.cs
stupidnessplusplus Nov 13, 2024
7183fc6
Add spiral layouter
stupidnessplusplus Nov 13, 2024
344d04e
Add tests
stupidnessplusplus Nov 13, 2024
aab0770
Add RectanglesVisualizer.cs
stupidnessplusplus Nov 13, 2024
1b644f3
Add pictures
stupidnessplusplus Nov 13, 2024
04dc69c
Add SizeParser.cs
stupidnessplusplus Nov 17, 2024
9f9d08e
Add Program.cs with Main method
stupidnessplusplus Nov 17, 2024
9e00853
Change RectanglesVisualizer
stupidnessplusplus Nov 17, 2024
a516105
Add failed tests visualization
stupidnessplusplus Nov 17, 2024
8ab4c3d
Fix exception when creating empty image
stupidnessplusplus Nov 19, 2024
f9d9483
Add homework projects to tdd solution
stupidnessplusplus Nov 19, 2024
eb47d4f
Delete CircularCloudLayouter_Constructor_Tests.cs
stupidnessplusplus Nov 22, 2024
9dffe35
Rename tests class
stupidnessplusplus Nov 22, 2024
1cc941b
Remove public Center property
stupidnessplusplus Nov 22, 2024
16f8ea4
Style changes
stupidnessplusplus Nov 22, 2024
82eefab
Rewrite test using linq
stupidnessplusplus Nov 22, 2024
e19896c
Remove Debug.Assert
stupidnessplusplus Nov 25, 2024
9ade8c9
Add DirectionOperations.cs
stupidnessplusplus Nov 25, 2024
c493b78
Store direction to previous rectangle
stupidnessplusplus Nov 25, 2024
47fc9fa
Change to iteration by directions
stupidnessplusplus Nov 25, 2024
7fa5911
Rewrite using SortedList
stupidnessplusplus Nov 25, 2024
ca73e6c
Add ability to run with random sizes
stupidnessplusplus Nov 25, 2024
b1b1fc2
Add example sizes file
stupidnessplusplus Nov 25, 2024
74e492e
Remove method for second rectangle
stupidnessplusplus Nov 29, 2024
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
26 changes: 22 additions & 4 deletions cs/tdd.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BowlingGame", "BowlingGame\BowlingGame.csproj", "{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BowlingGame", "BowlingGame\BowlingGame.csproj", "{AD0F018A-732E-4074-8527-AB2EEC8D0BF3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "Samples\Samples.csproj", "{B5108E20-2ACF-4ED9-84FE-2A718050FC94}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples", "Samples\Samples.csproj", "{B5108E20-2ACF-4ED9-84FE-2A718050FC94}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloud", "..\homework\TagsCloud\TagsCloud.csproj", "{0AE3DF47-DC8C-454E-B0C6-88EA9BAA7433}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloud_Tests", "..\homework\TagsCloud_Tests\TagsCloud_Tests.csproj", "{7007521F-03A5-4130-93B9-2B35320C07DE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudVisualization", "..\homework\TagsCloudVisualization\TagsCloudVisualization.csproj", "{46F6E948-679C-4320-8B2E-5965838C6CC8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +27,18 @@ Global
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5108E20-2ACF-4ED9-84FE-2A718050FC94}.Release|Any CPU.Build.0 = Release|Any CPU
{0AE3DF47-DC8C-454E-B0C6-88EA9BAA7433}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AE3DF47-DC8C-454E-B0C6-88EA9BAA7433}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AE3DF47-DC8C-454E-B0C6-88EA9BAA7433}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AE3DF47-DC8C-454E-B0C6-88EA9BAA7433}.Release|Any CPU.Build.0 = Release|Any CPU
{7007521F-03A5-4130-93B9-2B35320C07DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7007521F-03A5-4130-93B9-2B35320C07DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7007521F-03A5-4130-93B9-2B35320C07DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7007521F-03A5-4130-93B9-2B35320C07DE}.Release|Any CPU.Build.0 = Release|Any CPU
{46F6E948-679C-4320-8B2E-5965838C6CC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46F6E948-679C-4320-8B2E-5965838C6CC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46F6E948-679C-4320-8B2E-5965838C6CC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46F6E948-679C-4320-8B2E-5965838C6CC8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Binary file added homework/Pictures/spiral_random_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/Pictures/spiral_random_250.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/Pictures/spiral_random_25000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/Pictures/spiral_same_size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homework/Pictures/spiral_width.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions homework/Readme.md
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)
10 changes: 10 additions & 0 deletions homework/TagsCloud/Direction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TagsCloud;

public enum Direction
{
None,
Left,
Right,
Up,
Down,
}
9 changes: 9 additions & 0 deletions homework/TagsCloud/ICircularCloudLayouter.cs
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);
}
118 changes: 118 additions & 0 deletions homework/TagsCloud/SortedRectanglesList.cs
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
Copy link

@masssha1308 masssha1308 Nov 24, 2024

Choose a reason for hiding this comment

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

1 - давай перепишем с использованием sortedList
2 - не совсем поняла задумку: почему один и тот же прямоугольник храним четырежды? кажется все взаимодействие с этим классом сводится к проверке пересечения прямоугольников (но это можно проверить и без дублирования листов)

Copy link
Author

Choose a reason for hiding this comment

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

Я когда это решение писал, думал про SortedList, но почему-то подумал, что в нем взятие по индексу происходит за O(log(n)), что мне не подходило. Оказывается, нет.
Эта структура нужна только для оптимизации, чтобы избавиться от постоянной проверки пересечения со всеми прямоугольниками сразу. Она хранит прямоугольники, отсортированные по каждой из сторон, и при проверке на пересечение возвращает еще и индекс пересечения, чтобы потом, когда сдвинем прямоугольник, не проверять все прямоугольники сначала, а с этого индекса.

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

Choose a reason for hiding this comment

The 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);
}
}
Loading