Browse
Code
for
All Platforms
in
All Categories
sorted by
Popular
• More ...
• More ...
• More ...
• More ...
• More ...
• More ...
• More ...
• More ...
• More ...
• More ...

# 2d vector handling class

Released 4 years ago , Last update 4 years ago

Fast 2d vector handling class by templated operators overload. Useful for game development, or math programming.

This class manages 2d vectors by templated , overloaded operators. Common operations included are scaling, sum, subtraction, dotproduct, modulus, distance , squared distance and others, angle beetween vectors , also templated types for the most used numeric formats are included.

## Usage examples :

``````CVec2f v1,v2,v3;

float d,l;

v1.set( 10,20 );            // initialise vectors

v2.set( 30,40 );            // initialise vectors

v3=v1-v2;                    // subtraction

v3=-v1-v2;                  // sign before vector argument

v3+=v1;                      // increment

v3-=v1;                       // decrement

v1=v2*10.0f;               // scaling

v1*=10.0f;                   // scaling

v2=v3/10.0f;               // inverse scaling

v3/=5.0f;                    // inverse scaling

v3=lerp( v1,v2,0.5 );  // linear interpolation between vectors

v3= project ( v1,v2 ); // Calculates the projection of  v1 onto v2``````

v3=perpendicular( v1,v2 ); // Calculates the component of v1 perpendicular to v2

``````v3=orthogonalize( v1,v2 );   // orthogonalization

v3 = cross ( v2,v1 );  // vectorial cross product

v3=reflect( v1, v2 ); // reflect vector v1 respect v2

d= dot( v1,v2 );         // dot product

v3.normalize();         // normalize vector

l=v1.length();            // lenght of vector

l=v1.squaredlength();   // squared lenght

v2.zero();          // sets all vector components to zero

v1.fabsf(); // absoulte values for each vector components

// logic comparisons

if ( v2!=v1 ) cout << "vectors are different";
if ( v2==v1 ) cout << "vectors are identical";
if ( v2>=v1 ) cout << "vectors v2 is greater or equal than v1";
if ( v2<=v1 ) cout << "vectors v2 is smaller or equal than v1";
if ( v2>v1 ) cout << "vectors v2 is greater than v1";
if ( v2``````
``` ```
``` ```
``` ```
``` Pricing 14-day money-back guarantee \$4.99 Personal License Perpetual license 1 application Can distribute binary products only Commercial use purchase now Read Full License ```
``` ```
``` ```
``` Read more ```
``` 1 license From » \$4.99 View Licenses Get A Quote What do you need? Custom development Integration Customization / Reskinning Consultation When do you need it? Soon Next week Next month Anytime Full Name Email What you need in one sentence Detailed description of your requirements Thanks for getting in touch! Your quote details have been received and we'll get back to you soon. Close Sign-in now Sign-in Or enter your name and Email No comments have been posted yet. Read all comments ```
``` ```
``` Selling Digital Products Your Online Store Business Software Computer Games Source-Code Libraries EU VAT collection The Binpress Marketplace Browse the marketplace Publish your products Publishing Guidelines Software Development Tutorials Learn More Binpress Guides Blog Forum Frequently Asked Questions Digital Fulfillment on your site Binpress About us Contact Us Terms & Conditions Privacy Policy We're Hiring! Your own digital store !window.jQuery && document.write('<script src="js/jq/jquery.min.js"><\/script>') !window.jQuery.ui && document.write('<script src="js/jq/jquery-ui.min.js"><\/script>') /*{literal}<![CDATA[*/ window.olark||(function(c){var f=window,d=document,l=f.location.protocol=="https:"?"https:":"http:",z=c.name,r="load";var nt=function(){f[z]=function(){(a.s=a.s||[]).push(arguments)};var a=f[z]._={},q=c.methods.length;while(q--){(function(n){f[z][n]=function(){f[z]("call",n,arguments)}})(c.methods[q])}a.l=c.loader;a.i=nt;a.p={0:+new Date};a.P=function(u){a.p[u]=new Date-a.p[0]};function s(){a.P(r);f[z](r)}f.addEventListener?f.addEventListener(r,s,false):f.attachEvent("on"+r,s);var ld=function(){function p(hd){hd="head";return["<",hd,"></",hd,"><",i,' onl' + 'oad="var d=',g,";d.getElementsByTagName('head')[0].",j,"(d.",h,"('script')).",k,"='",l,"//",a.l,"'",'"',"></",i,">"].join("")}var i="body",m=d[i];if(!m){return setTimeout(ld,100)}a.P(1);var j="appendChild",h="createElement",k="src",n=d[h]("div"),v=n[j](d[h](z)),b=d[h]("iframe"),g="document",e="domain",o;n.style.display="none";m.insertBefore(n,m.firstChild).id=z;b.frameBorder="0";b.id=z+"-loader";if(/MSIE[ ]+6/.test(navigator.userAgent)){b.src="javascript:false"}b.allowTransparency="true";v[j](b);try{b.contentWindow[g].open()}catch(w){c[e]=d[e];o="javascript:var d="+g+".open();d.domain='"+d.domain+"';";b[k]=o+"void(0);"}try{var t=b.contentWindow[g];t.write(p());t.close()}catch(x){b[k]=o+'d.write("'+p().replace(/"/g,String.fromCharCode(92)+'"')+'");d.close();'}a.P(2)};ld()};nt()})({loader: "static.olark.com/jsclient/loader0.js",name:"olark",methods:["configure","extend","declare","identify"]}); /* custom configuration goes here (www.olark.com/documentation) */ olark.identify('3407-350-10-2744');/*]]>{/literal}*/Questions? Feedback? powered by Olark live chat software adroll_adv_id = "2RIEOSPSWNCFZOUITXIIM7"; adroll_pix_id = "X4QCHJ7ZUBFSTP7ACJ34TI"; (function () { var oldonload = window.onload; window.onload = function(){ __adroll_loaded=true; var scr = document.createElement("script"); var host = (("https:" == document.location.protocol) ? "https://s.adroll.com" : "http://a.adroll.com"); scr.setAttribute('async', 'true'); scr.type = "text/javascript"; scr.src = host + "/j/roundtrip.js"; ((document.getElementsByTagName('head') || [null])[0] || document.getElementsByTagName('script')[0].parentNode).appendChild(scr); if(oldonload){oldonload()}}; }()); maxmind_user_id = "62188"; (function() { var mt = document.createElement('script'); mt.type = 'text/javascript'; mt.async = true; mt.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'device.maxmind.com/js/device.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mt, s); })(); if(!NREUMQ.f){NREUMQ.f=function(){NREUMQ.push(["load",new Date().getTime()]);var e=document.createElement("script");e.type="text/javascript";e.src=(("http:"===document.location.protocol)?"http:":"https:")+"//"+"js-agent.newrelic.com/nr-100.js";document.body.appendChild(e);if(NREUMQ.a)NREUMQ.a();};NREUMQ.a=window.onload;window.onload=NREUMQ.f;};NREUMQ.push(["nrfj","bam.nr-data.net","7d8608a34f","3053298","YFdVYEsAVxdYAhAICVkddldNCFYKFhINFQMYU0dEFhFLC18ICAQ=",0,153,new Date().getTime(),"","","","",""]); ```