-
Notifications
You must be signed in to change notification settings - Fork 306
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
Зиновьева Милана #244
base: master
Are you sure you want to change the base?
Зиновьева Милана #244
Changes from 3 commits
47e27fe
80c6f63
627f537
0b4df01
85cbeba
8f0befd
76bfcc8
d4fb1cd
4a2c28a
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,46 @@ | ||
using System.Drawing; | ||
|
||
namespace Layouter; | ||
|
||
class CircularCloudLayouter | ||
{ | ||
private Spiral spiral; | ||
private Point centercloud; | ||
|
||
private List<Rectangle> rectangles; | ||
|
||
public Point GetCenter => centercloud; | ||
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. опечатка |
||
|
||
public List<Rectangle> GetRectangles => rectangles; | ||
|
||
public CircularCloudLayouter(Point center){ | ||
this.centercloud = center; | ||
this.spiral = new Spiral(center); | ||
this.rectangles = new List<Rectangle>(); | ||
} | ||
|
||
public Rectangle PutNextRectangle(Size rectangleSize){ | ||
if (rectangleSize.IsEmpty){ | ||
throw new ArgumentNullException("rectangle is empty"); | ||
} | ||
if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0){ | ||
throw new ArgumentOutOfRangeException("side less or equal zero"); | ||
} | ||
Rectangle tempRectangle; | ||
do{ | ||
Point nextPoint = spiral.GetNextPoint(); | ||
tempRectangle = new Rectangle(nextPoint, rectangleSize); | ||
} | ||
while(IsRectangleIntersect(tempRectangle)); | ||
rectangles.Add(tempRectangle); | ||
return tempRectangle; | ||
} | ||
|
||
private bool IsRectangleIntersect(Rectangle rectangleChecked){ | ||
foreach(Rectangle rectangle in rectangles){ | ||
if (rectangleChecked.IntersectsWith(rectangle)) | ||
return true; | ||
} | ||
return false; | ||
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. Предлагаю переписать на linq |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Drawing; | ||
|
||
namespace Layouter; | ||
|
||
class Spiral | ||
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. Название не передает сути 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 readonly Point startPoint; | ||
private readonly double radiusStep; | ||
private readonly double angleStep; | ||
private double currentAngle; | ||
|
||
public Spiral(Point startPoint, double radiusStep = 1) | ||
{ | ||
if (radiusStep <= 0) throw new ArgumentOutOfRangeException(nameof(radiusStep), "radius step must be positive"); | ||
|
||
this.angleStep = Math.PI / 180; | ||
this.startPoint = startPoint; | ||
this.radiusStep = radiusStep; | ||
this.currentAngle = 0; | ||
} | ||
|
||
public Point GetNextPoint() | ||
{ | ||
var radius = radiusStep * currentAngle; | ||
|
||
var x = (int)(startPoint.X + radius * Math.Cos(currentAngle)); | ||
var y = (int)(startPoint.Y + radius * Math.Sin(currentAngle)); | ||
currentAngle += angleStep; | ||
|
||
return new Point(x, y); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
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. Выдели тесты в отдельный проект |
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
<PackageReference Include="xunit" Version="2.8.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using NUnit.Framework; | ||
using FluentAssertions; | ||
using System.Drawing; | ||
|
||
namespace Layouter; | ||
|
||
public class TestsSpiral | ||
{ | ||
|
||
Spiral currentSpiral; | ||
[SetUp] | ||
public void SetUp(){ | ||
currentSpiral = new Spiral(new Point(0, 0)); | ||
} | ||
|
||
[Test] | ||
public void Spiral_ThrowingWhenRadiusNonPositive(){ | ||
Action action = new Action(() => new Spiral(new Point(0,0), -9)); | ||
action.Should().Throw<ArgumentOutOfRangeException>().Which.Message.Should().Contain("radius step must be positive"); | ||
} | ||
|
||
[Test] | ||
public void GetNextPoint_CenterPointSetting(){ | ||
currentSpiral.GetNextPoint().Should().Be(new Point(0, 0)); | ||
} | ||
|
||
[Test] | ||
public void GetNextPoint_SeveralPointSetting(){ | ||
for (int i = 0; i < 180; i++) | ||
{ | ||
currentSpiral.GetNextPoint(); | ||
} | ||
currentSpiral.GetNextPoint().Should().Be(new Point((int)(-Math.PI), 0)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using NUnit.Framework; | ||
using FluentAssertions; | ||
using System.Drawing; | ||
|
||
namespace Layouter; | ||
|
||
public class TestsCloudVisualization | ||
{ | ||
private CircularCloudLayouter circularCloudLayouter; | ||
[SetUp] | ||
public void SetUp(){ | ||
circularCloudLayouter = new CircularCloudLayouter(new Point(0, 0)); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_SettingCenter() | ||
{ | ||
CircularCloudLayouter circularLayouter = new CircularCloudLayouter(new Point(2, 4)); | ||
circularLayouter.GetCenter.X.Should().Be(2); | ||
circularLayouter.GetCenter.Y.Should().Be(4); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_StartWithoutExceptions(){ | ||
Action action = new Action(() => new CircularCloudLayouter(new Point(2, 6))); | ||
action.Should().NotThrow(); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_ThrowingWhenLengthsNegative(){ | ||
Action action = new Action(() => circularCloudLayouter.PutNextRectangle(new Size(-1, -1))); | ||
action.Should().Throw<ArgumentOutOfRangeException>().Which.Message.Should().Contain("side less or equal zero"); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_ThrowingWhenRectangleEmpty(){ | ||
Action action = new Action(() => circularCloudLayouter.PutNextRectangle(Size.Empty)); | ||
action.Should().Throw<ArgumentNullException>().Which.Message.Should().Contain("rectangle is empty"); | ||
} | ||
|
||
[Test] | ||
public void CircularCloudLayouter_RectanglesEmptyAfterInitialization(){ | ||
circularCloudLayouter.GetRectangles.Should().BeEmpty(); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_PutFirstRectangle(){ | ||
Size rectangleSize = new Size(3, 7); | ||
Rectangle expectedRectangle = new Rectangle(new Point(0, 0), rectangleSize); | ||
circularCloudLayouter.PutNextRectangle(rectangleSize); | ||
|
||
circularCloudLayouter.GetRectangles.Count().Should().Be(1); | ||
circularCloudLayouter.GetRectangles.Should().Contain(expectedRectangle); | ||
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. collection.Should().ContainSingle() |
||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_PutSeveralRectangles(){ | ||
Size rectangleSize = new Size(3, 7); | ||
for(int i = 0;i < 20;i++){ | ||
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. Стоит использовать автоформатирование кода. Если используешь райдер, можно настроить автоформатирование при коммите |
||
circularCloudLayouter.PutNextRectangle(rectangleSize); | ||
} | ||
circularCloudLayouter.GetRectangles.Count().Should().Be(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. collection.Should().HaveCount() |
||
circularCloudLayouter.GetRectangles.Should().AllBeOfType(typeof(Rectangle)); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_LessThanAreaOfCircle() | ||
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 rectanglesSizes = new List<Size> | ||
{ | ||
new Size(10, 5), | ||
new Size(8, 8), | ||
new Size(12, 3), | ||
new Size(6, 10) | ||
}; | ||
|
||
foreach (var size in rectanglesSizes) | ||
{ | ||
circularCloudLayouter.PutNextRectangle(size); | ||
} | ||
|
||
int totalRectangleArea = circularCloudLayouter.GetRectangles | ||
.Sum(rect => rect.Width * rect.Height); | ||
|
||
int maxRadiusFromCenter = (int)circularCloudLayouter.GetRectangles | ||
.Max(rect => GetMaxDistanceToCorner(rect, circularCloudLayouter.GetCenter)); | ||
|
||
int circleArea = (int)(Math.PI * Math.Pow(maxRadiusFromCenter, 2)); | ||
totalRectangleArea.Should().BeLessThan(circleArea); | ||
} | ||
|
||
private double GetMaxDistanceToCorner(Rectangle rect, Point center) | ||
{ | ||
var corners = new[] | ||
{ | ||
new Point(rect.Left, rect.Top), | ||
new Point(rect.Right, rect.Top), | ||
new Point(rect.Left, rect.Bottom), | ||
new Point(rect.Right, rect.Bottom) | ||
}; | ||
return corners.Max(corner => Distance(center, corner)); | ||
} | ||
|
||
private double Distance(Point p1, Point p2) | ||
{ | ||
return (int)Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)); | ||
} | ||
|
||
[Test] | ||
public void PutNextRectangle_SeveralRectanglesDontIntersect(){ | ||
var rectanglesSizes = new List<Size> | ||
{ | ||
new Size(10, 5), | ||
new Size(8, 8), | ||
new Size(12, 3), | ||
new Size(6, 10) | ||
}; | ||
|
||
foreach (var size in rectanglesSizes) | ||
{ | ||
circularCloudLayouter.PutNextRectangle(size); | ||
} | ||
|
||
List<Rectangle> rectanglesTemp = circularCloudLayouter.GetRectangles; | ||
for (int i = 0; i < rectanglesTemp.Count; i++){ | ||
for (int j = i + 1; j < rectanglesTemp.Count; j++) | ||
rectanglesTemp[i].IntersectsWith(rectanglesTemp[j]).Should().BeFalse(); | ||
} | ||
} | ||
} |
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.
не используемые зависимости стоит удалить