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

Шмаков Данил #192

Open
wants to merge 11 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ x64/
x86/
bld/
[Bb]in/
![Bbin]/**/mystem.exe
[Oo]bj/

# Visual Studio 2015 cache/options directory
Expand Down
36 changes: 36 additions & 0 deletions TagCloud/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Drawing;

namespace TagCloud;

public class CircularCloudLayouter
{
private List<Rectangle> rectangles;
private ICloudShaper shaper;

public IReadOnlyList<Rectangle> Rectangles => rectangles;

public CircularCloudLayouter(ICloudShaper shaper)
{
rectangles = new List<Rectangle>();
this.shaper = shaper;
}

public Rectangle PutNextRectangle(Size size)
{
if (size.Width <= 0)
throw new ArgumentException("Size width must be positive number");
if (size.Height <= 0)
throw new ArgumentException("Size height must be positive number");

Rectangle rectangle = Rectangle.Empty;
foreach (var point in shaper.GetPossiblePoints())
{
rectangle = new Rectangle(point, size);
if (!Rectangles.Any(rect => rect.IntersectsWith(rectangle)))
break;
}

rectangles.Add(rectangle);
return rectangle;
}
}
9 changes: 9 additions & 0 deletions TagCloud/CloudDrawers/ICloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;
using TagCloud;

namespace TagCloudTests;

public interface ICloudDrawer
{
void Draw(List<TextRectangle> rectangle);
}
57 changes: 57 additions & 0 deletions TagCloud/CloudDrawers/TagCloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Drawing;
using TagCloud.Extensions;
using TagCloudTests;

namespace TagCloud;

public class TagCloudDrawer : ICloudDrawer
{
private readonly string path;
private readonly IColorSelector selector;
private readonly string name;
private readonly Font font;

private TagCloudDrawer(string path, string name, IColorSelector selector, Font font)
{
this.path = path;
this.selector = selector;
this.font = font;
this.name = name;
}

public void Draw(List<TextRectangle> rectangles)
{
if (rectangles.Count == 0)
throw new ArgumentException("Empty rectangles list");

var containingRect = rectangles
.Select(r => r.Rectangle)
.GetMinimalContainingRectangle();

using var bitmap = new Bitmap(containingRect.Width + 2, containingRect.Height + 2);
using var graphics = Graphics.FromImage(bitmap);

graphics.DrawStrings(
selector,
rectangles
.Select(rect => rect.OnLocation(-containingRect.X + rect.X, -containingRect.Y + rect.Y))
.ToArray()
);

SaveToFile(bitmap);
}

private void SaveToFile(Bitmap bitmap)
{
var pathToFile = @$"{path}\{name}";
bitmap.Save(pathToFile);
Console.WriteLine($"Tag cloud visualization saved to file {path}");
}

public static TagCloudDrawer Create(string path, string name, Font font, IColorSelector selector)
{
if (!Directory.Exists(path))
throw new ArgumentException("Directory does not exist");
return new TagCloudDrawer(path, name, selector, font);
}
}
6 changes: 6 additions & 0 deletions TagCloud/CloudShaperFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagCloud;

public class CloudShaperFactory
{

}
15 changes: 15 additions & 0 deletions TagCloud/ColorSelectors/ConstantColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Drawing;
using TagCloudTests;

namespace TagCloud;

public class ConstantColorSelector : IColorSelector
{
private Color color;
public ConstantColorSelector(Color color)
{
this.color = color;
}

public Color PickColor() => color;
}
8 changes: 8 additions & 0 deletions TagCloud/ColorSelectors/IColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagCloudTests;

public interface IColorSelector
{
Color PickColor();
}
22 changes: 22 additions & 0 deletions TagCloud/ColorSelectors/RandomColorSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;

namespace TagCloudTests;

public class RandomColorSelector : IColorSelector
{
private readonly Random random;

public RandomColorSelector()
{
random = new Random(DateTime.Now.Microsecond);
}

public Color PickColor()
{
return Color.FromArgb(255,
random.Next(0, 255),
random.Next(0, 255),
random.Next(0, 255)
);
}
}
22 changes: 22 additions & 0 deletions TagCloud/Extensions/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;
using System.Text.RegularExpressions;

namespace TagCloud.Extensions;

public static class ColorConverter
{
public static bool TryConvert(string hexString, out Color color)
{
color = default;
var colorHexRegExp = new Regex(@"^#([A-Fa-f0-9]{6})$");
if (colorHexRegExp.Count(hexString) != 1)
return false;
var rgbValue = hexString
.Replace("#", "")
.Chunk(2)
.Select(chars => Convert.ToInt32(new string(chars), 16))
.ToArray();
color = Color.FromArgb(rgbValue[0], rgbValue[1], rgbValue[2]);
return true;
}
}
27 changes: 27 additions & 0 deletions TagCloud/Extensions/GraphicsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Drawing;
using TagCloudTests;

