Skip to content

Commit

Permalink
Merge pull request #249 from PRIME-TU-Delft/#165-alternative-basis
Browse files Browse the repository at this point in the history
Create new applet for alternative basis
  • Loading branch information
yustarandomname authored Oct 4, 2024
2 parents 37bca42 + a46018b commit 3b11b6f
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/lib/d3/Canvas2D.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
tickLength = 30,
showAxisNumbers = true,
enablePan = true,
customAxis = false,
draggables = []
}: CanvasProps = $props();
Expand Down Expand Up @@ -138,6 +139,7 @@
{showAxisNumbers}
{enablePan}
{draggables}
{customAxis}
>
{@render children()}
</CanvasD3>
Expand Down
7 changes: 6 additions & 1 deletion src/lib/d3/CanvasD3.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
enablePan?: boolean;
draggables?: Draggable[];
isSplit?: boolean;
customAxis?: boolean;
children: Snippet;
};
</script>
Expand Down Expand Up @@ -47,6 +48,7 @@
enablePan = true,
draggables = [],
isSplit = false,
customAxis = false,
children
}: Canvas2DProps = $props();
Expand Down Expand Up @@ -167,7 +169,10 @@
30})"
>
<g transform="translate({-cameraPosition.x}, {-cameraPosition.y})">
<Axis {showAxisNumbers} length={tickLength} />
{#if !customAxis}
<Axis {showAxisNumbers} length={tickLength} />
{/if}

{@render children()}

{#each draggables as d}
Expand Down
36 changes: 22 additions & 14 deletions src/lib/d3/Vector2D.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { Snippet } from 'svelte';
import { Vector2 } from 'three';
import Line2D from './Line2D.svelte';
import Point2D from './Point2D.svelte';
import Triangle2D from './Triangle2D.svelte';
type VectorProps = {
Expand Down Expand Up @@ -42,18 +43,10 @@
}
});
const coneStart = $derived(
origin.clone().add(direction.clone().multiplyScalar(length - coneHeight / 2))
);
const leftConePoint = $derived(
coneStart
.clone()
.add(new Vector2(-direction.y, direction.x).normalize().multiplyScalar(CONE_DIAMETER))
);
const rightConePoint = $derived(
coneStart
.clone()
.add(new Vector2(direction.y, -direction.x).normalize().multiplyScalar(CONE_DIAMETER))
const coneStart = $derived(length + coneHeight * (length > 0 ? -0.5 : 1.5));
const coneStartPos = $derived(
origin.clone().add(direction.clone().multiplyScalar(coneStart - coneHeight / 2))
);
</script>

Expand All @@ -74,10 +67,25 @@
-->

<!-- Line 2D -->
<Line2D start={origin} end={coneStart} {color} width={radius} {isDashed} />
<Line2D start={origin} end={coneStartPos} {color} width={radius} {isDashed} />

{#if !hideHead}
<Triangle2D points={[leftConePoint, endPoint, rightConePoint]} {color} />
{#if length == 0}
<Point2D position={new Vector2()} {color} />
{:else}
<g
transform={`translate(${origin.x}, ${origin.y}) rotate(${(direction.angle() * 180) / Math.PI}) translate(${coneStart - coneHeight / 2}, 0) rotate(${length < 0 ? 90 : -90})`}
>
<Triangle2D
points={[
new Vector2(CONE_DIAMETER, 0),
new Vector2(-CONE_DIAMETER, 0),
new Vector2(0, coneHeight)
]}
{color}
/>
</g>
{/if}
{/if}

{#if children}
Expand Down
73 changes: 73 additions & 0 deletions src/routes/applet/change_of_basis/alternative_basis/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<script>
import Canvas2D from '$lib/d3/Canvas2D.svelte';
import { Vector2 } from 'three';
import TransformedAxis from './TransformedAxis.svelte';
import Vector2D from '$lib/d3/Vector2D.svelte';
import { PrimeColor } from '$lib/utils/PrimeColors';
import Latex2D from '$lib/d3/Latex2D.svelte';
import { Draggable } from '$lib/controls/Draggables.svelte';
import { round } from '$lib/utils/MathLib';
import { Formula } from '$lib/utils/Formulas';
const b1 = new Vector2(1, 3);
const b2 = new Vector2(2, 1);
const draggables = [new Draggable(new Vector2(6, -2))];
const x = $derived(draggables[0].value.x);
const y = $derived(draggables[0].value.y);
const c1 = $derived(x + (2 / 5) * (y - 3 * x));
const c2 = $derived((-1 / 5) * (y - 3 * x));
const formulas = $derived.by(() => {
const vec = `\\begin{bmatrix} ${round(x)} \\\\ ${round(y)} \\end{bmatrix}`;
const f1 = new Formula('\\$1 = \\$2 \\$3 + \\$4 \\$5')
.addAutoParam(vec, PrimeColor.blue)
.addAutoParam(round(c1), PrimeColor.cyan)
.addAutoParam('\\mathbf{b_1}', PrimeColor.raspberry)
.addAutoParam(round(c2), PrimeColor.cyan)
.addAutoParam('\\mathbf{b_2}', PrimeColor.raspberry);
return [f1];
});
</script>

<Canvas2D customAxis {draggables} {formulas} showFormulasDefault cameraZoom={0.5}>
<TransformedAxis basis={[b1, b2]} />

<!-- Basis -->
<Vector2D direction={b1} length={b1.length()} color={PrimeColor.raspberry} />
<Latex2D latex={'\\mathbf{b_1}'} position={b1} extend={0.2} color={PrimeColor.raspberry} />

<Vector2D direction={b2} length={b2.length()} color={PrimeColor.raspberry} />
<Latex2D latex={'\\mathbf{b_2}'} position={b2} extend={0.2} color={PrimeColor.raspberry} />

<!-- Compound -->
<Vector2D
direction={b1.clone().multiplyScalar(c1)}
length={b1.clone().multiplyScalar(c1).length()}
color={PrimeColor.cyan}
/>
<Latex2D
latex={`\\mathbf{${round(c1)} b_1}`}
position={b1.clone().multiplyScalar(c1)}
extend={0.2}
color={PrimeColor.cyan}
/>

<Vector2D
origin={b1.clone().multiplyScalar(c1)}
direction={b2.clone().multiplyScalar(c2)}
length={b2.clone().multiplyScalar(c2).length()}
color={PrimeColor.cyan}
/>
<Latex2D
latex={`\\mathbf{${round(c2)}b_2}`}
offset={b1.clone().multiplyScalar(c1)}
position={b2.clone().multiplyScalar(c2)}
extend={0.2}
color={PrimeColor.cyan}
/>
</Canvas2D>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<script lang="ts">
import Latex2D from '$lib/d3/Latex2D.svelte';
import { GRID_SIZE_2D } from '$lib/utils/AttributeDimensions';
import { PrimeColor } from '$lib/utils/PrimeColors';
import { Vector2 } from 'three';
type AxisProps = {
length?: number;
showOrigin?: boolean;
basis: [Vector2, Vector2];
showAxisNumbers?: boolean;
};
let {
length = GRID_SIZE_2D,
showOrigin = true,
showAxisNumbers = true,
basis = [new Vector2(1, 0), new Vector2(0, 1)]
}: AxisProps = $props();
// Generate indeces for the grid lines from -length to length including 0
let axisIndeces = $derived([...Array(length + 1).keys()].flatMap((a) => [-a, a]));
function stokeWidth(index: number) {
if (index % 10 == 0) return 0.02;
if (index % 5 == 0) return 0.01;
return 0.005;
}
function radiansToDegrees(radians: number) {
return radians * (180 / Math.PI);
}
let xRotation = radiansToDegrees(Math.atan(basis[0].y / basis[0].x));
let yRotation = radiansToDegrees(Math.atan(-basis[1].x / basis[1].y));
</script>

<g>
<line
x1={0}
y1={-length}
x2={0}
y2={length}
stroke={PrimeColor.black + PrimeColor.opacity(0.5)}
stroke-width={0.04}
/>
<line
x1={-length}
y1={0}
x2={length}
y2={0}
stroke={PrimeColor.black + PrimeColor.opacity(0.5)}
stroke-width={0.04}
/>

{#each axisIndeces as index}
<!-- Grid Lines -->
<line
transform="rotate({yRotation}) scale({basis[1].length()})"
x1={index}
y1={-length}
x2={index}
y2={length}
stroke={PrimeColor.black + PrimeColor.opacity(0.5)}
stroke-width={stokeWidth(index)}
/>
<line
transform="rotate({xRotation}) scale({basis[0].length() / 2})"
x1={-length}
y1={index}
x2={length}
y2={index}
stroke={PrimeColor.black + PrimeColor.opacity(0.5)}
stroke-width={stokeWidth(index)}
/>

<!-- Tick marks -->
<line x1={index} y1={-0.1} x2={index} y2={0.1} stroke="black" stroke-width={0.02} />
<line x1={-0.1} y1={index} x2={0.1} y2={index} stroke="black" stroke-width={0.02} />

{#if index != 0 && showAxisNumbers}
<!-- X axis number labels -->
{#if index > 0 && index % 2 == 0}
<Latex2D latex={index.toLocaleString()} position={new Vector2(index - 0.07, -0.15)} />
{:else if index % 2 == 0}
<Latex2D latex={index.toLocaleString()} position={new Vector2(index - 0.15, -0.15)} />
{/if}

<!-- Y axis number labels -->
{#if index > 0 && index % 2 == 0}
<Latex2D latex={index.toLocaleString()} position={new Vector2(-0.3, index + 0.12)} />
{:else if index % 2 == 0}
<Latex2D latex={index.toLocaleString()} position={new Vector2(-0.55, index + 0.1)} />
{/if}
{/if}
{/each}

<!-- Axis labels -->
{#if showOrigin}
<Latex2D latex={'O'} offset={new Vector2(-0.28, -0.11)} />
{/if}
</g>

0 comments on commit 3b11b6f

Please sign in to comment.