Last active
March 31, 2016 06:25
-
-
Save asduser/b178ad7f69769e9416a976143eda6ef5 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
/** | |
* This class provides a functional to find a point inside a specific figure. | |
* First of all you have to define a figure coordinates. | |
* Then use an appropriate method "check( point )", which will return a result. | |
*/ | |
class PointManager { | |
constructor(setList) { | |
this.setX = []; | |
this.setY = []; | |
setList.forEach((s) => { | |
s.forEach((coord, index) => index %2 == 0 ? this.setX.push(coord) : this.setY.push(coord)); | |
}); | |
} | |
check(point) { | |
let xMax = this.getMaxOfArray(this.setX); | |
let yMax = this.getMaxOfArray(this.setY); | |
let xMin = this.getMinOfArray(this.setX); | |
let yMin = this.getMinOfArray(this.setY); | |
let x = point.x >= xMin && point.x <= xMax; | |
let y = point.y >= yMin && point.y <= yMax; | |
return x && y ? "YES" : "NO"; | |
} | |
getMaxOfArray(numArray) { | |
return Math.max.apply(null, numArray); | |
} | |
getMinOfArray(numArray) { | |
return Math.min.apply(null, numArray); | |
} | |
} | |
// Define a new figure instance. | |
let p1 = new PointManager([[10,20], [20, 30]]); | |
// Case #1 will return a negative result. | |
console.log(p1.check({x:5,y:4})); | |
// Case #2 will return a positive result. | |
console.log(p1.check({x:10,y:29})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment