[OSM-dev] Re: [RFC] Beautify scale bar

Jan-Benedict Glaw jbglaw at lug-owl.de
Sun May 14 12:46:10 BST 2006


On Sun, 2006-05-14 12:37:54 +0100, SteveC <steve at asklater.com> wrote:
> * @ 13/05/06 09:45:20 PM jbglaw at lug-owl.de wrote:
> > On Sat, 2006-05-13 18:00:42 +0200, Jan-Benedict Glaw <jbglaw at lug-owl.de> wrote:
> > > On Sat, 2006-05-13 14:48:01 +0100, SteveC <steve at asklater.com> wrote:
> > >   * round
> > >   * fabs
> > >   * log
> > 
> > I found out that I need to "import" the java.lang.StrictMath class and
> > that I can use these functions (due to the fact that these are static
> > I seem to not need to get a new instance of that class) as eg.
> > StrictMath.round().
> 
> I think you want java.lang.math? isn't strct math an implementation of
> some ISO standard and very heavy?
> 
> http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html
> 
> Would you mind updating using that?

Um, no. Reading the specs (even newer ones than 1.3), StrictMath tries
to be more exact than Math. Here's an updated patch. (One question
remaining: In C, I would have a macro like

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])

to get the number of elements in the array. Is there something similar
in Java to "fix" num_factors? Additionally, kilometersPerPixel() is
only used in a meter context, so maybe we'd rename it accordingly and
drop the factor of 1000 everywhere?)


Index: src/org/openstreetmap/processing/OsmApplet.java
===================================================================
--- src/org/openstreetmap/processing/OsmApplet.java	(revision 1024)
+++ src/org/openstreetmap/processing/OsmApplet.java	(working copy)
@@ -83,6 +83,7 @@
 
 package org.openstreetmap.processing;
 
+import java.lang.Math;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -396,13 +397,39 @@
 	}
 
 	private void draw_scale_bar() {
+		int num_factors = 3;
+		double factor[] = { 1.0f, 2.5f, 5.0f, };
+		int exponent = 0;
+		int used_factor = 0;
+		double remains = 1.0f;
+		int i;
+		int min_length = getWidth() / 6;
+		int bar_length;
 		int dist_bottom = 70;
 		int dist_right = 20;
-		int bar_length = getWidth() / 5;
 		int ending_bar_length = getHeight() / 30;
+
+		/* Find the nearest factor */
+		for (i = 0; i < num_factors; i++) {
+			double rest;
+			double log_value;
+
+			log_value = Math.log10(1000.0f * tiles.kilometersPerPixel()
+					       * min_length / factor[i]);
+			if ((rest = log_value - Math.floor(log_value)) < remains) {
+				remains = rest;
+				used_factor = i;
+				exponent = (int)Math.floor(log_value);
+			}
+		}
+
+		/* Calculate the exact bar length */
+		bar_length = (int) (factor[used_factor] * Math.pow(10.0, exponent)
+				    / (1000.0f * tiles.kilometersPerPixel()));
+
 		fill(0);
-    strokeWeight(10);
-    textSize(10);
+		strokeWeight(2);
+		textSize(10);
 		pushMatrix();
 		/* Horizonthal bar */
 		line(getWidth() - bar_length - dist_right, getHeight() - dist_bottom,
@@ -414,10 +441,12 @@
 		line(getWidth() - dist_right, getHeight() - dist_bottom + ending_bar_length / 2,
 		     getWidth() - dist_right, getHeight() - dist_bottom - ending_bar_length / 2);
 
-		String meters = "" + (tiles.kilometersPerPixel() * bar_length * 1000.0f);
+		/* Print the numeric scale value */
+		String meters = "" + Math.round(factor[used_factor]
+				     * Math.pow(10.0, exponent)) + "m";
 		translate(getWidth() - dist_right - bar_length + (bar_length - textWidth(meters))/2,
 			  getHeight() - dist_bottom + 5);
-    text(meters);
+		text(meters);
 		popMatrix();
 	}
 
-- 
Jan-Benedict Glaw       jbglaw at lug-owl.de    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 für einen Freien Staat voll Freier Bürger"  | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.openstreetmap.org/pipermail/dev/attachments/20060514/285080b8/attachment.pgp>


More information about the dev mailing list