/* Copyright: Year : (c) 2023 Author/licensor : Rene De Ren Contact details : zn375ix3@gmail.com Location : The Netherlands Licensee : Waterschap brabantse delta Address: Bouvignelaan 5, 4836 AA Breda Permission is hereby granted, to the licensee , to use this software only for the purposes of testing it in its R&D lab. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The author shall be notified of any and all improvements or adaptations this software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //A class to interact and manipulate machines with a parabolic curve class Graph { /*------------------- Construct and set vars -------------------*/ constructor() { // current x and y this.x = 0; this.y = 0; //previous y and x this.y_prev = 0; this.x_prev = 0; //slope of current value this.slope = 0; } set slope(value){ this._slope = Math.round( value * 100 ) / 100; } get slope(){ return this._slope; } calc(x,y){ //store old values this.x_prev = this.x; this.y_prev = this.y; //store new values this.x = x; this.y = y; //calc slope this.slope = this.calc_slope(this.x,this.y,this.x_prev,this.y_prev); } calc_slope(x,y,x_prev,y_prev){ let slope = 0; let incline = false; let decline = false; if( x_prev > x){ decline = true; incline = false; } else if( x_prev < x ){ decline = false; incline = true; } //determine decline / incl if( (!decline && !incline) ){ slope = 0; } else if(decline){ slope = -( (y - y_prev) / ( x - x_prev) ) ; } else if(incline){ slope = ( (y - y_prev) / ( x - x_prev) ) ; } return slope; } // 1 a dimensional peak ( THIS WILL FIND ONLY 1 PEAK!! so assuming the array only has one !) one_dim_peak_finder(array){ //define array length let array_length = array.length; //start position in array is middle let recursive_pos = array_length/2; //define end conditions let max_iterations = 100; let done = false; let iteration = 0; let peak_found = false; while(!done){ //find peak going left if( array[recursive_pos-1] > array[recursive_pos] ){ //calc new pos recursive_pos = (recursive_pos-1) / 2; } //find peak going right else if( array[recursive_pos+1] > array[recursive_pos] ){ //calc new pos recursive_pos = (recursive_pos+1) / 2; } //found peak! else{ done = true; peak_found = true; position_peak = recursive_pos; } //end prematurely if(max_iterations >= iteration){ done = true; peak_found = false; position_peak = null; } iteration++; } //build response let obj_peak = { found:peak_found , value_of_peak: array[position_peak] , position_peak:position_peak }; return obj_peak; } } // end of class /* var graph = new Graph(); //*/ module.exports = Graph;