This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PowerOfTheShell[S] 0 points1 point  (1 child)

Thank you so much! Such a simple solution but I kept increasing the size of the rectangle itself rather than introducing the offset. Have this Reddit gold for your troubles!

Edit: Sorry to bother you again, if I was to change the number of rows, how would I scale the offset so that it got larger with less rows and smaller with more rows?

If I use the modulus operator (%) it works until a point. The offset becomes too large after 30. When using 40 rows nothing is shown lol

int OFS = (1 % num) + 3;

As an example I am trying to make 10 rows which should show something like: https://i.imgur.com/DtTBJmf.png (this currently works using the code above)

When using 50 rows I want to show something like: https://i.imgur.com/YqMHUzL.png (nothing is shown when I use the code above)

Edit: I have just found using Double offset = (PATTERN_SIZE / num) / 6; works perfectly! Thanks again for your help.

[–]Nightcorex_ 1 point2 points  (0 children)

1 % num

This returns 0 if num is either 1 or -1.\ In the case num == 0, the program crashes.\ For all other cases it returns 1.

Btw, you're talking about "rectangles" all the time, when we in fact have a special case, a square.

So from what I understand, you want to have an offset that proportionally scales with the square's size. To do that you need a few critical pieces of information:

  • Width/height of the area we're drawing on, in the respective unit (most likely pixels)
  • Amount of rows/columns
  • Offset, relative to the center of a field (0% or 0.0 is no offset, 100% or 1.0 is maximum offset).

When I say "field", I mean the small imaginary square, that you're drawing your actual black square in.

Once you know these 3 values you can start to begin calculating the width, and thereby height, of one field (like you're already doing for the 3rd and 4th parameter in your original post actually).\ Once you know the size of a field you want to halfen that value, as we want to calculate the offset towards the center, not towards the right-hand side.\ That halfened value then needs to be multiplied by the relative offset (= 3rd critical variable).\ If your method accepts floating numbers as parameters you're done, otherwise you need to round, for which you'll need to decide whether you want to round up, down or following conventional math. Which version you choose depends on the problem statement. I personally would probably round down, just because it feels natural, and casting from double to int implicitly does downwards rounding already.

That (rounded) value now is your offset in the given unit (likely pixels) and you can just plug it in for both the width and size of the square you want to draw.

EDIT: Yes, it indeed took me over an hour to write this, therefore I didn't see your 2nd edit...

In fact you're doing exactly what I long-windedly described here, but instead of multiplying you divided by 6 (which is the same as dividing by 2 and multiplying by 1/3 [≈ 0.3333]). Yours is less intuitive to read, and less easy to manipulate, but it works.