Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Getting all points in line

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting all points in line

    In my code i create line by using two points.
    Point p1 = new Point(2234, 2236);
    Point p2 = new Point(2250, 2210);

    What i need to do is get Point[] array containing all points in that line. This is math related and i have not enough math skills to solve how to do this. Can anyone help me? Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting all points in line

    What is a point? A physical pixel on a screen? When is a point on the line vs a point next to the line? Does a point's x,y only include integer/whole numbers?

    If you take a piece of graph paper and select two squares on the paper and drew a line between those two squares, which squares that your line crosses or is nest to would be "on the line"? How wide a line would you draw from one square to the other square? Only a pencil width from center to center or a line fully as wide as a square? If you were then to draw a line from center to center of all the "points on the line" would you expect the line to be straight or very wavy?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting all points in line

    y = mx + b

    If the above doesn't make sense, I'd recommend doing a web search. This formula will help you get all 'points' on the line, depending upon the definition of the Point as Norm alludes to

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting all points in line

    Here is image to explain better what im doing:



    So imagine every grid cell as object Cell(x,y);

    The red cell is the start Cell and green one is goal.

    I need to create "straight" path from start to goal. The "path" is array of Cell objects. The problem is how can i create it?
    Last edited by Mike; September 13th, 2010 at 10:36 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting all points in line

    Nice graphics. Now can you mark the squares that are "on the straight path"?

    The problem is your points are not points. They're squares.

    Are all the squares inside the lines in attached image "on the path"?
    Attached Images Attached Images
    Last edited by Norm; September 13th, 2010 at 10:59 AM.

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting all points in line

    Two example paths:

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting all points in line

    Ok, your example doesn't follow the y = mx + b definition for a line.

    In the left most path, why didn't you also put the squares above the blue ones on the path. If you rotate the image 180 deg they would be on in the same position as the blue ones?

    The path on the right makes no sense at all. There would be half a dozen ways to draw a line between those end points.

    In my attached image there would be only 2 points on the line using the y=mx+b equation. The other points would be beside it.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Getting all points in line

    EDIT: I guess this is irrelevant for you now, but it is still interesting.

    If the squares with any edges inside the "path" are included, I found something interesting that I cant quite get into a formula.

    Follow me below. When I talk about width and height, I am talking about a large square, where each of the start and end points provided are two corners of the square. So for the one above, the large square would have a width of 4 and a height of 7.

    Ok, so:
    For every "path" with a slope of 1: when you increase the width by 1, the number of squares increase by 3.
    For every "path" with a slope of 2: when you increase the width by 1, the number of squares increase by 5.
    For every "path" with a slope of 3: when you increase the width by 1, the number of squares increase by 7.
    And so on in that pattern.

    There is also something else I found:
    For every large square with a width of 2: the number of squares in the path increase by 2 compared to the preceding slope.
    For every large square with a width of 3: the number of squares in the path increase by 4 compared to the preceding slope.
    For every large square with a width of 4: the number of squares in the path increase by 6 compared to the preceding slope.
    For every large square with a width of 5: the number of squares in the path increase by 8 compared to the preceding slope.
    And so on in that pattern also.


    So, for the above path:
    The large square has a width of 4 and a height of 7.
    The slope is 2.
    So the number of squares is 16.


    I'm not sure how to make a formula out of those trends. Any thoughts?


    EDIT AGAIN: I may have figured out a formula. It works for all my test cases. All my test cases have positive integer slopes.
    Formula: squares = (Width+Length)+(Length-2)
    It is odd, but it works...
    Last edited by aussiemcgr; September 13th, 2010 at 12:17 PM.

Similar Threads

  1. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  2. Creating a class for points in three dimensions.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 8th, 2010, 07:13 PM
  3. on-line shopping
    By sriraj.kundan in forum Web Frameworks
    Replies: 13
    Last Post: August 1st, 2009, 02:03 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM