-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create new applet for alternative basis
- Loading branch information
Showing
5 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/routes/applet/change_of_basis/alternative_basis/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
102 changes: 102 additions & 0 deletions
102
src/routes/applet/change_of_basis/alternative_basis/TransformedAxis.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |