Skip to content

Instantly share code, notes, and snippets.

@dejaime

dejaime/bar.cpp Secret

Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejaime/836813eebb5d262bfa1f to your computer and use it in GitHub Desktop.
Save dejaime/836813eebb5d262bfa1f to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////////////
// Released under Public Domain.
/////////////////////////////////////////////////////////////////////////
#include <vector>
#include <iostream>
#define VECTOR_MIN -10
#define VECTOR_MAX 500
#define NUM_OF_VALUES 6
using std::cout;
using std::endl;
typedef std::vector<int> myVector;
//scroll bar abstraction
struct bar {
//Data for the scroll bar abstraction
int min, max, currPosition;
//Constructor
bar (int p_min, int p_max)
:min(p_min), max(p_max), currPosition(p_min){}
//move the scroll bar
//to the right
void move_right () {
if (currPosition<max) ++currPosition;
}
//to the left
void move_left () {
if (currPosition>min) --currPosition;
}
};
void print_values(myVector *v, bar b, int printed_values){
//Total Size of the bar
int bSize = (b.max - b.min);
//Size of the Vector
int vSize = (VECTOR_MAX - VECTOR_MIN);
cout<<"\n\nCurrent Bar Position: "<<b.currPosition<<"\n";
//Relative position in the bar.
// half the bar is 0.5
// one quarter from the left is 0.25
//Get the current position from the left, from 0 to Size
// to abstract MIN and MAX
float bRelativePosition =( b.currPosition - b.min );
//Divide by the size to give us the relative position
bRelativePosition /= (float)bSize;
//Calculate the first value to be shown, using the bar position
// and the total size of the vector.
int firstValue = (int)( (float)vSize * bRelativePosition );
//Check whether the value need to be clipped in order to
// show all the wanted values.
if ( (VECTOR_MAX - firstValue) < printed_values ) {
firstValue = VECTOR_MAX - printed_values;
}
cout<<"Values, ranging from "<<firstValue<<
" to "<<firstValue + printed_values-1<<" "<<" [position, value]\n";
for (int i = firstValue; i < firstValue + printed_values; ++i) {
cout<<"["<<i<<", "<<(*v)[i]<<"] ";
}
cout<<endl;
}
int main () {
myVector v;
//Create a bar with arbitrary MAX and MIN
bar b(-30, 200);
//Reserve the necessary memory
v.reserve(VECTOR_MAX-VECTOR_MIN);
//Go through the vector assigning values
for (int i = 0; i < VECTOR_MAX-VECTOR_MIN; ++i){
//Assign double the position value to each entry (arbitrary).
v[i] = i*2;
}
int c; //Just to control our "console app bar"
do {
c = getchar(); //Update c (need to hit Enter)
if (c == '+') b.move_right(); //numpad + moves it to the right
else if (c == '-') b.move_left(); //numpad - moves it to the left
print_values(&v, b, NUM_OF_VALUES); //print the numbers
} while (c!='x'); // x char exits the application
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment