-
Notifications
You must be signed in to change notification settings - Fork 278
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
base: master
Are you sure you want to change the base?
Ренат Зубакин #222
Changes from 8 commits
664ae52
351efe7
f431745
f6bf236
66241fe
dad14b2
b28d5fc
91a3c63
b599e22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
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); | ||
} | ||
} | ||
} |
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
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. И тут нет интерфейса |
||
{ | ||
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.
Sorry, something went wrong. |
||
{ | ||
return WordPositions.Any(rec => currRectangle.IntersectsWith(rec)); | ||
} | ||
|
||
|
||
public Rectangle ComperessRectangle(Rectangle rectangle) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
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; | ||
} | ||
} | ||
} |
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 | ||
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. Нет абстракции - какого-то интерфейса |
||
{ | ||
public class CloudLayouterDrawer | ||
{ | ||
private Random random; | ||
public int margin; | ||
public int imageWidth; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
public int imageHeight; | ||
|
||
public CloudLayouterDrawer(int margin) | ||
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. Цвет фона можно сделать параметром рисовальщика |
||
{ | ||
this.margin= margin; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
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)) | ||
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. можно упростить до using var bitmap = new Bitmap(imageWidth, imageHeight) 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. ругает версию шарпа 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. Поднять - никто не запрещал) |
||
{ | ||
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); | ||
} | ||
|
||
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. Сохранение в файл отлично декомпозируется в отдельный метод |
||
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)); | ||
} | ||
|
||
} | ||
} |
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(); | ||
} | ||
} |
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() | ||
{ | ||
|
||
} | ||
} | ||
} |
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")] |
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) |
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.
Sorry, something went wrong. |
||
public double Radius { get; private set; } | ||
public double Angle { get; private set; } | ||
|
||
|
||
public SpiralDistribution(Point center) | ||
{ | ||
Center = center; | ||
This comment was marked as resolved.
Sorry, something went wrong. 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. throw new ArgumentException(); должен жить на отдельной строчке 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. Считаю, что конструктор должен быть максимально простым, никаких проверок внутри не должно быть, его задача - инициализировать объект, если хочешь сделать проверку на входные параметры, то лучше сделать статичный метод который это будет проверять, и потом сам вызовет тебе приватный конструктор, Как вариант валидацию выносить в отдельный приватный метод |
||
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); | ||
} | ||
} | ||
} |
This comment was marked as resolved.
Sorry, something went wrong.