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

Jan-Benedict Glaw jbglaw at lug-owl.de
Sun May 14 13:53:09 BST 2006


On Sun, 2006-05-14 13:35:15 +0100, Tom Carden <tom at tom-carden.co.uk> wrote:
> On 14/05/06, Jan-Benedict Glaw <jbglaw at lug-owl.de> wrote:
> >(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?
> 
> Arrays have built in length in java - factor.length is what you're
> looking for :)

Uh, ah, thanks.  Thanks.

> >Additionally, kilometersPerPixel() is
> >only used in a meter context, so maybe we'd rename it accordingly and
> >drop the factor of 1000 everywhere?)
> 
> Sure. Or just make a metersPerPixel function too if you need it.

That sounds like even more bloat:) Here's an updated patch:

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,38 @@
 	}
 
 	private void draw_scale_bar() {
+		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 < factor.length; i++) {
+			double rest;
+			double log_value;
+
+			log_value = Math.log10(tiles.metersPerPixel()
+					       * 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)
+				    / tiles.metersPerPixel());
+
 		fill(0);
-    strokeWeight(10);
-    textSize(10);
+		strokeWeight(2);
+		textSize(10);
 		pushMatrix();
 		/* Horizonthal bar */
 		line(getWidth() - bar_length - dist_right, getHeight() - dist_bottom,
@@ -414,10 +440,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();
 	}
 
@@ -651,7 +679,7 @@
 
 	public void recalcStrokeWeight() {
 		// 10m roads, but min 2px width
-		strokeWeight = max(0.010f / tiles.kilometersPerPixel(), 2.0f); 
+		strokeWeight = max(10.0f / tiles.metersPerPixel(), 2.0f);
 	}
 
 	public void mouseMoved() {
Index: src/org/openstreetmap/client/Tile.java
===================================================================
--- src/org/openstreetmap/client/Tile.java	(revision 1024)
+++ src/org/openstreetmap/client/Tile.java	(working copy)
@@ -313,8 +313,10 @@
 		}
 	}
 
-	public float kilometersPerPixel() {
-		return (float)((40008.0 / 360.0) * 45.0 * (float)Math.pow(2.0, -6 - (double)zoom));
+	public float metersPerPixel() {
+		return (float)((40008.0 / 360.0) * 45.0
+			       * (float)Math.pow(2.0, -6 - (double)zoom))
+			       * 1000.0f;
 	}
 
 	public synchronized ImBundle getEle() {

-- 
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/3eb9f5ae/attachment.pgp>


More information about the dev mailing list