add osmdroid overlays + bonuspack

This commit is contained in:
Hannes Janetzek
2012-10-27 13:35:51 +02:00
parent 65a6f91f3c
commit ab5962d56c
114 changed files with 9562 additions and 1636 deletions

View File

@@ -20,7 +20,8 @@ package org.oscim.utils;
*/
public final class GeometryUtils {
/**
* Calculates the center of the minimum bounding rectangle for the given coordinates.
* Calculates the center of the minimum bounding rectangle for the given
* coordinates.
*
* @param coordinates
* the coordinates for which calculation should be done.
@@ -55,14 +56,16 @@ public final class GeometryUtils {
* @return true if the given way is closed, false otherwise.
*/
static boolean isClosedWay(float[] way) {
return Float.compare(way[0], way[way.length - 2]) == 0 && Float.compare(way[1], way[way.length - 1]) == 0;
return Float.compare(way[0], way[way.length - 2]) == 0
&& Float.compare(way[1], way[way.length - 1]) == 0;
}
private GeometryUtils() {
throw new IllegalStateException();
}
static boolean linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
static boolean linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3,
double x4, double y4) {
// Return false if either of the lines have zero length
if (x1 == x2 && y1 == y2 || x3 == x4 && y3 == y4) {
return false;
@@ -111,9 +114,11 @@ public final class GeometryUtils {
// since p1-p2 is parallel with p3-p4
if (collinearityTestForP3 == 0) {
// The lines are collinear. Now check if they overlap.
if (x1 >= x3 && x1 <= x4 || x1 <= x3 && x1 >= x4 || x2 >= x3 && x2 <= x4 || x2 <= x3 && x2 >= x4
if (x1 >= x3 && x1 <= x4 || x1 <= x3 && x1 >= x4 || x2 >= x3 && x2 <= x4
|| x2 <= x3 && x2 >= x4
|| x3 >= x1 && x3 <= x2 || x3 <= x1 && x3 >= x2) {
if (y1 >= y3 && y1 <= y4 || y1 <= y3 && y1 >= y4 || y2 >= y3 && y2 <= y4 || y2 <= y3 && y2 >= y4
if (y1 >= y3 && y1 <= y4 || y1 <= y3 && y1 >= y4 || y2 >= y3 && y2 <= y4
|| y2 <= y3 && y2 >= y4
|| y3 >= y1 && y3 <= y2 || y3 <= y1 && y3 >= y2) {
return true;
}
@@ -124,7 +129,8 @@ public final class GeometryUtils {
return true;
}
static boolean doesIntersect(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1,
static boolean doesIntersect(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1,
double l2y1,
double l2x2, double l2y2) {
double denom = ((l2y2 - l2y1) * (l1x2 - l1x1)) - ((l2x2 - l2x1) * (l1y2 - l1y1));
@@ -157,7 +163,8 @@ public final class GeometryUtils {
* ...
* @return ...
*/
public static boolean lineIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
public static boolean lineIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4,
int y4) {
double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (denom == 0.0) { // Lines are parallel.
return false;
@@ -171,108 +178,4 @@ public final class GeometryUtils {
return false;
}
// private static final int OUT_LEFT = 1;
// private static final int OUT_TOP = 2;
// private static final int OUT_RIGHT = 4;
// private static final int OUT_BOTTOM = 8;
//
//
// private static int outcode(double x, double y) {
// /*
// * Note on casts to double below. If the arithmetic of
// * x+w or y+h is done in float, then some bits may be
// * lost if the binary exponents of x/y and w/h are not
// * similar. By converting to double before the addition
// * we force the addition to be carried out in double to
// * avoid rounding error in the comparison.
// *
// * See bug 4320890 for problems that this inaccuracy causes.
// */
// int out = 0;
// if (this.width <= 0) {
// out |= OUT_LEFT | OUT_RIGHT;
// } else if (x < this.x) {
// out |= OUT_LEFT;
// } else if (x > this.x + (double) this.width) {
// out |= OUT_RIGHT;
// }
// if (this.height <= 0) {
// out |= OUT_TOP | OUT_BOTTOM;
// } else if (y < this.y) {
// out |= OUT_TOP;
// } else if (y > this.y + (double) this.height) {
// out |= OUT_BOTTOM;
// }
// return out;
// }
// from http://shamimkhaliq.50megs.com/Java/lineclipper.htm
// private static int outCodes(Point P)
// {
// int Code = 0;
//
// if(P.y > yTop) Code += 1; /* code for above */
// else if(P.y < yBottom) Code += 2; /* code for below */
//
// if(P.x > xRight) Code += 4; /* code for right */
// else if(P.x < xLeft) Code += 8; /* code for left */
//
// return Code;
// }
//
// private static boolean rejectCheck(int outCode1, int outCode2)
// {
// if ((outCode1 & outCode2) != 0 ) return true;
// return(false);
// }
//
//
// private static boolean acceptCheck(int outCode1, int outCode2)
// {
// if ( (outCode1 == 0) && (outCode2 == 0) ) return(true);
// return(false);
// }
//
// static boolean CohenSutherland2DClipper(Point P0,Point P1)
// {
// int outCode0,outCode1;
// while(true)
// {
// outCode0 = outCodes(P0);
// outCode1 = outCodes(P1);
// if( rejectCheck(outCode0,outCode1) ) return(false);
// if( acceptCheck(outCode0,outCode1) ) return(true);
// if(outCode0 == 0)
// {
// double tempCoord; int tempCode;
// tempCoord = P0.x; P0.x= P1.x; P1.x = tempCoord;
// tempCoord = P0.y; P0.y= P1.y; P1.y = tempCoord;
// tempCode = outCode0; outCode0 = outCode1; outCode1 = tempCode;
// }
// if( (outCode0 & 1) != 0 )
// {
// P0.x += (P1.x - P0.x)*(yTop - P0.y)/(P1.y - P0.y);
// P0.y = yTop;
// }
// else
// if( (outCode0 & 2) != 0 )
// {
// P0.x += (P1.x - P0.x)*(yBottom - P0.y)/(P1.y - P0.y);
// P0.y = yBottom;
// }
// else
// if( (outCode0 & 4) != 0 )
// {
// P0.y += (P1.y - P0.y)*(xRight - P0.x)/(P1.x - P0.x);
// P0.x = xRight;
// }
// else
// if( (outCode0 & 8) != 0 )
// {
// P0.y += (P1.y - P0.y)*(xLeft - P0.x)/(P1.x - P0.x);
// P0.x = xLeft;
// }
// }
// }
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2012 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.utils;
import android.graphics.Point;
// from http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
public class LineClipper {
private static final int INSIDE = 0; // 0000
private static int LEFT = 1; // 0001
private static int RIGHT = 2; // 0010
private static int BOTTOM = 4; // 0100
private static int TOP = 8; // 1000
// Compute the bit code for a point (x, y) using the clip rectangle
// bounded diagonally by (xmin, ymin), and (xmax, ymax)
private int xmin, xmax, ymin, ymax;
public boolean clip(Point p1, Point p2, int minx, int miny, int maxx, int maxy) {
this.xmin = minx;
this.ymin = miny;
this.xmax = maxx;
this.ymax = maxy;
return cohenSutherlandLineClip(p1.x, p1.y, p2.x, p2.y);
}
private int outCode(int x, int y) {
int code;
code = INSIDE; // initialised as being inside of clip window
if (x < xmin) // to the left of clip window
code |= LEFT;
else if (x > xmax) // to the right of clip window
code |= RIGHT;
if (y < ymin) // below the clip window
code |= BOTTOM;
else if (y > ymax) // above the clip window
code |= TOP;
return code;
}
// CohenSutherland clipping algorithm clips a line from
// P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with
// diagonal from (xmin, ymin) to (xmax, ymax).
private boolean cohenSutherlandLineClip(int x0, int y0, int x1, int y1) {
// compute outcodes for P0, P1, and whatever point lies outside the clip rectangle
int outcode0 = outCode(x0, y0);
int outcode1 = outCode(x1, y1);
boolean accept = false;
while (true) {
if ((outcode0 | outcode1) == 0) { // Bitwise OR is 0. Trivially accept and get out of loop
accept = true;
break;
} else if ((outcode0 & outcode1) != 0) { // Bitwise AND is not 0. Trivially reject and get out of loop
break;
} else {
// failed both tests, so calculate the line segment to clip
// from an outside point to an intersection with clip edge
int x = 0;
int y = 0;
// At least one endpoint is outside the clip rectangle; pick it.
int outcodeOut = (outcode0 == 0) ? outcode1 : outcode0;
// Now find the intersection point;
// use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0)
if ((outcodeOut & TOP) != 0) { // point is above the clip rectangle
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
} else if ((outcodeOut & BOTTOM) != 0) { // point is below the clip rectangle
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
} else if ((outcodeOut & RIGHT) != 0) { // point is to the right of clip rectangle
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
} else if ((outcodeOut & LEFT) != 0) { // point is to the left of clip rectangle
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
// Now we move outside point to intersection point to clip
// and get ready for next pass.
if (outcodeOut == outcode0) {
x0 = x;
y0 = y;
outcode0 = outCode(x0, y0);
} else {
x1 = x;
y1 = y;
outcode1 = outCode(x1, y1);
}
}
}
// TODO do sth with the result x0...
return accept;
}
}