Active questions tagged geometry - Code Golf Stack Exchange most recent 30 from codegolf.stackexchange.com 2019-07-24T09:37:51Z https://codegolf.stackexchange.com/feeds/tag/geometry http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://codegolf.stackexchange.com/q/187763 11 Counting generalized polyominoes Peter Kagey https://codegolf.stackexchange.com/users/53884 2019-07-06T17:05:24Z 2019-07-19T21:23:48Z <p>This challenge will have you count pseudo-<a href="https://en.wikipedia.org/wiki/Polyform" rel="noreferrer">polyforms</a> on the <a href="https://en.wikipedia.org/wiki/Snub_square_tiling" rel="noreferrer">snub square tiling</a>.</p> <p>I think that this sequence does not yet exist on the <a href="https://oeis.org/" rel="noreferrer">OEIS</a>, so this challenge exists to compute as many terms as possible for this sequence.</p> <h1>Definitions</h1> <p>The snub square tiling is a semiregular tiling of the plane that consists of equilateral triangles and squares.</p> <p><a href="https://i.stack.imgur.com/RXSwgm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RXSwgm.png" alt="snub square tiling"></a></p> <p>A pseudo-polyform on the snub square tiling is a plane figure constructed by joining together these triangles and squares along their shared sides, analogous to a polyomino. Here is an example of a six-cell and an eight-cell pseudo-polyform:</p> <p><a href="https://i.stack.imgur.com/Ecn14m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ecn14m.png" alt="enter image description here"></a></p> <h1>Examples</h1> <p>For <code>n = 1</code> there are two 1-cell pseudo-polyforms, namely the square and the triangle:</p> <p><img src="https://i.stack.imgur.com/f5bsp.png" height="150"></p> <p>For <code>n = 2</code> there are two 2-cell pseudo-polyforms, namely a square with a triangle and two triangles.</p> <p><img src="https://i.stack.imgur.com/b5TaU.png" height="150"></p> <p>For <code>n = 3</code> there are four 3-cell pseudo-polyforms.</p> <p><img src="https://i.stack.imgur.com/kE8vd.png" height="150"></p> <h1>Challenge</h1> <p>The goal of this challenge is to compute as many terms as possible in this sequence, which begins <code>2, 2, 4, ...</code> and where the n-th term is the number of n-cell pseudo-polyforms up to rotation and reflection.</p> <p>Run your code for as long as you'd like. The winner of this challenge will be the user who posts the most terms of the sequence, along with their code. If two users post the same number of terms, then whoever posts their last term earliest wins.</p> <p>(Once there are enough known terms to prove that this sequence does not already exist in the OEIS, I'll create an entry in the OEIS and list the contributor as a co-author if he or she desires.)</p> https://codegolf.stackexchange.com/q/185760 24 Expand a hexagon xnor https://codegolf.stackexchange.com/users/20260 2019-05-18T02:13:56Z 2019-07-16T06:28:22Z <p>Given an ASCII art hexagon as input, output one whose sides are all one unit longer.</p> <pre><code> _____ ____ / \ / \ / \ / \ / \ \ \ =&gt; \ \ \ / \ / \____/ \ / \_____/ </code></pre> <p>The input hexagons will have 180 degree symmetry, but otherwise the sides can be all different lengths. Above, the side lengths (2, 4, 3) get enlarged to (3, 5, 4). The side lengths will be nonzero.</p> <p>The hexagons are made of underscores <code>_</code>, slashes <code>/</code>, and backslashes <code>\</code>. Note that of the horizontal edges (made with underscores), the top edge is on its own line but the bottom edge is not.</p> <p><strong>I/O</strong></p> <p>I'm going to be a stickler and require <a href="/questions/tagged/ascii-art" class="post-tag" title="show questions tagged &#39;ascii-art&#39;" rel="tag">ascii-art</a> formatting here: the input and output should be a string with newlines representing the image, not a list of lines. Of course, your code may print each line in turn to produce the image, or read STDIN a line a time for input if your language can do that.</p> <p><strong>Details</strong></p> <p>The input may include an optional trailing newline if you wish, but will otherwise have no empty lines. You can choose to either have no trailing spaces in the input, or spaces to pad each line to the same length (that of the longest line).</p> <p>The output should be flush with the left edge of the screen, like the input. You may have extra newlines above and below as well as trailing spaces.</p> <p><strong>Test cases</strong></p> <p>Input followed by output.</p> <pre><code> _ / \ \_/ __ / \ / \ \ / \__/ </code></pre> <hr> <pre><code> ____ / \ / \ \ \ \ / \____/ _____ / \ / \ / \ \ \ \ / \ / \_____/ </code></pre> <hr> <pre><code> _ / \ / / / / / / / / \_/ __ / \ / \ / / / / / / / / \ / \__/ </code></pre> <p><strong>Leaderboard</strong></p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="true"> <div class="snippet-code"> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=185760" width="100%" height="100%" style="border: none;"&gt;Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.&lt;/iframe&gt;&lt;style&gt;html,body{margin:0;padding:0;height:100%;overflow:hidden}&lt;/style&gt;</code></pre> </div> </div> </p> https://codegolf.stackexchange.com/q/188333 36 Construct a pentagon avoiding compass use Sriotchilism O'Zaic https://codegolf.stackexchange.com/users/56656 2019-07-13T16:35:54Z 2019-07-15T23:15:16Z <h2>Rules</h2> <p>You will start with only two elements: Points <span class="math-container">\$A\$</span> and <span class="math-container">\$B\$</span> such that <span class="math-container">\$A \neq B\$</span>. These points occupy a plane that is infinite in all directions.</p> <p>At any step in the process you may do any of the three following actions:</p> <ol> <li><p>Draw a line that passes through two points.</p></li> <li><p>Draw a circle centered at one point such that another point lies on the circle.</p></li> <li><p>Add a new point where two objects (lines and circles) intersect.</p></li> </ol> <p>Your goal is to create 5 points such that they form the vertices of a <a href="https://en.wikipedia.org/wiki/Pentagon#Regular_pentagons" rel="noreferrer">regular pentagon</a> (a convex polygon with 5 sides equal in length) using as few circles as possible. You may of course have other points but 5 of them must for a regular pentagon. You do not have to draw the edges of the pentagon for your scoring.</p> <h2>Scoring</h2> <p>When comparing two answers the one that draws fewer circles is better. In the case of a tie in circles the answer that draws the fewest lines is better. In the case of a tie in both circles and lines the answer that adds the fewest points is better.</p> <h2>Anti-Rules</h2> <p><em>While the rules list is exhaustive and details everything you can do this list is not, just because I don't say you can't do something does not mean you can.</em></p> <ul> <li><p>You cannot create "arbitrary" objects. Some constructions you will find will do thinks like add a point at an "arbitrary" location and work from there. You cannot add new points at locations other than intersections.</p></li> <li><p>You cannot copy a radius. Some constructions will involve taking a compass setting it to a radius between two points and then picking it up and drawing a circle elsewhere. You cannot do this.</p></li> <li><p>You cannot perform limiting processes. All constructions must take a finite number of steps. It is not good enough to approach the answer asymptotically.</p></li> <li><p>You cannot draw an arc or part of a circle in order to avoid counting it as a circle in your scoring. If you want to visually use arcs when showing or explaining your answer because they take up less space go ahead but they count as a circle for scoring.</p></li> </ul> <h2>Tools</h2> <p>You can think through the problem on <a href="https://www.geogebra.org/graphing" rel="noreferrer">GeoGebra</a>. Just go over to the shapes tab. The three rules are equivalent to the point, line and circle with center tools.</p> <h2>Burden of Proof</h2> <p>This is standard but I would like to reiterate. If there is a question as to whether a particular answer is valid the burden of proof is on the answerer to show that their answer is valid rather than the public to show that the answer is not.</p> <h2>What is this doing on my Code-Golf site?!</h2> <p>This is a form of <a href="/questions/tagged/atomic-code-golf" class="post-tag" title="show questions tagged &#39;atomic-code-golf&#39;" rel="tag">atomic-code-golf</a> similar to <a href="/questions/tagged/proof-golf" class="post-tag" title="show questions tagged &#39;proof-golf&#39;" rel="tag">proof-golf</a> albeit in a bit of a weird programming language. There is currently <a href="https://codegolf.meta.stackexchange.com/a/16689/56656">a +22/-0 consensus on the meta</a> that this sort of thing is allowed.</p> https://codegolf.stackexchange.com/q/187313 29 Golf the smallest circle! Barranka https://codegolf.stackexchange.com/users/56701 2019-06-23T19:08:09Z 2019-06-30T03:38:30Z <h2>The problem:</h2> <p>Given a non-empty set of points in the Cartesian plane, find the smallest circle that encloses them all (<a href="https://en.wikipedia.org/wiki/Smallest-circle_problem" rel="nofollow noreferrer">Wikipedia link</a>).</p> <p>This problem is trivial if the number of points is three or less (if there's one point, the circle has a radius of zero; if there are two points, the line segment that joins the points is the diameter of the circle; if there are three (non-colinear) points, it's possible to get the equation of a circle that touches them all if they form a non-obtuse triangle, or a circle that touches only two points and encloses the third if the triangle is obtuse). So, for the sake of this challenge, the number of points should be greater than three.</p> <h2>The challenge:</h2> <ul> <li><strong>Input:</strong> A list of 4 or more non-colinear points. The points should have X and Y coordinates; coordinates can be floats. To ease the challenge, no two points should share the same X coordinate.<br> For example: <code>[(0,0), (2,1), (5,3), (-1,-1)]</code></li> <li><strong>Output:</strong> A tuple of values, <code>(h,k,r)</code>, such that <span class="math-container">\$(x-h)^2 + (y-k)^2 = r^2\$</span> is the equation of the smallest circle that encloses all points.</li> </ul> <h2>Rules:</h2> <ul> <li>You can choose whatever input method suits your program.</li> <li>Output should be printed to <code>STDOUT</code> or returned by a function.</li> <li>"Normal", general-purpose, languages are preferred, but any esolang is acceptable.</li> <li>You can assume that the points are not colinear.</li> <li>This is code-golf, so the smallest program in bytes wins. The winner will be selected one week after the challenge is posted. <ul> <li>Please include the language you used and the length in bytes as header in the first line of your answer: <code># Language: n bytes</code></li> </ul></li> </ul> <h2>Test cases:</h2> <ul> <li>1: <ul> <li>Input: <code>[(-8,0), (3,1), (-6.2,-8), (3,9.5)]</code></li> <li>Output: <code>[-1.6, 0.75, 9.89]</code></li> </ul></li> <li>2: <ul> <li>Input: <code>[(7.1,-6.9), (-7,-9), (5,10), (-9.5,-8)]</code></li> <li>Output: <code>[-1.73, 0.58, 11.58]</code></li> </ul></li> <li>3: <ul> <li>Input: <code>[(0,0), (1,2), (3,-4), (4,-5), (10,-10)]</code></li> <li>Output: <code>[5.5, -4, 7.5]</code></li> </ul></li> <li>4: <ul> <li>Input: <code>[(6,6), (-6,7), (-7,-6), (6,-8)]</code></li> <li>Output: <code>[0, -0.5, 9.60]</code></li> </ul></li> </ul> <p>Happy golfing!!!</p> <hr> <h2>Related challenge:</h2> <ul> <li><a href="https://codegolf.stackexchange.com/questions/183191/area-of-a-2d-convex-hull/">Area of a 2D convex hull</a></li> </ul> https://codegolf.stackexchange.com/q/101234 34 Evaluate the aspect ratio of a triangle Fatalize https://codegolf.stackexchange.com/users/41723 2016-11-27T11:09:13Z 2019-06-24T10:43:15Z <p>Given three sidelengths of a triangle, evaluate its aspect ratio <em>AR</em> given the following formula:</p> <p><a href="https://i.stack.imgur.com/NUubd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NUubd.png" alt="enter image description here"></a></p> <p>where</p> <p><a href="https://i.stack.imgur.com/ZkGvs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZkGvs.png" alt="enter image description here"></a></p> <p>The closer to equilaterality a triangle is, the closer to <code>1</code> its aspect ratio is. The aspect ratio is bigger or equal to <code>1</code> for valid triangles.</p> <h3>Inputs</h3> <p>The input is three real positive numbers which can be encapsulated in a list or anything similar if need be.</p> <p>Your program must output the same value no matter what the order in which the three sidelengths are inputted is.</p> <p>Those three numbers will always be valid sidelengths of a triangle (degenerate triangles like one with sidelengths <code>1</code>, <code>1</code> and <code>2</code> will not be given as input). You need not worry about floating point inaccuracies when values become extremely close to a degenerate triangle (e.g. it is acceptable that your program would error <code>division by 0</code> for input <code>[1, 1, 1.9999999999999999]</code>).</p> <p>The input can be given through <code>STDIN</code>, as a function argument, or anything similar.</p> <h3>Outputs</h3> <p>The output is a real number bigger or equal to <code>1</code> with the standard accuracy that is acceptable in your language.</p> <p>The output may be printed to <code>STDOUT</code>, returned from a function, or anything similar.</p> <h3>Test cases</h3> <pre><code>Inputs Output 1 1 1 1 3 4 5 1.25 42 42 3.14 ≈ 6.9476 14 6 12 1.575 6 12 14 1.575 0.5 0.6 0.7 ≈ 1.09375 </code></pre> <h3>Scoring</h3> <p>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so the shortest answer in bytes wins.</p> https://codegolf.stackexchange.com/q/183191 11 Area of a 2D convex hull Vladimir Reshetnikov https://codegolf.stackexchange.com/users/8063 2019-04-14T21:51:15Z 2019-06-21T16:42:07Z <p>You are given an array/list/vector of pairs of integers representing cartesian coordinates <span class="math-container">\$(x, y)\$</span> of points on a 2D Euclidean plane; all coordinates are between <span class="math-container">\$−10^4\$</span> and <span class="math-container">\$10^4\$</span>, duplicates are allowed. Find the area of the <a href="https://en.wikipedia.org/wiki/Convex_hull" rel="nofollow noreferrer">convex hull</a> of those points, rounded to the nearest integer; an exact midpoint should be rounded to the closest even integer. You may use floating-point numbers in intermediate computations, but only if you can guarantee that the final result will be always correct. This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so the shortest correct program wins.</p> <p>The <em>convex hull</em> of a set of points <span class="math-container">\$P\$</span> is the smallest convex set that contains <span class="math-container">\$P\$</span>. On the Euclidean plane, for any single point <span class="math-container">\$(x,y)\$</span>, it is the point itself; for two distinct points, it is the line containing them, for three non-collinear points, it is the triangle that they form, and so forth.</p> <p>A good visual explanation of what a convex hulls, is best described as imagining all points as nails in a wooden board, and then stretching a rubber band around them to enclose all the points:<br> <a href="https://i.stack.imgur.com/0c83c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0c83c.png" alt="enter image description here"></a></p> <p>Some test cases:</p> <pre><code>Input: [[50, -13]] Result: 0 Input: [[-25, -26], [34, -27]] Result: 0 Input: [[-6, -14], [-48, -45], [21, 25]] Result: 400 Input: [[4, 30], [5, 37], [-18, 49], [-9, -2]] Result: 562 Input: [[0, 16], [24, 18], [-43, 36], [39, -29], [3, -38]] Result: 2978 Input: [[19, -19], [15, 5], [-16, -41], [6, -25], [-42, 1], [12, 19]] Result: 2118 Input: [[-23, 13], [-13, 13], [-6, -7], [22, 41], [-26, 50], [12, -12], [-23, -7]] Result: 2307 Input: [[31, -19], [-41, -41], [25, 34], [29, -1], [42, -42], [-34, 32], [19, 33], [40, 39]] Result: 6037 Input: [[47, 1], [-22, 24], [36, 38], [-17, 4], [41, -3], [-13, 15], [-36, -40], [-13, 35], [-25, 22]] Result: 3908 Input: [[29, -19], [18, 9], [30, -46], [15, 20], [24, -4], [5, 19], [-44, 4], [-20, -8], [-16, 34], [17, -36]] Result: 2905 </code></pre> https://codegolf.stackexchange.com/q/186815 8 Partition and Restructure HyperNeutrino https://codegolf.stackexchange.com/users/68942 2019-06-13T12:44:12Z 2019-06-20T23:54:11Z <p>Given two contiguous shapes of the same area, determine the optimal way to divide the first shape into a minimum number of contiguous segments such that they can be rearranged to form the second shape. In other words, find the minimum number of segments required that can form both of the shapes.</p> <p>"Contiguous" means that every square in the shape can be reached from any other square by walking across edges. Shapes and segments are allowed to have holes.</p> <p>"Rearrange" means you move the segments around; you can translate, rotate, and reflect them.</p> <p>The shapes are contained on a grid; in other words, each shape consists of a collection of unit squares joined by their corners/edges.</p> <h1>Input Specifications</h1> <p>The input will be provided in some reasonable format - list of points, array of strings representing each grid, etc. You can also take the sizes of the grid if requested. The grids will have the same dimensions and the two shapes are guaranteed to have the same area, and the area will be positive.</p> <h1>Output Specifications</h1> <p>The output should just be a single positive integer. Note that there will always be a positive answer because in the worst case scenario, you just divide the shapes into <code>N</code> unit squares.</p> <h1>Examples</h1> <p>The examples are presented as a grid with <code>.</code> representing a blank and <code>#</code> representing part of the shape.</p> <h2>Case 1</h2> <p><strong>Input</strong></p> <pre><code>..... .###. .#.#. .###. ..... ###.. ..#.. ..#.. ..### ..... </code></pre> <p><strong>Output</strong></p> <pre><code>2 </code></pre> <p><strong>Explanation</strong></p> <p>You can divide it into two L-shaped blocks of 4:</p> <pre><code># ### </code></pre> <h2>Case 2</h2> <p><strong>Input</strong></p> <pre><code>#... ##.. .#.. .##. .##. #### .... .... </code></pre> <p><strong>Output</strong></p> <pre><code>2 </code></pre> <p><strong>Explanation</strong></p> <p>You can split the shapes like so:</p> <pre><code>A... AA.. .A. .BB. .AA. BBAA .... .... </code></pre> <p>You could also do:</p> <pre><code>A... AA.. .B.. .BB. .AB. AABB .... .... </code></pre> <h2>Case 3</h2> <p><strong>Input</strong></p> <pre><code>#....# ###### .####. .####. </code></pre> <p><strong>Output</strong></p> <pre><code>2 </code></pre> <p><strong>Explanation</strong></p> <pre><code>A....B AAABBB .ABBB. .AAAB. </code></pre> <p>(This test case demonstrates the necessity to rotate/reflect shapes for optimal output)</p> <h2>Case 4</h2> <p><strong>Input</strong></p> <pre><code>.###. ..#.. .##.. .##.. </code></pre> <p><strong>Output</strong></p> <pre><code>2 </code></pre> <p><strong>Explanation</strong></p> <p>No matter how you select blocks, selecting a 2x1 from the first shape necessarily prevents the other two from being grouped together; thus, you can use one 2x1 and two 1x1s. However, (thanks @Jonah), you can split it into a 3-block L shape and a single square like so:</p> <pre><code>.AAB. ..A.. .AA.. .BA.. </code></pre> https://codegolf.stackexchange.com/q/40052 58 Draw the South Korean flag Martin Ender https://codegolf.stackexchange.com/users/8478 2014-10-20T10:11:09Z 2019-05-12T10:38:37Z <p>When I stumbled upon <a href="https://codegolf.stackexchange.com/questions/18664/lets-draw-flag-of-nepal">this question</a> I remembered that I had also once seen precise rules for the construction of the South Korean flag. And this is quite a different construction.</p> <p><img src="https://i.stack.imgur.com/lLYwt.png" alt="enter image description here"></p> <p><sup><a href="http://en.wikipedia.org/wiki/File:Flag_of_South_Korea.svg" rel="noreferrer">Source: Wikipedia</a></sup></p> <p>So you're to draw an exact construction of this one now!</p> <h2>Specification</h2> <p>You can render the flag to file or screen using vector or raster graphics. If your output is rasterised, your image must have dimensions of 450x300 pixels or more.</p> <p>All length ratios and positions need to be exactly as shown in the following diagram:</p> <p><img src="https://i.stack.imgur.com/En7VV.png" alt="enter image description here"></p> <p><sup><a href="http://en.wikipedia.org/wiki/File:Flag_of_South_Korea_(construction_sheet).svg" rel="noreferrer">Source: Wikipedia</a></sup></p> <p>Note that the centres of the two small circles align with the diagonal.</p> <p>For the colours, you must use the following palette:</p> <ul> <li>White: #FFFFFF</li> <li>Black: #000000</li> <li>Red: #C60C30</li> <li>Blue: #003478</li> </ul> <p>The black boundary must not be drawn <em>unless</em> your drawing system does not let you specify a 3:2 canvas for technical reasons (otherwise, you should use a 3:2 canvas and the flag should fill all of it). If you do need to add the boundary, it may be arbitrarily thick - the flag should be considered strictly <em>inside</em> the black boundary. Any other helping lines or length indicators must not be drawn. I.e., the flag should appear exactly as in the first image.</p> <h2>Scoring</h2> <p>This is code golf, so the shortest answer (in bytes) wins.</p> <p>As a word of warning, using existing compression (like embedding a compressed GIF) falls into <a href="https://codegolf.meta.stackexchange.com/q/1061/8478">several standard loopholes</a>.</p> https://codegolf.stackexchange.com/q/185140 14 Dihedral group D4 composition with custom labels xnor https://codegolf.stackexchange.com/users/20260 2019-05-04T01:28:03Z 2019-05-09T17:15:36Z <p>The dihedral group <span class="math-container">\$D_4\$</span> is the <a href="https://en.wikipedia.org/wiki/Symmetry_group" rel="nofollow noreferrer">symmetry group</a> of the square, that is the moves that transform a square to itself via rotations and reflections. It consists of 8 elements: rotations by 0, 90, 180, and 270 degrees, and reflections across the horizontal, vertical, and two diagonal axes.</p> <p><a href="https://i.stack.imgur.com/0Y558.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0Y558.gif" alt="The 8 elements of D4 acting on the square."></a></p> <p><em>The images are all from <a href="http://ecademy.agnesscott.edu/~lriddle/ifs/symmetric/D4example.htm" rel="nofollow noreferrer">this lovely page</a> by Larry Riddle.</em></p> <p>This challenge is about composing these moves: given two moves, output the move that's equivalent to doing them one after another. For instance, doing move 7 followed by move 4 is the same as doing move 5.</p> <p><a href="https://i.stack.imgur.com/JTollm.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JTollm.gif" alt="Composition example"></a></p> <p>Note that switching the order to move 4 then move 7 produces move 6 instead.</p> <p>The results are tabulated below; this is the <a href="https://en.wikipedia.org/wiki/Cayley_table" rel="nofollow noreferrer">Cayley table</a> of the group <span class="math-container">\$D_4\$</span>. So for example, inputs <span class="math-container">\$7, 4\$</span> should produce output <span class="math-container">\$5\$</span>.</p> <p><span class="math-container">\begin{array}{*{20}{c}} {} &amp; {\begin{array}{*{20}{c}} 1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8 \\ \end{array} } \\ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ \end{array} } &amp; {\boxed{\begin{array}{*{20}{c}} 1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8 \\ 2 &amp; 3 &amp; 4 &amp; 1 &amp; 8 &amp; 7 &amp; 5 &amp; 6\\ 3 &amp; 4 &amp; 1 &amp; 2 &amp; 6 &amp; 5 &amp; 8 &amp; 7\\ 4 &amp; 1 &amp; 2 &amp; 3 &amp; 7 &amp; 8 &amp; 6 &amp; 5\\ 5 &amp; 7 &amp; 6 &amp; 8 &amp; 1 &amp; 3 &amp; 2 &amp; 4\\ 6 &amp; 8 &amp; 5 &amp; 7 &amp; 3 &amp; 1 &amp; 4 &amp; 2\\ 7 &amp; 6 &amp; 8 &amp; 5 &amp; 4 &amp; 2 &amp; 1 &amp; 3\\ 8 &amp; 5 &amp; 7 &amp; 6 &amp; 2 &amp; 4 &amp; 3 &amp; 1\\ \end{array} }} \\ \end{array}</span></p> <p><strong>Challenge</strong></p> <p>Your goal is to implement this operation in as few bytes as possible, but in addition to the code, <strong>you also choose the labels</strong> that represent the moves 1 through 8. The labels must be <em>8 distinct numbers from 0 to 255</em>, or the 8 one-byte characters their code points represent.</p> <p>Your code will be given two of the labels from the 8 you've chosen, and must output the label that corresponds to their composition in the dihedral group <span class="math-container">\$D_4\$</span>.</p> <p><strong>Example</strong></p> <p>Say you've chosen the characters C, O, M, P, U, T, E, R for moves 1 through 8 respectively. Then, your code should implement this table.</p> <p><span class="math-container">\begin{array}{*{20}{c}} {} &amp; {\begin{array}{*{20}{c}} \, C \, &amp; \, O \, &amp; M \, &amp; P \, &amp; U \, &amp; \, T \, &amp; \, E \, &amp; R \, \\ \end{array} } \\ {\begin{array}{*{20}{c}} C \\ O \\ M \\ P \\ U \\ T \\ E \\ R \\ \end{array} } &amp; {\boxed{\begin{array}{*{20}{c}} C &amp; O &amp; M &amp; P &amp; U &amp; T &amp; E &amp; R \\ O &amp; M &amp; P &amp; C &amp; R &amp; E &amp; U &amp; T\\ M &amp; P &amp; C &amp; O &amp; T &amp; U &amp; R &amp; E\\ P &amp; C &amp; O &amp; M &amp; E &amp; R &amp; T &amp; U\\ U &amp; E &amp; T &amp; R &amp; C &amp; M &amp; O &amp; P\\ T &amp; R &amp; U &amp; E &amp; M &amp; C &amp; P &amp; O\\ E &amp; T &amp; R &amp; U &amp; P &amp; O &amp; C &amp; M\\ R &amp; U &amp; E &amp; T &amp; O &amp; P &amp; M &amp; C\\ \end{array} }} \\ \end{array}</span></p> <p>Given inputs E and P, you should output U. Your inputs will always be two of the letters C, O, M, P, U, T, E, R, and your output should always be one of these letters.</p> <p><strong>Text table for copying</strong></p> <pre><code>1 2 3 4 5 6 7 8 2 3 4 1 8 7 5 6 3 4 1 2 6 5 8 7 4 1 2 3 7 8 6 5 5 7 6 8 1 3 2 4 6 8 5 7 3 1 4 2 7 6 8 5 4 2 1 3 8 5 7 6 2 4 3 1 </code></pre> https://codegolf.stackexchange.com/q/85141 105 Draw the national flag of Iceland insertusernamehere https://codegolf.stackexchange.com/users/41859 2016-07-11T09:32:23Z 2019-04-25T10:54:14Z <p>This year's UEFA Euro 2016 is over and besides a couple of negative headlines there has been a very positive surprise as well – <em>the Iceland national football team</em>. Let's draw their national flag.</p> <p><strong>Input</strong></p> <p>Well, obviously this challenge has no input.</p> <p><strong>Output</strong></p> <ul> <li>Draw the flag of Iceland in any applicable visual format of <em>at least 100 x 72 pixels</em> or <em>25 x 18 characters</em>.</li> <li>Save the output to a file or present it instantly – example formats are: images like png, jpg etc., vector graphics, draw on HTML canvas or even use non-whitespace characters for visualization.</li> <li><a href="https://en.wikipedia.org/wiki/Flag_of_Iceland#Colors_of_the_flag" rel="noreferrer">Use these colors</a>: <em>blue</em>: <code>#0048e0</code>, <em>white</em>: <code>#ffffff</code> and <em>red</em>: <code>#d72828</code>.</li> <li>If your language doesn't support specific color values, use the standard values for <em>red</em>, <em>blue</em> and <em>white</em> from the <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#Colors" rel="noreferrer">ANSI color codes</a>.</li> <li>Draw the flag with the correct proportions, as shown in the figure below:<br><br> <img src="https://upload.wikimedia.org/wikipedia/commons/9/93/Flag_of_Iceland_%28with_dimensions%29.svg"></li> </ul> <p><strong>Boilerplate</strong></p> <ul> <li>You can write a program or a function. If it is an anonymous function, please include an example of how to invoke it.</li> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> so shortest answer in bytes wins.</li> <li>Standard loopholes are disallowed.</li> </ul> <hr> <p><strong>Leaderboard</strong></p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var QUESTION_ID = 85141; // Obtain this from the url // It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 41859; // This should be the user ID of the challenge author. var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;function answersUrl(index) {return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&amp;pagesize=100&amp;order=desc&amp;sort=creation&amp;site=codegolf&amp;filter=" + ANSWER_FILTER;}function commentUrl(index, answers) {return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&amp;pagesize=100&amp;order=desc&amp;sort=creation&amp;site=codegolf&amp;filter=" + COMMENT_FILTER;}function getAnswers() {jQuery.ajax({url: answersUrl(answer_page++),method: "get",dataType: "jsonp",crossDomain: true,success: function (data) {answers.push.apply(answers, data.items);answers_hash = [];answer_ids = [];data.items.forEach(function(a) {a.comments = [];var id = +a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id] = a;});if (!data.has_more) more_answers = false;comment_page = 1;getComments();}});}function getComments() {jQuery.ajax({url: commentUrl(comment_page++, answer_ids),method: "get",dataType: "jsonp",crossDomain: true,success: function (data) {data.items.forEach(function(c) {if (c.owner.user_id === OVERRIDE_USER)answers_hash[c.post_id].comments.push(c);});if (data.has_more) getComments();else if (more_answers) getAnswers();else process();}});}getAnswers();var SCORE_REG = /&lt;h\d&gt;\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d&lt;&gt;]*(?:&lt;(?:s&gt;[^\n&lt;&gt;]*&lt;\/s&gt;|[^\n&lt;&gt;]+&gt;)[^\n\d&lt;&gt;]*)*&lt;\/h\d&gt;)/;var OVERRIDE_REG = /^Override\s*header:\s*/i;function getAuthorName(a) {return a.owner.display_name;}function process() {var valid = [];answers.forEach(function(a) {var body = a.body;a.comments.forEach(function(c) {if(OVERRIDE_REG.test(c.body))body = '&lt;h1&gt;' + c.body.replace(OVERRIDE_REG, '') + '&lt;/h1&gt;';});var match = body.match(SCORE_REG);if (match)valid.push({user: getAuthorName(a),size: +match,language: match,link: a.share_link,});});valid.sort(function (a, b) {var aB = a.size,bB = b.size;return aB - bB});var languages = {};var place = 1;var lastSize = null;var lastPlace = 1;valid.forEach(function (a) {if (a.size != lastSize)lastPlace = place;lastSize = a.size;++place;var answer = jQuery("#answer-template").html();answer = answer.replace("{{PLACE}}", lastPlace + ".").replace("{{NAME}}", a.user).replace("{{LANGUAGE}}", a.language).replace("{{SIZE}}", a.size).replace("{{LINK}}", a.link);answer = jQuery(answer);jQuery("#answers").append(answer);var lang = a.language;if (! /&lt;a/.test(lang)) lang = '&lt;i&gt;' + lang + '&lt;/i&gt;';lang = jQuery(lang).text().toLowerCase();languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link, uniq: lang};});var langs = [];for (var lang in languages)if (languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function (a, b) {if (a.uniq &gt; b.uniq) return 1;if (a.uniq &lt; b.uniq) return -1;return 0;});for (var i = 0; i &lt; langs.length; ++i){var language = jQuery("#language-template").html();var lang = langs[i];language = language.replace("{{LANGUAGE}}", lang.lang).replace("{{NAME}}", lang.user).replace("{{SIZE}}", lang.size).replace("{{LINK}}", lang.link);language = jQuery(language);jQuery("#languages").append(language);}}</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>body { text-align: left !important}#answer-list {padding: 10px;width: 290px;float: left;}#language-list {padding: 10px;width: 290px;float: left;}table thead {font-weight: bold;}table td {padding: 5px;}</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;&lt;link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/codegolf/all.css?v=617d0685f6f3"&gt;&lt;div id="answer-list"&gt;&lt;h2&gt;Leaderboard&lt;/h2&gt;&lt;table class="answer-list"&gt;&lt;thead&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Author&lt;/td&gt;&lt;td&gt;Language&lt;/td&gt;&lt;td&gt;Size&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody id="answers"&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div id="language-list"&gt;&lt;h2&gt;Winners by Language&lt;/h2&gt;&lt;table class="language-list"&gt;&lt;thead&gt;&lt;tr&gt;&lt;td&gt;Language&lt;/td&gt;&lt;td&gt;User&lt;/td&gt;&lt;td&gt;Score&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody id="languages"&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;table style="display: none"&gt;&lt;tbody id="answer-template"&gt;&lt;tr&gt;&lt;td&gt;{{PLACE}}&lt;/td&gt;&lt;td&gt;{{NAME}}&lt;/td&gt;&lt;td&gt;{{LANGUAGE}}&lt;/td&gt;&lt;td&gt;&lt;a href="{{LINK}}"&gt;{{SIZE}}&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;table style="display: none"&gt;&lt;tbody id="language-template"&gt;&lt;tr&gt;&lt;td&gt;{{LANGUAGE}}&lt;/td&gt;&lt;td&gt;{{NAME}}&lt;/td&gt;&lt;td&gt;&lt;a href="{{LINK}}"&gt;{{SIZE}}&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</code></pre> </div> </div> </p> <p><sup>If you can't find your answer, format your language and byte count as explained in the <a href="https://codegolf.meta.stackexchange.com/q/5139/41859">Leaderboard's "<em>Info Section</em>"</a>.</sup></p> <hr> <p><sup>This challenge is inspired by <a href="https://codegolf.stackexchange.com/questions/64140/draw-the-national-flag-of-france">Draw the national flag of france</a>.</sup></p> https://codegolf.stackexchange.com/q/126336 19 What is the area of this polygon? JAD https://codegolf.stackexchange.com/users/59530 2017-06-14T13:25:04Z 2019-04-15T14:20:40Z <h1>Calculate the area of a polygon.</h1> <p>Inspired by this <a href="https://www.youtube.com/watch?v=0KjG8Pg6LGk&amp;t=0s" rel="nofollow noreferrer">shoelace algorithm video.</a></p> <h2>Task</h2> <p>Your job is to create a program or function that calculates the area of a polygon. Program or function is defined according the the default definition in meta.</p> <h2>Input</h2> <p>You will receive the X and Y coordinates of each vertex of the polygon. You can take the input as a list of tuples (<code>[[x1, y1], [x2, y2], etc]</code>), a matrix, or a flat list (<code>[x1, y1, x2, y2, etc]</code>). Two lists containing <code>x</code> and <code>y</code> coordinates respectively are allowed as well. The vertices are numbered counterclockwise and the first vertex is the same as the last vertex provided, thus closing the polygon.</p> <p>If you want you can take the input without the last vertex (so receive each coordinate just once).</p> <p>You can assume that the edges of the polygons don't intersect. You can also assume that all vertices have integer coordinates.</p> <h2>Output</h2> <p>The area of the polygon. All standard output methods are allowed. If your language does not allow for float division and the solution would not be an integer, you are allowed to return a fraction. The fraction does not necessarily have to be simplified, so returning <code>2/4</code> would be allowed.</p> <h2>Winning criterium</h2> <p>Shortest code wins!</p> <h2>Test cases</h2> <pre><code>[[4,4],[0,1],[-2,5],[-6,0],[-1,-4],[5,-2],[4,4]] 55 </code></pre> <p><a href="https://i.stack.imgur.com/GWDit.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GWDit.png" alt="enter image description here"></a></p> <pre><code>[[1,1],[0,1],[1,0],[1,1]] 0.5 1/2 </code></pre> <p><a href="https://i.stack.imgur.com/hwdr5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hwdr5.png" alt="enter image description here"></a></p> https://codegolf.stackexchange.com/q/50299 169 Draw an Image as a Voronoi Map Martin Ender https://codegolf.stackexchange.com/users/8478 2015-05-16T15:06:38Z 2019-03-27T12:08:45Z <p><em>Credits to Calvin's Hobbies for nudging my challenge idea in the right direction.</em></p> <p>Consider a set of points in the plane, which we will call <strong>sites</strong>, and associate a colour with each site. Now you can paint the entire plane by colouring each point with the colour of the closest site. This is called a Voronoi map (or <a href="http://en.wikipedia.org/wiki/Voronoi_diagram" rel="noreferrer">Voronoi diagram</a>). In principle, Voronoi maps can be defined for any distance metric, but we'll simply use the usual Euclidean distance <code>r = √(x² + y²)</code>. <em>(<strong>Note:</strong> You do not necessarily have to know how to compute and render one of these to compete in this challenge.)</em></p> <p>Here is an example with 100 sites:</p> <p><img src="https://i.stack.imgur.com/9v7DT.png" alt="enter image description here"></p> <p>If you look at any cell, then all points within that cell are closer to the corresponding site than to any other site.</p> <p>Your task is to approximate a given image with such a Voronoi map. You're given the image in any convenient raster graphics format, as well as an integer <strong>N</strong>. You should then produce up to <strong>N</strong> sites, and a colour for each site, such that the Voronoi map based on these sites resembles the input image as closely as possible.</p> <p>You can use the Stack Snippet at the bottom of this challenge to render a Voronoi map from your output, or you can render it yourself if you prefer.</p> <p>You <em>may</em> use built-in or third-party functions to compute a Voronoi map from a set of sites (if you need to).</p> <p>This is a popularity contest, so the answer with the most net votes wins. Voters are encouraged to judge answers by</p> <ul> <li>how well the original images and their colours are approximated.</li> <li>how well the algorithm works on different kinds of images.</li> <li>how well the algorithm works for small <strong>N</strong>.</li> <li>whether the algorithm adaptively clusters points in regions of the image that require more detail.</li> </ul> <h2>Test Images</h2> <p>Here are a few images to test your algorithm on (some of our usual suspects, some new ones). Click the pictures for larger versions.</p> <p><a href="http://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Great_Wave_off_Kanagawa2.jpg/800px-Great_Wave_off_Kanagawa2.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/56MVN.png" alt="Great Wave"></a> <a href="https://i.stack.imgur.com/5eUHY.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/SO19D.png" alt="Hedgehog"></a> <a href="https://i.stack.imgur.com/IIiLc.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Ji4fa.png" alt="Beach"></a> <a href="https://i.stack.imgur.com/4iKrE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XiClh.png" alt="Cornell"></a> <a href="https://i.stack.imgur.com/Du35b.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/PNA8p.png" alt="Saturn"></a> <a href="http://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Brown_bear_%28Ursus_arctos_arctos%29_running.jpg/796px-Brown_bear_%28Ursus_arctos_arctos%29_running.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/vFMZu.png" alt="Brown Bear"></a> <a href="https://i.stack.imgur.com/XC4zY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/buEyI.png" alt="Yoshi"></a> <a href="https://i.stack.imgur.com/fCTEl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/B66Zs.png" alt="Mandrill"></a> <a href="http://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/600px-Crab_Nebula.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/6AjIN.png" alt="Crab Nebula"></a> <a href="https://i.imgur.com/UM4JWlM.png" rel="noreferrer"><img src="https://i.imgur.com/nzNTH54.png" alt="Geobits&#39; Kid"></a> <a href="http://upload.wikimedia.org/wikipedia/en/e/e8/Escher_Waterfall.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/JTsT2.png" alt="Waterfall"></a> <a href="https://i.stack.imgur.com/I3XrT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/l2wGm.png" alt="Scream"></a></p> <p><em>The beach in the first row was drawn by <a href="http://pairlee.tumblr.com/" rel="noreferrer">Olivia Bell</a>, and included with her permission.</em></p> <p>If you want an extra challenge, try <a href="https://i.stack.imgur.com/PzxvJ.png" rel="noreferrer">Yoshi with a white background</a> and get his belly line right.</p> <p>You can find all of these test images <a href="https://imgur.com/a/I8x5G" rel="noreferrer">in this imgur gallery</a> where you can download all of them as a zip file. The album also contains a random Voronoi diagram as another test. For reference, <a href="http://pastebin.com/ZQRNwNTk" rel="noreferrer">here is the data that generated it</a>.</p> <p>Please include example diagrams for a variety of different images and <strong>N</strong>, e.g. 100, 300, 1000, 3000 (as well as pastebins to some of the corresponding cell specifications). You may use or omit black edges between the cells as you see fit (this may look better on some images than on others). Do not include the sites though (except in a separate example maybe if you want to explain how your site placement works, of course).</p> <p>If you want to show a large number of results, you can create a gallery over on <a href="http://imgur.com/" rel="noreferrer">imgur.com</a>, to keep the size of the answers reasonable. Alternatively, put thumbnails in your post and make them links to larger images, like I did <a href="https://codegolf.stackexchange.com/a/50301/8478">in my reference answer</a>. You can get the small thumbnails by appending <code>s</code> to the file name in the imgur.com link (e.g. <code>I3XrT.png</code> -> <code>I3XrTs.png</code>). Also, feel free to use other test images, if you find something nice.</p> <h2>Renderer</h2> <p>Paste your output into the following Stack Snippet to render your results. The exact list format is irrelevant, as long as each cell is specified by 5 floating point numbers in the order <code>x y r g b</code>, where <code>x</code> and <code>y</code> are the coordinates of the cell's site, and <code>r g b</code> are the red, green and blue colour channels in the range <code>0 ≤ r, g, b ≤ 1</code>.</p> <p>The snippet provides options to specify a line width of the cell edges, and whether or not the cell sites should be shown (the latter mostly for debugging purposes). But note that the output is only re-rendered when the cell specifications change - so if you modify some of the other options, add a space to the cells or something.</p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function draw() { document.getElementById("output").innerHTML = svg } function drawMap() { var cells = document.getElementById("cells").value; var showSites = document.getElementById("showSites").checked; var showCells = document.getElementById("showCells").checked; var lineWidth = parseFloat(document.getElementById("linewidth").value); var width = parseInt(document.getElementById("width").value); var height = parseInt(document.getElementById("height").value); var e = prefix.replace('{{WIDTH}}', width) .replace('{{HEIGHT}}', height); cells = cells.match(/(?:\d*\.\d+|\d+\.\d*|\d+)(?:e-?\d+)?/ig); var sitesDom = ''; var sites = [] for (i = 0; i &lt; cells.length; i+=5) { x = parseFloat(cells[i]); y = parseFloat(cells[i+1]); r = Math.floor(parseFloat(cells[i+2])*255); g = Math.floor(parseFloat(cells[i+3])*255); b = Math.floor(parseFloat(cells[i+4])*255); sitesDom += '&lt;circle cx="' + x + '" + cy="' + y + '" r="1" fill="black"/&gt;'; sites.push({ x: x, y: y, r: r, g: g, b: b }); } if (showCells) { var bbox = { xl: 0, xr: width, yt: 0, yb: height }; var voronoi = new Voronoi(); var diagram = voronoi.compute(sites, bbox); for (i = 0; i &lt; diagram.cells.length; ++i) { var cell = diagram.cells[i]; var site = cell.site; var coords = ''; for (var j = 0; j &lt; cell.halfedges.length; ++j) { var vertex = cell.halfedges[j].getStartpoint(); coords += ' ' + vertex.x + ',' + vertex.y; } var color = 'rgb('+site.r+','+site.g+','+site.b+')'; e += '&lt;polygon fill="'+color+'" points="' + coords + '" stroke="' + (lineWidth ? 'black' : color) + '" stroke-width="'+Math.max(0.6,lineWidth)+'"/&gt;'; } } if (showSites) e += sitesDom; e += suffix; e += '&lt;br&gt; Using &lt;b&gt;'+sites.length+'&lt;/b&gt; cells.'; svg = e; draw(); } var prefix = '&lt;svg width="{{WIDTH}}" height="{{HEIGHT}}"&gt;', suffix = "&lt;/svg&gt;", scale = 0.95, offset = 225, radius = scale*offset, svg = "";</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>svg { position: relative; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="http://www.raymondhill.net/voronoi/rhill-voronoi-core.js"&gt;&lt;/script&gt; Line width: &lt;input id="linewidth" type="text" size="1" value="0" /&gt; &lt;br&gt; Show sites: &lt;input id="showSites" type="checkbox" /&gt; &lt;br&gt; Show cells: &lt;input id="showCells" type="checkbox" checked="checked" /&gt; &lt;br&gt; &lt;br&gt; Dimensions: &lt;input id="width" type="text" size="1" value="400" /&gt; x &lt;input id="height" type="text" size="1" value="300" /&gt; &lt;br&gt; Paste cell specifications here &lt;br&gt; &lt;textarea id="cells" cols="60" rows="10" oninput='drawMap()'&gt;&lt;/textarea&gt; &lt;br&gt; &lt;br&gt; &lt;div id="output"&gt;&lt;/div&gt;</code></pre> </div> </div> </p> <p>Massive credits to Raymond Hill for writing <a href="https://github.com/gorhill/Javascript-Voronoi" rel="noreferrer">this really nice JS Voronoi library</a>.</p> <h3>Related Challenges</h3> <ul> <li><a href="https://codegolf.stackexchange.com/q/42217/8478">Paint by Numbers</a></li> <li><a href="https://codegolf.stackexchange.com/q/34484/8478">Photomosaics or: How Many Programmers Does it Take to Replace a Light Bulb?</a></li> </ul> https://codegolf.stackexchange.com/q/38093 57 Little Chandler is sad. Draw him a cloud to cheer him up absinthe https://codegolf.stackexchange.com/users/21682 2014-09-23T03:47:37Z 2019-03-22T21:45:51Z <p>Little Chandler is sad. Draw him a cloud to cheer him up.<br> <em>Note: Drawing a cloud won't actually cheer him up.</em></p> <p>A circle can be defined as a 3-tuple <code>(x,y,r)</code> where <code>x</code> is the x position of the circle on a Cartesian plane, <code>y</code> is the y position of the circle on a Cartesian plane, and <code>r</code> is the radius of the circle. <code>x</code> and <code>y</code> may be negative. <code>r</code> is always positive. The input is a list of circles in the form of space separated 3-tuples. For instance:</p> <pre><code>3,1,1 3,2,1.5 1,2,0.7 0.9,1.2,1.2 1,0,0.8 </code></pre> <p>The <code>3,1,1</code> means "A circle with centre point at <code>3,1</code> with 1 radius. <code>3,2,1.5</code> means "A circle with centre point at <code>3,2</code> with 1.5 radius.</p> <p>If we draw all of these circles of the input on a graph, it looks like this (I've included grid lines and labels for clarity only; they are not required):</p> <p><img src="https://i.stack.imgur.com/jyZzd.png" alt="Graph"></p> <p>Notice how all the circles are <em>cohesive</em>. That is, they're all overlapped together such that it forms one large group without any small groups of circles separated from the rest. The input is <em>guaranteed</em> to be cohesive. </p> <p>Suppose now we draw a line that travels around the "border" formed by these circles, without any of the other lines. It would be like drawing the border of the silhouette formed by all the circles. The resulting cloud would look something like this:</p> <p><img src="https://i.stack.imgur.com/zDuUa.png" alt="cloud" title="Yes, I know it looks nothing like a cloud. Just... use your imagination."></p> <p>So this cloud has been formed by drawing only the arcs of the circles in the input that form a border, resulting in a single shape. In other words, the cloud is formed by only drawing the arcs that are not within another circle. Your program will take in input in the form explained above, and output an image that displays the resulting cloud. The overall shape of the cloud must be correct, but the scale, colour, line thickness, and how it looks at vertexes is up to you. Note that the cloud must be visible, so you can't pull something like "This program draws a white cloud on a white background", "This program draws a cloud at a infinitesimally small scale", "This program draws a cloud with 0 line thickness", etc. Also note that the colour of the border must be different from the colour of the fill or background.</p> <p>Another example. Input: </p> <pre><code>1,4,2 5,2,1 3,1,1 3.5,2,1.2 3,3,0.7 1,2,0.7 </code></pre> <p>Output:</p> <p><img src="https://i.stack.imgur.com/mLzXk.png" alt="cloud2"></p> <p>If there is a "hole" in the cloud, you should draw the hole as well. Input:</p> <pre><code>0,5,4 3,4,4 4,3,4 5,0,4 4,-3,4 3,-4,4 0,-5,4 -3,-4,4 -4,-3,4 -5,0,4 -4,3,4 -3,4,4 </code></pre> <p>Output:</p> <p><img src="https://i.stack.imgur.com/F5K2j.png" alt="cloud3"></p> <p>Here is an important rule: your program must draw <em>only</em> the lines that form the border. This means that you CANNOT simply draw the circles fully, and then draw the circles slightly smaller with a white fill -- because that method still draws lines that don't form a border, it just covers them up afterwards. The purpose of the rule was to prevent the "draw the circles, then draw the circles again with a white fill" implementations, or anything similar to that. The answer is expected to actually calculate where to draw things before drawing it.</p> <p>This is code golf, so the shortest character count wins. </p> https://codegolf.stackexchange.com/q/129983 20 Rectangular difference Nathan Merrill https://codegolf.stackexchange.com/users/20198 2017-07-05T15:26:40Z 2019-03-11T15:53:00Z <p>In this challenge, you are given two overlapping rectangles, and you need to calculate the rectangles created by removing one from the other.</p> <p>For example, if you remove the red rectangle from the black one:</p> <p><a href="https://i.stack.imgur.com/cisSA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cisSA.png" alt="rectangles"></a></p> <p>You end up with one of the following two rectangle sets:</p> <p><a href="https://i.stack.imgur.com/N6TeR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N6TeR.png" alt="split-one"></a> <a href="https://i.stack.imgur.com/MNbb6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MNbb6.png" alt="split-two"></a></p> <p>You'll also need to handle the following:</p> <p><a href="https://i.stack.imgur.com/0kFov.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0kFov.png" alt="All test cases"></a></p> <p>To be more explicit:</p> <ul> <li>You will input the coordinates of two rectangles, A and B.</li> <li>You need to output the fewest non-overlapping rectangles that cover all of the area of A without B. Any possible covering is allowed</li> <li>Rectangular coordinates are passed as 4 integers. You can pass them in two pairs (representing the two corner points), or as a tuple/list of 4 integers. Your inputs and outputs need to be consistent.</li> <li>A and B will not necessarily overlap or touch, and each will have an area of at least 1</li> </ul> <h1>Test cases:</h1> <pre><code>[(0 0) (5 5)] [(3 4) (8 7)] -&gt; [(0 0) (5 4)] [(0 4) (3 5)] # or [(0 0) (3 5)] [(3 0) (5 4)] [(2 4) (10 11)] [(5 5) (6 6)] -&gt; [(2 4) (10 5)] [(2 5) (5 6)] [(6 5) (10 6)] [(2 6) (10 11)] #Other sets of 4 rectangles are possible [(3 3) (8 8)] [(0 1) (10 8)] -&gt; #No rectangles should be output [(0 0) (5 5)] [(1 1) (10 2)] -&gt; [(0 0) (1 5)] [(1 0) (2 1)] [(2 0) (5 5)] #Other sets of 3 rectangles are possible [(1 5) (7 8)] [(0 0) (1 10)] -&gt; [(1 5) (7 8)] #Only possible output [(4 1) (10 9)] [(2 5) (20 7)] -&gt; [(4 1) (10 5)] [(4 7) (10 9)] #Only possible output [(1 1) (8 8)] [(0 6) (9 9)] -&gt; [(1 1) (8 6)] #Only possible output </code></pre> <p>This is a <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so make your code as short as possible!</p> https://codegolf.stackexchange.com/q/162798 27 Diamond creator + Muhammad Salman https://codegolf.stackexchange.com/users/79762 2018-04-19T06:57:16Z 2019-03-11T00:20:54Z <h1>Challenge :</h1> <p>Given an integer <code>n</code> as input. Create a diamond that is 2x the given number <code>n</code>.</p> <h1>Input :</h1> <p>Input is integer <code>n</code> and 2 &lt; n ≤ 3000.</p> <h1>Output :</h1> <p>Output will be a string and it will be in form of a diamond consisting of <code>+</code> with an addition line at the start showing <code>n</code> using <code>+</code></p> <h1>Examples :</h1> <pre> D(3) : +++ + +++ +++++ +++++ +++ + D(5) : +++++ + +++ +++++ +++++++ +++++++++ +++++++++ +++++++ +++++ +++ + D(6) : ++++++ + +++ +++++ +++++++ +++++++++ +++++++++++ +++++++++++ +++++++++ +++++++ +++++ +++ + </pre> <h1>Winning Criteria :</h1> <p>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> so shortest code in bytes for each programming language wins.</p> https://codegolf.stackexchange.com/q/157162 6 Stereographic projection of polyhedra PyRulez https://codegolf.stackexchange.com/users/16842 2018-03-03T21:50:07Z 2019-02-27T22:01:12Z <p>You will create a program that generates the stereographic projection of polyhedra.</p> <p>In particular, to keep things simple, we'll only focus on n-<a href="https://en.wikipedia.org/wiki/Chamfer_(geometry)" rel="nofollow noreferrer">chamfered</a> <a href="https://en.wikipedia.org/wiki/Regular_dodecahedron" rel="nofollow noreferrer">dodecahedron</a>.</p> <p>Given a natural number <code>n</code> as input, and also a natural number <code>d</code> your program will output an <code>d</code> pixels by <code>d</code> pixels image which is the <a href="https://en.wikipedia.org/wiki/Stereographic_projection" rel="nofollow noreferrer">stereographic projection</a> of the dodecahedron which has been chamfered <code>n</code> times. This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so the shortest program wins!</p> <h2>Examples</h2> <ul> <li><code>0</code> -> <a href="https://commons.wikimedia.org/wiki/File:Dodecahedron_stereographic_projection.png" rel="nofollow noreferrer"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Dodecahedron_stereographic_projection.png/240px-Dodecahedron_stereographic_projection.png" alt=""></a> (the regular dodecahedron)</li> </ul> <h2>Notes</h2> <ul> <li>You must select a color for the background, edges, and vertices. The color of the edges and vertices may coincide, but the color of the background must coincide with neither.</li> <li>You may treat the polyhedra either <a href="https://en.wikipedia.org/wiki/Spherical_polyhedron" rel="nofollow noreferrer">spherically</a> or straight edged.</li> <li>The center of the image must be the center of a pentagon.</li> <li>Since stereographic projection results in an infinitely large image, you only need to have at least 75% of the vertices in the image.</li> <li>EDIT: Since I never specified you can chamfer by any non-trivial amount. That being said, it would be nice if it looked like the images of chamfering below (i.e. almost regular).</li> </ul> <h2>Smiley Bonus</h2> <p>The n-chamfered dodecahedron is the $GP(2^n,0)$ goldberg polyhedron (where $GP(x,y)$ means a polyhedron consisting of hexagons and pentagons where if you start at one pentagon and move x spaces one direction, turn 60 degrees clockwise, and y spaces in that direction, you end up at another pentagon). Therefore, it might be easier to just calculate $GP(x,y)$ for arbitrary $x$ and $y$. If you do, you get a Smiley bonus (I can't award points since the challenge has already started).</p> <h1>Background</h1> <p>A dodecahedron is polyhedron with 12 sides, each pentagons. It looks like this</p> <p><a href="https://en.wikipedia.org/wiki/File:Dodecahedron.gif" rel="nofollow noreferrer"><img src="https://upload.wikimedia.org/wikipedia/commons/7/73/Dodecahedron.gif" alt=""></a></p> <p>When you chamfer a shape, you replace each face with a smaller face, and each edge with a hexagonal face. For instance, here is what happens when you chamfer a dodecahedron (note, this is not the same <a href="https://en.wikipedia.org/wiki/Truncated_icosahedron" rel="nofollow noreferrer">shape</a> as a soccer/football)</p> <p><a href="https://commons.wikimedia.org/wiki/File:Truncated_rhombic_triacontahedron.png" rel="nofollow noreferrer"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/Truncated_rhombic_triacontahedron.png/240px-Truncated_rhombic_triacontahedron.png" alt=""></a></p> <p>Here is what it looks like when you chamfer it again (i.e. n=2) <a href="http://levskaya.github.io/polyhedronisme/?recipe=A10ccD&amp;palette=%23ff7777%20%23dddddd" rel="nofollow noreferrer">polyHédronisme</a> (sorry, the image export doesn't seem to be working).</p> <p>Stereographic projection means that vertices of the polyhedron on a sphere. Place a plane tangent to it, and point on the opposite side of the sphere (called the view point). Then, draw a line between each point of the polyhedron and the view point. Extend this line to the plane, and map that point there. Do this for all the points. See above for an example of the stereographic projection of the dodecahedron.</p> https://codegolf.stackexchange.com/q/179938 25 How lit is this room? 🔥 pt. 1 bushdid911 https://codegolf.stackexchange.com/users/72190 2019-02-14T20:54:58Z 2019-02-26T08:35:03Z <p>Related to <a href="https://codegolf.stackexchange.com/questions/151501/how-lit-is-this-mountain">this question</a>.</p> <p>A <em>room</em> is defined to be a (not necessarily convex) non-intersecting polygon, expressed as an ordered list of 2-dimensional coordinates. A sufficiently bright lightbulb is placed at a specific point inside the room, and emits light in every direction. Your task is to find the total illuminated area of the room. You may take in input in any reasonable format. The points on the polygon/room as well as the coordinates of the light source are rational numbers. They can be taken in clockwise or counterclockwise, either format is fine. The test case in the problem is given counterclockwise.</p> <p>The following picture shows two example rooms, where the purplish dot represents the light source, and the shaded region represents the illuminated area.<a href="https://i.stack.imgur.com/ggWS0.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ggWS0.jpg" alt="A drawing of an illuminated room - shaded area is illuminated"></a></p> <p>Test case:</p> <pre><code>(1/2, 18) (1,3) (5,1/2) (7,5) (12,7) (16,3) (15,11) (8,19) (3,7) Light source located at (5,8) Answer: 815523/6710 ≈ 121.538 </code></pre> <p>Here is a graphical depiction of the solution to that test case. The two points that define the solution that are not on the original polygon are (55/61, 363/61) and (856/55, 357/55). <a href="https://i.stack.imgur.com/cWgdG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cWgdG.png" alt="enter image description here"></a></p> <p>This formula may be helpful in calculating the area. <a href="https://i.stack.imgur.com/BabEn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/BabEn.png" alt="https://en.wikipedia.org/wiki/Shoelace_formula"></a> </p> <p>Since this is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, the shortest code in bytes wins.</p> https://codegolf.stackexchange.com/q/180226 8 Can this container hold this much liquid? Gabriel Mills https://codegolf.stackexchange.com/users/82676 2019-02-21T19:25:40Z 2019-02-25T01:12:12Z <h1>Can this container hold this much liquid?</h1> <h3>Challenge Synopsis</h3> <p>As you most likely know, <a href="https://en.wikipedia.org/wiki/Liquid" rel="nofollow noreferrer">liquids</a> have an indefinite shape and a definite volume. As such, they always take the shape of their container. They cannot, however, expand to fill their container.</p> <p>Your job today is to determine whether or not a certain amount of liquid (represented by a certain number of <code>L</code> characters or numbers representing the volume of the part, as per suggestion) can fit into a container of a certain size (represented by a matrix of <code>C</code> characters) with some amount of empty space (represented by space characters) within it. The container will always have <code>C</code> characters all the way around the perimeter.</p> <p>Your program will return a <a href="https://codegolf.meta.stackexchange.com/questions/2190/interpretation-of-truthy-falsey">truthy/falsey value</a> based on whether the liquid will fit into the container. It will only fit if there is an area of connected empty space (made up of spaces adjacent to one another horizontally, diagonally, or vertically) within the container for each part of the liquid that is separated from the rest (either by a space or by two newline characters).</p> <h3>Test Cases</h3> <pre><code>LLL L ----- True CCCCC C CC C CC CCCCC LLL LL ------ True CCCCCC C C C C CCC CCCCCC L L LLL ----- False (Not enough space) CCCCC CCCCC C CC CCCCC LL ------ False (Spaces are not connected but liquid is) CCCCCC CCCC C C CCCC CCCCCC L L ------ True CCCCCC CCCC C C CCCC CCCCCC L L ------ True (There is a pocket of empty space which holds both parts of the liquid) CCCCCC CCC C CCCCCC CCCCCC L L ------ True (There is a pocket of empty space for each part of the liquid) CCCCCC CCCC C C CCCC CCCCCC L L L LL ------ True CCCCCCCCC CCCC C C C CCCCCCC CCCCCC CC CCCCCCCCC L L ----- True CCCCC CCCCC C CC CCCCC </code></pre> <p><i>Feel free to suggest test cases!</i></p> <h3>Rules</h3> <ul> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so the shortest answer in bytes wins.</li> <li>Standard loopholes are disallowed.</li> </ul> https://codegolf.stackexchange.com/q/32898 39 Check if point lies inside triangle xnor https://codegolf.stackexchange.com/users/20260 2014-07-03T19:05:54Z 2019-02-12T21:43:44Z <p>Your goal is to determine whether a given 2D point X lies within the area of the triangle with given vertices A,B,C.</p> <p>Write a function that takes in the coordinates of the test point X and the three triangle vertices (so that's 8 coordinates total) and returns True if the point lies inside that triangle, and False if it lies outside.</p> <p>Don't worry about edge cases. If the point lies on the boundary of the triangle (edge or vertex) or the triangle is actually a line segment, your code can do whatever, including crashing. Also don't worry about numerical stability or floating-point precision.</p> <p>Your code must be a named function. Code snippets will not be accepted.</p> <p>Fewest characters wins.</p> <p><strong>Input:</strong></p> <p>Eight real numbers representing coordinates. The numbers will lie in the range <code>(-1,1)</code>.</p> <p>The exact input format is flexible. You could, for example, take in eight numbers, a list of eight numbers, a list of four points each given by a tuple, a 2*4 matrix, four complex numbers, two lists of the x-coordinates and y-coordinates, and so on. </p> <p>The input needs to just be the numbers in some container, with no additional data. You can't use the input to do any preprocessing, nor may you require any constraints on the input, such as requiring the points be given in ascending y coordinate. Your input must allow any eight coordinates (though your code can behave arbitrarily in the edge cases mentioned earlier).</p> <p>Please state your input format. </p> <p><strong>Output:</strong></p> <p>Either the corresponding Boolean <code>True</code>/<code>False</code>, the corresponding number <code>1</code>/<code>0</code>, or the analogues in your language.</p> <p><strong>Test cases</strong></p> <p>The inputs are given a list <code>[X,A,B,C]</code> of four tuples, the test point first, then the three triangle vertices. I've grouped them into those whose outputs should be <code>True</code> and those that should be <code>False</code>.</p> <p><code>True</code> instances:</p> <pre><code>[(-0.31961, -0.12646), (0.38478, 0.37419), (-0.30613, -0.59754), (-0.85548, 0.6633)] [(-0.87427, -0.00831), (0.78829, 0.60409), (-0.90904, -0.13856), (-0.80685, 0.48468)] [(0.28997, -0.03668), (-0.28362, 0.42831), (0.39332, -0.07474), (-0.48694, -0.10497)] [(-0.07783, 0.04415), (-0.34355, -0.07161), (0.59105, -0.93145), (0.29402, 0.90334)] [(0.36107, 0.05389), (0.27103, 0.47754), (-0.00341, -0.79472), (0.82549, -0.29028)] [(-0.01655, -0.20437), (-0.36194, -0.90281), (-0.26515, -0.4172), (0.36181, 0.51683)] [(-0.12198, -0.45897), (-0.35128, -0.85405), (0.84566, 0.99364), (0.13767, 0.78618)] [(-0.03847, -0.81531), (-0.18704, -0.33282), (-0.95717, -0.6337), (0.10976, -0.88374)] [(0.07904, -0.06245), (0.95181, -0.84223), (-0.75583, -0.34406), (0.16785, 0.87519)] [(-0.33485, 0.53875), (-0.25173, 0.51317), (-0.62441, -0.90698), (-0.47925, 0.74832)] </code></pre> <p><code>False</code> instances:</p> <pre><code>[(-0.99103, 0.43842), (0.78128, -0.10985), (-0.84714, -0.20558), (-0.08925, -0.78608)] [(0.15087, -0.56212), (-0.87374, -0.3787), (0.86403, 0.60374), (0.01392, 0.84362)] [(0.1114, 0.66496), (-0.92633, 0.27408), (0.92439, 0.43692), (0.8298, -0.29647)] [(0.87786, -0.8594), (-0.42283, -0.97999), (0.58659, -0.327), (-0.22656, 0.80896)] [(0.43525, -0.8923), (0.86119, 0.78278), (-0.01348, 0.98093), (-0.56244, -0.75129)] [(-0.73365, 0.28332), (0.63263, 0.17177), (-0.38398, -0.43497), (-0.31123, 0.73168)] [(-0.57694, -0.87713), (-0.93622, 0.89397), (0.93117, 0.40775), (0.2323, -0.30718)] [(0.91059, 0.75966), (0.60118, 0.73186), (0.32178, 0.88296), (-0.90087, -0.26367)] [(0.3463, -0.89397), (0.99108, 0.13557), (0.50122, -0.8724), (0.43385, 0.00167)] [(0.88121, 0.36469), (-0.29829, 0.21429), (0.31395, 0.2734), (0.43267, -0.78192)] </code></pre> https://codegolf.stackexchange.com/q/179464 43 Covering a Skyline with brush strokes Adám https://codegolf.stackexchange.com/users/43319 2019-02-04T11:12:33Z 2019-02-11T19:47:36Z <p>Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.</p> <p><code>[1,3,2,1,2,1,5,3,3,4,2]</code>, visualised as:</p> <pre><code> 5 5 4 3 5334 32 2 53342 13212153342 </code></pre> <p>needs nine brush strokes:</p> <pre><code> 1 2 3 4 5555 66 7 88888 99999999999 </code></pre> <h3>Examples</h3> <p><code>[1,3,2,1,2,1,5,3,3,4,2]</code> → <code>9</code></p> <p><code>[5,8]</code> → <code>8</code></p> <p><code>[1,1,1,1]</code> → <code>1</code></p> <p><code>[]</code> → <code>0</code></p> <p><code>[0,0]</code> → <code>0</code></p> <p><code></code> → <code>2</code></p> <p><code>[2,0,2]</code> → <code>4</code></p> <p><code>[10,9,8,9]</code> → <code>11</code></p> https://codegolf.stackexchange.com/q/1938 15 Code-Golf: Lattice Points inside a Circle Dr. belisarius https://codegolf.stackexchange.com/users/315 2011-04-03T14:14:26Z 2019-02-08T22:38:26Z <p>The following picture shows the problem: </p> <p><img src="https://i.stack.imgur.com/XmqFf.png" alt="enter image description here"></p> <p>Write a function that, given an integer as the circle radius, calculates the number of lattice points inside the <em>centered</em> circle (including the boundary).</p> <p>The image shows: </p> <pre><code>f = 5 (blue points) f = 13 (blue + red points) </code></pre> <p>other values for your checking/debugging: </p> <pre><code>f = 29 f = 317 f = 3,141,549 f = 12,566,345 </code></pre> <p>Should have a reasonable performance. Let's say less than a minute for f. </p> <p>Shortest code wins. Usual Code-Golf rules apply. </p> <p>Please post the calculation and timing of f as an example.</p> https://codegolf.stackexchange.com/q/163704 33 Triangular Lattice Points close to the Origin Bubbler https://codegolf.stackexchange.com/users/78410 2018-04-29T06:58:21Z 2019-02-08T02:37:11Z <h1>Background</h1> <p>A <strong><a href="http://mathworld.wolfram.com/TriangularGrid.html" rel="noreferrer">triangular grid</a></strong> is a grid formed by tiling the plane regularly with equilateral triangles of side length 1. The picture below is an example of a triangular grid.</p> <p><img src="https://i.stack.imgur.com/pmooq.gif" alt=""></p> <p>A <strong>triangular lattice point</strong> is a vertex of a triangle forming the triangular grid.</p> <p>The <strong>origin</strong> is a fixed point on the plane, which is one of the triangular lattice points.</p> <h1>Challenge</h1> <p>Given a non-negative integer <code>n</code>, find the number of triangular lattice points whose Euclidean distance from the origin is less than or equal to <code>n</code>.</p> <h1>Example</h1> <p>The following figure is an example for <code>n = 7</code> (showing only 60-degree area for convenience, with point A being the origin):</p> <p><img src="https://i.imgur.com/D1kUGxq.png" alt=""></p> <h1>Test Cases</h1> <pre><code>Input | Output --------------- 0 | 1 1 | 7 2 | 19 3 | 37 4 | 61 5 | 91 6 | 127 7 | 187 8 | 241 9 | 301 10 | 367 11 | 439 12 | 517 13 | 613 14 | 721 15 | 823 16 | 931 17 | 1045 18 | 1165 19 | 1303 20 | 1459 40 | 5815 60 | 13057 80 | 23233 100 | 36295 200 | 145051 500 | 906901 1000 | 3627559 </code></pre> <p><strong>Hint</strong>: This sequence is <em>not</em> <a href="https://oeis.org/A003215" rel="noreferrer">OEIS A003215</a>.</p> <h1>Rules</h1> <p>Standard rules for <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> apply. The shortest submission wins.</p> <p>Please include how you solved the challenge in your submission.</p> https://codegolf.stackexchange.com/q/18664 28 Let's draw the flag of Nepal ST3 https://codegolf.stackexchange.com/users/9160 2014-01-16T22:05:16Z 2019-02-03T13:08:18Z <p>Nepal’s flag (<a href="https://en.wikipedia.org/wiki/Nepalese_Flag">Wikipedia</a>, <a href="http://www.numberphile.com/videos/nepal_flag.html">Numberphile</a>) looks very different from any other. It also has specific drawing instructions (included in the Wikipedia article). I want you guys to make a program which will draw the flag of Nepal.</p> <p>The user inputs the requested height of the flag (from 100 to 10000 pixels) and the program outputs the flag of Nepal. You can choose any way to draw the flag: everything from ASCII art to OpenGL.</p> <p>This is a popularity contest, so the winner will be the highest voted answer on the 1st of February, so don’t worry about length of code, but remember that shorter code may get more up-votes.</p> <p>There is only one requirement: you are not allowed to use web resources.</p> <p>Have fun :)</p> <p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Flag_of_Nepal.svg/394px-Flag_of_Nepal.svg.png" alt="image of the flag of Nepal from Wikimedia Commons"></p> https://codegolf.stackexchange.com/q/146059 46 Is my triangle right? mdahmoune https://codegolf.stackexchange.com/users/71398 2017-10-23T14:33:24Z 2019-02-03T05:40:04Z <p>Given <code>a, b, c</code> the length of the three sides of a triangle, say if the triangle is <strong>right-angled</strong> (i.e. has one angle equal to 90 degrees) or not.</p> <h2>Input</h2> <p>Three <strong>positive</strong> integer values <strong>in any order</strong></p> <h2>Output</h2> <p>Either a specific true output (<code>true</code>, <code>1</code>, <code>yes</code>, ...) or a specific false output (<code>false</code>, <code>0</code>, <code>no</code>, ...)</p> <h2>Example</h2> <pre><code>5, 3, 4 --&gt; yes 3, 5, 4 --&gt; yes 12, 37, 35 --&gt; yes 21, 38, 50 --&gt; no 210, 308, 250 --&gt; no </code></pre> <h2>Rules</h2> <ul> <li>The input and output can be given <a href="http://meta.codegolf.stackexchange.com/q/2447/42963">in any convenient format</a>.</li> <li>In your submission, please state the true and the false values.</li> <li>No need to handle <strong>negative</strong> values or <strong>invalid edge</strong> triple</li> <li>Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.</li> <li>If possible, please include a link to an online testing environment so other people can try out your code!</li> <li><a href="http://meta.codegolf.stackexchange.com/q/1061/42963">Standard loopholes</a> are forbidden.</li> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> so all usual golfing rules apply, and the shortest code (in bytes) wins.</li> </ul> https://codegolf.stackexchange.com/q/92410 17 A cube of text ݀ acrolith https://codegolf.stackexchange.com/users/56258 2016-09-06T21:18:35Z 2019-02-01T22:44:29Z <p>Last time you made <a href="https://codegolf.stackexchange.com/questions/88926/a-square-of-text">a square of text</a>, but now, can you make a <strong>cube</strong> of text?</p> <h2>The Challenge</h2> <p>Given a string, output the string in the form of a cube.</p> <p>You can assume the string will always have 2 chars or more, and will only have printable ascii characters.</p> <h2>How to Make a Text Cube</h2> <p><img src="https://i.stack.imgur.com/hABjp.png" alt="terrible mspaint skills.png"></p> <h2>Test Cases</h2> <pre><code>Input: Test Output: Test e ss s e e tseT T s e e e ss Test Input: Hello, world! Output: Hello, world! e dd l l l l r r o o o , w w w , , o o o r l l l l l d e e !dlrow ,olleH H d e e l l l r l l o o o w , , , w w o o o l r r l l l e dd Hello, world! Input: Hi Output: Hi iHH Hi </code></pre> <h2>Reference Implementation in Python</h2> <pre class="lang-python prettyprint-override"><code>text = raw_input("Enter a string: ") print " " * (len(text) - 1) + text spaces = len(text) - 2 _spaces = spaces for i in range(1, len(text) - 2 + 1): print " " * spaces + text[i] + " " * _spaces + text[(i + 1) * -1] + " " * (_spaces - spaces) + text[(i + 1) * -1] spaces -= 1 print text[::-1] + " " * _spaces + text spaces = _spaces - 1 for i in range(1, len(text) - 2 + 1): print text[(i + 1) * -1] + " " * _spaces + text[i] + " " * spaces + text[i] spaces -= 1 print text </code></pre> <h2>Rules</h2> <ul> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so shortest answer in bytes wins! Tiebreaker is most upvoted.</li> <li>Standard loopholes are disallowed.</li> <li>Trailing newline and trailing spaces are allowed.</li> </ul> https://codegolf.stackexchange.com/q/38159 10 Create a grid as close to a square as possible Martijn https://codegolf.stackexchange.com/users/21443 2014-09-25T08:46:45Z 2019-01-27T11:44:23Z <p><strong>Create a function or program that makes a grid as close to an square as possible</strong></p> <ul> <li>You will be given an integer <em>N</em> as input, whole numbers (1,2,3,25, etc)</li> <li>The output must be a perfect rectangular grid of <em>N</em> letters as close to a square as possible</li> <li>The (wannabe)square must consist of one of the the letters O or X as specified by user</li> </ul> <p><strong>Points</strong>:</p> <ul> <li>Hardcoded to only O or X: +1</li> <li>A param(0/1, true/false, something similar) to rotate output (like with 5 or 8): -10</li> <li>Design the square (use both O and X in some sort of pattern): -5</li> </ul> <p>A pattern is considered valid if it contains both types of characters (where x/y-axis >= 3) and the pattern stays the same when flipped horizontally or vertically (exchanging Xs with Os is allowed)</p> <p><strong>Examples</strong></p> <pre><code>INPUT: 4 INPUT: 5 INPUT: 8 INPUT: 9 OO OOOOO XXXX XOX OO XXXX OXO or rotated 90deg XOX </code></pre> <p><strong>Examples which aren't allowed (not same length row or columns)</strong></p> <pre><code>BAD RESULT: 5a BAD RESULT: 5b BAD RESULT: 8 OOO OO OOO OO OO OOO O OO </code></pre> <p>If possible please provide an online example.</p> https://codegolf.stackexchange.com/q/161789 11 Distance between two points on the Moon mdahmoune https://codegolf.stackexchange.com/users/71398 2018-04-10T13:42:57Z 2019-01-22T00:41:57Z <p>Given latitude/longitude of two points on the Moon <code>(lat1, lon1)</code> and <code>(lat2, lon2)</code>, compute the distance between the two points in kilometers, by using <strong>any formula</strong> that gives the same result as the haversine formula.</p> <h2>Input</h2> <ul> <li>Four integer values <code>lat1, lon1, lat2, lon2</code> in degree (angle) or </li> <li>four decimal values <code>ϕ1, λ1, ϕ2, λ2</code> in radians.</li> </ul> <h2>Output</h2> <p>Distance in kilometers between the two points (decimal with any precision or rounded integer).</p> <h2>Haversine formula</h2> <p><a href="https://i.stack.imgur.com/hUhjO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hUhjO.png" alt="d = 2 r \arcsin\left(\sqrt{\sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)"></a></p> <p>where</p> <ul> <li><code>r</code> is the radius of the sphere (assume that the Moon's radius is 1737 km),</li> <li><code>ϕ1</code> latitude of point 1 in radians</li> <li><code>ϕ2</code> latitude of point 2 in radians</li> <li><code>λ1</code> longitude of point 1 in radians</li> <li><code>λ2</code> longitude of point 2 in radians</li> <li><code>d</code> is the circular distance between the two points</li> </ul> <p>(source: <a href="https://en.wikipedia.org/wiki/Haversine_formula" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Haversine_formula</a>)</p> <h2>Other possible formulas</h2> <ul> <li><code>d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1))</code> <a href="https://codegolf.stackexchange.com/questions/161789/distance-between-two-points-on-the-moon#comment391848_161789"><em>@miles</em>' formula</a>. </li> <li><code>d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1))</code> <a href="https://codegolf.stackexchange.com/questions/161789/distance-between-two-points-on-the-moon/161802#comment391978_161802"><em>@Neil</em>'s formula</a>.</li> </ul> <h2>Example where inputs are degrees and output as rounded integer</h2> <pre><code>42, 9, 50, 2 --&gt; 284 50, 2, 42, 9 --&gt; 284 4, -2, -2, 1 --&gt; 203 77, 8, 77, 8 --&gt; 0 10, 2, 88, 9 --&gt; 2365 </code></pre> <h2>Rules</h2> <ul> <li>The input and output can be given <a href="http://meta.codegolf.stackexchange.com/q/2447/42963">in any convenient format</a>.</li> <li>Specify in the answer whether the inputs are in <strong>degrees</strong> or <strong>radians</strong>.</li> <li>No need to handle <strong>invalid latitude/longitude values</strong></li> <li>Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.</li> <li>If possible, please include a link to an online testing environment so other people can try out your code!</li> <li><a href="http://meta.codegolf.stackexchange.com/q/1061/42963">Standard loopholes</a> are forbidden.</li> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> so all usual golfing rules apply, and the shortest code (in bytes) wins.</li> </ul> https://codegolf.stackexchange.com/q/178906 8 Count the Closed Polygons Kevin Cruijssen https://codegolf.stackexchange.com/users/52210 2019-01-18T14:01:33Z 2019-01-21T14:53:32Z <h2>Input:</h2> <p>An <code>NxM</code> grid or multi-line string (or other reasonable input-format), containing only printable ASCII (unicode range <code>[32,126]</code>).</p> <h2>Output:</h2> <p>The amount of closed polygons of the same character that can be found, with two special rules:</p> <ul> <li>Spaces are wildcards and can be used (multiple times) for any character</li> <li><code>o</code>, <code>O</code>, and <code>0</code> are counted as closed polygons themselves</li> </ul> <h2>Challenge rules:</h2> <ul> <li>(Anti-)Diagonal connections between the same characters (or spaces) are included to form closed polygons.</li> <li>You cannot go over other characters (except for the wild-card spaces). (I.e. in the first test case/example below, you cannot form two triangles with the <code>A</code>'s by going over the <code>x</code>.) So all characters used for a closed polygon should be connected (horizontally, vertically, and/or (anti-)diagonally).</li> <li>Polygons are at least three characters (excluding the single characters <code>o</code>, <code>O</code>, <code>0</code>).</li> <li>Lines of adjacent characters are not closed polygons.</li> <li>The same characters cannot be used for multiple polygons, excluding wildcard spaces.</li> <li>Wildcard spaces cannot be counted as <code>o</code>, <code>O</code>, or <code>0</code>.</li> <li>Three or more spaces alone cannot form a closed polygon. It should always have at least one non-space (and non <code>o</code>/<code>O</code>/<code>0</code>) character.</li> <li>Input can be in any reasonable format. Can be a character-matrix, new-line delimiter string, string-array, character-array with added integer width, etc.</li> <li>The inputs will always be an N by M rectangle (or square), so no weird input-shapes</li> <li>Since the same characters cannot be used more than once and we want to have as many closed polygons, using multiple characters to form two (or more) closed polygons instead of one larger polygon is of course the intended goal in the counting (which is also why closed polygons formed by <code>o</code>, <code>O</code>, or <code>0</code> will never be counted, since they are already closed polygons individually).</li> <li>Uppercase and lowercase letters are of course counted as individual characters.</li> </ul> <h2>General rules:</h2> <ul> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a>, so shortest answer in bytes wins.<br> Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.</li> <li><a href="https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422">Standard rules apply</a> for your answer with <a href="https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods/">default I/O rules</a>, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.</li> <li><a href="https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default">Default Loopholes</a> are forbidden.</li> <li>If possible, please add a link with a test for your code (i.e. <a href="https://tio.run/#" rel="nofollow noreferrer">TIO</a>).</li> <li>Also, adding an explanation for your answer is highly recommended.</li> </ul> <h2>Examples / Test Cases:</h2> <p>Input:</p> <pre><code>AAAw AxA4 'AoQ </code></pre> <p>Output: <code>2</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/t8csd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t8csd.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>1822uaslkoo 12*2sl ljoo a* 0a91)j\$* ()*#J9dddj* *Q#ID dJj!" *UJD SO&amp;*93 </code></pre> <p>Output: <code>12</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/9EEsk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9EEsk.png" alt="enter image description here"></a></p> <p>Note that:<br> - The yellow one below is not a polygon, because the <code>o</code>'s are already counted as separated polygons<br> - The purple and brown ones aren't closed<br> - The red, grey, green, and light-blue use one or more non-space characters that were already used for other closed polygons<br> <a href="https://i.stack.imgur.com/I9E4r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I9E4r.png" alt="enter image description here"></a></p> <hr> <p>Input (dimensions are <code>2x4</code>):</p> <pre><code>3 3 2 </code></pre> <p>Output: <code>3</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/rx7Bm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rx7Bm.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>AAAA AAAA AAxA </code></pre> <p>Output: <code>3</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/BQdQm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BQdQm.png" alt="enter image description here"></a></p> <p>Of course other polygons are possible here, but no more than 3. Here another valid example with <code>3</code> polygons:<br> <a href="https://i.stack.imgur.com/tfrA1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tfrA1.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>0QoO </code></pre> <p>Output: <code>3</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/QdcaH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QdcaH.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>W w Ww </code></pre> <p>Output: <code>3</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/VlAeR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VlAeR.png" alt="enter image description here"></a></p> <p>Note that the top layer space is used for all three polygons. Here are the three polygons individually highlighted:<br> <a href="https://i.stack.imgur.com/ioBZF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ioBZF.png" alt="enter image description here"></a> <a href="https://i.stack.imgur.com/pUvRb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pUvRb.png" alt="enter image description here"></a> <a href="https://i.stack.imgur.com/FM5gF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FM5gF.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>W W WW </code></pre> <p>Output: <code>3</code>, because the same three polygons as in the previous test can be formed. <strong>So no, it's not <code>2</code> with these two polygons:</strong><br> <a href="https://i.stack.imgur.com/xFFi1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xFFi1.png" alt="enter image description here"></a></p> <hr> <p>Input:</p> <pre><code>abcdQefg hQiQjQQk QlQmnopQ QqrstQQu QvQQQwxy QQz0QQQQ </code></pre> <p>Output: <code>3</code>, because these polygons can be formed:<br> <a href="https://i.stack.imgur.com/eGwTK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eGwTK.png" alt="enter image description here"></a></p> https://codegolf.stackexchange.com/q/177443 7 Hilbertize an image xakepp35 https://codegolf.stackexchange.com/users/81659 2018-12-12T14:48:24Z 2019-01-20T19:10:53Z <p>For a computer vision app I want to do a mapping of an image, in such a way that every pixel fit <a href="https://en.wikipedia.org/wiki/Hilbert_curve" rel="nofollow noreferrer">hilbert curve</a>, instead of conventional layout. So task could be as follows: </p> <hr> <h2>Task description</h2> <p>Given square 2D image <code>A</code> with side <code>2^^N</code>, (<code>N&gt;0</code>), with common Z-order, or row-major representation of pixels in RAM. I need to run a hilbert curve through this image (Like a digital camera, where photodiodes are soldered in a hilbert curve and numbered and accessed in a 1-d ordered manner). Then pixels along this curve just layed down as a regular scanline in image B. So now any consequent samples in output array B now should represent spatially close points in input array A.</p> <hr> <h2>Example for <code>N=2</code></h2> <p><a href="https://i.stack.imgur.com/RiLeE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RiLeE.png" alt="Example infographic"></a></p> <pre><code>Input: array A (imagination of 2D image) 0 1 E F 3 2 D C 4 7 8 B 5 6 9 A Input: array A (1D actual layout in memory) 0 1 E F 3 2 D C 4 7 8 B 5 6 9 A Output: array B (answer) 0 1 2 3 4 5 6 7 8 9 A B C D E F </code></pre> <p>Note, that values inside arrays are "values". In this example, for array B accidentally they became same as indices. So basically I want index mapping function B[i]=A[?], e.g B<a href="https://i.stack.imgur.com/KdtBj.png" rel="nofollow noreferrer">2</a> = A[E]. Hope code golfing helps.</p> <hr> <h2>Example for N=9:</h2> <p>Taken popular 512*512 test image. Each pixel was threated as 32bit integer, in RGBA format and algorithm proceeded:</p> <p>Input:</p> <p><a href="https://i.stack.imgur.com/KdtBj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KdtBj.png" alt="enter image description here"></a></p> <p>Output:</p> <p><a href="https://i.stack.imgur.com/vL4lG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vL4lG.jpg" alt="enter image description here"></a></p> <hr> <h2>Reverse question:</h2> <p>Here is <a href="https://codegolf.stackexchange.com/questions/101871/hilbertify-an-image">reversed-task question on codegolf</a>.</p> <p>In terms of that question, I want to do opposite transform: "I want to <em>Reravel</em> image A, then <em>Unravel</em> to image B". In terms of examples of that question: I want to take <em>photo the lion</em> on the camera with hilbert-curved-sensor (look <em>Output</em> array for lion photo) and get that long "scanline of spatially close pixels" (Input for lion photo, in that question)</p> <hr> <h2>Reference implementation:</h2> <p>If i take <code>d2xy()</code> function <a href="https://people.sc.fsu.edu/~jburkardt/c_src/hilbert_curve/hilbert_curve.html" rel="nofollow noreferrer">from LGPL sources found here</a> I may write a following program, which works, but maybe not its best way:</p> <pre><code>#include "hilbert_curve.h" int main(){ int *A,*B,i,x,y; for(i=0;i&lt;(1&lt;&lt;(N&lt;&lt;1));++i){ d2xy(N,i,&amp;x,&amp;y); B[i]=A[y*(1&lt;&lt;N)+x]; } } </code></pre> <hr> <h2>Resources:</h2> <ul> <li><a href="http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves" rel="nofollow noreferrer">Spatial indexing with Quadtrees and Hilbert Curves</a></li> <li><a href="http://people.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-2_themes-hilbert-sec.html#fu-intr-2_hilbertprog_title_1" rel="nofollow noreferrer"><code>hilbert</code> function, which returns a rendering of Hilbert Curves</a></li> </ul> https://codegolf.stackexchange.com/q/55770 35 Visually Explain the Pythagorean Theorem Maltysen https://codegolf.stackexchange.com/users/31343 2015-08-31T21:06:35Z 2019-01-17T15:35:50Z <p>A common visual explanation of the Pythagorean theorem is as such:</p> <p><a href="https://i.stack.imgur.com/PJFZl.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/PJFZl.gif" alt="3 boxes"></a></p> <p>The squares are meant to represent the side length's squared, and the areas of <code>a + b = c</code>, just like the Pythagorean theorem says.</p> <p>This part is what you have to show.</p> <h1>Your task</h1> <ul> <li>You will get two integers as input, meant to represent sides <code>a</code> and <code>b</code> of a right triangle (ex. <code>3, 4</code>).</li> <li>You will then make squares out of the lengths <code>a</code>, <code>b</code>, and <code>c</code> out of the <code>#</code> character. For example here is 3:</li> </ul> <pre><code>### ### ### </code></pre> <ul> <li>You will then format these into a math equation that explains the particular Pythagorean triplet:</li> </ul> <pre><code> ##### #### ##### ### #### ##### ### #### ##### ### + #### = ##### </code></pre> <ul> <li>Notice how the <code>=</code> and <code>+</code> signs have spaces on both sides and how everything is on the bottom.</li> <li>You will never get values for <code>a</code> and <code>b</code> that make <code>c</code> non-integral.</li> <li>This is <a href="/questions/tagged/code-golf" class="post-tag" title="show questions tagged &#39;code-golf&#39;" rel="tag">code-golf</a> so shortest code in <strong>bytes</strong> wins!</li> </ul> <h1>Test Cases</h1> <p>(more coming once I have time, these are really hard to make by hand)</p> <pre><code>3, 4 ##### #### ##### ### #### ##### ### #### ##### ### + #### = ##### 6, 8 ########## ########## ######## ########## ######## ########## ###### ######## ########## ###### ######## ########## ###### ######## ########## ###### ######## ########## ###### ######## ########## ###### + ######## = ########## 4, 3 ##### #### ##### #### ### ##### #### ### ##### #### + ### = ##### 5, 12 ############# ############ ############# ############ ############# ############ ############# ############ ############# ############ ############# ############ ############# ############ ############# ##### ############ ############# ##### ############ ############# ##### ############ ############# ##### ############ ############# ##### + ############ = ############# </code></pre>