<commit_before>"
A layout that packs rectangles in as small space as possible (at least it tries to)

"
Class {
	#name : #RSRectanglePackLayout,
	#superclass : #RSLayout,
	#instVars : [
		'freeSpace',
		'gap',
		'padding',
		'bounds',
		'preferredAngle'
	],
	#category : #'Roassal3-Layouts-Core'
}

{ #category : #'space-calculation' }
RSRectanglePackLayout >> bestSpaceFor: anElement [
	| elementsPadedBox suitableSpaces |
	
	elementsPadedBox := anElement encompassingRectangle extendBy: padding * 2.
	
	suitableSpaces := freeSpace select: [ :space |
		elementsPadedBox width <= space width and:
			[ elementsPadedBox height <= space height ] ].
	
	^ suitableSpaces
		fold: [ :best :current |
			| bestExt currentExt | 
			((currentExt := self extentByInserting: elementsPadedBox into: current) <
			(bestExt := self extentByInserting: elementsPadedBox into: best) or: [ 
				currentExt = bestExt and: [
					current halfPerimeter < best halfPerimeter ] ])
						ifTrue: [ current ]
						ifFalse: [ best ] ]
]

{ #category : #accessing }
RSRectanglePackLayout >> boundingBox [
	^ bounds
]

{ #category : #hook }
RSRectanglePackLayout >> doExecute: elements [
	| sortedElements maxSpace |
	bounds := 0@0.
	
	maxSpace := elements inject: 0@0 into: [ :sum :el |
			sum + el extent ].
		
	padding := maxSpace / elements size * gap / 2.
	maxSpace :=(1 + gap) * maxSpace.
	
	freeSpace := Set new.
	freeSpace add: (0@0 corner: maxSpace ).
	
	sortedElements := SortedCollection sortBlock:  [ :larger :smaller |
		larger  encompassingRectangle halfPerimeter >
		smaller encompassingRectangle halfPerimeter ].
	sortedElements addAll: elements.
	
	sortedElements size <= 2 ifTrue: [ 
		RSHorizontalLineLayout on: sortedElements.
		^ self
	].
	sortedElements
		do: [ :element | self packIn: element ]
		displayingProgress: 'Laying out elements'
]

{ #category : #'space-calculation' }<commit_msg>Rectangle pack layout: consider gap parameter when number of elements is less than 3.<commit_after>"
A layout that packs rectangles in as small space as possible (at least it tries to)

"
Class {
	#name : #RSRectanglePackLayout,
	#superclass : #RSLayout,
	#instVars : [
		'freeSpace',
		'gap',
		'padding',
		'bounds',
		'preferredAngle'
	],
	#category : #'Roassal3-Layouts-Core'
}

{ #category : #'space-calculation' }
RSRectanglePackLayout >> bestSpaceFor: anElement [
	| elementsPadedBox suitableSpaces |
	
	elementsPadedBox := anElement encompassingRectangle extendBy: padding * 2.
	
	suitableSpaces := freeSpace select: [ :space |
		elementsPadedBox width <= space width and:
			[ elementsPadedBox height <= space height ] ].
	
	^ suitableSpaces
		fold: [ :best :current |
			| bestExt currentExt | 
			((currentExt := self extentByInserting: elementsPadedBox into: current) <
			(bestExt := self extentByInserting: elementsPadedBox into: best) or: [ 
				currentExt = bestExt and: [
					current halfPerimeter < best halfPerimeter ] ])
						ifTrue: [ current ]
						ifFalse: [ best ] ]
]

{ #category : #accessing }
RSRectanglePackLayout >> boundingBox [
	^ bounds
]

{ #category : #hook }
RSRectanglePackLayout >> doExecute: elements [
	| sortedElements maxSpace |
	bounds := 0@0.
	
	maxSpace := elements inject: 0@0 into: [ :sum :el |
			sum + el extent ].
		
	padding := maxSpace / elements size * gap / 2.
	maxSpace :=(1 + gap) * maxSpace.
	
	freeSpace := Set new.
	freeSpace add: (0@0 corner: maxSpace ).
	
	sortedElements := SortedCollection sortBlock:  [ :larger :smaller |
		larger  encompassingRectangle halfPerimeter >
		smaller encompassingRectangle halfPerimeter ].
	sortedElements addAll: elements.
	
	sortedElements size <= 2 ifTrue: [ 
		RSHorizontalLineLayout new gapSize: gap; on: sortedElements.
		^ self
	].
	sortedElements
		do: [ :element | self packIn: element ]
		displayingProgress: 'Laying out elements'
]

{ #category : #'space-calculation' }
{'fraction_non_alphanumeric': 0.11185914034179181, 'fraction_numerical': 0.005955463490419472, 'mean_word_length': 3.9652956298200515, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3013177', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1229, 'n_words': 340}