# Getting all points in line

• September 13th, 2010, 10:45 AM
Mike
Getting all points in line
In my code i create line by using two points.
Code :

```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
• September 13th, 2010, 11:02 AM
Norm
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?
• September 13th, 2010, 11:12 AM
copeg
Re: Getting all points in line
Code :

`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
• September 13th, 2010, 11:33 AM
Mike
Re: Getting all points in line
Here is image to explain better what im doing:

http://img705.imageshack.us/img705/772/gridk.jpg

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? :)
• September 13th, 2010, 11:52 AM
Norm
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"?
• September 13th, 2010, 12:21 PM
Mike
Re: Getting all points in line
• September 13th, 2010, 12:46 PM
Norm
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.
• September 13th, 2010, 12:59 PM
aussiemcgr
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...