Results 1 to 8 of 8
Thread: Points in a line
- 07-03-2012, 03:02 AM #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 05:23 AM.
- 07-03-2012, 03:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Points in a line
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".a way of finding all the points of any line from point A to point B?
- 07-03-2012, 03:30 AM #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.
- 07-03-2012, 04:29 AM #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?
- 07-03-2012, 04:32 AM #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.
- 07-03-2012, 05:07 AM #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.
and here is the output: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+")"); } }
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...
- 07-03-2012, 05:09 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Points in a line
the site is not loading btw
- 07-03-2012, 04:29 PM #8
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
[jFreeChart] Converting screen-points to points, which are relative to the axis
By fyaxic in forum AWT / SwingReplies: 1Last Post: 08-11-2011, 10:46 AM -
How to move the points on a predrawn line?
By pizzadude223 in forum Java AppletsReplies: 1Last Post: 07-20-2010, 12:56 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08-24-2008, 12:30 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote.gif)


Bookmarks