Skip to content

Instantly share code, notes, and snippets.

@geniiii
Forked from oatmealine/graphingCalc.cpp
Last active November 12, 2019 14:40
Show Gist options
  • Save geniiii/61d1bc0e0db48493d38a8486122a9389 to your computer and use it in GitHub Desktop.
Save geniiii/61d1bc0e0db48493d38a8486122a9389 to your computer and use it in GitHub Desktop.
simple graphing calculator in c++ - edit line 27 and 28
#include <chrono>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
constexpr uint32_t LENGTH = 120;
constexpr int HEIGHT = 28;
using Duration =
std::chrono::duration<float, std::chrono::milliseconds::period>;
template <typename T>
static float time_diff(const std::chrono::time_point<T>& a,
const std::chrono::time_point<T>& b) {
return Duration(a - b).count();
}
template <typename T>
static void get_input(T& value) {
using streamsize = std::numeric_limits<std::streamsize>;
if (std::cin.peek() != '\n') {
while (!(std::cin >> value)) {
printf("Invalid value!\n");
std::cin.clear();
std::cin.ignore(streamsize::max(), '\n');
}
}
std::cin.ignore(streamsize::max(), '\n');
}
int main() {
using std::chrono::system_clock;
uint32_t length = LENGTH;
printf("oat's graphing calculator tm!\nEnter graph length (%d): ", length);
get_input(length);
int height = HEIGHT;
printf("Enter graph height (%d): ", height);
get_input(height);
printf("Ok! Hold on...\n");
std::vector<float> vals(length);
std::vector<float> vals2(length);
// calculating
const auto& calcStart = system_clock::now();
for (uint32_t i = 1; i < length; i++) {
float x = float(i) / length;
float y = 0.3f / x; // enter formula(s) here!
float y2 = std::sqrt(x);
vals[i] = y;
vals2[i] = y2;
}
printf("Calculating took %.3fms\n\n",
time_diff(system_clock::now(), calcStart));
// rendering (?)
const auto& renderStart = system_clock::now();
for (int y = height - 1; y >= 0; y--) {
putchar('|');
for (uint32_t x = 1; x < length; x++) {
const bool firstRender = std::floor(vals[x] * height) == float(y);
const bool secondRender = std::floor(vals2[x] * height) == float(y);
if (firstRender || secondRender) {
putchar(firstRender && secondRender ? '$' : '#');
} else {
putchar(y == 0 ? '-' : ' ');
}
}
putchar('\n');
}
printf("Rendering took %.3fms",
time_diff(system_clock::now(), renderStart));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment