-
Notifications
You must be signed in to change notification settings - Fork 240
Вильданов Савелий #202
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
Open
Saveliy21
wants to merge
1
commit into
kontur-courses:master
Choose a base branch
from
Saveliy21:master
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
Вильданов Савелий #202
Changes from all commits
Commits
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 hidden or 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,17 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud2.CloudForms; | ||
|
|
||
| public class CircularSpiral(Point center) : ICloudForm | ||
| { | ||
| private double _angle; | ||
| private const double AngleStep = 0.01; | ||
|
|
||
| public Point GetNextPoint() | ||
| { | ||
| _angle += AngleStep; | ||
| var x = (int) (center.X + _angle * Math.Cos(_angle)); | ||
| var y = (int) (center.Y + _angle * Math.Sin(_angle)); | ||
| return new Point(x, y); | ||
| } | ||
| } |
This file contains hidden or 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 TagCloud2.CloudForms; | ||
|
|
||
| public enum Direction | ||
| { | ||
| Up, | ||
| Right, | ||
| Down, | ||
| Left, | ||
| } |
This file contains hidden or 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,16 @@ | ||
| namespace TagCloud2.CloudForms; | ||
|
|
||
| public static class DirectionExtension | ||
| { | ||
| public static Direction ClockwiseRotate(this Direction direction) | ||
| { | ||
| return direction switch | ||
| { | ||
| Direction.Up => Direction.Right, | ||
| Direction.Right => Direction.Down, | ||
| Direction.Down => Direction.Left, | ||
| Direction.Left => Direction.Up, | ||
| _ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null) | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud2.CloudForms; | ||
|
|
||
| public interface ICloudForm | ||
| { | ||
| public Point GetNextPoint(); | ||
| } |
This file contains hidden or 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,42 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud2.CloudForms; | ||
|
|
||
| public class SquareSpiral(Point centre) : ICloudForm | ||
| { | ||
| private const int Step = 1; | ||
| private int _stepCounter = 1; | ||
| private int _neededSteps = 1; | ||
| private Direction _direction = Direction.Up; | ||
| private Point _previus = centre; | ||
|
|
||
| public Point GetNextPoint() | ||
| { | ||
| _previus += GetOffsetSize(_direction); | ||
|
|
||
| _stepCounter--; | ||
|
|
||
| if (_stepCounter == 0) | ||
| { | ||
| _direction = _direction.ClockwiseRotate(); | ||
|
|
||
| if (_direction == Direction.Up || _direction == Direction.Down) | ||
| { | ||
| _neededSteps++; | ||
| } | ||
|
|
||
| _stepCounter = _neededSteps; | ||
| } | ||
|
|
||
| return _previus; | ||
| } | ||
|
|
||
| private Size GetOffsetSize(Direction direction) => direction switch | ||
| { | ||
| Direction.Up => new Size(0, Step), | ||
| Direction.Right => new Size(Step, 0), | ||
| Direction.Down => new Size(0, -Step), | ||
| Direction.Left => new Size(-Step, 0), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(direction)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вместо исключений всюду в программе используй паттерн Result |
||
| }; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.CloudGenerator; | ||
|
|
||
| public interface IRectanglesGenerator | ||
| { | ||
| public Result<IList<WordInShape>> GetWordsInShape(IDictionary<string, int> wordToWeight); | ||
| } |
This file contains hidden or 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,50 @@ | ||
| using System.Drawing; | ||
| using TagCloud; | ||
| using TagCloud2.CloudLayout; | ||
| using TagCloud2.Drawer; | ||
|
|
||
| namespace TagCloud2.CloudGenerator; | ||
|
|
||
| public class RectanglesGenerator(ICloudLayouter cloudLayouter, DrawerSettings drawerSettings) : IRectanglesGenerator | ||
| { | ||
| private readonly List<WordInShape> _wordsInShape = new(); | ||
| private const int MinRectangleWidth = 5; | ||
| private const int MinRectangleHeight = 5; | ||
|
|
||
|
|
||
| public Result<IList<WordInShape>> GetWordsInShape(IDictionary<string, int> wordToWeight) | ||
| { | ||
| foreach (var word in wordToWeight) | ||
| { | ||
| var current = word.Key; | ||
| var size = GenerateRectangleSize(word, wordToWeight.Count); | ||
| var rectangle = cloudLayouter.PutNextRectangle(size); | ||
| if (rectangle.Width / current.Length < 1) | ||
| { | ||
| return Result.Fail<IList<WordInShape>>("Too small cloud size, try to take greater parameter"); | ||
| } | ||
|
|
||
| var fontSize = GenerateFontSize(rectangle, current); | ||
| _wordsInShape.Add(new WordInShape(current, rectangle, fontSize)); | ||
| } | ||
|
|
||
| return _wordsInShape; | ||
| } | ||
|
|
||
| private float GenerateFontSize(Rectangle rectangle, string word) | ||
| { | ||
| var fontSizeWidth = rectangle.Width / word.Length; | ||
| var fontSizeHeight = rectangle.Height; | ||
| return Math.Min(fontSizeWidth, fontSizeHeight); | ||
| } | ||
|
|
||
| private Size GenerateRectangleSize(KeyValuePair<string, int> word, int count) | ||
| { | ||
| var length = word.Key.Length; | ||
| var value = word.Value; | ||
| var width = length * value * drawerSettings.CloudSize.Width / count; | ||
| var height = value * drawerSettings.CloudSize.Height / count; | ||
| return new Size(Math.Max(width, MinRectangleWidth), | ||
| Math.Max(height, MinRectangleHeight)); | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud2.CloudGenerator; | ||
|
|
||
| public record WordInShape(string Word, Rectangle Rectangle, float FontSize); |
This file contains hidden or 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,17 @@ | ||
| using System.Drawing; | ||
| using TagCloud2.CloudGenerator; | ||
|
|
||
| namespace TagCloud2.CloudLayout; | ||
|
|
||
| public static class CloudExtension | ||
| { | ||
| public static Size GetCloudSize(this IList<WordInShape> words) | ||
| { | ||
| var left = words.Min(x => x.Rectangle.Left); | ||
| var right = words.Max(x => x.Rectangle.Right); | ||
| var top = words.Min(x => x.Rectangle.Top); | ||
| var bottom = words.Max(x => x.Rectangle.Bottom); | ||
| var size = new Size( Math.Abs(right) + Math.Abs(left),Math.Abs(bottom) + Math.Abs(top)); | ||
| return size; | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| using System.Drawing; | ||
| using TagCloud2.CloudForms; | ||
|
|
||
| namespace TagCloud2.CloudLayout; | ||
|
|
||
| public class CloudLayouter(ICloudForm cloudForm) : ICloudLayouter | ||
| { | ||
| public readonly List<Rectangle> Rectangles = new(); | ||
|
|
||
| public Rectangle PutNextRectangle(Size rectangleSize) | ||
| { | ||
| Rectangle rectangle; | ||
|
|
||
| do | ||
| { | ||
| var point = cloudForm.GetNextPoint(); | ||
| point.Offset(-rectangleSize.Width / 2, -rectangleSize.Height / 2); | ||
| rectangle = new Rectangle(point, rectangleSize); | ||
| } while (Rectangles.Any(r => r.IntersectsWith(rectangle))); | ||
|
|
||
| Rectangles.Add(rectangle); | ||
| return rectangle; | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagCloud2.CloudLayout; | ||
|
|
||
| public interface ICloudLayouter | ||
| { | ||
| public Rectangle PutNextRectangle(Size rectangleSize); | ||
| } |
This file contains hidden or 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; | ||
|
|
||
| namespace TagCloud2.ColoringAlgorithms; | ||
|
|
||
| public class GradientColor(Color gradientFrom, Color gradientTo) : IColorAlgorithm | ||
| { | ||
| public Result<Color[]> GetColors(int count) | ||
| { | ||
| if (!gradientFrom.IsKnownColor || !gradientTo.IsKnownColor) | ||
| { | ||
| return Result.Fail<Color[]>("Gradient colors are unknown"); | ||
| } | ||
|
|
||
| return Enumerable.Range(0, count) | ||
| .Select(i => Color.FromArgb( | ||
| (int) (gradientFrom.R + (gradientTo.R - gradientFrom.R) * (float) i / (count - 1)), | ||
| (int) (gradientFrom.G + (gradientTo.G - gradientFrom.G) * (float) i / (count - 1)), | ||
| (int) (gradientFrom.B + (gradientTo.B - gradientFrom.B) * (float) i / (count - 1)) | ||
| )) | ||
| .ToArray(); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using System.Drawing; | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.ColoringAlgorithms; | ||
|
|
||
| public interface IColorAlgorithm | ||
| { | ||
| public Result<Color[]> GetColors(int count); | ||
| } |
This file contains hidden or 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,19 @@ | ||
| using System.Drawing; | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.ColoringAlgorithms; | ||
|
|
||
| public class RandomColor : IColorAlgorithm | ||
| { | ||
| public Result<Color[]> GetColors(int count) | ||
| { | ||
| Random random = new Random(); | ||
|
|
||
| return Enumerable.Range(0, count) | ||
| .Select(_ => Color.FromArgb( | ||
| random.Next(256), | ||
| random.Next(256), | ||
| random.Next(256))) | ||
| .ToArray(); | ||
| } | ||
| } |
This file contains hidden or 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,14 @@ | ||
| using System.Drawing; | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.ColoringAlgorithms; | ||
|
|
||
| public class SingleColor(Color color) : IColorAlgorithm | ||
| { | ||
| public Result<Color[]> GetColors(int count) | ||
| { | ||
| return !color.IsKnownColor | ||
| ? Result.Fail<Color[]>("Unknown color") | ||
| : Enumerable.Repeat(color, count).ToArray(); | ||
| } | ||
| } |
This file contains hidden or 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; | ||
| using TagCloud2.ColoringAlgorithms; | ||
|
|
||
| namespace TagCloud2.Drawer; | ||
|
|
||
| public record DrawerSettings(IColorAlgorithm WordsColor, Size CloudSize, string Font); |
This file contains hidden or 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 @@ | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.Drawer; | ||
|
|
||
| public interface ICloudDrawer | ||
| { | ||
| public Result<None> DrawTagsCloudFromFile(string filepath); | ||
| } |
This file contains hidden or 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,68 @@ | ||
| using System.Drawing; | ||
| using TagCloud; | ||
| using TagCloud.TextPreparator; | ||
| using TagCloud2.CloudGenerator; | ||
| using TagCloud2.CloudLayout; | ||
| using Color = System.Drawing.Color; | ||
| using Font = System.Drawing.Font; | ||
| using Size = System.Drawing.Size; | ||
|
|
||
|
|
||
| namespace TagCloud2.Drawer; | ||
|
|
||
| public class RectanglesCloudDrawer( | ||
| DrawerSettings drawerSettings, | ||
| IWordsFrequency wordsFrequency, | ||
| IRectanglesGenerator rectanglesGenerator) | ||
| : ICloudDrawer | ||
| { | ||
| private Result<Bitmap> DrawCloud(IList<WordInShape> words, Size size) | ||
| { | ||
| var bmp = new Bitmap(size.Width, size.Height); | ||
| using var graphics = Graphics.FromImage(bmp); | ||
|
|
||
| var bgColor = Color.White; | ||
| graphics.Clear(bgColor); | ||
| var colors = drawerSettings.WordsColor.GetColors(words.Count); | ||
| var colorsValue = colors.Value; | ||
| if (!colors.IsSuccess) | ||
| { | ||
| return Result.Fail<Bitmap>(colors.Error); | ||
| } | ||
|
|
||
| var rectBrush = new SolidBrush(bgColor); | ||
| var i = 0; | ||
| var fontFamilies = FontFamily.Families; | ||
| var isFontExist = fontFamilies.Any(f => | ||
| f.Name.Equals(drawerSettings.Font, StringComparison.OrdinalIgnoreCase)); | ||
| if (!isFontExist) | ||
| { | ||
| return Result.Fail<Bitmap>("Current font doesn't exist"); | ||
| } | ||
|
|
||
| foreach (var (word, rect, fontSize) in words) | ||
| { | ||
| var textBrush = new SolidBrush(colorsValue[i++]); | ||
| graphics.FillRectangle(rectBrush, rect); | ||
| var font = new Font(drawerSettings.Font, fontSize); | ||
|
|
||
| var stringFormat = new StringFormat() | ||
| { | ||
| Alignment = StringAlignment.Center, | ||
| LineAlignment = StringAlignment.Center | ||
| }; | ||
|
|
||
| graphics.DrawString(word, font, textBrush, rect, stringFormat); | ||
| } | ||
|
|
||
| return bmp; | ||
| } | ||
|
|
||
| public Result<None> DrawTagsCloudFromFile(string filepath) | ||
| { | ||
| return wordsFrequency.GetWordsFrequencyFromFile(filepath) | ||
| .Then(word => rectanglesGenerator.GetWordsInShape(word)) | ||
| .Then(wordsInShape => DrawCloud(wordsInShape, wordsInShape.GetCloudSize())) | ||
| .Then(result => SaviorImages.SaveImage(result, "TagCloud", "PNG")); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using TagCloud; | ||
|
|
||
| namespace TagCloud2.FileReader; | ||
|
|
||
| public interface IFileReader | ||
| { | ||
| public Result<IEnumerable<string>> TryReadFile(string filePath); | ||
| } |
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.
Вместо исключений всюду в программе используй паттерн Result