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

Яценко Ирина #219

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cs/BowlingGame/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Roll(int pins)

public int GetScore()
{
throw new NotImplementedException();
return 0;
}
}

Expand Down
5 changes: 5 additions & 0 deletions cs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
![Getting Started](./images/50rectanglesR.jpg)

![Getting Started](./images/150rectangles.jpg)

![Getting Started](./images/500.jpg)
14 changes: 14 additions & 0 deletions cs/TagsCloudVisualization/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
51 changes: 51 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Drawing;
using System.Linq;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter : ICircularCloudLayouter
{
private readonly Cloud cloud;
private readonly IPointDistributor distributor;

public CircularCloudLayouter(Point center, IPointDistributor type)
{
cloud = new Cloud(center);
distributor = type;
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
throw new ArgumentException();

if (cloud.Rectangles.Count == 0)
return AddToCenterPosition(rectangleSize);

var rectangle = new Rectangle(distributor.GetPosition(), rectangleSize);

while (HaveIntersection(rectangle))
{
rectangle.Location = distributor.GetPosition();
}

cloud.Rectangles.Add(rectangle);

return rectangle;
dmnovikova marked this conversation as resolved.
Show resolved Hide resolved
}

private Rectangle AddToCenterPosition(Size rectangleSize)
{
dmnovikova marked this conversation as resolved.
Show resolved Hide resolved
var newRectangle = new Rectangle(new Point(cloud.Center.X - rectangleSize.Width / 2,
cloud.Center.Y - rectangleSize.Height / 2), rectangleSize);

cloud.Rectangles.Add(newRectangle);

return newRectangle;
}

private bool HaveIntersection(Rectangle newRectangle) =>
cloud.Rectangles.Any(rectangle => rectangle.IntersectsWith(newRectangle));
}
}
16 changes: 16 additions & 0 deletions cs/TagsCloudVisualization/Cloud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Drawing;

namespace TagsCloudVisualization
{
public class Cloud
{
public Cloud(Point center)
{
Center = center;
}

public readonly Point Center;
public readonly List<Rectangle> Rectangles = new();
}
}
9 changes: 9 additions & 0 deletions cs/TagsCloudVisualization/ICircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace TagsCloudVisualization
{
public interface ICircularCloudLayouter
{
Rectangle PutNextRectangle(Size rectangleSize);
}
}
9 changes: 9 additions & 0 deletions cs/TagsCloudVisualization/IPointDistributor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace TagsCloudVisualization
{
public interface IPointDistributor
{
Point GetPosition();
}
}
15 changes: 15 additions & 0 deletions cs/TagsCloudVisualization/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Drawing;

