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 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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);
}
}
}
90 changes: 90 additions & 0 deletions cs/TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

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 CircularCloudLayouter(Point center, IDistribution distribution)
{
if (center != distribution.Center)
throw new ArgumentException();

Center = center;
SolarHunger marked this conversation as resolved.
Show resolved Hide resolved
Distribution = distribution;
WordPositions = new List<Rectangle>();
}

public Point Center { get; }
public List<Rectangle> WordPositions { get; }
public IDistribution Distribution { get; }


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

var rectanglePosition = Distribution.GetNextPoint();
var currRectangle = new Rectangle(rectanglePosition, rectangleSize);

while (CheckIntersection(currRectangle))
{
rectanglePosition = Distribution.GetNextPoint();
currRectangle.Location = rectanglePosition;
}

currRectangle = ComperessRectangle(currRectangle);
WordPositions.Add(currRectangle);
return currRectangle;
}


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 => currRectangle.IntersectsWith(rec));
}


public Rectangle ComperessRectangle(Rectangle rectangle)

This comment was marked as resolved.

{
var changes = 1;
while (changes > 0)
{
rectangle = CompressByAxis(rectangle, true, out changes);
rectangle = CompressByAxis(rectangle, false, out changes);
}

return rectangle;
}


private Rectangle CompressByAxis(Rectangle rectangle, bool isByX, out int changes)
{
changes = 0;
var stepX = rectangle.X < Center.X ? 1 : -1;
var stepY = rectangle.Y < Center.Y ? 1 : -1;

while ((isByX && rectangle.X != Center.X) ||
(!isByX && rectangle.Y != Center.Y))
{
var newRectangle = isByX
? new Rectangle(new Point(rectangle.X + stepX, rectangle.Y), rectangle.Size)
: new Rectangle(new Point(rectangle.X, rectangle.Y + stepY), rectangle.Size);

if (!CheckIntersection(newRectangle))
{
rectangle = newRectangle;
changes++;
continue;
}

break;
}

return rectangle;
}
}
}
68 changes: 68 additions & 0 deletions cs/TagsCloudVisualization/CloudLayouterDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace TagsCloudVisualization
Copy link

Choose a reason for hiding this comment

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

Нет абстракции - какого-то интерфейса

{
public class CloudLayouterDrawer
{
private Random random;
public int margin;
public int imageWidth;

This comment was marked as resolved.

public int imageHeight;

public CloudLayouterDrawer(int margin)
Copy link

Choose a reason for hiding this comment

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

Цвет фона можно сделать параметром рисовальщика

{
this.margin= margin;

This comment was marked as resolved.

random = new Random();
}

public void DrawCloud(string filename, IEnumerable<Rectangle> rectangles)
{
if (!rectangles.Any())
throw new ArgumentException();
SetImageSize(rectangles);

var newRectanglesPositions = GetNewRectanglesPositions(rectangles);

using (var bitmap = new Bitmap(imageWidth, imageHeight))
Copy link

@gisinka gisinka Dec 10, 2023

Choose a reason for hiding this comment

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

можно упростить до using var bitmap = new Bitmap(imageWidth, imageHeight)

Copy link
Author

Choose a reason for hiding this comment

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

ругает версию шарпа

Copy link

Choose a reason for hiding this comment

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

Поднять - никто не запрещал)

{
using (var graphics = Graphics.FromImage(bitmap))
{

foreach (var rectangle in newRectanglesPositions)
{
var color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
var brush = new SolidBrush(color);
graphics.FillRectangle(brush, rectangle);
}

Copy link

Choose a reason for hiding this comment

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

Сохранение в файл отлично декомпозируется в отдельный метод

var projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName;
var filePath = Path.Combine(projectDirectory, "images", filename);
bitmap.Save(filePath, ImageFormat.Png);
}
}
}

private void SetImageSize(IEnumerable<Rectangle> rectangles)
{
var maxX = rectangles.Select(rec => rec.X + rec.Width / 2).Max();
var minX = rectangles.Select(rec => rec.X - rec.Width / 2).Min();
var maxY = rectangles.Select(rec => rec.Y + rec.Height / 2).Max();
var minY = rectangles.Select(rec => rec.Y - rec.Height / 2).Min();

imageWidth = maxX - minX + margin;
imageHeight = maxY - minY + margin;
}

private IEnumerable<Rectangle> GetNewRectanglesPositions(IEnumerable<Rectangle> rectangles)
{
return rectangles.Select(rec =>
new Rectangle(rec.X + imageWidth / 2, rec.Y + imageHeight / 2, rec.Width, rec.Height));
}

}
}
10 changes: 10 additions & 0 deletions cs/TagsCloudVisualization/IDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;

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

namespace TagsCloudVisualization
{
public class Program
{
private static void Main()
{

}
}
}
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")]
3 changes: 3 additions & 0 deletions cs/TagsCloudVisualization/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!["������ 1"](./images/example3.png)
!["������ 2"](./images/example1.png)
!["������ 3"](./images/example2.png)
39 changes: 39 additions & 0 deletions cs/TagsCloudVisualization/SpiralDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class SpiralDistribution : IDistribution
{
public Point Center { get; private set; }

This comment was marked as resolved.

public double Radius { get; private set; }
public double Angle { get; private set; }


public SpiralDistribution(Point center)
{
Center = center;

This comment was marked as resolved.

Copy link

Choose a reason for hiding this comment

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

throw new ArgumentException(); должен жить на отдельной строчке

Copy link

Choose a reason for hiding this comment

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

Считаю, что конструктор должен быть максимально простым, никаких проверок внутри не должно быть, его задача - инициализировать объект, если хочешь сделать проверку на входные параметры, то лучше сделать статичный метод который это будет проверять, и потом сам вызовет тебе приватный конструктор,
Ну и декомпозиция, все дела
Да и тесты так как то приятнее писать когда вызываешь какой нибудь метод Create вместо new

Как вариант валидацию выносить в отдельный приватный метод

Radius = 0;
Angle = 0;
}
public Point GetNextPoint()
{
var x = Radius * Math.Cos(Angle) + Center.X;
var y = Radius * Math.Sin(Angle) + Center.Y;

Angle += 0.1;

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

return new Point((int)x, (int)y);
}
}
}