/**
* Super simple sketch of basic lens optics
*
* Stephan Mantler
* step@stepman.is | instagram.com/stepman
*/
/*
* A simple class for 2D points / vectors
* This could probably be folded into SVG.Point but here it's a
* separate class for clarity.
*/
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
// return a copy that is rotated 90 degrees counterclockwise
get ccw90() {
return new Vector(-this.y, this.x);
}
// return a copy of this vector with unit length
get normalized() {
return this.scaledBy(1/this.length);
}
// return a copy that is scaled by the specified factor
scaledBy(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
}
// returns a copy that is the sum of this and another vector
add(vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
}
// returns a copy that is the difference of this and another vector
subtract(vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
}
// return the dot product of this and another vector
dot(vector) {
return this.x * vector.x + this.y * vector.y;
}
// return the length of this vector
get length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
/*
* Implements a simple optical lens
*
* We assume the optical assembly is along the X axis, but this lens
* can be tilted in any way (effeectively rotating it off axis from
* the focus plane, which we assume is always vertical)
*/
class Lens {
constructor(focalLength, aperture = 4) {
this.focalLength = focalLength;
this.aperture = aperture;
this.position = new Vector(0, 0);
// optical axis of the lens
this.orientation = new Vector(1, 0).normalized;
}
// returns the effective diameter (from focal length & aperture)
get diameter() { return this.focalLength / this.aperture; }
// returns the effective radius
get radius() { return this.diameter / 2; }
// return a...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.