Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By curmudgeon
  • 1 Post By mrguy

Thread: Points in a line

  1. #1
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default 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:

    Points in a line-screenshot-07022012-07-56-31-pm.png

    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.

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

    Default 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. #3
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Points in a line

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

  4. #4
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

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



    Points in a line-screenshot-07022012-09-24-13-pm.png



    I don't know why. any suggestions?

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default 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.
    DarrylBurke likes this.

  6. #6
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default 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. #7
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Points in a line

    the site is not loading btw

  8. #8
    mrguy is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

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

    Points in a line-screenshot-07032012-09-26-40-am.png
    Fubarable likes this.

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  3. How to move the points on a predrawn line?
    By pizzadude223 in forum Java Applets
    Replies: 1
    Last Post: 07-20-2010, 12:56 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Tags for this Thread

Posting Permissions

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