Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Created August 13, 2016 18:44
Show Gist options
  • Save mathdoodle/6987997fa058e53047268e4d645cddca to your computer and use it in GitHub Desktop.
Save mathdoodle/6987997fa058e53047268e4d645cddca to your computer and use it in GitHub Desktop.
Finite Line Charge

World Scaffold

Overview

A scaffold application that separates typical setup code from the main animation loop where the action happens.

<!doctype html>
<html>
<head>
<style>
/* STYLE-MARKER */
</style>
<script src="https://jspm.io/system.js"></script>
<!-- SCRIPTS-MARKER -->
</head>
<body>
<canvas id='canvas'></canvas>
<script>
// CODE-MARKER
</script>
<script>
System.import('./index.js')
</script>
</body>
</html>
import World from './World'
import {scale} from './World'
import {i, j, k} from './units'
import {meter, kilogram, second, newton} from './units'
const L = 10 * meter;
/**
* Position of the charge
*/
// let X: UNITS.G3 = e1;
/**
*
*/
// const Q =
console.log(`A Newton is ${newton}`)
const world = new World()
const wire = new EIGHT.Cylinder({
})
wire.color = EIGHT.Color.gray
wire.length = scale(L, meter).a;
wire.radius = 0.05
wire.axis = scale(i, meter)
world.add(wire)
const element = new EIGHT.Cylinder()
element.color = EIGHT.Color.red
element.radius = wire.radius + 0.001 // Improves graphics
element.length = 0.05
// element.X = -0.5 * e3
// element.axis = e3
world.add(element)
const grid = new EIGHT.Grid({
uSegments: 8,
vSegments: 8
})
grid.color = EIGHT.Color.gray
world.add(grid)
const arrow = new EIGHT.Arrow();
// arrow.h = e3
world.add(arrow)
/**
* Animates the scene.
*/
function animate() {
world.clear()
world.draw()
requestAnimationFrame(animate)
}
requestAnimationFrame(animate)
//
// Basis elements
//
export const zero = EIGHT.Geometric3.zero()
export const one = EIGHT.Geometric3.one()
export const e1 = EIGHT.Geometric3.e1()
export const e2 = EIGHT.Geometric3.e2()
export const e3 = EIGHT.Geometric3.e3()
/**
* The pseudoscalar for Euclidean 3D Geometric Space.
*/
const I = e1 ^ e2 ^ e3
//
// Universal functions
//
export const exp = EIGHT.exp
export const log = EIGHT.log
export const cos = EIGHT.cos
export const sin = EIGHT.sin
//
// Constants
//
/**
* A complete turn, 2 * π.
*/
export const τ = 2 * Math.PI
{
"description": "Finite Line Charge",
"dependencies": {
"davinci-eight": "2.245.0",
"davinci-units": "1.0.0"
},
"operatorOverloading": true,
"name": "finite-line-charge",
"version": "0.1.0",
"author": "David Geo Holmes"
}
body {
margin: 0;
overflow: hidden;
}
canvas { width: 500px; height: 500px }
#stats { position: absolute; top: 0; left: 0; }
export const unitless = UNITS.G3.scalar(1)
export const i = UNITS.G3.e1
export const j = UNITS.G3.e2
export const k = UNITS.G3.e3
export const meter = UNITS.G3.meter
export const kilogram = UNITS.G3.kilogram
export const second = UNITS.G3.second;
export const newton = kilogram * meter / (second * second)
export const coulomb = UNITS.G3.coulomb
export const ε0 = 8.854e-12 * coulomb * coulomb / (meter * meter * newton)
// console.log(`${ε0.toPrecision(4)}`)
UNITS.G3.BASIS_LABELS = UNITS.G3.BASIS_LABELS_HAMILTON
// console.log(`${i},${j},${k}`)
import {e1, e2, e3} from './math'
export default class World {
private engine: EIGHT.Engine;
private scene: EIGHT.Scene;
private ambients: EIGHT.Facet[] = [];
private trackball: EIGHT.TrackballControls;
private dirLight: EIGHT.DirectionalLight;
private camera: EIGHT.PerspectiveCamera;
constructor() {
this.engine = new EIGHT.Engine('canvas')
.size(500,500)
.clearColor(0.1, 0.1, 0.1, 1.0)
.enable(EIGHT.Capability.DEPTH_TEST)
.enable(EIGHT.Capability.BLEND)
.blendFunc(EIGHT.BlendingFactorSrc.SRC_ALPHA, EIGHT.BlendingFactorDest.ONE);
this.scene = new EIGHT.Scene(this.engine)
this.camera = new EIGHT.PerspectiveCamera()
this.camera.eye.copy(e3-e2).normalize().scale(2)
this.camera.up.copy(e2)
this.ambients.push(this.camera)
this.dirLight = new EIGHT.DirectionalLight()
this.ambients.push(this.dirLight)
this.trackball = new EIGHT.TrackballControls(this.camera, window)
// Workaround because Trackball no longer supports context menu for panning.
this.trackball.noPan = true
this.trackball.subscribe(this.engine.canvas)
}
/**
* Adds a drawable object to the world.
*/
add(drawable: EIGHT.AbstractDrawable): void {
if (drawable) {
this.scene.add(drawable)
}
else {
}
}
/**
*
*/
clear(): void {
this.engine.clear()
this.trackball.update()
this.dirLight.direction.copy(this.camera.look).sub(this.camera.eye)
}
/**
*
*/
draw(): void {
this.scene.draw(this.ambients)
}
}
export function scale(quantity: UNITS.G3, scaleFactor: UNITS.G3): EIGHT.Geometric3 {
const dimless = quantity / scaleFactor
const uom = dimless.uom
if (uom.isOne()) {
const result = new EIGHT.Geometric3()
result.a = dimless.a
result.x = dimless.x
result.y = dimless.y
result.z = dimless.z
result.xy = dimless.xy
result.yz = dimless.yz
result.zx = dimless.zx
result.b = dimless.b
return result;
}
else {
throw new Error(`units of scaleFactor, ${scaleFactor}, is not consistent with quatity, ${quantity}.`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment