Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: A BoardGame

12 views
Skip to first unread message

Ralph

unread,
Mar 24, 2005, 2:33:15 PM3/24/05
to

"Linio" <Li...@seventeen.com> wrote in message
news:4242e4bf$0$1356$636a...@news.free.fr...
> Hello i know it may sounds stupid, but i'm currently working on a
> boardgame
> on a grid of 20*20 squares and i'd like to write a function to know which
> squares are between two square in parameter like this:
>
> public Vector checkBetween(int X1, int Y1, int X2, int Y2);
>
> and by calling it on for example checkBetween(18,6,15,9) in return i have
> (17,7) and (16,8).
>
> I tried to figure out a way of doing this by using a polygon but it
> doesn't
> work at all, when i call the function on a straight line (like (15,5) and
> (15,10) I have nothing in return when i call the function contains on my
> polygon...
>
> I hope I was clear because I ... am clearly not.
>
> If someone could help me out I would appreciate.
>
> Linio.
>
>

this might work.
basically just have to use the linear algebra formula y = mx + b
and calculate the slope

import java.util.*;
public class PointsBetween
{


public static ArrayList checkBetween(int X1, int Y1, int X2, int Y2)
{
ArrayList Alist = new ArrayList();
int [] point = new int [2];//x,y
// get the slope as a double
double slope = slope (X1,X2,Y1,Y2);
double B = getB(X1,Y1,slope);
int yprev = -100;
int yp=-999;
int xp=-999;
for (double cnt = X1;cnt<=X2;cnt+=0.10)
{
//y = mx + b
yp = (int)(slope*cnt+B);
if (yp!=yprev)
{
yprev = yp;
point[0]=(int)cnt;
point[1] = yp;
Alist.add(point);


}

}

return Alist;
}
public static double slope(double x1,double x2,double y1,double y2)
{

return (y2-y1)/(x2-x1);
}
public static double getB( double x, double y, double slope)
{
//y = mx +b
//y-mx = b
double B = y-slope*x;
return B;

}

}

hope this helps

Ralph


0 new messages