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

Бессараб Дмитрий #259

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Binary file added 1.bmp
Binary file not shown.
Binary file added 2.bmp
Binary file not shown.
Binary file added 3.bmp
Binary file not shown.
33 changes: 33 additions & 0 deletions TagsCloudVisualization/ArchemedianSpiral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class ArchemedianSpiral : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 1;
var spiralStep = 0.0;
var current = start;
yield return current;
while (true)
{
spiralStep += Math.PI / 180;
var x = start.X + (int)(zoom * spiralStep * Math.Cos(spiralStep));
var y = start.Y + (int)(zoom * spiralStep * Math.Sin(spiralStep));
var next = new Point(x, y);
if (!next.Equals(current))
{

Choose a reason for hiding this comment

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

А точно это проверка нужна?

yield return current;
current = next;
}
else continue;
}
}
}
}
32 changes: 32 additions & 0 deletions TagsCloudVisualization/ArchemedianSpiral_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;

namespace TagsCloudVisualization
{
[TestFixture]
public class ArchemedianSpiral_Should

Choose a reason for hiding this comment

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

Тесты желательно уносить в отдельную сборку

{

Choose a reason for hiding this comment

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

Можно еще тест что центр это первая точка

[Test]
public void GeneratePoints_MovingAwayFromTheStart()
{
var start = new Point(0, 0);
var points = new ArchemedianSpiral().GeneratePoints(start);
var nearPoint = points.ElementAt(100);
var farPoint = points.ElementAt(1000);

DistanceBetween(start, nearPoint).Should().BeLessThan(DistanceBetween(start, farPoint));
}

public int DistanceBetween(Point start, Point destination)
{
return (int)(Math.Sqrt((start.X - destination.X) * (start.X - destination.X) +
(start.Y - destination.Y) * (start.Y - destination.Y)));
}
}
}
80 changes: 80 additions & 0 deletions TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter
{
public readonly Point Center;
public readonly Size Size;
private readonly IEnumerable<Point> _points;
public List<Rectangle> Rectangles { get; set; }


public CircularCloudLayouter(Point center)
{
if (center.X > 0 && center.Y > 0)
Center = center;
else throw new ArgumentException("Center coordinates values have to be greater than Zero");

Choose a reason for hiding this comment

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

лучше заминить условие на провиположное и тогда не нужно будет писать else

Size = CountSize(center);
Rectangles = [];
_points = new ArchemedianSpiral().GeneratePoints(Center);
}

public CircularCloudLayouter(Point center, IEnumerable<Point> points) : this(center)
{
Center = center;
Size = CountSize(center);
Rectangles = [];
_points = points;
}

public CircularCloudLayouter(Point center, Func<Point, IEnumerable<Point>> pointGenerator) : this(center)
{

Choose a reason for hiding this comment

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

Почему на вход не интерфейс IPointGenerator принимаешь?

Center = center;
Size = CountSize(center);
Rectangles = [];
_points = pointGenerator(Center);
}

Choose a reason for hiding this comment

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

Кажется получилось много дублирующи конструкторов



private Size CountSize(Point center)
{
var width = (center.X % 2 == 0) ? center.X * 2 + 1 : Center.X * 2;
var height = (center.Y % 2 == 0) ? center.Y * 2 + 1 : center.Y * 2;
return new Size(width, height);
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
if (Rectangles.Count == 0)
{
var firstRectangle = new Rectangle(new Point(Center.X - rectangleSize.Width / 2,
Center.Y - rectangleSize.Height / 2), rectangleSize);
Rectangles.Add(firstRectangle);
return firstRectangle;
}

Choose a reason for hiding this comment

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

А зачем выделяем отдельно обработку первого прямоугольника?


foreach (var point in _points)
{
var supposed = new Rectangle(new Point(point.X - rectangleSize.Width / 2, point.Y - rectangleSize.Height / 2),
rectangleSize);
if (IntersectsWithAnyOther(supposed, Rectangles))
continue;
Rectangles.Add(supposed);
return supposed;
}
throw new ArgumentException("Not Enough Points Generated");
}

public static bool IntersectsWithAnyOther(Rectangle supposed, List<Rectangle> others)
{
return others.Select(x => x.IntersectsWith(supposed))
.Any(x => x == true);

Choose a reason for hiding this comment

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

можно просто .Any()

}
}
}
67 changes: 67 additions & 0 deletions TagsCloudVisualization/CircularCloudLayouter_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;
using System.Reflection;
using System.Drawing;


