Skip to content

Commit 0111623

Browse files
committed
fix: plots are no longer upside down
1 parent 67ed3e6 commit 0111623

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/components/FunctionPlot.vue

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const props = withDefaults(
3636
3737
let animationFrameID: number | null = null;
3838
39-
const { domain, size, invScale } = useGraphContext();
39+
const { domain, invScale } = useGraphContext();
4040
const { parentToWorld } = useMatrices();
4141
const { parseColor } = useColors();
4242
@@ -48,10 +48,8 @@ const functionDomain = computed(() =>
4848
);
4949
5050
const visiblePoints = computed(() => {
51-
const range = Math.abs(functionDomain.value.x - functionDomain.value.y);
52-
const step = range / size.value.x;
53-
const i = Math.ceil((props.end - functionDomain.value.x) / step);
54-
return points.value.slice(0, i);
51+
const i = Math.ceil((props.end - functionDomain.value.x) / props.step);
52+
return points.value.slice(0, i + 1);
5553
});
5654
5755
const path = computed(() => {
@@ -72,12 +70,22 @@ const path = computed(() => {
7270
function updatePoints() {
7371
const now = Date.now();
7472
points.value = [];
75-
const range = Math.abs(functionDomain.value.x - functionDomain.value.y);
76-
const step = range / size.value.x;
77-
for (let i = 0; i <= size.value.x; i++) {
78-
const x = functionDomain.value.x + i * step;
73+
const samples =
74+
Math.ceil((functionDomain.value.y - functionDomain.value.x) / props.step) +
75+
1;
76+
for (let i = 0; i < samples; i++) {
77+
let x: number;
78+
79+
// special case handling for the last point to account for
80+
// floating point precision issues
81+
if (i === samples - 1) {
82+
x = functionDomain.value.y;
83+
} else {
84+
x = functionDomain.value.x + i * props.step;
85+
}
86+
7987
const y = props.function(x, now);
80-
points.value.push(new Vector2(x, -y));
88+
points.value.push(new Vector2(x, y));
8189
}
8290
}
8391

0 commit comments

Comments
 (0)