A Proof of concept WPF application that generates random pixels and then sorts them based on the hue value.
This project was given to me as a take home coding test from a software company.
- Open the solution in Visual studio.
- Restore nuget packges
- Build and run
Verify you have dotnet cli by typing
dotnet
From the solution's root directory
- Resotre the packages
dotnet restore
- Build the source
dotnet build
- Optionally, you can run the tests
dotnet test
- Run the app with
dotnet run --project .\PixelSort.App\
The solution is divided into three projects
- PixelSort.App
- contains the wpf application
- PixelSort.Domain
- houses the application logic
- PixelSort.Doamin.Tests
- xUnit project that contains the test for PixelSort.Domain
- View the code coverage report here
-
I have used WritableBitmapSource to generate the image source. This class has a back buffer where the pixel informations are populated
-
I have uses a TashFactory class to run the methods that generate random pixels and sorts them.
-
I have used Microsoft.Extensions.DependencyInjection nuget package to set up DI containers.
-
xUnit was used as a the unit testing framework
-
The PixelSortviewModel acts as a controller for the view.
-
The Pixel class is the model.
-
The randomly generated pixels are created from first creating random RGBA values that is used to create a Color class. Then Pixels are created from a Color class instance. This has the benefit of using GetHue() method on the Color class to calculate the Hue value.
-
The GetHue() methods returns Hue as Float type. Hue is a value from 0 degree to 360 degree. Knowing this information, I have used bucket sort to implement the sorting. On the benchmarks it clearly outsmarts the comparison based sorting algorithms.
-
I have run some benchmarks, look here
-
The Color Sorting button click handler first checks if the pixels are populated before attempting to sort them.
-
If the pixels are already sorted, clicking the Color Sorting button will not sort the pixels again.
-
I have noticed the sorted image to be flipped 90 degrees to the left. To render this transformation, the PixelConverter class arranges the bytes from the sorted pixels in column first manner. The GetTransposedPixelsFromArgbColors method does this job.
-
Pixelconfiguration class has two static methods DefaultConfiguration and HighPixelConfiguration that sets up the resolution for the image.
- Set PixelSort.Benchmarks as startup project in Visual studio.
- Use Release configuration.
- Start the project with ctrl + F5
- The results will be in the following directory
PixelSort.Benchmarks\bin\Release\netcoreapp3.1\BenchmarkDotNet.Artifacts\results
- Make sure dotnet-reportgenerator-globaltool is installed details here
- Genrate coverate report using coverlet
dotnet test --collect:"XPlat Code Coverage"
- run the following, specify the path to the .xml
reportgenerator "-reports:*.xml" "-targetdir:C:\report" -reporttypes:MarkdownSummary
-
Add logging and exception handling logic
-
Currently when generating random pixels, the Hue values are also getting generated. Find an implementation where this calculation can be deferred or offloaded to a seperate thread.
-
Use a priority queue to store items inside a bucket. Will require the projects to be updated to .NET 6.0 since it has introduced the Priority Queue class.