Pixel Vision 8 CalculateDistance() Lua Tutorial

A fast way to calculate the distance between two points.

Usage

CalculateDistance ( x0, y0, x1, y1 )

Arguments

NameValueDescription
x0intThe first point’s x position.
y0intThe first point’s y position.
x1intThe second point’s x position.
y1intThe second point’s y position.

Returns

ValueDescription
intReturns an integer for the distance between the two points.

CalculateDistance Example

In this example, we will calculate the distance between 2 points and use a canvas to visualize it.

Running this code will output the following:

CalculateDistanceOutput_image_0.png

Learn more about making Pixel Vision 8 games by checking out the docs.

Step 1

Create a new file called code.lua.{1} in your project folder.

Step 2

Add the following local variables:

01 local pointA = NewPoint(8, 32)
02 local pointB = NewPoint(248, 232)
03 local canvas = NewCanvas(256, 240)
04 local distance = 0
05 local display = Display()

Step 3

Create a new function called Init():

06 function Init()
07 
08 end

Step 4

Add the following code to the script:

07   canvas:SetStroke(14, 1)

Set the canvas stroke to a white 1x1 pixel brush

Step 5

Create a new function called Update():

09 function Update(timeDelta)
10 
11 end

Step 6

Add the following code to the script:

10   pointB = MousePosition()

Update position B with the MousePosition

Step 7

Add the following condition to the script:

11   if(pointB.X > 0 and pointB.Y < display.X and pointB.Y > 0 and pointB.Y < display.Y) then
12   
13   end

Step 8

Add the following code to the condition :

12     distance = CalculateDistance(pointA.x, pointA.y, pointB.x, pointB.x)

Calculate the distance between pointA and pointB

Step 9

Create a new function called Draw():

15 function Draw()
16 
17 end

Step 10

Add the following code to the script:

16   RedrawDisplay()

Redraw the display

Step 11

Add the following code to the script:

17   canvas:Clear(0)

Clear the canvas with the background color

Step 12

Add the following code to the script:

18   canvas:DrawEllipse(pointA.x - 4, pointA.y - 4, 10, 10)
19   canvas:DrawEllipse(pointB.x - 4, pointB.y - 4, 10, 10)

Draw 2 circles around each point

Step 13

Add the following code to the script:

20   canvas:DrawLine(pointA.x, pointA.y, pointB.x, pointB.y)

Draw a line between the two points

Step 14

Add the following code to the script:

21   canvas:DrawText(tostring(distance), pointB.x, pointB.y - 12, "small", 14, - 4)

Draw the distance value above pointB

Step 15

Add the following code to the script:

22   canvas:DrawPixels()

Draw the canvas to the display

Step 16

Add the following code to the script:

23   DrawText("CalculateDistance()", 8, 8, DrawMode.Sprite, "large", 15)
24   DrawText("Lua Example", 8, 16, DrawMode.Sprite, "medium", 15, -4)

Final Code

When you are done, you should have the following code in the code.lua file:

`Lua 01 local pointA = NewPoint(8, 32) 02 local pointB = NewPoint(248, 232) 03 local canvas = NewCanvas(256, 240) 04 local distance = 0 05 local display = Display() 06 function Init() 07 canvas:SetStroke(14, 1) 08 end 09 function Update(timeDelta) 10 pointB = MousePosition() 11 if(pointB.X > 0 and pointB.Y < display.X and pointB.Y > 0 and pointB.Y < display.Y) then 12 distance = CalculateDistance(pointA.x, pointA.y, pointB.x, pointB.x) 13 end 14 end 15 function Draw() 16 RedrawDisplay() 17 canvas:Clear(0) 18 canvas:DrawEllipse(pointA.x - 4, pointA.y - 4, 10, 10) 19 canvas:DrawEllipse(pointB.x - 4, pointB.y - 4, 10, 10) 20 canvas:DrawLine(pointA.x, pointA.y, pointB.x, pointB.y) 21 canvas:DrawText(tostring(distance), pointB.x, pointB.y - 12, "small", 14, - 4) 22 canvas:DrawPixels() 23 DrawText("CalculateDistance()", 8, 8, DrawMode.Sprite, "large", 15) 24 DrawText("Lua Example", 8, 16, DrawMode.Sprite, "medium", 15, -4) 25 end