Page 1 of 1

Vector, Matrix and more

Posted: Sat May 22, 2021 12:46 pm
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

Re: Vector, Matrix and more

Posted: Sat May 22, 2021 12:51 pm
by Keya
Could you please give us a more detailed description about this? (what it is, what it's used for etc) thankyou!

Re: Vector, Matrix and more

Posted: Sat May 22, 2021 12:59 pm
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.

Re: Vector, Matrix and more

Posted: Sun May 23, 2021 12:33 am
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 :)

Re: Vector, Matrix and more

Posted: Sat Jun 05, 2021 1:40 pm
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

Re: Vector, Matrix and more

Posted: Sat Jul 17, 2021 6:02 am
by chikega
Very cool advanced math library for PureBasic .. thank you! 8)