Created
August 11, 2021 18:10
-
-
Save overcyn/9aef095b4f41f23d6345018b60ebf620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func add(x, y) { | |
| let p1 = {x: x, y: y} | |
| let points = array(); | |
| for (let i = 0; i < points.length; i++) { | |
| let p2 = points[i]; | |
| if (p2.x == p1.x && p2.y == p1.y) { | |
| continue; | |
| } | |
| let p3 = {x: p1.x - (p2.y - p1.y), y: p1.y + (p2.x - p1.x)}; | |
| let p4 = {x: p2.x - (p2.y - p1.y), y: p2.y + (p2.x - p1.x)}; | |
| let p3alt = {x: p1.x + (p2.y - p1.y), y: p1.y - (p2.x - p1.x)}; | |
| let p4alt = {x: p2.x + (p2.y - p1.y), y: p2.y - (p2.x - p1.x)}; | |
| if (contains(p3) && contains(p4) || (contains(p3alt) && contains(p4alt))) { | |
| return true; | |
| } | |
| } | |
| insert(p1) | |
| return false; | |
| } | |
| let set = {}; | |
| func contains(p) { | |
| return set[p.x + "," + p.y] !== undefined; | |
| } | |
| func insert(p) { | |
| set[p.x + "," + p.y] = p; | |
| } | |
| func array() { | |
| let array = []; | |
| let keys = set.keys; | |
| for (let i = 0; i < keys; i++) { | |
| let key = keys[i]; | |
| array.push(p[key]); | |
| } | |
| return array; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment