Skip to content

Instantly share code, notes, and snippets.

@codewzrd
Created January 30, 2024 00:01
Show Gist options
  • Save codewzrd/75a28c7c0da16099df1475e00d41ef8e to your computer and use it in GitHub Desktop.
Save codewzrd/75a28c7c0da16099df1475e00d41ef8e to your computer and use it in GitHub Desktop.
#include <map>
#include <vector>
#include "string.h"
#include <stdio.h>
#include <iostream>
using namespace std;
double findLowerKey(const std::map<double, vector<double>>& p_map, double p_key)
{
auto lower = p_map.lower_bound(p_key);
// Check which one is closest.
auto previous = std::prev(lower);
if ((p_key - previous->first) < (lower->first - p_key))
return previous->first;
return lower->first;
}
// vector<double> Periodic(std::map<double, vector<double>> &p_map, double p_key)
// {
// printf("%d\n", p_map.size());
// // try
// // {
// // if (p_map.count(p_key) > 0)
// // return p_map.at(p_key);
// // else if (p_key < p_map.begin()->first)
// // return p_map.begin()->second;
// // else if (p_key > p_map.end()->first)
// // return p_map.end()->second;
// // }
// // catch (...)
// // {
// // printf("code doesnt run \n");
// // }
// printf("Reached finding keys \n");
// auto lowerKey = findLowerKey(p_map, p_key);
// auto upperKey = p_map.upper_bound(p_key)->first;
// vector<double> actualVector;
// for (int i = 0; i < p_map[lowerKey].size(); i++)
// {
// auto result = PointSlope(p_key, lowerKey, p_map[lowerKey][i], upperKey, p_map[upperKey][i]);
// actualVector.push_back(result);
// for (int i = 0; i < actualVector.size(); i++)
// {
// cout << actualVector[i] << endl;
// }
// // m_logger->LogDoubles(actualVector);
// }
// return actualVector;
// }
/**
* @brief Takes in two coordinates and an x value in between them. It will return the y value that corresponds to that x value based on the slope between the points.
*
* @param xActual the key value that was orginally passed in
* @param x1 part of solving for slope
* @param y1 part of solving for slope
* @param x2 part of solving for slope
* @param y2 part of solving for slope
* @return yActual - interpolated values from mapped data
*/
double PointSlope(double xActual, double x1, double y1, double x2, double y2)
{
double interpolatorSlope = (y2 - y1) / (x2 - x1);
double yActual = interpolatorSlope * (xActual - x1) + y1;
return yActual;
}
vector<double> Periodic(std::map<double, vector<double>> &p_map, double p_key)
{
printf("%d\n", p_map.size());
auto lowerKey = findLowerKey(p_map, p_key);
auto upperKey = p_map.upper_bound(p_key)->first;
vector<double> actualVector;
for (int i = 0; i < p_map[lowerKey].size(); i++)
{
auto result = PointSlope(p_key, lowerKey, p_map[lowerKey][i], upperKey, p_map[upperKey][i]);
actualVector.push_back(result);
// Break out of the loop once actualVector has reached a size of 3
if (actualVector.size() == 3)
{
break;
}
}
// Print out the actualVector
for (int i = 0; i < actualVector.size(); i++)
{
cout << actualVector[i] << endl;
}
return actualVector;
}
// This is the main function
int main()
{
std::map<double, vector<double>> m_map = {{5, {1.5, 2.5, 3.5}}, {10, {4.5, 5.5, 6.5}}, {15, {7.5, 8.5, 9.5}}};
double m_key = 7.3;
// Call the Periodic function with m_map and m_key as parameters
Periodic(m_map, m_key);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment