Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Not exactly sure how to describe this but I need a formula I believe on how to plot a location of an obect and get it from point A to point B.

For example, I've got a black dot at 0,0...and I want to move him to 46,36 taking the most direct route (a line). If it were straight lines, it would be easy...but how do I do it with diagonals? I know there is some sort of formula for this used in games...I need it :)

For example, I've got a black dot at 0,0...and I want to move him to 46,36 taking the most direct route (a line). If it were straight lines, it would be easy...but how do I do it with diagonals? I know there is some sort of formula for this used in games...I need it :)

y = ax + b

then for a line passing thru (x1,y1) and (x2,y2) its

a = (y2 - y1)/(x2 - x1)

b = (y1 * (x2 - x1) - x1 * (y2 - y1))/(x2 - x1)

Given 'cartesian coordinates' that is with an x and y axis, you can express a formula for a straight line using the formula

y = ax + b

so for any x co-ordinate you can find the y co-ordinate which corresponds to the line.

In your case you have a line with b = 0 and a = 18/23

so at x = 46 you have y = 36

at x = 23 you have y = 18 so (23,18) is another point on your line.

I don't know if you'd want to use this in your program, one problem is that for a vertical line, a which is the gradient of your line becomes infinite. I just put it in as an afterthought in case it was what you were trying to work out.

By the way deighton, I can't get the right answer using this bit.

Me.DrawWidth = 4

Call fLine(799, 0, 950, 5000, Me, vbBlack, vbWhite)

End Sub

Private Function fLine(x1 As Long, y1 As Long, x2 As Long, y2 As Long, vObject As Object, lLineColor As Long, lPixelColor As Long)

Dim a As Double

Dim b As Double

Dim dTemp As Double

Dim bPlot As Boolean

Dim x As Double

Dim dLen As Double

Dim xO As Long, yO As Long

'vObject.Line (x1, y1)-(x2, y2), lLineColor

dLen = Sqr((x1 - x2) ^ 2 + (y1 - y2) ^ 2)

a = (y2 - y1) / (x2 - x1)

b = (y1 * (x2 - x1) - x1 * (y2 - y1)) / (x2 - x1)

For x = x1 To x2 Step (x2 - x1) / dLen

y = Int(a * x + b + 0.5)

If Not bPlot Or x <> xO Or y <> yO Then

vObject.PSet (x, y), lLineColor

bPlot = True

xO = x

yO = y

End If

Next

bPlot = False

For x = x1 To x2 Step (x2 - x1) / dLen

y = Int(a * x + b + 0.5)

If x <> xO And y <> yO Or Not bPlot Then

If bPlot Then vObject.PSet (xO, yO), lLineColor

vObject.PSet (x, y), lPixelColor

bPlot = True

xO = x

yO = y

End If

Next

End Function

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

sqr((x1 - x2) ^ 2 + (y1 - y2) ^2 ) 'pythagoras

the angle to the horizontal is atn((y2 - y1) / (x2 - x1))