Skip to content

Commit

Permalink
fix z-fighting in an example
Browse files Browse the repository at this point in the history
  • Loading branch information
SeiyaCooper committed Mar 23, 2024
1 parent 865de42 commit 6e21320
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 0 additions & 2 deletions site/pages/gallery/Axes.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import Example from "../../layouts/Example.astro";
const axes = new mp.Axes({
xRange: [-10, 10, 1], // You can set the length of every axis like this
});
axes.material = new mp.MobjectMaterial();
axes.material.depthTest = false;

// Draw function figures
const figure0 = axes.drawFunction2D((x) => x);
Expand Down
11 changes: 9 additions & 2 deletions src/mobjects/Axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class Axes extends Graph2D {
this.yRange = yRange;
this.zRange = zRange;

this.graphs = [];

this.addChild(this.xAxis);
this.addChild(this.yAxis);
this.addChild(this.zAxis);
Expand All @@ -44,12 +46,17 @@ export default class Axes extends Graph2D {
}
}

drawFunction2D(func, step = 0.1) {
drawFunction2D(func, { step = 0.1, autoStack = true } = {}) {
const range = this.xRange;
range[2] = step;
const graph = new FunctionGraph2D(func, { xRange: range });
const last = this.graphs[this.graphs.length - 1];
const z = autoStack && last ? last.z + 0.001 : 0.01;

const graph = new FunctionGraph2D(func, { xRange: range, z: z });
graph.update();

this.addChild(graph);
this.graphs.push(graph);
return graph;
}

Expand Down
7 changes: 4 additions & 3 deletions src/mobjects/FunctionGraph2D.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Graph2D from "./Graph2D.js";

export default class FunctionGraph2D extends Graph2D {
constructor(func = (x) => x, { xRange = [-8, 8, 0.1] } = {}) {
constructor(func = (x) => x, { xRange = [-8, 8, 0.1], z = 0 } = {}) {
super();
this.xRange = xRange;
this.func = func;
this.z = z;
}

update() {
Expand All @@ -14,9 +15,9 @@ export default class FunctionGraph2D extends Graph2D {
const func = this.func;

this.clear();
this.move([from, func(from), 0]);
this.move([from, func(from), this.z]);
for (let i = from + step; i <= to; i += step) {
this.line([i, func(i), 0]);
this.line([i, func(i), this.z]);
}
this.stroke();
return this;
Expand Down
10 changes: 8 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,11 +1445,13 @@ declare module "mobjects/Axis" {
}
declare module "mobjects/FunctionGraph2D" {
export default class FunctionGraph2D extends Graph2D {
constructor(func?: (x: any) => any, { xRange }?: {
constructor(func?: (x: any) => any, { xRange, z }?: {
xRange?: number[];
z?: number;
});
xRange: number[];
func: (x: any) => any;
z: number;
update(): this;
redraw(): this;
}
Expand All @@ -1471,8 +1473,12 @@ declare module "mobjects/Axes" {
xRange: number[];
yRange: number[];
zRange: number[];
graphs: any[];
addTip(): void;
drawFunction2D(func: any, step?: number): FunctionGraph2D;
drawFunction2D(func: any, { step, autoStack }?: {
step?: number;
autoStack?: boolean;
}): FunctionGraph2D;
set tickLength(val: number);
get tickLength(): number;
}
Expand Down

0 comments on commit 6e21320

Please sign in to comment.