Begin homegrown algorithm implementation

This commit is contained in:
Starbeamrainbowlabs 2016-05-16 20:55:11 +01:00
commit 57b89d3c59
6 changed files with 592 additions and 0 deletions

256
.gitignore vendored Normal file
View file

@ -0,0 +1,256 @@
# Created by https://www.gitignore.io/api/visualstudio
### VisualStudio ###
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml

17
SpritePacker.sln Normal file
View file

@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpritePacker", "SpritePacker\SpritePacker.csproj", "{6EF47B64-1920-4827-BEEF-B262D5A2D214}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.ActiveCfg = Debug|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.Build.0 = Debug|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.ActiveCfg = Release|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.Build.0 = Release|x86
EndGlobalSection
EndGlobal

238
SpritePacker/Packer.cs Normal file
View file

@ -0,0 +1,238 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;
using System.Drawing.Text;
namespace SpritePacker
{
public class Packer
{
private List<Sprite> sprites = new List<Sprite>();
public Packer()
{
}
public void Add(Sprite sprite)
{
sprites.Add(sprite);
}
public bool Remove(Sprite sprite)
{
return sprites.Remove(sprite);
}
public void Arrange()
{
List<Sprite> arrangedSprites = new List<Sprite>();
foreach(Sprite cspr in sprites)
{
Point scanLines = Point.Empty;
Point nextScanLines = scanLines;
while(true)
{
if (!cspr.IntersectsWith(arrangedSprites))
break;
// Scan along the X axis
cspr.Area.X = 0;
cspr.Area.Y = scanLines.Y;
bool foundPosition = false;
while(cspr.Area.X < scanLines.X)
{
if (!cspr.IntersectsWith(arrangedSprites))
{
foundPosition = true;
break;
}
// Get the edge furthest to the right
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
Sprite rightProblem = problems[0];
foreach (Sprite probSpr in problems)
if (probSpr.Area.Right > rightProblem.Area.Right)
rightProblem = probSpr;
// Move up to the position furthest to the right
// NOTE: We may need to add one here.
cspr.Area.X = furthestRightPos + 1;
}
if (!foundPosition)
{
// We didn't find anything along the x axis - let's scan the y axis next
cspr.Area.X = scanLines.X;
cspr.Area.Y = 0;
while (cspr.Area.Y < scanLines.Y)
{
if (!cspr.IntersectsWith(arrangedSprites))
{
foundPosition = true;
break;
}
// Get the edge furthest downwards
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
Sprite downProblem = problems[0];
foreach (Sprite probSpr in problems)
if (probSpr.Area.Bottom > downProblem.Area.Bottom)
downProblem = probSpr;
// Move up to the position furthest downwards
cspr.Area.Y = downProblem.Area.Bottom + 1;
}
}
// If we found a new position, then we don't need to move the scan lines up and try again
if (foundPosition)
break;
}
if (cspr.IntersectsWith(arrangedSprites))
{
// We have a conflict! Let's move it to try and sort this out.
List<Sprite> problemSprites = cspr.GetIntersectors(arrangedSprites);
// Find the problem sprite closest to (0, 0)
Sprite mainProblem = problemSprites[0];
foreach (Sprite pSpr in problemSprites) {
if ((pSpr.Area.X < mainProblem.Area.X && pSpr.Area.Y <= mainProblem.Area.Y) ||
(pSpr.Area.Y < mainProblem.Area.Y && pSpr.Area.X <= mainProblem.Area.X))
mainProblem = pSpr;
}
Point scanBox = new Point(mainProblem.Area.X, mainProblem.Area.Y);
}
else
{
arrangedSprites.Add(cspr);
}
}
}
}
public class Sprite
{
private Rectangle area;
private string filename;
public Rectangle Area
{
get { return area; }
set { area = value; }
}
public string Filename
{
get { return filename; }
set { filename = value; }
}
public Sprite(string inFilename)
{
Filename = inFilename;
// TODO: Fill in the area automagically based on the given image
throw new NotImplementedException("Todo: Fill in the area automagically based on the given image");
}
public static int GetLargestSize(List<Sprite> sprList)
{
int largestSoFar = 0;
foreach(Sprite spr in sprList)
{
if (spr.Area.Width > largestSoFar)
largestSoFar = spr.Area.Width;
}
}
public List<Sprite> GetIntersectors(List<Sprite> spriteList)
{
List<Sprite> result = new List<Sprite>();
foreach(Sprite spr in spriteList)
{
if (spr.IntersectsWith(this))
result.Add(spr);
}
return result;
}
public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
{
List<Sprite> result = new List<Sprite>();
foreach(Sprite spr in spriteList)
{
if (spr.IntersectsWithX(this))
result.Add(spr);
}
return result;
}
public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
{
List<Sprite> result = new List<Sprite>();
foreach(Sprite spr in spriteList)
{
if (spr.IntersectsWithY(this))
result.Add(spr);
}
return result;
}
public bool IntersectsWith(List<Sprite> otherSprites)
{
foreach (Sprite spr in otherSprites)
{
if (IntersectsWith(spr))
return true;
}
return false;
}
public bool IntersectsWith(Sprite otherSprite)
{
return otherSprite.Area.IntersectsWith(Area);
}
public bool IntersectsWithX(List<Sprite> otherSprites)
{
foreach (Sprite spr in otherSprites)
{
if (IntersectsWithX(spr))
return true;
}
return false;
}
public bool IntersectsWithY(List<Sprite> otherSprites)
{
foreach (Sprite spr in otherSprites)
{
if (IntersectsWithY(spr))
return true;
}
return false;
}
public bool IntersectsWithX(Sprite otherSprite)
{
if(Area.Right > otherSprite.Area.X &&
Area.X < otherSprite.Area.Right)
return true;
else
return false;
}
public bool IntersectsWithY(Sprite otherSprite)
{
if(Area.Bottom > otherSprite.Area.Y &&
Area.Y < otherSprite.Area.Bottom)
return true;
else
return false;
}
}
}

12
SpritePacker/Program.cs Normal file
View file

@ -0,0 +1,12 @@
using System;
namespace SpritePacker
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("SpritePacker")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Starbeamrainbowlabs")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{6EF47B64-1920-4827-BEEF-B262D5A2D214}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>SpritePacker</RootNamespace>
<AssemblyName>SpritePacker</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Packer.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>