Solved

Plot Axis Algorithm

Posted on 1997-03-04
3
1,102 Views
Last Modified: 2012-05-04
I am looking for a "good" algorithm that given a
arbitrary data range, will return a "good" data
range and "good" tick intervals?
0
Comment
Question by:rocco
3 Comments
 
LVL 3

Expert Comment

by:pc012197
ID: 1249527
Specify "good", "good" and "good".

0
 
LVL 2

Accepted Solution

by:
Slarti earned 100 total points
ID: 1249528
If I understand correctly, you want an algorithm that takes a range (min-max) and specifies a slightly larger range which has round numbers in the edges. Here's an algorithm which is not terribly efficient, but it works and it's relatively simple.

1. Take the base-10 log of (max-min). Truncate it to the nearest integer. Call this integer n.
2. Calculate 10^n. Do an integer division (max-min)/10^n. The result is the most significant decimal digit of max-min. Call it msd.
3. If you want the range to be up to the next most significant digit, then the range you should plot is (msd+1)*10^n. For instance, if max=4610 and min=4233, then you will get a range of 400, which is just slightly bigger than max-min. Call this range r.
4. Calculate 10^((int)log10(r)). Call this number t. It is the same as r but with a 1 as the first digit. In the example it will be 100.
5. Calculate an integer division of min/t and then multiply the result by t. This will give you a "good" value for the minimum. It will be equal in this case to 4200. Call this value xmin.
6. Calculate an integer division of max/t and then multiply the result by t, and add t to this value. Call the final result xmax. This will be a "good" maximum value. In the example it will be 4700.
7. Calculate r+t and store the result in r. This is the actual range of the graph.
8. We now have everything except the tick marks. xmin is the minimum you should plot, xmax is the maximum you should plot. r is equal to xmax-xmin.
9. The tick marks depend on how many tick marks you want to plot. Calculate r/t. This will give you the minimum number of ticks you should place if you want them all on round numbers. It will be between 1 and 9. Call this number tck. Multiply it by some factor so that you get as many tick marks as you want. For instance, if you want the number of tick marks to be between tckmin and tckmax, you could use the following algorithm:
for(atck=tck; atck<=tckmin; atck+=tck);
This will place a value slightly bigger than tckmin in atck. atck will contain the number of ticks to draw, which should then be drawn evenly spaced throughout the graph.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1249529
Is this "good"?

#define ticks 6
interval(int min, int max){
          int i,n,t;
          n = max-min;
          t = n/ticks;
          i = 1;
          while( i*10<t ){ i *= 10; }
          if( i*5 < t ){ i *= 5; }
          if( i*2 < t ){ i *= 2; }
          for( t=min-(min%i+i)%i; t < max; t+= i ){
                    printf("%d ",t);
          }
                printf("%d ",t);
}

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now