0

Lately I've been working on a grid line detection system, I know that there was an algorithm out there that did excactly what I wanted it to do, but I'm that kind of person who wants to make stuff themselves. ;)

So I've had some succes when checking with 1 line, but now that I use a grid of 20x20 to check the lines it doesn't work anymore.

The problem is found somewhere in the collision class I made for this system.

Somehow the numbers get out of the grid array and I don't know why

here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
  *
  * beschrijving
  *
  * @version 1.0 van 22-6-2016
  * @author 
  */

public class gridline extends JApplet {
  // Begin variabelen
  int[][] grid = new int[20][20];
  int[] light = new int[2];
  int[][] line = new int[2][2];
  // Einde variabelen

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(null);
    cp.setBounds(0, 0, 600, 600);
    // Begin componenten
    for (int a=0; a<20; a++) {
      for (int b=0; b<20; b++) {
        grid[a][b] = (int) (Math.random()*10);
      } // end of for
    } // end of for

    line[0][0] = (int) (Math.random()*20);
    line[0][1] = (int) (Math.random()*20);
    line[1][0] = (int) (Math.random()*20);
    line[1][1] = (int) (Math.random()*20);

    light[0] = (int) (Math.random()*20);
    light[1] = (int) (Math.random()*20);
    // Einde componenten

  } // end of init
  //Custom classes
  private boolean collide(int x1, int y1,int x2, int y2) {
    boolean collide = true;

    int tempx = x1 - x2;
    int tempy = y1 - y2;
    int sx = 0;
    int sy = 0;
    int x = 0;
    int y = 0;
    if (tempx == 0) {
      tempx = 1;
      sx = 1;
    } // end of if
    if (tempy == 0) {
      tempy = 1;
      sy = 1;
    } // end of if
    if (sx == 0) {
      x = tempx + tempx/Math.abs(tempx);
    } // end of if
    else {
      x = tempx;
    } // end of if-else
    if (sy == 0) {
      y = tempy + tempy/Math.abs(tempy);
    } // end of if
    else {
      y = tempy;
    } // end of if-else
    int absx = Math.abs(x);
    int absy = Math.abs(y);
    int nex = x/absx;
    int ney = y/absy;
    int off = 0;
    float count = 0;
    float step = 0;
    if (absx != absy) {
      if (absx == Math.min(absx,absy)) {
        step = (float) absx/absy;
        calc1: for (int a=0; a<absy; a++) {
          count += step;
          if (count > 1 && x1+off != x2) {
            count -= 1;
            off += nex;
          } // end of if
          if (grid[x1+off][y1+a*ney] == 9) {
            collide = false;
            break calc1;
          } // end of if
        } // end of for
      } // end of if
      else{
        step = (float) absy/absx;
        calc2: for (int a=0; a<absx; a++) {
          count += step;
          if (count > 1 && y1+off != y2) {
            count -= 1;
            off += ney;
          } // end of if
          if (grid[x1+a*nex][y1+off] == 9) {
            collide = false;
            break calc2;
          } // end of if
        } // end of for
      }
    } // end of if
    else {
      calc3: for (int a=0; a<absx; a++) {
        if (grid[x1+a*nex][y1+a*ney] == 9) {
          collide = false;
          break calc3;
        } // end of if
      } // end of for
    } // end of if-else

    return collide;
  }

  private int length(int x1, int y1, int x2, int y2) {
    double distance = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
    return (int) distance;
  }

  // Begin eventmethoden
  public void paint (Graphics g){
    boolean draw = true;
    Color col;
    for (int a=0; a<20; a++) {
      for (int b=0; b<20; b++) {
        draw = collide(a,b,light[0],light[1]);

        if (draw) {
          int len = Math.max(255-length(a*30+15,b*30+15,light[0]*30+15,light[1]*30+15),0);
          col = new Color(len,len,len);
          g.setColor(col);
          g.fillRect(a*30,b*30,30,30);
        } // end of if
        else{
          col = new Color(0,0,0);
          g.setColor(col);
          g.fillRect(a*30,b*30,30,30);
        }
      } // end of for
    } // end of for
  }
    // Einde eventmethoden

} // end of class gridline

I'll understand it if nobody wants to look through a code as big as this, but it could be helpful for your own projects and I'm completely okay with it if you copy and paste my code for your projects.

Many thanks in advace.

Lolslayer
  • 63
  • 8
  • 1) I get a `ArrayIndexOutOfBoundsException` at line 118 (116 in the original source with no package declaration. 2) *"it could be helpful for your own projects"* *"I know that there was an algorithm out there that did excactly what I wanted it to do, but I'm that kind of person who wants to make stuff"* (polite cough) I prefer using inbuilt methods, and already [have code that works perfectly](http://stackoverflow.com/a/14575043/418556). *"I'll understand it if nobody wants to look through a code as big as this"* But around 150 LOC is not so long. Some won't look at it, others will. – Andrew Thompson Jun 23 '16 at 10:20
  • I'm surprised that you have not adopted this [approach](http://stackoverflow.com/a/37958844/230513). – trashgod Jun 23 '16 at 10:48
  • Yeah,I wasn't very tactful in this question, thanks for the responds – Lolslayer Jun 23 '16 at 18:10
  • Tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jun 24 '16 at 05:53
  • 1
    @AndrewThompson thank you :) – Lolslayer Jun 27 '16 at 19:42

0 Answers0