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)
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