Solved

VB5 best route from point A to point B

Posted on 1998-10-21
13
146 Views
Last Modified: 2010-04-30
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 :)
0
Comment
Question by:ChrisK
  • 6
  • 5
  • 2
13 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1440927
The distance between points (x1,y1) and (x2,y2) is easily worked out

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

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


0
 
LVL 18

Expert Comment

by:deighton
ID: 1440928
If you want a standard linear equation in the form

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)
0
 
LVL 2

Author Comment

by:ChrisK
ID: 1440929
excellent...but what about x on your comment?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 18

Expert Comment

by:deighton
ID: 1440930
I'll try and explain my y = ax + b formula a bit more.

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.
0
 
LVL 2

Author Comment

by:ChrisK
ID: 1440931
Why wouldn't I want to use this?  To me it looks like I can draw an imaginary line from point A to point B and have a dot travel the correct pixel path to those points, which is exactly what I need.
0
 
LVL 2

Author Comment

by:ChrisK
ID: 1440932
If what you say only works for diagonals, that's fine too because then for straight lines I simply check the previous X or Y and make sure one of those matches the new X or Y signifying a straight line.  If not, then use the formula you posted.  That sounds right don't ya think?
0
 
LVL 18

Expert Comment

by:deighton
ID: 1440933
Yes thats true, I was making things too complcated.  
0
 

Expert Comment

by:ChasDevlin
ID: 1440934
The formula works fine for horizontals where a = 0, as you say when x1 = x2 you've got a vertical straight line.
0
 

Expert Comment

by:ChasDevlin
ID: 1440935
the angle to the horizontal is atn((y2 - y1) / (x2 - x1))

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

Expert Comment

by:deighton
ID: 1440936
You might me getting an answer in radians, to get back to degrees multiply by 360/(2 * pi)
0
 
LVL 2

Author Comment

by:ChrisK
ID: 1440937
You guys must be mathimaticians :)  Could you help out a mathimatically challenged soul by supplying a little example source :)  nothing big, just something like here's point A, here's point B, and move a pixel along the path.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1440938
Private Sub Command1_Click()
    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


0
 
LVL 2

Author Comment

by:ChrisK
ID: 1440939
Thanks...now I kinda understand it.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 code to programmatically convert pdf to excel 21 90
Modifying Conditional Format from VBA code 3 55
Help me. 3 54
MS Access 03, TransferText, decimal places 8 69
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question