# VB.NET - All points between 2 points. • ### Question

• The title says it.

Lets say i have point(0,0) and points(2,2).

i want to find all the point between them.

for this the answer is point(1,1)

but not all are like this. Some of the two points are like these:

point(55,67) and point(137,-44)

they're not jumping by the same factor.

even though, how can i get all the points between 2 points?? PLEASE HELP

Friday, April 6, 2012 4:03 PM

### Answers

• Given any two points you can get the equation of the straight line that connects them (but be careful if it is vertical).

Having got the equation, you can loop from the low X to the high X of the original points, calculating the Y that corresponds

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Friday, April 6, 2012 5:01 PM
• Hi Davud97,

The following code does what it is I think you are looking for.

To try it out create a new project and include a label, button and list box on the form, copy and paste the code then each time you click on the button two points (Pont1 and Point2) will be created with random X and Y co-ordinates. the current range is from  -40 to 40 for each, but you can change this. The difference between each pair of co-ordinates is calculated and the greater one is assigned to the variable Range. the difference between the X and Y co-ordinates of each pair is then divided by the Range giving the value (dy, dx) that each need to be incremented from Point1 to Point2. All the point in between are then calculated and (in this example) are added to a list box. For your purpose you may need to use a list. I hope this helps.

regards Ron

```Public Class Form1
Dim myRandom As System.Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Point1 As Point
Dim Point2 As Point
myRandom = New Random(Now.Millisecond)
Point1.X = myRandom.Next(-40, 41)
Point2.X = myRandom.Next(-40, 41)
Point1.Y = myRandom.Next(-40, 41)
Point2.Y = myRandom.Next(-40, 41)
GetPoints(Point1, Point2)
End Sub
Sub GetPoints(ByVal Point1 As Point, ByVal Point2 As Point)
Dim RangeX As Integer
Dim RangeY As Integer
Dim Range As Integer
Dim dx As Single
Dim dy As Single
Dim X, Y As Single
Dim newPoint As Point
RangeX = Math.Abs(Point1.X - Point2.X)
RangeY = Math.Abs(Point1.Y - Point2.Y)
If RangeX < RangeY Then
Range = RangeY
Else
Range = RangeX
End If
ListBox1.Items.Clear()
Label1.Text = Point1.ToString & " " & Point2.ToString
dx = (Point2.X - Point1.X) / Range
dy = (Point2.Y - Point1.Y) / Range
X = Point1.X
Y = Point1.Y
newPoint = Point1
For i = 1 To Range
ListBox1.Items.Add(newPoint.ToString)
X += dx
Y += dy
newPoint = New Point(X, Y)
Next
ListBox1.Items.Add(Point2.ToString)
End Sub
End Class```

Saturday, April 7, 2012 10:57 PM

### All replies

• Given any two points you can get the equation of the straight line that connects them (but be careful if it is vertical).

Having got the equation, you can loop from the low X to the high X of the original points, calculating the Y that corresponds

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Friday, April 6, 2012 5:01 PM
• What are you trying to do?

If you ask a mathematician they'll tell you there are an infinite number of points between 2 different points. To tell if a point is on the line you use the equation for a straight line, y = mx + c.

Computer's can't draw an infinite number of points - they just set pixels. To determine which pixels to set they use an algorithm such as Bresenham's line algorithm.

Friday, April 6, 2012 8:02 PM
• Hi Davud97,

The following code does what it is I think you are looking for.

To try it out create a new project and include a label, button and list box on the form, copy and paste the code then each time you click on the button two points (Pont1 and Point2) will be created with random X and Y co-ordinates. the current range is from  -40 to 40 for each, but you can change this. The difference between each pair of co-ordinates is calculated and the greater one is assigned to the variable Range. the difference between the X and Y co-ordinates of each pair is then divided by the Range giving the value (dy, dx) that each need to be incremented from Point1 to Point2. All the point in between are then calculated and (in this example) are added to a list box. For your purpose you may need to use a list. I hope this helps.

regards Ron

```Public Class Form1
Dim myRandom As System.Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Point1 As Point
Dim Point2 As Point
myRandom = New Random(Now.Millisecond)
Point1.X = myRandom.Next(-40, 41)
Point2.X = myRandom.Next(-40, 41)
Point1.Y = myRandom.Next(-40, 41)
Point2.Y = myRandom.Next(-40, 41)
GetPoints(Point1, Point2)
End Sub
Sub GetPoints(ByVal Point1 As Point, ByVal Point2 As Point)
Dim RangeX As Integer
Dim RangeY As Integer
Dim Range As Integer
Dim dx As Single
Dim dy As Single
Dim X, Y As Single
Dim newPoint As Point
RangeX = Math.Abs(Point1.X - Point2.X)
RangeY = Math.Abs(Point1.Y - Point2.Y)
If RangeX < RangeY Then
Range = RangeY
Else
Range = RangeX
End If
ListBox1.Items.Clear()
Label1.Text = Point1.ToString & " " & Point2.ToString
dx = (Point2.X - Point1.X) / Range
dy = (Point2.Y - Point1.Y) / Range
X = Point1.X
Y = Point1.Y
newPoint = Point1
For i = 1 To Range
ListBox1.Items.Add(newPoint.ToString)
X += dx
Y += dy
newPoint = New Point(X, Y)
Next
ListBox1.Items.Add(Point2.ToString)
End Sub
End Class```

Saturday, April 7, 2012 10:57 PM