forked from kontur-courses/tdd
-
Notifications
You must be signed in to change notification settings - Fork 0
HW #1
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
Open
Griboedoff
wants to merge
18
commits into
ToPR
Choose a base branch
from
master
base: ToPR
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
HW #1
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9568647
initial
2de7959
Fixed properties bug
0e6c688
add spiral generator
82742a8
add readme
3233348
Add examples
7925835
Add examples
de0a512
up readme
4ca61c4
up examples
467f0cd
up readme
57552e9
fix .gitattributes
91ac919
add shifting to center
11cbcc4
another fix of corrupted png
87ec727
up readme
71e807d
Refactoring
fa941b5
Add Tests for PointExtensions
ba2b3d1
fix bug in MoveRectangle
94ed49e
Add test for density of cloud and circle shape
b4400aa
Add vizualizer for generated image(in explicit test)
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 hidden or 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
This file contains hidden or 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,6 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <configuration> | ||
| <startup> | ||
| <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> | ||
| </startup> | ||
| </configuration> |
This file contains hidden or 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,112 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Drawing; | ||
| using System.Linq; | ||
|
|
||
| namespace TagsCloudVisualization | ||
| { | ||
| internal class CircularCloudLayouter | ||
| { | ||
| private Point center; | ||
| private readonly Spiral.Spiral spiral; | ||
| private readonly Rectangle cloudBorders; | ||
|
|
||
| public Point Center | ||
| { | ||
| get { return center; } | ||
| private set | ||
| { | ||
| if (value.X < 0 || value.Y < 0) | ||
| throw new ArgumentException("Center point should be non-negative"); | ||
| center = value; | ||
| } | ||
| } | ||
|
|
||
| public int Count => PlacedRectangles.Count; | ||
|
|
||
| private List<Rectangle> PlacedRectangles { get; } | ||
|
|
||
| private bool IsInValidPosition(Rectangle checkingRectangle) | ||
| => !PlacedRectangles.Any(rect => rect.IntersectsWith(checkingRectangle)) && cloudBorders.Contains(checkingRectangle); | ||
|
|
||
| private static Point GetRectangleCenterLocation(Size rectangleSize, Point nextSpiralPoint) | ||
| => new Point(nextSpiralPoint.X - rectangleSize.Width / 2, nextSpiralPoint.Y - rectangleSize.Height / 2); | ||
|
|
||
| public CircularCloudLayouter(Point center) | ||
| { | ||
| spiral = new Spiral.Spiral(center); | ||
| Center = center; | ||
| cloudBorders = new Rectangle(0, 0, center.X * 2, center.Y * 2); | ||
| PlacedRectangles = new List<Rectangle>(); | ||
| } | ||
|
|
||
| public Rectangle PutNextRectangle(Size rectangleSize) | ||
| { | ||
| if (rectangleSize.Height <= 0 || rectangleSize.Width <= 0) | ||
| throw new ArgumentException($"Size must be positive {rectangleSize}"); | ||
|
|
||
| var nextRectangle = FindNextRectanglePosition(rectangleSize); | ||
|
|
||
| if (nextRectangle.IsEmpty) return nextRectangle; | ||
|
|
||
| nextRectangle = MoveToCenter(nextRectangle); | ||
| PlacedRectangles.Add(nextRectangle); | ||
| return nextRectangle; | ||
| } | ||
|
|
||
| private Rectangle FindNextRectanglePosition(Size rectangleSize) | ||
| { | ||
| var nextSpiralPoint = spiral.GetNextSpiralPoint(); | ||
| var nextRectangle = new Rectangle(GetRectangleCenterLocation(rectangleSize, nextSpiralPoint), rectangleSize); | ||
|
|
||
| while (!IsInValidPosition(nextRectangle)) | ||
| { | ||
| nextSpiralPoint = spiral.GetNextSpiralPoint(); | ||
| nextRectangle = new Rectangle(GetRectangleCenterLocation(rectangleSize, nextSpiralPoint), rectangleSize); | ||
| if (!cloudBorders.Contains(nextSpiralPoint)) | ||
| throw new ArgumentException("Can't place rectangle because cloud is too small"); | ||
| } | ||
|
|
||
| return nextRectangle; | ||
| } | ||
|
|
||
| private Rectangle MoveToCenter(Rectangle rectangle) | ||
| { | ||
| var newRectangle = Rectangle.Empty; | ||
| while (rectangle != newRectangle) | ||
| { | ||
| if(!newRectangle.IsEmpty) | ||
| rectangle = newRectangle; | ||
| var vectorToCenter = center - new Size(rectangle.GetCenter()); | ||
| newRectangle = TryMove(rectangle, vectorToCenter.SnapByX()); | ||
| newRectangle = TryMove(newRectangle, vectorToCenter.SnapByY()); | ||
| } | ||
| return rectangle; | ||
| } | ||
|
|
||
| private Rectangle TryMove(Rectangle rectangle, Point shift) | ||
| { | ||
| var newRect = new Rectangle(rectangle.Location + new Size(shift), rectangle.Size); | ||
| var isInValidPosition = IsInValidPosition(newRect); | ||
| if (isInValidPosition) | ||
| rectangle = newRect; | ||
| return rectangle; | ||
| } | ||
|
|
||
| public Bitmap ToBitmap() | ||
| { | ||
| var image = new Bitmap(cloudBorders.Width, cloudBorders.Height); | ||
| var g = Graphics.FromImage(image); | ||
| g.FillRectangle(Brushes.Black, cloudBorders); | ||
|
|
||
| foreach (var rectangle in PlacedRectangles) | ||
| { | ||
| g.FillRectangle(Brushes.Aquamarine, rectangle); | ||
| g.DrawRectangle(Pens.Blue, rectangle); | ||
| } | ||
| g.DrawRectangle(Pens.Red, new Rectangle(center, new Size(1, 1))); | ||
|
|
||
| return image; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,18 @@ | ||
| using System; | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization | ||
| { | ||
| public static class PointExtensions | ||
|
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 static Point SnapByX(this Point p) | ||
| { | ||
| return new Point(p.X / (p.X != 0 ? Math.Abs(p.X) : 1), 0); | ||
| } | ||
|
|
||
| public static Point SnapByY(this Point p) | ||
| { | ||
| return new Point(0, p.Y / (p.Y != 0 ? Math.Abs(p.Y) : 1)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,12 @@ | ||
| namespace TagsCloudVisualization | ||
| { | ||
| static class Program | ||
| { | ||
| /// <summary> | ||
| /// Главная точка входа для приложения. | ||
| /// </summary> | ||
| static void Main() | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| ###Examples: | ||
|
|
||
|  | ||
|
|
||
|  |
This file contains hidden or 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 TagsCloudVisualization | ||
| { | ||
| public static class RectangleExtension | ||
| { | ||
| public static Point GetCenter(this Rectangle rect) => rect.Location + new Size(rect.Width / 2, rect.Height / 2); | ||
| } | ||
| } |
This file contains hidden or 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,40 @@ | ||
| using System; | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization.Spiral | ||
| { | ||
| public class Spiral | ||
| { | ||
| private readonly Point center; | ||
| private readonly double deltaAngle; | ||
| private readonly double deltaRadius; | ||
| private Point currentPoint; | ||
| private double currentAngle; | ||
| private double currentRadius; | ||
|
|
||
| public double CurrentAngle => currentAngle; | ||
| public double CurrentRadius => currentRadius; | ||
|
|
||
| public Spiral(Point center, double deltaAngle = Math.PI / 180, double deltaRadius = 0.001) | ||
| { | ||
| this.center = center; | ||
| this.deltaAngle = deltaAngle; | ||
| this.deltaRadius = deltaRadius; | ||
| currentPoint = this.center; | ||
| currentAngle = 0; | ||
| currentRadius = 0; | ||
| } | ||
|
|
||
| public Point GetNextSpiralPoint() | ||
| { | ||
| var prevPoint = currentPoint; | ||
| currentAngle += deltaAngle; | ||
| currentRadius += deltaRadius; | ||
|
|
||
| var newX = (int) (currentRadius * Math.Cos(currentAngle) + center.X); | ||
| var newY = (int) (currentRadius * Math.Sin(currentAngle) + center.Y); | ||
| currentPoint = new Point(newX, newY); | ||
| return prevPoint; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| using System.Drawing; | ||
| using System.Windows.Forms; | ||
|
|
||
| namespace TagsCloudVisualization.Spiral | ||
| { | ||
| public class SpiralVizualizer : Form | ||
| { | ||
| private readonly Point center; | ||
|
|
||
| public SpiralVizualizer(Point center) | ||
| { | ||
| this.center = center; | ||
| } | ||
|
|
||
| protected override void OnPaint(PaintEventArgs e) | ||
| { | ||
| var graphics = e.Graphics; | ||
| var spiral = new Spiral(center); | ||
| var prev = spiral.GetNextSpiralPoint(); | ||
| for (var i = 0; i < 1000; i++) | ||
| { | ||
| var item = spiral.GetNextSpiralPoint(); | ||
| graphics.DrawLine(Pens.Orange, prev, item); | ||
| prev = item; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,91 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <PropertyGroup> | ||
| <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
| <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
| <ProjectGuid>{DFCD4863-382C-4B4D-A9EE-AF500921EE18}</ProjectGuid> | ||
| <OutputType>WinExe</OutputType> | ||
| <AppDesignerFolder>Properties</AppDesignerFolder> | ||
| <RootNamespace>TagsCloudVisualization</RootNamespace> | ||
| <AssemblyName>TagsCloudVisualization</AssemblyName> | ||
| <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
| <FileAlignment>512</FileAlignment> | ||
| <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugSymbols>true</DebugSymbols> | ||
| <DebugType>full</DebugType> | ||
| <Optimize>false</Optimize> | ||
| <OutputPath>bin\Debug\</OutputPath> | ||
| <DefineConstants>DEBUG;TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugType>pdbonly</DebugType> | ||
| <Optimize>true</Optimize> | ||
| <OutputPath>bin\Release\</OutputPath> | ||
| <DefineConstants>TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Reference Include="FluentAssertions, Version=4.15.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL"> | ||
| <HintPath>..\packages\FluentAssertions.4.15.0\lib\net45\FluentAssertions.dll</HintPath> | ||
| <Private>True</Private> | ||
| </Reference> | ||
| <Reference Include="FluentAssertions.Core, Version=4.15.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL"> | ||
| <HintPath>..\packages\FluentAssertions.4.15.0\lib\net45\FluentAssertions.Core.dll</HintPath> | ||
| <Private>True</Private> | ||
| </Reference> | ||
| <Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> | ||
| <HintPath>..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll</HintPath> | ||
| <Private>True</Private> | ||
| </Reference> | ||
| <Reference Include="System" /> | ||
| <Reference Include="System.Core" /> | ||
| <Reference Include="System.Xml.Linq" /> | ||
| <Reference Include="System.Data.DataSetExtensions" /> | ||
| <Reference Include="Microsoft.CSharp" /> | ||
| <Reference Include="System.Data" /> | ||
| <Reference Include="System.Deployment" /> | ||
| <Reference Include="System.Drawing" /> | ||
| <Reference Include="System.Net.Http" /> | ||
| <Reference Include="System.Windows.Forms" /> | ||
| <Reference Include="System.Xml" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Compile Include="CircularCloudLayouter.cs" /> | ||
| <Compile Include="PointExtensions.cs" /> | ||
| <Compile Include="Program.cs" /> | ||
| <Compile Include="RectangleExtension.cs" /> | ||
| <Compile Include="Spiral\Spiral.cs" /> | ||
| <Compile Include="Spiral\SpiralVizualizer.cs" /> | ||
| <Compile Include="Tests\CircularCloudLayouter_Should.cs" /> | ||
| <Compile Include="Tests\ImageForm.cs" /> | ||
| <Compile Include="Tests\PointExtensions_Should.cs" /> | ||
| <Compile Include="Tests\Spiral_Should.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Include="App.config" /> | ||
| <None Include="packages.config" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Content Include="ReadMe.md" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Folder Include="examples" /> | ||
| <Folder Include="Spiral" /> | ||
| </ItemGroup> | ||
| <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
| <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
| Other similar extension points exist, see Microsoft.Common.targets. | ||
| <Target Name="BeforeBuild"> | ||
| </Target> | ||
| <Target Name="AfterBuild"> | ||
| </Target> | ||
| --> | ||
| </Project> |
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.
кажется, что этот метод всё-таки можно ещё немного декомпозировать. Например, можно вытащить поиск подходящей позиции для прямоугольника.