namespace TagCloud.Extensions;

public static class GraphicsExtensions
{
public static void DrawRectangles(this Graphics graphics, IColorSelector selector, Rectangle[] rectangles, int lineWidth = 1)
{
using var pen = new Pen(selector.PickColor(), lineWidth);
foreach (var rectangle in rectangles)
{
graphics.DrawRectangle(pen, rectangle);
pen.Color = selector.PickColor();
}
}

public static void DrawStrings(this Graphics graphics, IColorSelector selector, TextRectangle[] rectangles)
{
using var brush = new SolidBrush(selector.PickColor());
foreach (var rectangle in rectangles)
{
graphics.DrawString(rectangle.Text, rectangle.Font, brush, rectangle.X, rectangle.Y);
brush.Color = selector.PickColor();
}
}
}
11 changes: 11 additions & 0 deletions TagCloud/Extensions/PointExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Drawing;

namespace TagCloud.Extensions;

public static class PointExtension
{
public static double GetDistanceTo(this Point first, Point second)
{
return Math.Sqrt((first.X - second.X) * (first.X - second.X) + (first.Y - second.Y) * (first.Y - second.Y));
}
}
36 changes: 36 additions & 0 deletions TagCloud/Extensions/RectangleEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Drawing;

namespace TagCloud.Extensions;

public static class RectangleEnumerableExtensions
{
public static bool HasIntersectedRectangles(this IEnumerable<Rectangle> rectangles)
{
return rectangles
.SelectMany(
(x, i) => rectangles.Skip(i + 1),
(x, y) => Tuple.Create(x, y)
)
.Any(tuple => tuple.Item1.IntersectsWith(tuple.Item2));
}

public static Rectangle GetMinimalContainingRectangle(this IEnumerable<Rectangle> rectangles)
{
int minX = int.MaxValue, minY = int.MaxValue;
int maxX = int.MinValue, maxY = int.MinValue;

foreach (var rectangle in rectangles)
{
if (rectangle.X < minX)
minX = rectangle.X;
if (rectangle.Y < minY)
minY = rectangle.Y;
if (rectangle.Right > maxX)
maxX = rectangle.Right;
if (rectangle.Bottom > maxY)
maxY = rectangle.Bottom;
}

return new Rectangle(minX, minY, maxX - minX, maxY- minY);
}
}
8 changes: 8 additions & 0 deletions TagCloud/ICloudShaper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagCloud;

public interface ICloudShaper
{
IEnumerable<Point> GetPossiblePoints();
}
61 changes: 61 additions & 0 deletions TagCloud/Option.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using CommandLine;

namespace TagCloudApplication;

public class Options
{
[Option('d', "destination", HelpText = "Set destination path.", Default = @"..\..\..\Results")]
public string DestinationPath { get; set; }

[Option('s', "source", HelpText = "Set source path.", Default = @"..\..\..\Results\text.txt")]
public string SourcePath { get; set; }

[Option('n', "name", HelpText = "Set name.", Default = "default.png")]
public string Name { get; set; }

[Option('c', "color",
HelpText = """
Set color.
random - Random colors
#F0F0F0 - Color hex code
""",
Default = "random")]
public string ColorScheme { get; set; }

[Option('f', "font", HelpText = "Set font.", Default = "Arial")]
public string Font { get; set; }

[Option("size", HelpText = "Set font size.", Default = 20)]
public int FontSize { get; set; }

[Option("unusedParts",
HelpText = """
Set unused parts of speech.
A - прилагательное
ADV - наречие
ADVPRO - местоименное наречие
ANUM - числительное-прилагательное
APRO - местоимение-прилагательное
COM - часть композита - сложного слова
CONJ - союз
INTJ - междометие
NUM - числительное
PART - частица
PR - предлог
S - существительное
SPRO - местоимение-существительное
V - глагол
""",
Default = new[] { "PR", "PART", "CONJ", "INTJ" })]
public string[] UnusedPartsOfSpeech { get; set; }

[Option("density", HelpText = "Set density.", Default = 0.1)]
public double Density { get; set; }

[Option("width", HelpText = "Set width.", Default = 100)]
public int Width { get; set; }


[Option("height", HelpText = "Set height.", Default = 100)]
public int Height { get; set; }
}
Loading