regarding getting points in a line(code required);

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

regarding getting points in a line(code required);

Postby uvrpavan » Fri May 18, 2007 4:18 am

i have two points from one end of the screen to another end.
i want to get the points on the canvas between these two points to move an object. then what should i need to do?
send some code for this
please
uvrpavan
 
Posts: 19
Joined: Sun Feb 11, 2007 8:52 am

Postby DevelopmentTeam » Fri May 18, 2007 5:46 am

Code: Select all
public void lineBresenham(int x0, int y0, int x1, int y1, Color color)
    {
        int dy = y1 - y0;
        int dx = x1 - x0;
        int stepx, stepy;

        if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
        if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
        dy <<= 1;                                                  // dy is now 2*dy
        dx <<= 1;                                                  // dx is now 2*dx

        // Draw pixel at X0, Y0

        if (dx > dy) {
            int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
            while (x0 != x1) {
                if (fraction >= 0) {
                    y0 += stepy;
                    fraction -= dx;                                // same as fraction -= 2*dx
                }
                x0 += stepx;
                fraction += dy;                                    // same as fraction -= 2*dy
        // Draw pixel at X0, Y0
            }
        } else {
            int fraction = dx - (dy >> 1);
            while (y0 != y1) {
                if (fraction >= 0) {
                    x0 += stepx;
                    fraction -= dy;
                }
                y0 += stepy;
                fraction += dx;
        // Draw pixel at X0, Y0
            }
        }
    }


In this function I have replaced the code lines where you have to draw the pixel. If you want to move an image in the line path, you need to draw the image at X0, Y0 at places where I have put //draw pixel at X0, Y0.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron