I am trying to create a text based graphing program and I am running in to some trouble. My trouble isn't so much with the programming its with one particular algorithm in creating the points.

So, here is sample of what the graph will look like with points included.
(y scale runs along horizontal, x on vertical)

p1 = 1,1
p2 = 2,2
p3 = 3,3


1.0     2.0     3.0
1.0+-------+-------+
   |o
   |
   |
2.0+       o
   |
   |
   |
3.0+               o

I have the line to the right of the | or + set up as a character array that is 16 in size. How should I go about putting the o in the correct spot? I am thinking about setting it up as some sort of percentage and then converting that number to an integer and using that integer in the array as follows: char[numconverted] = 'o', but I can't figure out how I would go about calculating the percentage. Obviously, my graph isn't that small, either so, this would have to work on any size character array.

Edited 3 Years Ago by tensity

Take your value. Check it will fit in the range (so in the example above, if your value is less than 1 or grater than 3, stop now).

So now you need to know how far along the range your value is. The maximum value along the range in the example above is 2.

To get how far along the range your value is, get how far along the range your value is (in this case, because your range begins at 1.0, that's your_value - 1.0). Then, divide what you get by the size of the range:

(your_value - 1.0) / (2.0)

or more generally

(your_value - lower_bound) / (upper_bound-lower_bound).

You'll get a number from zero to one. if you really want it to be a percentage, multiply that by a hundred.

So, now you know how far along the whole range your value is. Might be 1. Might be zero. Might be 0.4

You know that one division along the range is, in this case, one-sixteenth of the whole way i.e. one division is (1/16) of the way along the whole range.

So how many divisions along is your value? If your value is 0.4 along the whole range, how many sixteenths are in that? Easy.

0.4 / (1/16) = 6.4

You can only work in whole unit, so round it.

Let's do an example. Where should we plot 2.4?

2.4 is 1.4 units into the range (because the range starts at one). How far along is that, as a ratio? It's 1.4 / 2.0 = 0.7 along. if you want, you can call that 70%.

How many divisions along is that? Each divison is (1/16) of the way along, because there are 16 divisions, and we want to know which division is 0.7 along, so 0.7 / (1/16) = 11.2 divisions along. Round to 11, plot our value of 2.4 at the eleventh division.

Edited 3 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.