Skip to content
Open

HW #1

Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

.idea/*
# User-specific files
*.suo
*.user
Expand Down Expand Up @@ -195,4 +196,4 @@ FakesAssemblies/
# Visual Studio 6 workspace options file
*.opt

*Solved.cs
*Solved.cs
6 changes: 6 additions & 0 deletions TagsCloudVisualization/App.config
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>
112 changes: 112 additions & 0 deletions TagsCloudVisualization/CircularCloudLayouter.cs
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

кажется, что этот метод всё-таки можно ещё немного декомпозировать. Например, можно вытащить поиск подходящей позиции для прямоугольника.

{
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;
}
}
}
18 changes: 18 additions & 0 deletions TagsCloudVisualization/PointExtensions.cs
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

эти Extension методы конечно простые, но всё равно лучше было бы их протестировать

{
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));
}
}
}
12 changes: 12 additions & 0 deletions TagsCloudVisualization/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TagsCloudVisualization
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
static void Main()
{
}
}
}
5 changes: 5 additions & 0 deletions TagsCloudVisualization/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
###Examples:

![Ex1](https://github.com/Griboedoff/tdd/blob/master/TagsCloudVisualization/examples/1477297245.png?raw=true "Example1")

![Ex2](https://github.com/Griboedoff/tdd/blob/master/TagsCloudVisualization/examples/1477297261.png?raw=true "Example2")
9 changes: 9 additions & 0 deletions TagsCloudVisualization/RectangleExtension.cs
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);
}
}
40 changes: 40 additions & 0 deletions TagsCloudVisualization/Spiral/Spiral.cs
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;
}
}
}
28 changes: 28 additions & 0 deletions TagsCloudVisualization/Spiral/SpiralVizualizer.cs
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;
}
}
}
}
91 changes: 91 additions & 0 deletions TagsCloudVisualization/TagsCloudVisualization.csproj
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>
Loading