Skip to content

Commit

Permalink
Merge pull request #227 from PRIME-TU-Delft/fix-diagonal-parrallelogram
Browse files Browse the repository at this point in the history
Fix diagonal parrallelogram
  • Loading branch information
yustarandomname authored Apr 30, 2024
2 parents 66f6fd5 + 783b852 commit 05abf68
Show file tree
Hide file tree
Showing 8 changed files with 9,844 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
2 changes: 1 addition & 1 deletion src/lib/d3-components/RightAngle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<!-- draw two lines to represent right angle if perpendicular -->

{#if Math.abs(u1.dot(u2)) <= 0.00000000001 && !u1.equals(u2)}
{#if Math.abs(u1.dot(u2)) <= 0.005 && !u1.equals(u2)}
<Line
{color}
width={lineWidth}
Expand Down
21 changes: 12 additions & 9 deletions src/lib/threlte-components/planes/AutoPlane.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
if (!vs) return [];
// group values in array of indeces [1, 1, 2, 3, 1, 3] => { 1: [0,1,4], 2: [2], 3: [3,5] }
const groups = vs.reduce((a, value, index) => {
if (value in a) {
a[value].push(index);
} else {
a[value] = [index];
}
return a;
}, {} as { [key: number]: number[] });
const groups = vs.reduce(
(a, value, index) => {
if (value in a) {
a[value].push(index);
} else {
a[value] = [index];
}
return a;
},
{} as { [key: number]: number[] }
);
return vs.map((v, index) => {
const offset = groups[v].findIndex((i) => i === index);
Expand Down
27 changes: 15 additions & 12 deletions src/routes/[...catchall]/FolderList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
return { file, folder };
})
.reduce((acc, curr) => {
const file = {
title: curr.file,
url: `/applet/${curr.folder}/${curr.file}`
};
.reduce(
(acc, curr) => {
const file = {
title: curr.file,
url: `/applet/${curr.folder}/${curr.file}`
};
if (curr.folder in acc) {
acc[curr.folder].push(file);
} else {
acc[curr.folder] = [file];
}
return acc;
}, {} as Record<string, File[]>);
if (curr.folder in acc) {
acc[curr.folder].push(file);
} else {
acc[curr.folder] = [file];
}
return acc;
},
{} as Record<string, File[]>
);
</script>

<Accordion.Root class="container my-10 mx-auto">
Expand Down
122 changes: 102 additions & 20 deletions src/routes/applet/dot_product/diagonal_parallelogram/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,69 +1,151 @@
<script lang="ts">
import { Arc2D, Canvas2D, Latex2D, Vector2D, Draggable2D } from '$lib/d3-components';
import { Canvas2D, Latex2D, Vector2D, Draggable2D } from '$lib/d3-components';
import { PrimeColor } from '$lib/utils/PrimeColors';
import { Vector2 } from 'three';
import { Vector2, Vector3 } from 'three';
import { GridType } from '$lib/d3-components/grids/GridTypes';
import { Formula } from '$lib/utils/Formulas';
import RightAngle from '$lib/d3-components/RightAngle.svelte';
import { snapPointToLine } from '$lib/utils/MathLib';
import StaticImg from './staticImg.svelte';
let w = new Vector2(2, 3);
let v = new Vector2(3, -2);
//[] todo consistant var naming -> camelcase or __
$: vPlusW = w.clone().add(v);
const snap_distance = 0.5;
let formulas: Formula[] = [];
//vars for interactive section
let origin = new Vector2(1.5, 0);
let w_plus_o = new Vector2(2, 3); //vectors are calculated this way because a draggable takes a global position, not a vector relative to origin
$: w = w_plus_o.clone().sub(origin);
let v_plus_o = new Vector2(3.5, -2);
$: v = v_plus_o.clone().sub(origin);
$: orthV = new Vector2(v.y, v.clone().x * -1).multiplyScalar(20); //line orth to v
$: orthW = new Vector2(w.y, w.clone().x * -1).multiplyScalar(20); //line orth to v
function snap_v(_: Vector2) {
let snap_v = snapPointToLine(v, orthW.clone().multiplyScalar(-1), orthW, snap_distance);
if (snap_v != null) {
v = snap_v;
v_plus_o = v.clone().add(origin);
}
}
function snap_w(_: Vector2) {
let snap_w = snapPointToLine(w, orthV.clone().multiplyScalar(-1), orthV, snap_distance);
if (snap_w != null) {
w = snap_w;
w_plus_o = w.clone().add(origin);
}
}
function setFormulas(b: boolean) {
const plus = ' | \\mathbf{v + w} |';
const min = '|\\mathbf{v - w}|';
if (b) {
const f3 = new Formula('{\\$1} = \\$2')
.addParam(1, plus, PrimeColor.raspberry)
.addParam(2, min, PrimeColor.orange);
formulas = [f3];
} else {
const f3 = new Formula('{\\$1} \\neq \\$2')
.addParam(1, plus, PrimeColor.raspberry)
.addParam(2, min, PrimeColor.orange);
formulas = [f3];
}
}
$: snap_v(v);
$: snap_w(w);
$: vPlusW = v.clone().add(w);
$: vMinusW = v.clone().sub(w);
// keeps track of orthogonality
$: isOrthogonal =
Math.abs(w.clone().normalize().dot(v.clone().normalize())) <= 0.05 && !v.equals(w);
$: setFormulas(isOrthogonal);
</script>

<Canvas2D>
<Draggable2D snap id="b1" bind:position={w} color={PrimeColor.blue} />
<Draggable2D snap id="b2" bind:position={v} color={PrimeColor.blue} />
<Canvas2D gridType={GridType.None} {formulas}>
<!-- Imported static side -->
<StaticImg />
<!-- Interactive side of image -->

<!-- Arcs -->
<Arc2D points={[v, w]} distance={0.75} color={PrimeColor.darkGreen} />
<Draggable2D id="w+o" bind:position={w_plus_o} color={PrimeColor.darkGreen} />
<Draggable2D id="v+o" bind:position={v_plus_o} color={PrimeColor.cyan} />

<!-- Bases -->
<Vector2D direction={v} length={v.length()} color={PrimeColor.blue} let:endPoint>
<Latex2D position={endPoint} latex={'\\mathbf{v}'} offset={new Vector2(-0.2, 0.5)} />
<Vector2D {origin} direction={v} length={v.length()} color={PrimeColor.cyan}>
<Latex2D
position={origin}
latex={'\\mathbf{v}'}
offset={v.clone().normalize().multiplyScalar(1.5).add(new Vector2(0, 0.2))}
color={PrimeColor.cyan}
/>
</Vector2D>
<Vector2D direction={w} length={w.length()} color={PrimeColor.darkGreen} let:endPoint>
<Latex2D position={endPoint} latex={'\\mathbf{w}'} offset={new Vector2(-0.2, 0.2)} />
<Vector2D {origin} direction={w} length={w.length()} color={PrimeColor.darkGreen}>
<Latex2D
position={origin}
latex={'\\mathbf{w}'}
offset={w.clone().normalize().multiplyScalar(1.5).add(new Vector2(0.1, 0))}
color={PrimeColor.darkGreen}
/>
</Vector2D>

<!-- v+w -->
<Vector2D direction={vPlusW} length={vPlusW.length()} color={PrimeColor.raspberry} let:endPoint>
<Vector2D
{origin}
direction={vPlusW}
length={vPlusW.length()}
color={PrimeColor.raspberry}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{v} + \\mathbf{w}'}
latex={'\\mathbf{v + w}'}
offset={new Vector2(0.2, 0.2)}
color={PrimeColor.raspberry}
/>
</Vector2D>

<!-- v-w -->
<Vector2D
origin={w}
origin={w.clone().add(origin)}
direction={vMinusW}
length={vMinusW.length()}
color={PrimeColor.orange}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{v} - \\mathbf{w}'}
latex={'\\mathbf{v - w}'}
offset={new Vector2(-0.2, -0.2)}
color={PrimeColor.orange}
/>
</Vector2D>

<!-- Helper lins -->
<Vector2D
origin={w}
origin={w.clone().add(origin)}
direction={v}
length={v.length()}
color={PrimeColor.black}
isDashed
hideHead
/>
<Vector2D
origin={v}
origin={v.clone().add(origin)}
direction={w}
length={w.length()}
color={PrimeColor.black}
isDashed
hideHead
/>
<RightAngle {origin} vs={[v, w]} />
</Canvas2D>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<script lang="ts">
import { Latex2D, Vector2D } from '$lib/d3-components';
import { PrimeColor } from '$lib/utils/PrimeColors';
import { Vector2 } from 'three';
import RightAngle from '$lib/d3-components/RightAngle.svelte';
//vars for static section
let o_static = new Vector2(-4, -1);
let w_static = new Vector2(1.5, 3);
let v_static = new Vector2(2, -1);
let vPlusW_static = v_static.clone().add(w_static);
let vMinusW_static = v_static.clone().sub(w_static);
</script>

<!-- Static part of image -->

<!-- static Bases -->
<Vector2D
origin={o_static}
direction={v_static}
length={v_static.length()}
color={PrimeColor.cyan}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{v}'}
offset={new Vector2(-0.4, 0.5)}
color={PrimeColor.cyan}
/>
</Vector2D>
<Vector2D
origin={o_static}
direction={w_static}
length={w_static.length()}
color={PrimeColor.darkGreen}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{w}'}
offset={new Vector2(-0.2, 0.2)}
color={PrimeColor.darkGreen}
/>
</Vector2D>

<!-- static v+w -->
<Vector2D
origin={o_static}
direction={vPlusW_static}
length={vPlusW_static.length()}
color={PrimeColor.raspberry}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{v + w}'}
offset={new Vector2(0.2, 0.2)}
color={PrimeColor.raspberry}
/>
</Vector2D>

<!-- static v-w -->
<Vector2D
origin={w_static.clone().add(o_static)}
direction={vMinusW_static}
length={vMinusW_static.length()}
color={PrimeColor.orange}
let:endPoint
>
<Latex2D
position={endPoint}
latex={'\\mathbf{v - w}'}
offset={new Vector2(-0.2, -0.2)}
color={PrimeColor.orange}
/>
</Vector2D>

<!-- Static Helper lines -->
<Vector2D
origin={w_static.clone().add(o_static)}
direction={v_static}
length={v_static.length()}
color={PrimeColor.black}
isDashed
hideHead
/>
<Vector2D
origin={v_static.clone().add(o_static)}
direction={w_static}
length={w_static.length()}
color={PrimeColor.black}
isDashed
hideHead
/>

<RightAngle origin={o_static} vs={[v_static, w_static]} />
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
<Vector3D direction={v_a} color={PrimeColor.blue} length={v_len} />
<Latex3D
latex={'\\mathbf{v}'}
position={v_a.clone().multiplyScalar(0.5).add(new Vector3(-0.7, -0.7, 0))}
position={v_a
.clone()
.multiplyScalar(0.5)
.add(new Vector3(-0.7, -0.7, 0))}
color={PrimeColor.blue}
/>

Expand Down
Loading

0 comments on commit 05abf68

Please sign in to comment.