Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                
// Standard pythagoras
const pythagoras = (a, b) => Math.sqrt(a * a + b * b); // c = Math.sqrt(a*a + b*b)

// Using it to establish a simple distance calculation
// Here [x0, y0] .. define coordinate pairs in 2 dimensions
const distance2D = (x0, y0, x1, y1) => pythagoras(x1 - x0, y1 - y0);

// Expanding it to N dimensions
// vector is a list
// vector.reduce executes a reducer function (that you provide) on each element of the array, resulting in single output value. *1
const pythagorasND = (vector) => Math.sqrt(
  vector.reduce((sum, value) => sum + value * value)
);

// Expanding the distance function to N dimensions
// p0 and p1 are lists or vectors defining poinst in a respective dimension
// p0.map creates a new array populated with the results of calling a provided function on every element in the calling array
const distanceND = (p0, p1) => pythagorasND(
  p0.map((value, index) => value - p1[index])
);

// Keep in mind, for constant dimension sizes, it is always more performant to manually write out the distance function for the given dimension
const listA = [2,3,4,5];
const listB = [1,2,3,4];

console.log(distanceND(listA, listB)); // Prints: 2.0

// Practical Tasks
// Task 1: Create a function dedicated to finding the distance between two coordinate points in three dimensions by finishing these functions
// Replace 0s with your code

const pythagoras3D = (a, b, c) => 0;

const distance3D = (x0, y0, z0, x1, y1, y2) => pythagoras3D(
  0, 0, 0
);

// Results -- Should output: ~1.732
console.log("Task 1: expected", 1.732);

console.log(
  "Task 1: result", 
  distance3D(
    1, 2, 3, // First coordinate [x0, y0, z0]
    2, 3, 4  // Second coordinate [x1, y1, z1]
  )
); 

// Task 2: Find the closest point to our current position by finishing this formula
const ourPosition = [65,25];

const points = [
  [33,144],
  [155,55],
  [66,177],
  [76,12],
  [33,45]
];

let minimumDistance = 999999;
let closestPoint = null;

// Iterate through all the points, this can also be represented using array reduce, but is generally less readable that way
for (let i = 0; i < points.length; i++) {
  let point = points[i];
  let distance = 0;
  if (distance < minimumDistance) { // if the distance is less than the previous closest position, set the position and new minimum distance
    closestPoint = point;
    minimumDistance = distance;
  }
}

console.log("Task 2: expected", [76, 12], "with a distance of", 17.029);
console.log("Task 2: result", "The closest point is ", closestPoint, "with a distance of", minimumDistance);



              
            
!
999px

Console