How To... - QB64 Wiki

How To...

From QB64 Wiki

Jump to: navigation, search

This tutorial is actually many tutorials in one. It explains how to do tasks that seem easy to understand but hard to actually code if you don't know exactly how it works.


How To...


...Get the Distance Between Two Coordinates

We will start with getting the distance between two coordinates:


0--------

|  . (x,y)

|

|       . (x2,y2)


We know the distance between x and x2 (x2-x) and the distance between y and y2 (y2-y). But how do we get the distance between (x,y) and (x2,y2)?


Well, first we take the distance between x and x2 and call it distx:


distx = x2 - x


Then we do the same for the distance in the y direction:


disty = y2 - y


Now the distance between (x,y) and (x2,y2) is:


distance = SQR(distx ^ 2 + disty ^ 2)


Example:

x = 100 y = 100 x2 = 110 y2 = 110 distx = x2 - x disty = y2 - y distance = SQR(distx ^ 2 + disty ^ 2) PRINT distance


To know the distance between the points is necessary to move a point towards another point as you will see in the next section where we will create a line.

...Create a Line (and move a object towards a coordinate):

First we need the distance in the x direction which is x2 - x, then we need the distance in the y direction which is y2 - y.


So we have distx = x2-x and disty = y2-y. We will also need the actual distance between the coordinates which is SQR(distx^2 + disty^2).


To know how much to move x and y to make it move in the direction of x2 and y2 we need to know in how many parts the distx and disty differs from the actual distance. Dividing distx with distance gives how many parts x needs to move to approach the distance and dividing disty with distance gives how many parts y needs to move to approach the distance.


We can name the variable that holds how many parts it needs to move deltax and deltay, so the calculation is deltax = distx/distance, and the calculation for deltay is deltay = disty/distance.


Example:

SCREEN 13 'we need a graphical screenmode to plot pixels, let's use SCREEN 13. 'these are the coordinates for the line: x = 140 y = 35 x2 = 100 y2 = 100 'here the distance of the x-axis and y-axis are calculated: distx = x2 - x disty = y2 - y 'here the distance between the coordinates are calculated: distance = SQR(distx ^ 2 + disty ^ 2) 'we make deltax and deltay 0 before the FOR...NEXT loop. deltax = 0 deltay = 0 FOR k = 0 TO distance PSET (x + deltax, y + deltay), 15 'deltax is now calculated as the amount it needs to move from x and y to aproach the distance needed. deltax = deltax + distx / distance deltay = deltay + disty / distance NEXT 'let's set two pixels at the start and end of the line to prove that it actually arrived at the correct coordinates: PSET (x, y), 12 PSET (x2, y2), 11


If you want a object to move towards a destination you can alter the code this way:


Example:

SCREEN 13 x = 140 y = 35 x2 = 100 y2 = 100 deltax = 0 deltay = 0 DO CLS 'clear each loop. _LIMIT 30 'this is used to limit the loop to 30 frames per second (otherwise it would be too fast). 'as you might notice the code is the same as before, except that FOR...NEXT has been removed. distx = x2 - x disty = y2 - y distance = SQR(distx ^ 2 + disty ^ 2) PSET (x + deltax, y + deltay), 15 deltax = deltax + distx / distance deltay = deltay + disty / distance PSET (x, y), 12 PSET (x2, y2), 11 _DISPLAY 'we display each loop to avoid flicker (displays what has been drawn since the CLS). LOOP UNTIL x + deltax < 0 OR x + deltax > 319 OR y + deltay < 0 OR y + deltay > 199


I made it go past the x2 and y2 coordinate to show that it isn't limited that way. If you want it to stop at the x2 and y2 coordinate you simply change the condition for the loop to UNTIL CINT(x+deltax) = x2 and CINT(y+deltay) = y2. I use CINT because deltax can be a fractional number and = is exact comparision.

Views
  • Page
  • Discussion
  • View source
  • History
Personal tools
  • Log in
Toolbox
  • What links here
  • Related changes
  • Special pages
  • Printable version