Skip to content

Commit

Permalink
Position logics in library
Browse files Browse the repository at this point in the history
  • Loading branch information
umbertoloria committed Nov 10, 2023
1 parent 6783665 commit 50c8ada
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,33 @@ export const blend = (a: RGB, b: RGB, percentage: number) => add(

// type Filter = (c: RGB) => RGB
// export const wrap = (filter: Filter, c: RGB) => copy(filter(copy(c)));


/// POSITION
export type POS = {
x: number;
y: number;
}

export const getCenter = (): POS => ({x: .5, y: .5}) as const;
export const getDistance = (a: POS, b: POS): number => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
export const getMaxRadiusInCube = (): number => Math.sqrt(.5);
export const gradientRadius = (a: POS, center: POS, maxDist: number): number => {
const dist = getDistance(a, center);
const distProp = dist / maxDist;
if (distProp > 1) {
return 0;
}
return 1 - distProp;
};

export const gradientLinear = (pos: number, from: number, to: number): number => {
const distProp = (pos - from) / (to - from);
if (distProp > 1) {
return 1;
}
if (distProp < 0) {
return 0;
}
return distProp;
};

0 comments on commit 50c8ada

Please sign in to comment.