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

# Thread: Getting all points in line

1. ## 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  Reply With Quote

3. ## 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?  Reply With Quote

4. ## 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  Reply With Quote

5. ## 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?   Reply With Quote

6. ## 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"?  Reply With Quote

7. ## Re: Getting all points in line

Two example paths:   Reply With Quote

8. ## 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.  Reply With Quote

9. ## 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...  Reply With Quote