GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaScript/Maths/GridGet.js at master · TheAlgorithms/JavaScript

Expand file tree

/

GridGet.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (44 loc) · 1.77 KB
/

GridGet.js

Copy path

File metadata and controls

53 lines (44 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
author: TangibleDream
license: GPL-3.0 or later
These methods will find x or y given the element and columns for a 2 dimensional array.
If your array is a perfect square, you can find columns by getting the square
root of the length of the array.
Let's say for instance you had an array of 10 by 10 or 100, elements and you wanted to
find the shortest distance between element 3, and element 49. In this case coding out
a function to return the distance without finding x and y for both elements I found to
be painful. If you first find x and y, where 3 becomes 4,1 and 49 becomes 10,5, you can
find distance by first subtracting x from x and y from y this returns 6,4 or -6,-4.
Next apply absolute value to assure the results are positive,
last choose the maximum value of the set, or 6.
+--+--+--+--+--+--+--+--+--+--+
| | | | 3| | | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | |49|
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | 3| | | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | 1| | | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | | 2| | | | |
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | 3| 4| 5|6!|
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
*/
const gridGetX = (columns, index) => {
while (index + 1 > columns) {
index = index - columns
}
return index + 1
}
const gridGetY = (columns, index) => {
return Math.floor(index / columns) + 1
}
export { gridGetX, gridGetY }
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.