A BoardGame

L

Linio

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.
 
K

kg

Hi,
I don't think I understand. What should the function return if you have
something like, (3,1) and (5,2) ?
 
R

Ralph

Linio said:
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
 
R

Ralph

Linio said:
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
 
M

Matt Humphrey

Linio said:
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.

Look for Bresenham's algorithm for drawing lines. It will incrementally
give you the squares a line crosses (that is, the pixels that would be
drawn.)

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
L

Linio

Thanks guys.

I've found on my own a method by drawing a polygon, but i'll try both of
your methods for experimental purpose.

Linio

Ralph said:
Linio said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top