views:

35

answers:

1

Hi there everyone. I'm trying to do some advanced features with android maps and to do that I need to do some operations on vectors. Now - I read the answer from this and it gave me some hints and tips. However, there is a part which I don't understand. Please allow me to quote this:

Now that we have the ray with its start and end coordinates, the problem shifts from "is the point within the polygon" to "how often intersects the ray a polygon side". Therefor we can't just work with the polygon points as before (for the bounding box), now we need the actual sides. A side is always defined by two points.

side 1: (X1/Y1)-(X2/Y2) side 2: (X2/Y2)-(X3/Y3) side 3: (X3/Y3)-(X4/Y4)

So my understanding is that every side of the triangle is actually a vector. But how is it possible to substract 2 points? Let's say I got a triangle with 3 vertices: A(1,1) , B(2,2), C (1,3). So according to that, I have to do, for example, (1,1)-(2,2) in order to calculate one of the sides. The question is how to do it programatically in java/android? Below I'm attaching the code which I already developed:

    /** Creating the containers for screen 
     *  coordinates taken from geoPoints
     */

    Point point1_screen = new Point();
    Point point2_screen = new Point();
    Point point3_screen = new Point();

    /* Project them from the map to screen */
    mapView.getProjection().toPixels(point1, point1_screen);
    mapView.getProjection().toPixels(point2, point2_screen);
mapView.getProjection().toPixels(point3, point3_screen);

    int xA = point1_screen.x;
    int yA = point1_screen.y;

    int xB = point2_screen.x;
    int yB = point2_screen.y;

    int xC = point3_screen.x;
    int yC = point3_screen.y;

    int[] xPointsArray = new int[3];
    int[] yPointsArray = new int[3];

    xPointsArray[0] = xA;
    xPointsArray[1] = xB;
    xPointsArray[2] = xC;

    yPointsArray[0] = yA;
    yPointsArray[1] = yB;
    yPointsArray[2] = yC;

    Arrays.sort(xPointsArray);

    int xMin = xPointsArray[0];
    int yMin = yPointsArray[0];

    int xMax = xPointsArray[xPointsArray.length-1];
    int yMax = xPointsArray[xPointsArray.length-1]; 

    int e = (xMax - xMin) / 100;                                    // for ray calcultions

    int width = mapView.getWidth();
    int height = mapView.getHeight();

    if(pPoint.x < xMin || pPoint.x > xMax || pPoint.y > yMin || pPoint.y < yMax)
    {

        DisplayInfoMessage(pPoint.x + " < " + xMin + " AND "  + pPoint.x + " > " + xMax + " || " + pPoint.y + " < " + yMin + " AND "  + pPoint.y + " > " + yMax );
    //  DisplayInfoMessage("Minimum is: "+  yPointsArray[0] + " and the maximum is: "+ yPointsArray[xPointsArray.length-1]);

    }
    else
    {
         GeoPoint start_point = new GeoPoint(xMin - e, pPoint.y);
         Point start_point_container = new Point();  
         mapView.getProjection().toPixels(start_point, start_point_container);

            int a, b, c, tx, ty;
            int d1, d2, hd;
            int ix, iy;
            float r;
            // calculating vector for 1st line

            tx = xB - xA;
            ty = yB - yA;

            // equation for 1st line

            a = ty;
            b = tx;
            c = xA*a - yA*b;

            // get distances from line for line 2

            d1 = a*xB + b*yB + c;
            d2 = a*pPoint.x + b*pPoint.y + c;



            DisplayInfoMessage("You clicked inside the triangle!" + "TRIANGLE POINTS: A("+xA+","+yA+") B("+xB+","+yB+") C("+xC+","+yC+")");
    }

The pPoint hold the coordinates of the point which user clicked. I hope that I explained my problem well enough. Can someone give me some help with that? Appreciated!

+1  A: 

I'm not an Android developer, but I see that android.graphics.drawable.shapes.Shape lacks the contains() method found in java.awt.Shape. It appears you'll have to develop your own test, as suggested in the article you cited. In addition, you might want to look at crossing/winding number algorithms.

But how is it possible to subtract 2 points?

Subtraction of vectors is well defined, and easily implemented in Java. Given two points as vectors, the components of the difference represent the tangent (slope) of a line connecting the points. The example in the article implements this in the following lines:

//get tangent vector for line 1
tx = v1x2 - v1x1;
ty = v1y2 - v1y1;

The foundation for the approach shown is discussed further in Line and Segment Intersections.

trashgod
Thanks for the hint trashgod - that helped.
Pavel