📜 ⬆️ ⬇️

Visual analysis of the quality of the code

Good day everyone!

Inspired by one gif-koi, I decided what would happen if I wrote the reverse application and looked at the code “map”?

First, we need to solve the problem of “place so many pixels in a square.”
It is clear that the square is not always possible and not necessary. Therefore, you can arrange them in a rectangle with the addition.
Not finding an algorithm that tells me that 129 = 11 * 12-3, I decided to write it myself.

Some horrible code
public static ImageSize Generate(uint PixelsCount, double MaximumDeltaPercent=1f, uint MaximumAbsoluteDelta=10, double MaximumAspectRatio=1.77) { //check if this is empty image if (PixelsCount==0) return new ImageSize(1,1); //check if this is a square double sqrt = Math.Truncate(Math.Sqrt(PixelsCount)); if (Math.Pow(sqrt, 2) == PixelsCount) return new ImageSize((ushort)sqrt, (ushort) sqrt); //  ,  AspectRatio - ,     .      . if (PixelsCount<=10) MaximumAspectRatio = double.MaxValue; double percentdelta = PixelsCount/(MaximumDeltaPercent*100); uint height = (ushort) Math.Truncate(Math.Sqrt(PixelsCount)); uint width = (ushort) (height+1); while (width/(height*1.0f)<MaximumAspectRatio) { ulong Result = width*height; if (Result==PixelsCount) { if (width<=ushort.MaxValue && height<=ushort.MaxValue) { return new ImageSize((ushort) width, (ushort) height); } } if (Result>PixelsCount) { if ((Result-PixelsCount)<=percentdelta || Result-PixelsCount<=MaximumAbsoluteDelta) { if (width <= ushort.MaxValue && height <= ushort.MaxValue) { return new ImageSize((ushort) width, (ushort) height); } } height--; continue; } if (Result < PixelsCount) { width++; } } throw new Exception(string.Format("     {0}    Aspect Ratio.   \n : PC:{0} MDP:{1} MAD:{2} MAR{3}",PixelsCount,MaximumDeltaPercent,MaximumAbsoluteDelta,MaximumAspectRatio)); } 

Then we draw a beautiful picture either in the stream or in the file.
')
Examples of the resulting images:
One of the big files of my mini-project

The code is on average uniform gray interspersed with green. But, striped.

Explorer.exe from Win8.1

The executable is kind of weird, with a black stripe in the middle. That, in general, is obvious.
In the end resources of icons are guessed.

Examples of the resulting images:
Workqueue.c from linux kernel

The code is on average uniform gray interspersed with blue. Overall, he seems bluish.

In general, experiment, I hope your code will not turn out to be uniformly brown!

The source in the archive
Compiled program
PS If you suddenly know a beautiful algorithm for turning a number into a product with the rest, I beg you to tell me about it.

Source: https://habr.com/ru/post/214165/


All Articles