Front Page     Archives     My Profile     Members     Links     Keep Us Alive!  
  Log In / Register    
 // ARTICLE
Issue: July'07
pristd - priVect2D: A simple 2D Vector Class
Author: Pritchard
Posted By: Pritchard
Category: General
The goal of pristd is to reduce code rewriting by providing classes which may prove useful in future projects. This is hard to do with more complex objects (such as primitives), but more simple for objects such as vectors.

Binary Included: Yes
Source Included: Yes
Download Here: http://file-pasta.com/d/1700.7z

How To Install:

Requirements:
You'll need something that can extract .7z files. 7-zip and winrar will do. Please make sure you have at least FBC version .17b. If the library doesn't work by default, recompile and test again please.

Setting Up:
Create a pristd folder in your freebasic compiler\inc\ directory. Copy the .bi file for priVect2D to that folder. Copy the output library file to your freebasic compiler\lib\osnamegoeshere\ folder.

Using priVect2D in your Code:
Simply #include once "pristd\priVect2D.bi". The classes can be found in the pri namespace.

Mini-Doc:

The Vect2D class has a small variety of functions to control information about your vector, and view the relationship between yours and another. It has overloaded several operators to make using your vector class even easier.

Example:

Ball falls and rises depending on gravity.

  '' Allow us to use the vector class.

#include once "pristd\priVect2D.bi"
'' FB graphics headers and useful thinger.
#include once "fbgfx.bi"
Randomize timer

'' A type for a ball. No radius please. :P
Type Ball
Pos as pri.Vect2D
Vel as pri.Vect2D
End Type

'' I love 32-bit...
screenres 640, 480, 32

'' Ball position
'' Set up the ball at a random position on our screen.
Dim as Ball Ball
Ball.Pos = type( Rnd * 640, Rnd * 480 )

'' World Gravity
'' 9.81 going doooown
Dim as pri.Vect2D Grav = type( 0, .981 )


Screenlock
Do

'' Increase ball velocity by gravity.
Ball.Vel += Grav
'' Increase the ball position by velocity
Ball.Pos += Ball.Vel

'' Reset ball position and velocity if anything's out of bounds.
If Ball.Pos.X < 0 then
Ball.Pos = type( Rnd * 640, Rnd * 480 )
Ball.Vel = -Grav * 20
End If
If Ball.Pos.Y < 0 then
Ball.Pos = type( Rnd * 640, Rnd * 480 )
Ball.Vel = -Grav * 20
End If
If Ball.Pos.X >= 640 then
Ball.Pos = type( Rnd * 640, Rnd * 480 )
Ball.Vel = -Grav * 20
End If
If Ball.Pos.Y >= 480 then
Ball.Pos = type( Rnd * 640, Rnd * 480 )
Ball.Vel = -Grav * 20
End If

'' Draw the ball.
Circle ( Ball.Pos.X, Ball.Pos.Y ), 20, rgb(255, 255, 255), , , , f

'' 'flip' our page
Screenunlock
sleep 31, 1
Screenlock
cls

Loop until multikey(FB.SC_ESCAPE)
Screenunlock


Excuse typos in the mini-documentation. They are hopefully not present in the code :p

 // DOWNLOADS

http://pritchard.hmcsoft.org/Files/privect2d.7z 19.3kb

 // RELATED LINKS

http://www.freebasic.net/forum/viewtopic.php?t=8963
 // DISCUSS THIS ARTICLE


Pritchard

For the latest version...
2007-07-23 18:24:19
Please see http://pritchard.hmcsoft.org/Files/priVect2D.7z

The link posted in the article itself is *not* guaranteed to be up by tomorrow. However, my hosting from the cool guy at hmcsoft *is* guaranteed to be up.

The latest version contains bug fixes and new features.

Thanks,
Pritchard

HexDude


2007-08-06 12:31:59
brilliant work as usual, a very useful idea and excellently implemented.

 // POST COMMENT

Please Log In to post comments.