This site requires JavaScript, please enable it in your browser!
Greenfoot back
delijamiki
delijamiki wrote ...

2018/3/14

Problem with colinear points

delijamiki delijamiki

2018/3/14

#
Hello, i have a little problem with this peace of code which should calculate if there is or there isnt colinear points in array called "ostring", which is object of class "point",with x and y coordinates "ocounter" is a varibale that represent number of elements in array. I must mention that on every mouse click , coordinates of the mouse are stored in the array The problem i have is that when "ocounter" reaches 4, it throws me a NullPointerException and AritmeticException divide by zero, heres the code
if(ocounter>2)
                    {
                    for(i=0;i<ocounter-2;i++)
                    {
                         
                        int y1=ostring[i].getY();
                        int x1=ostring[i].getX();
                        for(j=i+1;j<ocounter-1;j++)
                        {
                             int y2=ostring[j].getY();
                             int x2=ostring[j].getX();
                                
                            for(z=j+1;z<ocounter;z++)
                            {
                                
                                
                               
                                int y3=ostring[z].getY();
                                int x3=ostring[z].getX();
                                 if((y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2))
                                {
                                   
                                    Greenfoot.stop();
                                }
                                
                            }
                            
                        }
                    }
                }
Yehuda Yehuda

2018/3/14

#
Can you paste the full class code, from before the first import to the last place the caret can go, and the terminal window output with the errors. (It's difficult to find the problem with just an if-statement, and so many things that have to do with the rest of the class inside.)
delijamiki delijamiki

2018/3/15

#
I have fixed the problem, thanks for help. The problem was in the position of this block in act method.
You need to login to post a reply.