This commit is contained in:
Randy8080 committed 2022-10-18 11:37:51 +08:00
1 parent 7fc7945881
commit 098238ad13
286 files changed
+8596

No files matched your search

@@ -0,0 +1,26 @@
---
title: How to calculate the angle of a line defined by two points in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to calculate the angle of a line defined by two points only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// In radians
const radiansAngle = (p1, p2) => Math.atan2(p2.y - p1.y, p2.x - p1.x);
// In degrees
const degreesAngle = (p1, p2) => Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
```
@@ -0,0 +1,22 @@
---
title: How to calculate the distance between two points in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to calculate the distance between two points only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const distance = (p1, p2) => Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
```
@@ -0,0 +1,22 @@
---
title: How to calculate the linear interpolation between two numbers in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to calculate the linear interpolation between two numbers only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const lerp = (a, b, amount) => (1 - amount) * a + amount * b;
```
@@ -0,0 +1,22 @@
---
title: How to calculate the midpoint between two points in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to calculate the midpoint between two points only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const midpoint = (p1, p2) => [(p1.x + p2.x) / 2, (p1.y + p2.y) / 2];
```
@@ -0,0 +1,22 @@
---
title: How to check if a point is inside a rectangle in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to check if a point is inside a rectangle only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isInside = (point, rect) => point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom;
```
@@ -0,0 +1,24 @@
---
title: How to check if a rectangle contains other one in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to check if a rectangle contains other one only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// Returns true if `a` contains `b`
// (x1, y1) and (x2, y2) are top-left and bottom-right corners
const contains = (a, b) => (a.x1 <= b.x1 && a.y1 <= b.y1 && a.x2 >= b.x2 && a.y2 >= b.y2);
```
@@ -0,0 +1,24 @@
---
title: How to check if a rectangle overlaps other one in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to check if a rectangle overlaps other one only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// Returns true if `a` overlaps `b`
// (x1, y1) and (x2, y2) are top-left and bottom-right corners
const overlaps = (a, b) => (a.x1 < b.x2 && b.x1 < a.x2) || (a.y1 < b.y2 && b.y1 < a.y2);
```
@@ -0,0 +1,22 @@
---
title: How to convert degrees to radians in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to convert degrees to radians only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const degsToRads = deg => deg * Math.PI / 180.0;
```
@@ -0,0 +1,22 @@
---
title: How to convert radians to degrees in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to convert radians to degrees only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const radsToDegs = rad => rad * 180 / Math.PI;
```
@@ -0,0 +1,22 @@
---
title: How to normalize the ratio of a number in a range in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to normalize the ratio of a number in a range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const normalizeRatio = (value, min, max) => (value - min) / (max - min);
```
@@ -0,0 +1,30 @@
---
title: How to round a number to the nearest multiple of a given value in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Math
layout: note
---
In this Article we will go through how to round a number to the nearest multiple of a given value only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const roundNearest = (value, nearest) => Math.round(value / nearest) * nearest;
```
### Example
```js {.wrap}
roundNearest(100, 30); // 90
roundNearest(200, 30); // 210
roundNearest(200, 40); // 200
```