# Points in a line

• 07-03-2012, 03:02 AM
mrguy
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:

Attachment 3907

do you guys know a way of finding all the points of any line from point A to point B? Code examples appreciated.
• 07-03-2012, 03:21 AM
pbrockway2
Re: Points in a line
Quote:

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".
• 07-03-2012, 03:30 AM
mrguy
Re: Points in a line
Yes this helps, all I need is it to make it not "gappy" for the x.
• 07-03-2012, 04:29 AM
mrguy
Re: Points in a line
I added this to it but It gives me strange results...

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);                         }```

Attachment 3908

:frusty:I don't know why. any suggestions?
• 07-03-2012, 04:32 AM
curmudgeon
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.
• 07-03-2012, 05:07 AM
mrguy
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.

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...:(sweat):
• 07-03-2012, 05:09 AM
mrguy
Re: Points in a line