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

Ренат Зубакин #222

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions cs/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.8" />
</startup>
</configuration>
81 changes: 81 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;

This comment was marked as resolved.

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using static NUnit.Framework.Constraints.Tolerance;

namespace TagsCloudVisualization
{
public class TagsCloudVisualization
{
public Point Center;
public List<Rectangle> WordPositions;
public double Radius;
public double Angle;

public TagsCloudVisualization(Point center)
{
Center = center;
Radius = 0;
Angle = 0;
WordPositions = new List<Rectangle>();
}

public Rectangle PutNextRectangle(Size rectangleSize)
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
{
while (true)
{
if (rectangleSize.Width < 1 || rectangleSize.Height < 1)
throw new ArgumentException();

var x = Radius * Math.Cos(Angle) + Center.X;
var y = Radius * Math.Sin(Angle)+ Center.Y;

var currRectangle = new Rectangle(new Point((int)x, (int)y), rectangleSize);

if (!CheckIntersection(currRectangle))
{
WordPositions.Add(currRectangle);
return currRectangle;
}

Angle += 0.1;

if (Angle >= Math.PI * 2)
{
Angle = 0;
Radius += 0.5;
}
}
}


public bool CheckIntersection(Rectangle currRectangle)
{
return WordPositions.Any(rec =>
{
var isRecIntersect = Rectangle.Intersect(rec,currRectangle);
return !isRecIntersect.IsEmpty;
});
}

public void Drawing()
{
int w = 10000;
int h = 10000;
Bitmap bitmap = new Bitmap(w, h);
Graphics graphics = Graphics.FromImage(bitmap);
Brush brush = new SolidBrush(Color.Blue);

// Рисование прямоугольников
foreach (var word in WordPositions)
{
var x = word.X + 5000;
var y = word.Y + 5000;
graphics.FillRectangle(brush, x,y,word.Width,word.Height);
}

bitmap.Save("C:\\Users\\hellw\\Desktop\\output.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}
131 changes: 131 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using static NUnit.Framework.Constraints.Tolerance;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

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

И тут нет интерфейса

{
public Point Center;
public List<Rectangle> WordPositions;
public double Radius;
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
public double Angle;

public CircularCloudLayouter(Point center)
{
Center = center;
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
Radius = 0;
Angle = 0;
WordPositions = new List<Rectangle>();
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
while (true)
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
{
if (rectangleSize.Width < 1 || rectangleSize.Height < 1)
throw new ArgumentException();

var x = Radius * Math.Cos(Angle) + Center.X;
var y = Radius * Math.Sin(Angle) + Center.Y;

var currRectangle = new Rectangle(new Point((int)x, (int)y), rectangleSize);

if (!CheckIntersection(currRectangle))
{
currRectangle = RectangleCompression(currRectangle);
WordPositions.Add(currRectangle);
return currRectangle;
}

Angle += 0.1;

if (Angle >= Math.PI * 2)
{
Angle = 0;
Radius += 0.1;
}
}
}

public bool CheckIntersection(Rectangle currRectangle)
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved

This comment was marked as resolved.

{
return WordPositions.Any(rec =>
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
{
var x = Math.Max(currRectangle.X, rec.X);
var num1 = Math.Min(currRectangle.X + currRectangle.Width, rec.X + rec.Width);
var y = Math.Max(currRectangle.Y, rec.Y);
var num2 = Math.Min(currRectangle.Y + currRectangle.Height, rec.Y + rec.Height);
var res = num1 > x && num2 > y ? new Rectangle(x, y, num1 - x, num2 - y) : Rectangle.Empty;
return !res.IsEmpty;
});
}

public Rectangle RectangleCompression(Rectangle rectangle)
{
var changes = 1;
while (changes > 0)
{
CompressionsAxis(ref rectangle, ref changes, Center.X,
(Point point) => point.X,
(Point point, int step) => new Point(point.X + step, point.Y));
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
CompressionsAxis(ref rectangle, ref changes, Center.Y,
(Point point) => point.Y,
(Point point, int step) => new Point(point.X, point.Y + step));
}

return rectangle;
}

public void CompressionsAxis(ref Rectangle rectangle, ref int changes, int targetCoord,
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
Func<Point, int> currCoord,
Func<Point, int, Point> changePoint)
{
var step = (currCoord(rectangle.Location) < targetCoord) ? 1 : -1;
changes = 0;
while (currCoord(rectangle.Location) != targetCoord)
{
var newRectangle =
new Rectangle(changePoint(new Point(rectangle.X, rectangle.Y), step), rectangle.Size);
if (!CheckIntersection(newRectangle))
{
rectangle = newRectangle;
changes += 1;
continue;
}

break;
}
}

public void Drawing(string name)
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException();

int w = 500;
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
int h = 500;

Bitmap bitmap = new Bitmap(w, h);
Graphics graphics = Graphics.FromImage(bitmap);
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
Random random = new Random();

foreach (var word in WordPositions)
{
Color randomColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
Brush brush = new SolidBrush(randomColor);
var x = word.X + w/2;
var y = word.Y + h/2;
graphics.FillRectangle(brush, x, y, word.Width, word.Height);
}

string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
string filePath = Path.Combine(projectDirectory, "images",name);
Console.WriteLine(filePath);
bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
102 changes: 102 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using NUnit.Framework;

namespace TagsCloudVisualization
{
[TestFixture]
public class CircularCloudLayouterTests
{
[Test]
public void CircularCloudLayouter_Initialize_Params()
{
var center = new Point(1, 2);
var tagsCloud = new CircularCloudLayouter(center);

Assert.AreEqual(0, tagsCloud.Radius);
Assert.AreEqual(0, tagsCloud.Angle);
Assert.AreEqual(0, tagsCloud.WordPositions.Count);
Assert.AreEqual(center, tagsCloud.Center);
}

[TestCaseSource(nameof(PutNextRectangleIncorrectArguments))]
public void PutNextRectangle_With_Incorrect_Arguments(Size rectangleSize, CircularCloudLayouter tagsCloud)
Copy link

Choose a reason for hiding this comment

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

Хороший путь говорить, что произойдет
в данном случае
PutNextRectangle_ThrowsArgumentException_WhenIncorrectArguments

{
Assert.Throws<ArgumentException>(() => tagsCloud.PutNextRectangle(rectangleSize));
}

[Test]
public void PutNextRectangleFirstRectanglePositionEqualCenter()
Copy link

Choose a reason for hiding this comment

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

PutNextRectangle_ShouldPlaceFirstOnCenter или что-нибудь такое, чтобы читаемость была лучше

{
var tagsCloud = new CircularCloudLayouter(new Point(4, 2));
tagsCloud.PutNextRectangle(new Size(3, 1));
Assert.AreEqual(tagsCloud.Center, tagsCloud.WordPositions[0].Location);
}

[TestCaseSource(nameof(CheckIntersectionCaseData))]
public bool CheckIntersectionTest(Size size1, Rectangle rectangle)
Copy link

Choose a reason for hiding this comment

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

size1?

{
var tagsCloud = new CircularCloudLayouter(new Point(0, 0));
tagsCloud.PutNextRectangle(size1);
return tagsCloud.CheckIntersection(rectangle);
}

[TestCaseSource(nameof(RectangleCompressionCaseData))]
public Point RectangleCompression(CircularCloudLayouter cloud, Rectangle rectangle)
{
return cloud.RectangleCompression(rectangle).Location;
}

private static IEnumerable<TestCaseData> PutNextRectangleIncorrectArguments()
Copy link

Choose a reason for hiding this comment

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

Хорошая практика уносить тестовые данные в отдельные файлы: Что-тоТамTestData

{
var tagsCloud = new CircularCloudLayouter(new Point());

yield return new TestCaseData(new Size(-1, 1), tagsCloud)
.SetName("PutNextReactangle_Throws_ArgumentException_When_Width_Is_Negative");

yield return new TestCaseData(new Size(1, -1), tagsCloud)
.SetName("PutNextReactangle_Throws_ArgumentException_When_Height_Is_Negative");

yield return new TestCaseData(new Size(0, 1), tagsCloud)
.SetName("PutNextReactangle_Throws_ArgumentException_When_Width_Is_Zero");

yield return new TestCaseData(new Size(1, 0), tagsCloud)
.SetName("PutNextReactangle_Throws_ArgumentException_When_Height_Is_Zero");
}

private static IEnumerable<TestCaseData> CheckIntersectionCaseData()
{
yield return new TestCaseData(new Size(4, 2), new Rectangle(new Point(1, 1), new Size(2, 2)))
.SetName(
"CheckIntersection_Return_True_When_Rectangle_Intersection_With_Any_Rectangle_In_CircularCloudLayouter")
.Returns(true);

yield return new TestCaseData(new Size(1, 1), new Rectangle(new Point(1, 0), new Size(1, 1)))
.SetName(
"CheckIntersection_Return_False_When_Rectangle_Have_Common_Side_With_Another_Rectangle")
.Returns(false);

yield return new TestCaseData(new Size(4, 2), new Rectangle(new Point(4, 4), new Size(2, 2)))
.SetName(
"CheckIntersection_Return_True_When_Rectangle_Intersection_With_Any_Rectangle_In_CircularCloudLayouter")
.Returns(false);
}

private static IEnumerable<TestCaseData> RectangleCompressionCaseData()
{
var cloudEmpty = new CircularCloudLayouter(new Point());
var cloudWithElements = new CircularCloudLayouter(new Point());
cloudWithElements.PutNextRectangle(new Size(1, 1));
var rectangle = new Rectangle(new Point(5, 5), new Size(1, 1));

yield return new TestCaseData(cloudEmpty, rectangle)
.SetName("RectangleCompression_When_Cloud_Is_Empty_Set_Rectangle_Position_On_Center")
.Returns(cloudEmpty.Center);

yield return new TestCaseData(cloudWithElements, rectangle)
.SetName("RectangleCompression_When_Cloud_Has_Rectangles_Set_Rectangle_Position_Closer_To_Center")
.Returns(new Point(0, 1));
}
}
}
17 changes: 17 additions & 0 deletions cs/TagsCloudVisualization/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class Program
{
static void Main()
{
Console.ReadKey();
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CircularCloudLayouter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CircularCloudLayouter")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("80edaa6b-8db4-4a7e-8d3d-7032262731b6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading