Vector, Matrix and more

Developed or developing a new product in PureBasic? Tell the world about it.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Vector, Matrix and more

Post by GPI »

https://github.com/GPIforGit/math/releases/

There are two ways to use math:


1) Simple copy the directory to your project and

Code: Select all

	XIncludeFile "math/math.pbi"
2) use the install-script, this will copy all the include-files to your pure-basic-compiler-directory. You can then use without any additional task use "math" with:

Code: Select all

	XIncludeFile #PB_Compiler_Home + "Include/math/math.pbi"

Warning: Don't use "UseModule Math" - it will break things.


there are 4 vectors

Code: Select all

	math::vec2
	math::vec3
	math::vec4

a quaternion

Code: Select all

	math::quat

any many matrices

Code: Select all

	math::mat2x2
	math::mat2x3
	math::mat2x4
	math::mat3x2
	math::mat3x3
	math::mat3x4
	math::mat4x2
	math::mat4x3
	math::mat4x4
For most procedure/macro the first parameter is a return-value.
Example-Code: Create two vectors and add them. Debug the result

Code: Select all

	Define.math::vec3 v1,v2,vres
	math::vec3_set(v1, 1,2,3)
	math::vec3_set(v2, 2,4,5)

	math::vec3_add(vres, v1,v2)

	Debug math::string(vres)
there a "dispatcher" macros to simplfy things. These macros will detect the type of the parameter and choose the correct procedure.
same example:

Code: Select all

	Define.math::vec3 v1,v2,vres
	math::set_float(v1, 1,2,3)
	math::set_float(v2, 2,4,5)

	math::add(vres, v1,v2)

	Debug math::string(vres)
example with a matrix:

Code: Select all

	Define.math::vec3 v
	Define.math::vec3 vres
	Define.math::mat3x3 m

	math::set_float(m, 1) ; createas a identity matrix

	math::set_float(v,2) ; set all values of the vector to 2

	math::mul(vres, m, v) ; equals vres = m4*v

	Debug math::string(vres) + " = " + math::string(m) + " * " + math::string(v)
equals to

Code: Select all

	Define.math::vec3 v
	Define.math::vec3 vres
	Define.math::mat3x3 m

	math::Mat3x3_set_Scalar(m, 1) ; createas a identity matrix

	math::vec3_set_Scalar(v,2) ; set all values of the vector to 2

	math::Mat3x3_mul_Vec3(vres, m, v) ; equals vres = m4*v

	Debug math::string(vres) + " = " + math::string(m) + " * " + math::string(v)

there are many more procedures in math:: - for example dot, length, cross and so on.
see test.pb for more example
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Vector, Matrix and more

Post by Keya »

Could you please give us a more detailed description about this? (what it is, what it's used for etc) thankyou!
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Vector, Matrix and more

Post by GPI »

for example, when you want to create a 3D-Grafik with OpenGL:

http://www.opengl-tutorial.org/beginner ... -matrices/

You need vector and matrix - calculation and "math" can handle this for you.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Vector, Matrix and more

Post by luis »

Looks like a lot of work and I like how you structured the interface for the programmer even within the limitations of the language. I personally really miss OOP for things like these.

Thank you for sharing it, I will probably steal some ideas :)
"Have you tried turning it off and on again ?"
A little PureBasic review
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Vector, Matrix and more

Post by GPI »

Updated:
https://github.com/GPIforGit/math/releases/tag/1.1

; Change log:
; 1.1:
; - add rotate_float, translate_float, scale_float - replace the vector with floats
; - replaced many macros with procedures and all procedures returns now the *res-pointer. this allows nested calls
; like vec3_mul(res, vec3_add(a, b,c), a) equals "a=b+c: res=a*a" equals "res = (b+c)*(b+c)"
; - add many vec2_*, vec3_*, vec4_* for dot, length, normalize and so on
; - add this comment
; - add "euler_angles" from glm
User avatar
chikega
User
User
Posts: 34
Joined: Fri Dec 04, 2020 3:19 am

Re: Vector, Matrix and more

Post by chikega »

Very cool advanced math library for PureBasic .. thank you! 8)
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
Post Reply