title: Using C#, what is the best logic to determine the "left" location of a object on a page so its horizontally evenly distributed?
body: <p>I am trying to  dynamically layout a bunch of squares on a surface (could be winform app, web page, etc) and I am trying to figure out a function where </p>
  
  <p><strong>Given:</strong><br>
  1. The page has a fixed width<br>
  2. The square has a fixed width</p>
  
  <p><strong>Input</strong>: The input into the function is:</p>
  
  <pre><code>   The number of squares
  </code></pre>
  
  <p><strong>Output</strong>: The output is an array that has:</p>
  
  <pre><code>  the "left" location of each square to lay them out so they are equally spaced out horizontally on the page.
  </code></pre>
  
  <p>My program is in C# but I guess there are two parts of the question</p>
  
  <ol>
  <li>What i guess is more of a math question that then could be solved in any programming language</li>
  <li>The easiest way to code that in C#.</li>
  </ol>
  
  <p>I am more focused on #1 right now as i presume once the logic could be defined then #2 might be very simple but I still tagged the question as C# as well just because that is what my program is in.</p>
  
  <p>Here is an example:</p>
  
  <p>Lets say the surface has a width of 800 pixels.  The square is 50 pixels wide</p>
  
  <p>I am looking for some code that, given the number of squares required, it tells me where the left pixel should be </p>
  
  <p>so for example, if I only have one square, the left location would be 375 because it would be </p>
  
  <pre><code>  (TOTAL WIDTH OF PAGE / 2) -  (WIDTH OF SQUARE / 2)
  
    (800 /2) - (50/2) or 400 - 25 - 375
  </code></pre>
  
  <p>if i put the left location of the square at 375 then it would be perfectly centered on the page</p>
  
  <p>if I had two squares it would be </p>
  
  <pre><code>First one:
  
    (TOTAL WIDTH OF PAGE / 4) -  (WIDTH OF SQUARE / 2)
  
    ((800 / 4) * 1) - (50/2) or 200 - 25 - 175
  
  Second One:
  
    ((800 / 4) * 3) - (50/2) or 600 - 25 - 575
  </code></pre>
  
  <p>if i had three squares, it would be  175, 375, 575</p>
  
  <p>I am trying to see if there is a more generalized function that i can create out of this (versus having hard coded logic for each number of squares) so that would work for any number of squares (the most i would realistically have would be 10 but I am trying not to hard code it.  I know that if I have more than 16 squares then they won't fix on a page horizontally even if they are side by side but that is fine as I don't see ever getting to that point.</p>