-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|700|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- Socoder -> Concept/Design -> Box Dispensing Posted : Sunday, 15 May 2022, 00:04 therevillsgames Algorithm Help Please I want to place items outside and around a rectangle, say if the rectangle is 10 x 5 and I have 100 items, so I place 10 across the top line, 5 across the right line, 10 across bottom and then 5 across the left... then for the rest increase the ’’size’’ of the rectangle so I can add all of the items... any ideas? Posted : Sunday, 15 May 2022, 00:04 therevillsgames  Image to help  Posted : Sunday, 15 May 2022, 01:20 Dan What you should have is the width/height of the boxes and the spacing in between. When you have it, it is easy to calculate how many boxes fit in the top part. You can do that with one side. Then you calculate the (count of the top part*2 + the count of the sides*2)+4 (4 because of the 4 boxes at the corners) If that number does not fit the amount of boxes. you can increase the rectangle. In the case of the picture, the start of rectangle would be the top left white box, and the end would be the bottom right box. Then do the calculation again. Posted : Sunday, 15 May 2022, 01:25 Dan To calculate the top count, you simply use, say 5 (box width) + 2 (spacing) = 7 pixel If the rectangle is 200 pixel in width, then calculate 200/7, and you get how many squares can fit there (rounded down) at least, this is how i would begin with. Posted : Sunday, 15 May 2022, 01:49 therevillsgames Sounds easy But I'm having a hard time thinking how to do it in code with dynamic values... Posted : Sunday, 15 May 2022, 01:58 Dan Just start with what i gave you. see the result, then you may discover how to apply dynamic values. Because as soon as you start thinking in that way, a solution may come up. Posted : Sunday, 15 May 2022, 02:07 Dan The trouble, at leas for me is, that i do not know what you want to do. see these question to understand the problem: What happens if you have 1 box instead of 100 ? What should be, if you have 2 boxes ? 3 ? 4? if you have 4 boxes, shall they be on the top side ? or shall they be placed one at each side ? If you have enough boxes to fill the top side and to place 2 boxes at the right hand side, shall the rectangle be made smaller, so that the amout of boxes are always arround the rectangle ? ... So basically start with baby steps, then expand as needed. Posted : Sunday, 15 May 2022, 02:27 Dan If you have to place the boxes around the fixed size (i mean not going smaller) rectangle, then calculate if they fit. If they do not, increase the size and do the calculation again until they fit. arbitrary code is: rem increase the rectangle until the boxes are fitting While Count_of_boxes>fitting CountW=RectW/(boxw+2) countH=RectH/(boxh+2) fitting=(countW*2)+(CountH*2)+4 If fitting>Count_of_boxes then RectW=RectW+((boxw+2)*2) RectH=RectH+((boxh+2)*2) End If Wend --v Posted : Sunday, 15 May 2022, 02:50 Pakz I do not completely understand it. But could you take the length of all the lines of the box. And the width/height and spacing of all the tiny boxes? Then try to get this to fit? Posted : Sunday, 15 May 2022, 03:05 therevillsgames If the number of the tiny boxes were fixed, I could just do: no_of_boxes / 4 = x.... then x / width of rect and x / height of rect and place them okay Basically I want to place the tiny boxes starting top left and going around the rectangle separated by the width of the tiny boxes, if there are too many then we go around again but further out. I'll have a mess around on JSE. Posted : Sunday, 15 May 2022, 03:24 therevillsgames Not quite right but something like this: Graphics 1920, 1080 DebugMode on #Const \$X=0 #Const \$Y=1 #Const \$W=2 #Const \$H=3 cx = ScreenWidth()/2 cy = ScreenHeight()/2 w = 1000 h = 500 #Const NO_OF_ITEMS = 500 Dim items(\$NO_OF_ITEMS , 3) i = 0 sx = cx - w / 2 sy = cy - h / 2 tw = 15 xx = sx - tw yy = sy - tw tempw = w temph = h stage = 0 extra = 5 Repeat items(i, \$X) = xx items(i, \$Y) = yy items(i, \$W) = tw items(i, \$H) = tw i = i + 1 if stage == 0 then xx = xx + tw + extra if xx > tempw + sx then stage = stage + 1 endif endif if stage == 1 yy = yy + tw + extra if yy > temph + sy then stage = stage + 1 endif endif if stage == 2 xx = xx - tw - extra if xx < sx then stage = stage + 1 endif endif if stage == 3 yy = yy - tw - extra if yy < sy then stage = 0 sx = sx - 40 sy = sy - 40 tempw = tempw + 40 temph = temph +40 xx = sx yy = sy endif endif Until i > \$NO_OF_ITEMS - 1 Repeat CLS:ResetDraw() DrawRect cx - w / 2, cy - h / 2, w, h For i= 0 to \$NO_OF_ITEMS ix = items(i, \$X) iy = items(i, \$Y) iw = items(i, \$W) ih = items(i, \$H) DrawRect ix, iy, iw, ih next Flip Forever --v Posted : Sunday, 15 May 2022, 03:24 Pakz The first thought I had that I would code a slider. This to modify the spacing on the second pass. I hope you figure it out! Posted : Sunday, 15 May 2022, 04:48 Jayenkai // External Boxes // by Jayenkai // Created 2022/5/14 // Is it jigsaw pieces, or is it dealt cards? Who can tell! DebugMode On Graphics 640,480,2 CLS:Flip // Grid size GridWidth=10 GridHeight=5 v=0:w=1 // Draw Position SpaceWidth=32 // When drawing, what's the size? SpaceHeight=32 OffsetX=4*SpaceWidth // Where card 0,0 will be drawn OffsetY=4*SpaceHeight // Function stuff InputN=0 // These are our "function"'s values.. I'll let you figure out how to do that in your language of choice! ReturnX=0;ReturnY=0 Repeat CLS ResetDraw:SetWidth 8 DrawRect OffsetX,OffsetY,(GridWidth*SpaceWidth),(GridHeight*SpaceHeight) SetWidth 2 for x=0 to GridWidth-1 for y=0 to GridHeight-1 DrawRect OffsetX+(x*SpaceWidth)+2,OffsetY+(y*SpaceHeight)+2,SpaceWidth-4,SpaceHeight-4 next next v=v+w if v>199 then w=-1 if v<1 then w=1 SetFontSize SpaceHeight/4 For m=0 to v InputN=m Gosub FindCoords DrawRect ReturnX+2,ReturnY+2,SpaceWidth-4,SpaceHeight-4 Text ReturnX+SpaceWidth/2,ReturnY+SpaceHeight/2,m,1 Next SeedRnd Floor(Mills/1500) SetCol 100,255,100 InputN=Rand(0,200) Gosub FindCoords DrawRect ReturnX+2,ReturnY+2,SpaceWidth-4,SpaceHeight-4 Text ReturnX+SpaceWidth/2,ReturnY+SpaceHeight/2,InputN,1 SetFontSize SpaceHeight*2 Text OffsetX+GridWidth*SpaceWidth*0.5,OffsetY+GridHeight*SpaceHeight*0.5,InputN,1 Flip Forever .FindCoords ReturnX=0;ReturnY=-1 // Start from "one above the top left" Layer=0 Remain=InputN Repeat // >> Right Gap=GridWidth+Layer if Remain>0 and Remain<=Gap then ReturnX=ReturnX+Remain:Remain=0 if Remain>0 and Remain> Gap then ReturnX=ReturnX+Gap:Remain=Remain-Gap // vv Down Gap=GridHeight+Layer*2+1 if Remain>0 and Remain<=Gap then ReturnY=ReturnY+Remain:Remain=0 if Remain>0 and Remain> Gap then ReturnY=ReturnY+Gap:Remain=Remain-Gap // << Left Gap=GridWidth+Layer*2+1 if Remain>0 and Remain<=Gap then ReturnX=ReturnX-Remain:Remain=0 if Remain>0 and Remain> Gap then ReturnX=ReturnX-Gap:Remain=Remain-Gap // ^^ Up Gap=GridHeight+Layer*2+2 if Remain>0 and Remain<=Gap then ReturnY=ReturnY-Remain:Remain=0 if Remain>0 and Remain> Gap then ReturnY=ReturnY-Gap:Remain=Remain-Gap // >> More right, to get back to x=0 Gap=Abs(ReturnX) if Remain>0 and Remain<=Gap then ReturnX=ReturnX+Remain:Remain=0 if Remain>0 and Remain> Gap then ReturnX=ReturnX+Gap:Remain=Remain-Gap Layer=Layer+1 Until Remain<1 ReturnX=OffsetX+ReturnX*SpaceWidth ReturnY=OffsetY+ReturnY*SpaceHeight Return Couldn't work out a formula. Made a loop! -=-=- ''Load, Next List!'' Posted : Sunday, 15 May 2022, 05:32 Dan aaah i thought you want 1 row/column of the boxes around the square. Posted : Monday, 16 May 2022, 03:07 therevillsgames Thanks all and Jay - that's one way to do it and here is the BlitzMax (Strict as the non-strict version didnt work at all ) version of Jay's code: Strict Graphics 800,600 Global GridWidth=10 Global GridHeight=5 Local v=0 Local w=1 ' Draw Position Global SpaceWidth=32 ' When drawing, what's the size? Global SpaceHeight=32 Global OffsetX=(800/2) - ((GridWidth*SpaceWidth)/2) Global OffsetY=(600/2) - ((GridHeight*SpaceHeight)/2) ' Function stuff Local ReturnX=0 Local ReturnY=0 Repeat Cls DrawRect OffsetX,OffsetY,(GridWidth*SpaceWidth),(GridHeight*SpaceHeight) v=v+w If v>199 Then w=-1 If v<1 Then w=1 For Local m=0 To v FindCoords(m,ReturnX, ReturnY) DrawRect ReturnX+2, ReturnY+2, SpaceWidth-4, SpaceHeight-4 Next Flip Until AppTerminate() Function FindCoords(InputN, ReturnX Var , ReturnY Var ) ReturnX=0 ReturnY=-1 ' Start from "one above the top left" Local Layer=0 Local Remain=InputN Local gap Repeat '/ >> Right Gap=GridWidth+Layer If Remain>0 And Remain<=Gap Then ReturnX=ReturnX+Remain Remain=0 EndIf If Remain>0 And Remain> Gap Then ReturnX=ReturnX+Gap Remain=Remain-Gap EndIf ' vv Down Gap=GridHeight+Layer*2+1 If Remain>0 And Remain<=Gap Then ReturnY=ReturnY+Remain Remain=0 EndIf If Remain>0 And Remain> Gap Then ReturnY=ReturnY+Gap Remain=Remain-Gap EndIf ' << Left Gap=GridWidth+Layer*2+1 If Remain>0 And Remain<=Gap Then ReturnX=ReturnX-Remain Remain=0 EndIf If Remain>0 And Remain> Gap Then ReturnX=ReturnX-Gap Remain=Remain-Gap EndIf ' ^^ Up Gap=GridHeight+Layer*2+2 If Remain>0 And Remain<=Gap Then ReturnY=ReturnY-Remain Remain=0 EndIf If Remain>0 And Remain> Gap Then ReturnY=ReturnY-Gap Remain=Remain-Gap EndIf ' >> More Right, To get back To x=0 Gap=Abs(ReturnX) If Remain>0 And Remain<=Gap Then ReturnX=ReturnX+Remain Remain=0 EndIf If Remain>0 And Remain> Gap Then ReturnX=ReturnX+Gap Remain=Remain-Gap EndIf Layer=Layer+1 Until Remain<1 ReturnX=OffsetX+ReturnX*SpaceWidth ReturnY=OffsetY+ReturnY*SpaceHeight End Function --v Posted : Friday, 20 May 2022, 23:11 therevillsgames This is what I ended up with: Method ShuffleTiles:Int() Local ww:Float = previewImage.width / 2 Local hh:Float = previewImage.height / 2 Local startX:Int = -ww - tileconfig.baseWidth Local startY:Int = -hh - tileconfig.baseHeight Local stage:Int = 0 Local largestSide:Int If tileconfig.baseWidth > tileconfig.baseHeight largestSide = tileconfig.baseWidth + tileConfig.borderWidth Else largestSide = tileconfig.baseHeight + tileConfig.borderHeight EndIf Local endX:Int = startX + previewImage.width + largestSide Local endY:Int = startY + previewImage.height + largestSide Local xx:Int = startX + largestSide Local yy:Int = startY For Local tile:TJigsawTile = EachIn tiles Local r:Int = Rand(0, 3) Local rotation:Int Select r Case 0 rotation = 0 Case 1 rotation = 90 Case 2 rotation = 180 Case 3 rotation = 270 End Select tile.rotation = rotation tile.targetRotation = rotation tile.x = xx tile.y = yy Select stage Case 0 xx:+largestSide If xx > endX stage:+1 End If Case 1 yy:+largestSide If yy > endY stage:+1 End If Case 2 xx:-largestSide If xx <= startX stage:+1 End If Case 3 yy:-largestSide If yy < startY stage = 0 startX:-largestSide startY:-largestSide endX:+largestSide endY:+largestSide yy = startY End If End Select Next EndMethod --v  Posted : Friday, 20 May 2022, 23:59 Dan   Posted : Saturday, 21 May 2022, 01:13 rockford Noice!!! Posted : Saturday, 21 May 2022, 02:20 Jayenkai \O/yeay\o/ Posted : Saturday, 21 May 2022, 03:02 therevillsgames Thanks for the help guys! Now I need an option name for it... "Clean Random Placement"... or the opposite "Random Messy Placement"? both suck   Posted : Saturday, 21 May 2022, 03:33 Jayenkai Scattered Border? Posted : Saturday, 21 May 2022, 04:28 Dan Or RandomizedBorder 