Added NoOverlap option

This commit is contained in:
Jaex 2019-12-15 15:39:15 +03:00
parent b5fc7103e4
commit 31bc399444

View file

@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
@ -83,6 +84,11 @@ public int ImageCount
[DefaultValue(100)]
public int RandomOpacityMax { get; set; }
[DefaultValue(false)]
public bool NoOverlap { get; set; }
private List<Rectangle> imageRectangles = new List<Rectangle>();
public DrawRandomImages()
{
this.ApplyDefaultPropertyValues();
@ -98,6 +104,8 @@ public override Image Apply(Image img)
if (files.Length > 0)
{
imageRectangles.Clear();
using (Graphics g = Graphics.FromImage(img))
using (ImageFilesCache imageCache = new ImageFilesCache())
{
@ -143,8 +151,22 @@ private void DrawImage(Image img, Image img2, Graphics g)
int xOffset = img.Width - width - 1;
int yOffset = img.Height - height - 1;
Rectangle rect = new Rectangle(MathHelpers.Random(Math.Min(0, xOffset), Math.Max(0, xOffset)),
MathHelpers.Random(Math.Min(0, yOffset), Math.Max(0, yOffset)), width, height);
Rectangle rect;
int attemptCount = 0;
do
{
rect = new Rectangle(MathHelpers.Random(Math.Min(0, xOffset), Math.Max(0, xOffset)),
MathHelpers.Random(Math.Min(0, yOffset), Math.Max(0, yOffset)), width, height);
attemptCount++;
if (attemptCount >= 1000)
{
return;
}
} while (NoOverlap && imageRectangles.Any(x => x.IntersectsWith(rect)));
imageRectangles.Add(rect);
if (RandomAngle)
{