Search code examples
javaalgorithmqueuepixeldda

Cant figure this algorithm to pick image pixel by slope approach. (Any other approach is welcome)


What I'm trying to do is take 2 mouceclick input which gives me pixel coordinate x[0],y[0] and x[1],y[1]. Then I get a queue of array containing pixels coordinate of every pixel where the line joining these points would make. Don't need the line to be seen at all.

I decided to take the slope prospective such that 1 pixel change in x coordinate would change (x[1]-x[0])]/(y[1]-y[0]) in y coordinate. I keep getting arithmetic error.

Edit: Used the DDA algorithm and still getting / by zero error even if all values is pre-asigned to something non-zero.

  Queue<int[]> queue=new LinkedList<int[]>();
            int dx = Math.abs(x[1] - x[0]);
            int dy = Math.abs(y[1] - y[0]);
            int sx = (x[0] < x[1]) ? 1 : -1;
            int sy = (y[0] < y[1]) ? 1 : -1;
            int err = dx / dy;
            
            int[] tog= {x[0],y[0]};
            queue.add(tog);                 //1st pixel into queue. nothing else

            while(true) {
                if (x[0] == x[1] && y[0] == y[1]) {
                    break;
                }
                int e2 = 2 * err;

                if (e2 > -dy) {
                    err = err - dy;
                    x[0] = x[0] + sx;
                }
                
                if (e2 < dx) {
                    err = err + dx;
                    y[0] = y[0] + sy;
                }
                tog[0]= x[0];
                tog[1]= y[0];
                queue.add(tog);
            }
    System.out.println(queue);

Solution

  • Thanks to the comment on using DDA, the problem I got is now fixed. I have used the following code for the counting of the pixels and storing their coordinates. I put this code inside the action listener for a mouse click.

    private void counter() {//---------counter takes arguments x and y which are array that contain x1x2 and y1y2 coordinates of 1st and 2nd click
    
            int dx = (x[1] - x[0]);
            int dy = (y[1] - y[0]);//---------makes it applicable for both inclinations (if we add up there a math.abs it would work on only the positive inclination line )
            step = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
            //------counts howmany pixels are to be recorded
            float Xinc = dx / (float) step;//----slope change with respect to x axis
            float Yinc = dy / (float) step;//----slope change with respect to y axis
            
            tog= new int[step][3];
            tog[0][0]=x[0]; tog[0][1]=y[0];
            tog[0][2]= (black[0]!=0) ? 1 : 0;//------------Tertiary operator where the condition is true, then while is true
            //---------------------------------------------------------------send value of x1 and y1 to listOfCoordinates
            float xt=x[0],yt=y[0];  int i=0, j=1;
            //-------------to get all the coordinates between the 2 points1111
            System.out.println(tog[0][0]+" "+tog[0][1]+" "+tog[0][2]);
            while (j<step){
                if(i==2) i=0;
                xt += Xinc;
                yt += Yinc;
                tog[j][i] = (int)xt;//------tog is the array where you store the coordinates of each pixel that overlaps the line made if the clicked points are connected
                tog[j][i+1] = (int)yt;
                j++;
            }
    //-------print tog here to see if it has the coordinates or not for check
        }