Line 53: Line 53:
  
 
[[Category:math]]
 
[[Category:math]]
[[Category:snippets]]
+
[[Category:code snippet]]
[[Category:pseudo-code]]
+
[[Category:pseudocode]]

Revision as of 18:22, 16 April 2017

This page contains snippets of useful math methods already turned into pseudo-code.

Geometry

Segments' intersection [1]

  1. vec2f intersetion( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
  2. float a = a2.x - a1.x;
  3. float b = b1.x - b2.x;
  4. float c = a2.y - a1.y;
  5. float d = b1.y - b2.y;
  6. float e = b1.x - a1.x;
  7. float f = b1.y - a1.y;
  8. float denom = a * d - b * c;
  9. if ( abs( denom ) < 1e-5 ) {
  10. // parrallel
  11. return 0;
  12. } else {
  13. float t = (e*d - b*f)/denom;
  14. float s = (a*f - e*c)/denom;
  15. if ( t >= 0 && t <= 1 && s >= 0 && s<=1 ) {
  16. return new vec2f( a1.x + t * ( a2.x - a1.x ), a1.y + t * ( a2.y - a1.y ) );
  17. }
  18. return 0;
  19. }
  20. }

Segments' crossing [1]

  1. boolean crosses( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
  2. float a = a2.x - a1.x;
  3. float b = b1.x - b2.x;
  4. float c = a2.y - a1.y;
  5. float d = b1.y - b2.y;
  6. float e = b1.x - a1.x;
  7. float f = b1.y - a1.y;
  8. float denom = a * d - b * c;
  9. if ( abs( denom ) < 1e-5 ) {
  10. // parrallel
  11. return false;
  12. } else {
  13. float t = (e*d - b*f)/denom;
  14. float s = (a*f - e*c)/denom;
  15. return ( t >= 0 && t <= 1 && s >= 0 && s<=1 );
  16. }
  17. }



  1. Jump up to: 1.0 1.1 Adapted from a stackoverflow post by ubuntu - stackoverflow.com

online identity ∋ [ social ∋ [mastodon♥, twitter®, facebook®, diaspora, linkedin®] ∥ repos ∋ [github®, gitlab♥, bitbucket®, sourceforge] ∥ media ∋ [itch.io®, vimeo®, peertube♥, twitch.tv®, tumblr®] ∥ communities ∋ [godotengine♥, openprocessing, stackoverflow, threejs]]