Distance Formula

 BBS: Inland Empire Archive
Date: 06-24-92 (19:51)             Number: 1339
From: JEAN CREPEAU                 Refer#: NONE
  To: ROBERT CHURCH                 Recvd: NO  
Subj: Distance Formula               Conf: (2) Quik_Bas
In a message to ALL, ROBERT CHURCH wrote:
RC=> I need some help with a little  math.  For a game I'm writing, I  need
    to not only find the distance between two points on a grid, but to find
    the POINTS on the grid.

RC=>    In  short, How can I find  all of the points between  two points on
    a grid?

        To calculate the distance between two points, it's easy...
                d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))

                where   (x1,y1) is your first point
                        (x2,y2) is the second point
                        d is the distance (in point units)

        To find all the points between  two points, you should use a  line-
like algorithme.

defsng a-z
dx=x2-x1
dy=y2-y1
if dx=0 and dy=0 then
        pset (x1,y1)            ' No line to draw
else
        if abs(dy)>abs(dx) then mx=dx/dy:my=1 else mx=1:my=dy/dx
        x=x1:y=y1
        do
                pset (x,y)
                x=x+mx:y=y+my
        loop until x=x2 and y=y2
endif

        This is not an optimized program nor has it been tested. It's  only
to show you the algorithme. In  your case, PSET should be replaced  by what
you want to do.

                Jean
---
 * Origin: INTERACESS Montreal (QC) Canada (514) 528-1415 (1:167/280)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET