# Thread: Points in a line

1. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

## Points in a line

Hello fellow programmers,

I am trying to create a game where you can put down elements like dirt and water (that's all I have yet I started yesterday) and it comes down as dots and they fall on each other and react differently.

much like this [Moderator edit: irrelevant link removed]

you use the mouse to place elements but it separates the dots when you make a large line of them. I know if I make a bunch of dots in between the two points I can make a line that is not separated. heres a picture:

do you guys know a way of finding all the points of any line from point A to point B? Code examples appreciated.
Last edited by DarrylBurke; 07-03-2012 at 06:23 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: Points in a line

a way of finding all the points of any line from point A to point B?
One way of approximating a continuous line with pixel values is to work across from the x-coordinate of A to the x-coordinate of B. For each value of x figure out the corresponding value of y so that x/y lies on the line. (The equation of the line will help) And for each x/y pair create an element of dirt. How effective this is depends a bit on the orientation of the line - near vertical ones may still appear "gappy".

3. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

## Re: Points in a line

Yes this helps, all I need is it to make it not "gappy" for the x.

4. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

## Re: Points in a line

I added this to it but It gives me strange results...

Java Code:
```			if(mouseType!=0){
int dx= mouseLast.x-mousePos.x;
int dy= mouseLast.y-mousePos.y;
if(dx>0){
for(int i=0;i<dx;i++){
makeSpeck(new Point(mouseLast.x+i,mouseLast.y+i*(dy/dx)),mouseType-1);
}
}
else if(dx<0){
for(int i=0;i>dx;i--){
makeSpeck(new Point(mouseLast.x+i,mouseLast.y+i*(dy/dx)),mouseType-1);
}
}
else
makeSpeck(new Point(mousePos.x,mousePos.y),mouseType-1);
}```

I don't know why. any suggestions?

5. Senior Member
Join Date
May 2010
Posts
436
Rep Power
4

## Re: Points in a line

It would probably help you and us if you simplified your problem to its most basic form by creating a minimal compilable and runnable example, one without any background images or function at all except for drawing the dotted line or whatever your goal is, and then posting this attempt here. This way we can all work with your workable code, it won't be so large as to completely overwhelm us, and perhaps we can find a way to get it to work. What I'm asking for is an SSCCE. Please check the link for all the details regarding this useful construct.

6. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

## Re: Points in a line

I made a simple version of what I want to make. Though it is not graphic, it still does the thing.

Java Code:
```import java.lang.Math.*;
import java.awt.*;
public class lineTest {
public static void main(String[] args){
Point p1=new Point(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
Point p2=new Point(Integer.parseInt(args[2]),Integer.parseInt(args[3]));
System.out.println("line from ("+p1.x+","+p1.y+") to ("+p2.x+","+p2.y+") has points:");
int dx= p1.x-p2.x;
int dy= p1.y-p2.y;
if(dx>0){
for(int i=0;i<=dx;i++){
System.out.println("("+(p2.x+i)+","+(p2.y+i*dy/dx)+")");
}
}
else if(dx<0){
for(int i=0;i>=dx;i--){
System.out.println("("+(p2.x+i)+","+(p2.y+i*dy/dx)+")");
}
}
else
System.out.println("("+p1.x+","+p1.y+")");
}
}```
and here is the output:

kyle@kyle-Dell-DM061:~/Documents/javaPrograms/MyStuff/dotWorld\$ java lineTest 0 0 20 3
line from (0,0) to (20,3) has points:
(20,3)
(19,3)
(18,3)
(17,3)
(16,3)
(15,3)
(14,3)
(13,2)
(12,2)
(11,2)
(10,2)
(9,2)
(8,2)
(7,2)
(6,1)
(5,1)
(4,1)
(3,1)
(2,1)
(1,1)
(0,0)
kyle@kyle-Dell-DM061:~/Documents/javaPrograms/MyStuff/dotWorld\$

the problem is it works...

7. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

8. Member
Join Date
Jul 2012
Posts
27
Rep Power
0

## Re: Points in a line

agggg... It works. simple error. I forgot that the second point was updating every time the mouse drags, not every frame. thanks everybody for putting up with me, I'm new to forums.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•