Skip to content

Commit

Permalink
improve picker algo
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Herd committed Mar 23, 2024
1 parent 53d2d30 commit b51644d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 22 deletions.
5 changes: 0 additions & 5 deletions docs/assets/find-solution-9b735a6f.js

This file was deleted.

5 changes: 5 additions & 0 deletions docs/assets/find-solution-e3cff708.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script type="module" crossorigin src="/diorama-2023/assets/index-c42d2bc7.js"></script>
<script type="module" crossorigin src="/diorama-2023/assets/index-2c0ea7f0.js"></script>
<link rel="stylesheet" href="/diorama-2023/assets/index-208a7c4c.css">
</head>
<body>
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const maxComputationTime = 500 as Milliseconds
export const randomizeThreshold = 11 // if amount of pics is bigger we have too many permutations to iterate through, switch to random strategy
export const iconSize = 20
export const initialImageAmount = 7
export const aspectRatioThreshold = 0.82

export const aspectRatioAndSize = ({ pictures, sizeHomogeneity, score }: Solution): Ord => {
return pictures.length <= 4 ? score : score * sizeHomogeneity
Expand Down
14 changes: 4 additions & 10 deletions src/layout/evaluate-solutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
import '../types/ramda'

import type { PositionedPicture, NonEmptyArray, Solution } from '../types/types'
import { evolve, last, pipe, prop, reject, sortBy } from 'ramda'
import { aspectRatioAndSize, aspectRatioThreshold } from '../constants'
import { evolve, last, pipe, prop, sortBy, takeLast } from 'ramda'
import { aspectRatioAndSize } from '../constants'

export const evaluateSolutions = (results: NonEmptyArray<Solution>): Solution => {
const withOutGaps = reject<Solution>(({ score }) => score < aspectRatioThreshold)(results)

const winner = pipe(
sortBy(aspectRatioAndSize) as any,
last
)(withOutGaps.length > 0 ? withOutGaps : results) as Solution

return evolve({ pictures: sortBy<PositionedPicture>(prop('url')) }, winner)
const sorted = pipe(sortBy(aspectRatioAndSize), takeLast(10), sortBy(prop('score')))(results)
return evolve({ pictures: sortBy<PositionedPicture>(prop('url')) }, last(sorted) as Solution)
}
12 changes: 8 additions & 4 deletions src/ui/diorama.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ type OwnProps = {
images: Picture[]
}


const scale = (value: Solution, dimension: Dimension, p: 'width' | 'height'): number =>
prop(p, dimension) / prop(p, value.dimension)

declare global {
interface Window {
solution: Solution
}
}
export const Diorama = ({ images: initialImages }: OwnProps): JSX.Element => {
const [ref, dimension] = useParentResize<HTMLOListElement>()
const images = useImageList(initialImages)
Expand All @@ -22,13 +26,13 @@ export const Diorama = ({ images: initialImages }: OwnProps): JSX.Element => {
const renderedList = useMemo(
() => (
<ol ref={ref} className="diorama-list">
{value?.pictures.map((pic, idx) => (
{(window.solution ?? value)?.pictures.map((pic, idx) => (
<PictureListItem
picture={pic}
idx={idx}
key={pic.url}
scaleX={scale(value, dimension, 'width')}
scaleY={scale(value, dimension, 'height')}
scaleX={scale(window.solution ?? value, dimension, 'width')}
scaleY={scale(window.solution ?? value, dimension, 'height')}
/>
))}
</ol>
Expand Down

0 comments on commit b51644d

Please sign in to comment.