namespace TagsCloudVisualization
{
internal class Program
{
public const int ImageWidth = 1000;
public const int ImageHeight = 1000;

public static void Main(string[] args)
{
new CircularCloudLayouter(new Point(ImageWidth / 2, ImageHeight / 2), new Spiral());
}
}
}
36 changes: 36 additions & 0 deletions cs/TagsCloudVisualization/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Общие сведения об этой сборке предоставляются следующим набором
// набора атрибутов. Измените значения этих атрибутов для изменения сведений,
// связанные с этой сборкой.
[assembly: AssemblyTitle("TagsCloudVisualization")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TagsCloudVisualization")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми
// для компонентов COM. Если необходимо обратиться к типу в этой сборке через
// из модели COM задайте для атрибута ComVisible этого типа значение true.
[assembly: ComVisible(false)]

// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM
[assembly: Guid("4f909c58-0835-4f9f-ba74-9530c220d676")]

// Сведения о версии сборки состоят из указанных ниже четырех значений:
//
// Основной номер версии
// Дополнительный номер версии
// Номер сборки
// Номер редакции
//
// Можно задать все значения или принять номера сборки и редакции по умолчанию
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
48 changes: 48 additions & 0 deletions cs/TagsCloudVisualization/Spiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Drawing;

namespace TagsCloudVisualization
{
public class Spiral : IPointDistributor
{
public Spiral()
{
step = 1;
deltaAngle = 0.1;
center = new Point(0, 0);
}

public Spiral(int step, Point center, double deltaAngle)
{
this.step = step;
this.center = center;
this.deltaAngle = deltaAngle;
}

private readonly int step;
private double angle;
private readonly double deltaAngle;
private Point center;
private bool centerOnPoint;

public Point GetPosition()
{
if (!centerOnPoint)
{
centerOnPoint = true;
return center;
}

angle += deltaAngle;

var k = step / (2 * Math.PI);
var radius = k * angle;

var position = new Point(
center.X + (int)(Math.Cos(angle) * radius),
center.Y + (int)(Math.Sin(angle) * radius));

return position;
}
}
}
106 changes: 106 additions & 0 deletions cs/TagsCloudVisualization/TagCloudRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;

namespace TagsCloudVisualization
{
public class TagCloudRenderer
{
private Bitmap bitmap;
private Graphics graphics;
private Pen pen;

public void DrawCloud(RectangleF[] rectangles, VisualizingSettings settings)
{
var smallestSizeOfRectangles = GetMinPoints(rectangles);
var unscaledImageSize = GetImageSizeWithRealSizeRectangles(rectangles, smallestSizeOfRectangles);

if (unscaledImageSize.Height <= settings.ImageSize.Height &&
unscaledImageSize.Width <= settings.ImageSize.Width)
{
bitmap = new Bitmap(settings.ImageSize.Width, settings.ImageSize.Height);
CustomizeGraphics(settings);
DrawRealSizeCloud(settings, rectangles);
return;
}

bitmap = new Bitmap(unscaledImageSize.Width, unscaledImageSize.Height);
CustomizeGraphics(settings);
DrawScaleCloud(settings, rectangles, unscaledImageSize, smallestSizeOfRectangles);
}

private void CustomizeGraphics(VisualizingSettings settings)
{
pen = new Pen(settings.PenColor);
graphics = Graphics.FromImage(bitmap);
graphics.Clear(settings.BackgroundColor);
}

private void DrawScaleCloud(
VisualizingSettings settings,
RectangleF[] rectangles,
Size unscaledImageSize,
Size smallestSizeOfRectangles)
{
graphics.TranslateTransform(Math.Abs(smallestSizeOfRectangles.Width), Math.Abs(smallestSizeOfRectangles.Height));
graphics.DrawRectangles(pen, rectangles);

var coefficient = GetScaleCoefficients(unscaledImageSize, settings.ImageSize);
graphics.ScaleTransform(coefficient.sx, coefficient.sy);

bitmap.Save(settings.ImageName + ".png", ImageFormat.Png);
}

private void DrawRealSizeCloud(VisualizingSettings settings, RectangleF[] rectangles)
{
graphics.Clear(settings.BackgroundColor);
graphics.DrawRectangles(pen, rectangles);

bitmap.Save(settings.ImageName + ".png", ImageFormat.Png);
}

private (float sx, float sy) GetScaleCoefficients(Size unscaledImageSize, Size imageSize)
{
var sx = unscaledImageSize.Width / imageSize.Width;
var sy = unscaledImageSize.Height / imageSize.Height;

return (sx, sy);
}

private Size GetImageSizeWithRealSizeRectangles(RectangleF[] rectangles, Size smallestSizeOfRectangles)
{
var maxSizesOfAllRectangles = GetMaxPoints(rectangles);

var height = maxSizesOfAllRectangles.Height - smallestSizeOfRectangles.Height + 1;
var width = maxSizesOfAllRectangles.Width - smallestSizeOfRectangles.Width + 1;

return new Size(width, height);
}

private Size GetMinPoints(RectangleF[] rectangles)
{
var minHeight = (int)rectangles.Select(x => x.Y).Min();
var minWidth = (int)rectangles.Select(x => x.X).Min();

return new Size(minWidth, minHeight);
}

private Size GetMaxPoints(RectangleF[] rectangles)
{
var maxHeight = float.MinValue;
var maxWidth = float.MinValue;

foreach (var rectangle in rectangles)
{
if (rectangle.Y + rectangle.Height > maxHeight)
maxHeight = rectangle.Y + rectangle.Height;

if (rectangle.X + rectangle.Width > maxWidth)
maxWidth = rectangle.X + rectangle.Width;
}

return new Size((int)maxWidth, (int)maxHeight);
}
}
}
Loading