namespace TagsCloudVisualization
{
[TestFixture]
public class CircularCloudLayouter_Should
{
[TestCase(1, 2, TestName = "Odd coordinate value results in an even size value")]
[TestCase(2, 5, TestName = "Even coordinate value results in an odd size value")]
public void CircularCloudLayouter_MakeRightSizeLayout(int coordinateValue, int sizeValue)
{
var center = new Point(coordinateValue, coordinateValue);
var size = new Size(sizeValue, sizeValue);

var layout = new CircularCloudLayouter(center);

layout.Size.Should().BeEquivalentTo(size);
}

[TestCase(-1, 1, TestName = "Negative X")]
[TestCase(1, -1, TestName = "Negative Y")]
[TestCase(0, 1, TestName = "Zero X")]
[TestCase(1, 0, TestName = "Zero Y")]
public void CircularCloudLayouter_GetsOnlyPositiveCenterCoordinates(int x, int y)
{
Action makeLayout = () => new CircularCloudLayouter(new Point(x, y));

makeLayout.Should().Throw<ArgumentException>()
.WithMessage("Center coordinates values have to be greater than Zero");
}

[Test]
public void PutNextRectangle_ShouldKeepEnteredSize()
{
var layout = new CircularCloudLayouter(new Point(5, 5));
var enteredSize = new Size(3, 4);
var returnedSize = layout.PutNextRectangle(enteredSize).Size;

returnedSize.Should().BeEquivalentTo(enteredSize);
}

[Test]
public void PutNextRectangle_HasNotEnoughPoints()
{
var points = new[] { new Point(0, 0), new Point(1, 1), new Point(2, 2) };
var layout = new CircularCloudLayouter(new Point(100, 100), points);
var rectangleSizes = new RandomSizeRectangle().GenerateRectangles(5);

var makeRectangles = () => {
foreach (var size in rectangleSizes)
layout.PutNextRectangle(size);
};

makeRectangles.Should().Throw<ArgumentException>()
.WithMessage("Not Enough Points Generated");
}
}
}
45 changes: 45 additions & 0 deletions TagsCloudVisualization/DeltaShaped.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
internal class DeltaSHaped : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 5;
var current = start;
yield return start;
while (true)
{
foreach (var pair in Heart())
{
var x = start.X + (int)(zoom * pair.Item1);
var y = start.Y + (int)(zoom * pair.Item2);
var next = new Point(x, y);
if (!next.Equals(current))
{
yield return current;
current = next;
}
else continue;
}
zoom += 2;
}
}

public IEnumerable<(double, double)> Heart()
{
for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180)
{
var x = 2 * Math.Cos(t) + Math.Cos(2*t);
var y = 2 * Math.Sin(t) - Math.Sin(2 * t);
yield return (x, y);
}
}
}
}
70 changes: 70 additions & 0 deletions TagsCloudVisualization/Drawings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
internal class Drawings
{
private static void Main()
{
var rectangleSizes = new RandomSizeRectangle().
GenerateRectangles(300);
var layout = new CircularCloudLayouter(new Point(1000, 1000));
var image = new Bitmap(layout.Size.Width, layout.Size.Height);
foreach (var size in rectangleSizes)
{
DrawRectangle(image, layout.PutNextRectangle(size));
image.Save(@"c:\\Images\\1.bmp");
}


rectangleSizes = new RandomSizeRectangle().
GenerateRectangles(300);
var points = new HeartShaped().GeneratePoints(new Point(1000, 1000));
layout = new CircularCloudLayouter(new Point(1000, 1000), points);
image = new Bitmap(layout.Size.Width, layout.Size.Height);
foreach (var size in rectangleSizes)
{
DrawRectangle(image, layout.PutNextRectangle(size));
image.Save(@"c:\\Images\\2.bmp");

Choose a reason for hiding this comment

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

лучше задавать относительне пути от запуска. Допустим не у всех есть диск c

}

rectangleSizes = new RandomSizeRectangle().
GenerateRectangles(300);
//points = new DeltaSHaped().GeneratePoints(new Point(1000, 1000));
layout = new CircularCloudLayouter(new Point(1000, 1000), new DeltaSHaped().GeneratePoints);
image = new Bitmap(layout.Size.Width, layout.Size.Height);
foreach (var size in rectangleSizes)
{
DrawRectangle(image, layout.PutNextRectangle(size));
image.Save(@"c:\\Images\\3.bmp");
}
}

Choose a reason for hiding this comment

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

Вижу копирование, а отличие толкьо точками на вход и путем до изображения


//private static void DrawPicture(Func<int, IEnumerable<Size>> RectSizeGenerator, int quantity,
// Func<Point, IEnumerable<Point>> ShapeGenerator, Point startPoint, string filename)
//{
// if (ShapeGenerator == ArchemedianSpiral.)
//}

private static void DrawRectangle(Bitmap image, Rectangle rectangle)
{
var brush = new SolidBrush(GetRandomColor());
var formGraphics = Graphics.FromImage(image);
formGraphics.FillRectangle(brush, rectangle);
brush.Dispose();
formGraphics.Dispose();
}

private static Color GetRandomColor()
{
var random = new Random();
return Color.FromArgb( random.Next(0, 255), random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
}
}
45 changes: 45 additions & 0 deletions TagsCloudVisualization/HeartShaped.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
internal class HeartShaped : IPointGenerator
{
public IEnumerable<Point> GeneratePoints(Point start)
{
var zoom = 1;
var current = start;
yield return start;
while (true)
{
foreach (var pair in Heart())
{
var x = start.X + (int)(zoom * pair.Item1);
var y = start.Y + (int)(zoom * pair.Item2);
var next = new Point(x, y);
if (!next.Equals(current))
{
yield return current;
current = next;
}
else continue;
}
zoom += 1;
}
}

public IEnumerable<(double, double)> Heart()
{
for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180)
{
var x = 16 * Math.Sin(t) * Math.Sin(t) * Math.Sin(t);
var y = -13 * Math.Cos(t) + 5 * Math.Cos(2 * t) + 2 * Math.Cos(3 * t) + Math.Cos(4 * t);
yield return (x, y);
}
}
}
}
14 changes: 14 additions & 0 deletions TagsCloudVisualization/IPointGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudVisualization
{
public interface IPointGenerator
{
IEnumerable<Point> GeneratePoints(Point start);
}
}
Loading