Archive

Archive for May, 2007

Drawing filled polygons in J2ME

May 11th, 2007 No comments

Check this sourceforge project for a really useful class.

http://sourceforge.net/projects/jmicropolygon/

Works really well on most of the phones we have tried it one. Once you have this class you can then do filled and stroked thick lines in J2ME.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Categories: J2ME, Java, Useful stuff Tags:

Find the point at which 2 lines intersect

May 11th, 2007 No comments

Given two lines ( defined as 2 x,y coordinates in an array ), this will return the x,y position at which the two lines intersect. If they do not intersect, it will return null.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public Coordinate intersects(Coordinate[] coA,Coordinate[] coB ) {
 
        double x11 = coA[0].getX();
        double y11 = coA[0].getY();
        double x12 = coA[1].getX();
        double y12 = coA[1].getY();
        double x21 = coB[0].getX();
        double y21 = coB[0].getY();
        double x22 = coB[1].getX();
        double y22 = coB[1].getY();
 
        double dx1 = x12 - x11;
        double dy1 = y12 - y11;
        double dx2 = x22 - x21;
        double dy2 = y22 - y21;
        double det = (dx2*dy1-dy2*dx1);
 
        Coordinate coReturn = null;
 
        if (det != 0.0) {
            double mu = ((x11 - x21)*dy1 - (y11 - y21)*dx1)/det;
            if (mu >= 0.0  &&  mu <= 1.0) {
                coReturn = new Coordinate();
                coReturn.setX((int)(x21 + mu*dx2 + 0.5));
                coReturn.setY((int)(y21 + mu*dy2 + 0.5));
            }
        }    
        return coReturn;
}
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Categories: Java, Useful stuff Tags:

Detect if point is in a rectangle / polygon

May 11th, 2007 No comments

This checks to see if a point x,y is within a rectangle or polygon defined by an array of coordinates co. The coordinate class is based on the Point class that comes with Java – just a simple x and y. This will assume all the points are contiguous. It was coded for use in J2ME, but depending on what version you are using you may have to replace the use of doubles for something MIDP1 can handle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
boolean pointInRect(double x,double y,Coordinate [] co){
    int j=(co.length-1);
    boolean inFlag = false;
    for(int i=0;i<co.length;i++){
        if (((((double)co[i].getY() <= y) && 
           (y < (double)co[j].getY())) ||
           (((double)co[j].getY() <= y) && 
           (y < (double)co[i].getY()))) &&
           (x < ((double)co[j].getX() - (double)co[i].getX()) * 
           (y - (double)co[i].getY()) / ((double)co[j].getY() -
           (double)co[i].getY()) + (double)co[i].getX()))
                inFlag = !inFlag;
        j = i;
    }
    return inFlag;
}

This is based off a piece of VB code I found somewhere on a Math forum ( where people exist that are brainier than me )

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Categories: Java, Useful stuff Tags: