-
Notifications
You must be signed in to change notification settings - Fork 229
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
Бабинцев Григорий #204
Open
qreaqtor
wants to merge
2
commits into
kontur-courses:master
Choose a base branch
from
qreaqtor:homework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Бабинцев Григорий #204
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Drawing; | ||
using TagCloud.Config; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.CloudDrawer | ||
{ | ||
public class CloudDrawer : ICloudDrawer | ||
{ | ||
private readonly AppConfig appConfig; | ||
|
||
public CloudDrawer(AppConfig appConfig) | ||
{ | ||
this.appConfig = appConfig; | ||
} | ||
|
||
public Result<Bitmap> DrawWords(IEnumerable<WordTag> words) | ||
{ | ||
var imageConfig = appConfig.ImageConfig; | ||
|
||
var bitmap = new Bitmap(imageConfig.Width, imageConfig.Height); | ||
|
||
var graphics = Graphics.FromImage(bitmap); | ||
|
||
graphics.Clear(imageConfig.Background); | ||
|
||
foreach (var word in words) | ||
{ | ||
var colorResult = appConfig.GetNextColor(); | ||
if (!colorResult.IsSuccess) | ||
return Result.Fail<Bitmap>(colorResult.Error); | ||
|
||
var brush = new SolidBrush(colorResult.Value); | ||
graphics.DrawString(word.Content, word.Font, brush, word.Rectangle); | ||
} | ||
|
||
return bitmap; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Drawing; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.CloudDrawer | ||
{ | ||
public interface ICloudDrawer | ||
{ | ||
Result<Bitmap> DrawWords(IEnumerable<WordTag> words); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Drawing; | ||
using TagCloud.Config; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.CloudLayouter | ||
{ | ||
public class CircularCloudLayouter : ICloudLayouter | ||
{ | ||
private AppConfig appConfig; | ||
|
||
public CircularCloudLayouter(AppConfig appConfig) | ||
{ | ||
this.appConfig = appConfig; | ||
} | ||
|
||
public Result<RectangleF> GetPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize) | ||
{ | ||
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
return Result.Fail<RectangleF>("the width and height of the rectangle must be positive numbers"); | ||
|
||
return Result.Of(() =>FindPossibleNextRectangle(cloudRectangles, rectangleSize)); | ||
} | ||
|
||
private RectangleF FindPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize) | ||
{ | ||
var cloudLayouterConfig = appConfig.CloudLayouterConfig; | ||
var imageConfig = appConfig.ImageConfig; | ||
|
||
var center = new Point(imageConfig.Width / 2, imageConfig.Height / 2); | ||
|
||
var radius = cloudLayouterConfig.Radius; | ||
var angle = cloudLayouterConfig.Angle; | ||
|
||
while (true) | ||
{ | ||
var point = new Point( | ||
(int)(center.X + radius * Math.Cos(angle)), | ||
(int)(center.Y + radius * Math.Sin(angle)) | ||
); | ||
|
||
var possibleRectangle = new RectangleF(point, rectangleSize); | ||
|
||
if (!cloudRectangles.Any(rectangle => rectangle.IntersectsWith(possibleRectangle))) | ||
return possibleRectangle; | ||
|
||
angle += cloudLayouterConfig.DeltaAngle; | ||
radius += cloudLayouterConfig.DeltaRadius; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Drawing; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.CloudLayouter | ||
{ | ||
public interface ICloudLayouter | ||
{ | ||
Result<RectangleF> GetPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Drawing; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.Config | ||
{ | ||
public class AppConfig | ||
{ | ||
public CloudLayouterConfig CloudLayouterConfig { get; set; } | ||
|
||
public FontConfig FontConfig { get; set; } | ||
|
||
public ImageConfig ImageConfig { get;set; } | ||
|
||
private Dictionary<ColorScheme, Func<Result<Color>>> ColorSchemas; | ||
|
||
private Random rnd; | ||
|
||
private int index; | ||
|
||
public AppConfig() | ||
{ | ||
CloudLayouterConfig = DefaultConfigs.DefaultCloudLayouterConfig; | ||
|
||
FontConfig = DefaultConfigs.DefaultFontConfig; | ||
|
||
ImageConfig = DefaultConfigs.DefaultImageConfig; | ||
|
||
rnd = new Random(); | ||
index = -1; | ||
|
||
ColorSchemas = new Dictionary<ColorScheme, Func<Result<Color>>> | ||
{ | ||
{ColorScheme.Random, ColorSchemeRandom}, | ||
{ColorScheme.RoundRobin, ColorSchemeRoundRobin}, | ||
}; | ||
} | ||
|
||
public Result<Color> GetNextColor() => ColorSchemas[ImageConfig.ColorScheme](); | ||
|
||
private Result<Color> ColorSchemeRandom() | ||
{ | ||
try | ||
{ | ||
index = rnd.Next(0, ImageConfig.WordColors.Length); | ||
|
||
return ImageConfig.WordColors[index]; | ||
} | ||
catch (Exception e) | ||
{ | ||
return Result.Fail<Color>($"Не удалось выбрать цвет: {e.Message}"); | ||
} | ||
} | ||
|
||
private Result<Color> ColorSchemeRoundRobin() | ||
{ | ||
index = (index + 1) % ImageConfig.WordColors.Length; | ||
|
||
return ImageConfig.WordColors[index]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Drawing; | ||
using TagCloud.Models; | ||
|
||
namespace TagCloud.Config | ||
{ | ||
public static class DefaultConfigs | ||
{ | ||
public static ImageConfig DefaultImageConfig | ||
{ | ||
get { return new ImageConfig(400, 400, Color.Black, [Color.Red, Color.Blue, Color.Green, Color.White], ColorScheme.RoundRobin); } | ||
} | ||
|
||
public static CloudLayouterConfig DefaultCloudLayouterConfig | ||
{ | ||
get { return new CloudLayouterConfig(2, 5, 2, 5); } | ||
} | ||
|
||
public static FontConfig DefaultFontConfig | ||
{ | ||
get { return new FontConfig("GenericSerif", 14, FontStyle.Regular); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace TagCloud.Models | ||
{ | ||
public record struct CloudLayouterConfig(int Radius, int DeltaRadius, int Angle, int DeltaAngle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace TagCloud.Models | ||
{ | ||
public enum ColorScheme | ||
{ | ||
RoundRobin, | ||
Random | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using System.Drawing; | ||
|
||
namespace TagCloud.Models | ||
{ | ||
public record struct FontConfig(string FontFamily, int FontSize, FontStyle Style); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using System.Drawing; | ||
|
||
namespace TagCloud.Models | ||
{ | ||
public record struct ImageConfig(int Width, int Height, Color Background, Color[] WordColors, ColorScheme ColorScheme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace TagCloud.Models | ||
{ | ||
public record struct Word(string Content, float FontSize); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using System.Drawing; | ||
|
||
namespace TagCloud.Models | ||
{ | ||
public record struct WordTag(RectangleF Rectangle, string Content, Font Font); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Autofac; | ||
using TagCloud.UI; | ||
using TagCloud.CloudDrawer; | ||
using TagCloud.Config; | ||
using TagCloud.ReadWriter; | ||
using TagCloud.TagCloudService; | ||
using TagCloud.WordsProcessor; | ||
using TagCloud.CloudLayouter; | ||
|
||
var builder = new ContainerBuilder(); | ||
|
||
builder.RegisterType<AppConfig>().AsSelf().SingleInstance(); | ||
|
||
builder.RegisterType<ConsoleReadWriter>().As<IReadWriter>().SingleInstance(); | ||
builder.RegisterType<FileReader>().As<IFileReader>().SingleInstance(); | ||
builder.RegisterType<WordProcessor>().As<IWordProcessor>().SingleInstance(); | ||
builder.RegisterType<CircularCloudLayouter>().As<ICloudLayouter>().SingleInstance(); | ||
builder.RegisterType<TagCloudService>().As<ITagCloudService>().SingleInstance(); | ||
builder.RegisterType<CloudDrawer>().As<ICloudDrawer>().SingleInstance(); | ||
|
||
builder.RegisterType<ConsoleUI>().AsSelf().SingleInstance(); | ||
|
||
var container = builder.Build(); | ||
|
||
var ui = container.Resolve<ConsoleUI>(); | ||
ui.Start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace TagCloud.ReadWriter | ||
{ | ||
public class ConsoleReadWriter : IReadWriter | ||
{ | ||
public TOut ReadLine<TOut>(string beforeInput, Func<string, Result<TOut>> convert) | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
WriteLine(beforeInput); | ||
|
||
var input = Console.ReadLine().Trim(); | ||
|
||
var result = convert(input); | ||
|
||
if (result.IsSuccess) | ||
return result.Value; | ||
|
||
WriteLine(result.Error); | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine($"Ошибка: {ex.Message}"); | ||
} | ||
} | ||
} | ||
|
||
public void WriteLine(string input) => Console.WriteLine(input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace TagCloud.ReadWriter | ||
{ | ||
public class FileReader : IFileReader | ||
{ | ||
public Result<IEnumerable<string>> ReadDataFromFile(string path) | ||
{ | ||
//if(!File.Exists(path)) | ||
// return Result.Fail<IEnumerable<string>>($"Файл {path} не найден"); | ||
|
||
return Result.Of(() => File.ReadAllLines(path).Select(line => line.Trim().ToLower())).RefineError($"Ошибка при чтении файла: {path}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace TagCloud.ReadWriter | ||
{ | ||
public interface IFileReader | ||
{ | ||
Result<IEnumerable<string>> ReadDataFromFile(string path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace TagCloud.ReadWriter | ||
{ | ||
public interface IReadWriter | ||
{ | ||
TOut ReadLine<TOut>(string beforeInputMsg, Func<string, Result<TOut>> convert); | ||
|
||
void WriteLine(string msg); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не тащи в Pull Request-ы закомментированный код.
Это мусор, который в лучшем случае затрудняет чтение. В худшем производит вопросы, почему код в репозитории закомментирован и надо ли его раскомментировать