Skip to content

Commit

Permalink
Bounds with steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 26, 2024
1 parent 3bcf685 commit 7110b10
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/components/config-editor/components/config-bounds.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
const props = withDefaults(defineProps<{
values: number[];
values: [number, number, number?];
name: string;
min?: number;
max?: number;
Expand All @@ -21,6 +21,7 @@ const props = withDefaults(defineProps<{
<tr>
<td>min</td>
<td>max</td>
<td v-if="values[2] !== undefined" width="33%">step</td>
</tr>
<tr>
<td>
Expand All @@ -29,6 +30,9 @@ const props = withDefaults(defineProps<{
<td>
<input type="number" v-model="values[1]" :step="step" :min="min" :max="max">
</td>
<td v-if="values[2] !== undefined">
<input type="number" v-model="values[2]" :min="0" :max="values[1] - values[0]">
</td>
</tr>
</table>
</div>
Expand Down
14 changes: 8 additions & 6 deletions src/lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,21 @@ export function createRandomTypesConfig({
LINK_BOUNDS,
LINK_FACTOR_DISTANCE_BOUNDS,
}: RandomTypesConfig): TypesConfig {
const precision = 8;

const gravity: number[][] = [];
for (let i=0; i<TYPES_COUNT; ++i) {
gravity.push([]);
for (let j=0; j<TYPES_COUNT; ++j) {
gravity[i].push(createRandomFloat(GRAVITY_BOUNDS, 2));
gravity[i].push(createRandomFloat(GRAVITY_BOUNDS, precision));
}
}

const linkGravity: number[][] = [];
for (let i=0; i<TYPES_COUNT; ++i) {
linkGravity.push([]);
for (let j=0; j<TYPES_COUNT; ++j) {
linkGravity[i].push(createRandomFloat(LINK_GRAVITY_BOUNDS, 2));
linkGravity[i].push(createRandomFloat(LINK_GRAVITY_BOUNDS, precision));
}
}

Expand All @@ -106,7 +108,7 @@ export function createRandomTypesConfig({
for (let i=0; i<TYPES_COUNT; ++i) {
linkFactorDistance.push([]);
for (let j=0; j<TYPES_COUNT; ++j) {
linkFactorDistance[i].push(createRandomFloat(LINK_FACTOR_DISTANCE_BOUNDS, 2));
linkFactorDistance[i].push(createRandomFloat(LINK_FACTOR_DISTANCE_BOUNDS, precision));
}
}

Expand All @@ -129,10 +131,10 @@ export function createRandomTypesConfig({
export function createDefaultRandomTypesConfig(typesCount: number): RandomTypesConfig {
return {
TYPES_COUNT: typesCount,
GRAVITY_BOUNDS: [-1, 0.5],
LINK_GRAVITY_BOUNDS: [-1, 0.5],
GRAVITY_BOUNDS: [-1, 0.5, 0.01],
LINK_GRAVITY_BOUNDS: [-1, 0.5, 0.01],
LINK_BOUNDS: [1, 3],
LINK_TYPE_BOUNDS: [0, 3],
LINK_FACTOR_DISTANCE_BOUNDS: [0.5, 1.5],
LINK_FACTOR_DISTANCE_BOUNDS: [0.5, 1.5, 0.01],
};
}
13 changes: 10 additions & 3 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,17 @@ export function createRandomInteger([from, until]: [number, number]): number {
return Math.round(Math.random() * (until - from)) + from;
}

export function createRandomFloat([from, until]: [number, number], precision?: number): number {
const result = Math.random() * (until - from) + from;
export function createRandomFloat([from, until, step]: [number, number, number?], precision?: number): number {
let result = Math.random() * (until - from) + from;
if (step !== undefined) {
result = roundWithStep(result, step);
}
if (precision !== undefined) {
return Number(result.toFixed(precision));
result = Number(result.toFixed(precision));
}
return result;
}

export function roundWithStep(value: number, step: number): number {
return Math.round(value / step) * step;
}
6 changes: 3 additions & 3 deletions src/lib/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export type InitialConfig = {
};
export type RandomTypesConfig = {
TYPES_COUNT: number;
GRAVITY_BOUNDS: [number, number];
LINK_GRAVITY_BOUNDS: [number, number];
GRAVITY_BOUNDS: [number, number, number?];
LINK_GRAVITY_BOUNDS: [number, number, number?];
LINK_BOUNDS: [number, number];
LINK_TYPE_BOUNDS: [number, number];
LINK_FACTOR_DISTANCE_BOUNDS: [number, number];
LINK_FACTOR_DISTANCE_BOUNDS: [number, number, number?];
};

5 changes: 0 additions & 5 deletions src/store/simulation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { computed, type Ref, ref, watch } from "vue";
import { defineStore } from "pinia";
import type { InitialConfig, RandomTypesConfig, TypesConfig, WorldConfig } from "@/lib/types/config";
import { createBaseWorldConfig } from "@/lib/config/world";
import { createBaseTypesConfig, createDefaultRandomTypesConfig, createRandomTypesConfig } from "@/lib/config/types";
import { create2dBaseInitialConfig, create3dBaseInitialConfig } from "@/lib/config/initial";
import { fullCopyObject } from "@/helpers/utils";
import { getRandomColor } from "@/lib/helpers";
import { useConfigStore, type ViewMode } from "@/store/config";
import { Simulation } from "@/lib/simulation";
import { create2dRandomDistribution, create3dRandomDistribution } from "@/lib/config/atoms";
Expand Down

0 comments on commit 7110b10

Please sign in to comment.