# Drawing "flexible" squares in a Row

let me see if I can explain this

I have a canvas the is X pixels wide, across it I need to draw some rectangles (the height is not important, just the width). Each Rectangle must be no smaller that W1 pixels wide, and no larger than W2 pixels wide (and all rectangles must be uniform)

So, give X, W1 and W2 what is the number of rectangles that can be drawn (N) and what is that actual width to draw them (W)

W>=W1 and W<=W2
X/W = Int(X/W)

If X is prime, there is no answer that meets ‘rectangle must be uniform’
If it is not, but none of its factors lie between W1 and W2, there is no answer.

With a range of numbers to choose from W1… W2, then there are a range of possible answers.
You need to choose a maximum or minimum number of rectangles.
And you will need to do that in a loop, from W1 to W2 , checking to see if the value is a factor of W

eg

``````//Wanting as many rects as possible
int  Rectangles =-1
Looping for    Thisvalue  from  W1 to W2

if    (X Mod ThisValue ) <> 0 then
//no good as there is a remainder, so not a factor
else
if (X Div ThisValue )  > Rectangles then Rectangles = (X Div ThisValue )
end if
End loop

//  if   Rectangles  >0 then Width of rectangle = X \ Rectangles
``````

Note : W does not have to be an integer

So you dont mind having ‘half a pixel’ left over?

Try changing the pseudocode to