(Created page with "This page contains codes of useful math methods already turned into code. == Geometry == == Segments' intersection <ref>Adapted from a stackoverflow post, by ''ubuntu'' - [...")
 
m (Frankiezafe moved page Notes:Math to Notes:Geometry without leaving a redirect)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page contains codes of useful math methods already turned into code.
+
This page contains snippets of useful math methods already turned into pseudo-code.
  
 
== Geometry ==
 
== Geometry ==
  
  
== Segments' intersection <ref>Adapted from a stackoverflow post, by ''ubuntu'' - [http://stackoverflow.com/questions/15297590/improve-in-coding-saving-how-to-check-if-two-line-segments-are-crossing-in-pytho stackoverflow.com]</ref> ==
+
=== Segments' intersection <ref name="two-line-segments-are-crossing">Adapted from a stackoverflow post by ''ubuntu'' - [http://stackoverflow.com/questions/15297590/improve-in-coding-saving-how-to-check-if-two-line-segments-are-crossing-in-pytho stackoverflow.com]</ref> ===
  
  PVector intersetion( PVector a1, PVector a2, PVector b1, PVector b2 ) {
+
  vec2f intersection( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
 
     float a = a2.x - a1.x;
 
     float a = a2.x - a1.x;
 
     float b = b1.x - b2.x;
 
     float b = b1.x - b2.x;
Line 16: Line 16:
 
     if ( abs( denom ) < 1e-5 ) {
 
     if ( abs( denom ) < 1e-5 ) {
 
       // parrallel
 
       // parrallel
       return null;
+
       return 0;
 
     } else {
 
     } else {
 
       float t = (e*d - b*f)/denom;
 
       float t = (e*d - b*f)/denom;
       return new PVector( a1.x + t * ( a2.x - a1.x ), a1.y + t * ( a2.y - a1.y ) );
+
       float s = (a*f - e*c)/denom;
 +
      if ( t >= 0 && t <= 1 && s >= 0 && s<=1 ) {
 +
        return new vec2f( a1.x + t * ( a2.x - a1.x ), a1.y + t * ( a2.y - a1.y ) );
 +
      }
 +
      return 0;
 
     }
 
     }
 
   }
 
   }
 +
 +
To transform this method into a line intersection detection, just comment the test on ''t'' and ''s''. The smarter thing to do is to make this test optional:
 +
 +
vec2f intersection( vec2f a1, vec2f a2, vec2f b1, vec2f b2, bool keep_in_segment = true ) {
 +
...
 +
      if ( ( keep_in_segment && t >= 0 && t <= 1 && s >= 0 && s<=1 ) || ( !keep_in_segment ) ) {
 +
 +
=== Segments' crossing <ref name="two-line-segments-are-crossing"/> ===
 +
 +
  boolean crosses( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
 +
    float a = a2.x - a1.x;
 +
    float b = b1.x - b2.x;
 +
    float c = a2.y - a1.y;
 +
    float d = b1.y - b2.y;
 +
    float e = b1.x - a1.x;
 +
    float f = b1.y - a1.y;
 +
    float denom = a * d - b * c;
 +
    if ( abs( denom ) < 1e-5 ) {
 +
      // parrallel
 +
      return false;
 +
    } else {
 +
      float t = (e*d - b*f)/denom;
 +
      float s = (a*f - e*c)/denom;
 +
      return ( t >= 0 && t <= 1 && s >= 0 && s<=1 );
 +
    }
 +
  }
 +
 +
 +
 +
 +
<references/>
 +
 +
[[Category:math]]
 +
[[Category:code snippet]]
 +
[[Category:pseudocode]]

Latest revision as of 21:38, 15 September 2017

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

Geometry

Segments' intersection [1]

vec2f intersection( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
   float a = a2.x - a1.x;
   float b = b1.x - b2.x;
   float c = a2.y - a1.y;
   float d = b1.y - b2.y;
   float e = b1.x - a1.x;
   float f = b1.y - a1.y;
   float denom = a * d - b * c;
   if ( abs( denom ) < 1e-5 ) {
     // parrallel
     return 0;
   } else {
     float t = (e*d - b*f)/denom;
     float s = (a*f - e*c)/denom;
     if ( t >= 0 && t <= 1 && s >= 0 && s<=1 ) {
       return new vec2f( a1.x + t * ( a2.x - a1.x ), a1.y + t * ( a2.y - a1.y ) );
     }
     return 0;
   }
 }

To transform this method into a line intersection detection, just comment the test on t and s. The smarter thing to do is to make this test optional:

vec2f intersection( vec2f a1, vec2f a2, vec2f b1, vec2f b2, bool keep_in_segment = true ) {
...
     if ( ( keep_in_segment && t >= 0 && t <= 1 && s >= 0 && s<=1 ) || ( !keep_in_segment ) ) {

Segments' crossing [1]

 boolean crosses( vec2f a1, vec2f a2, vec2f b1, vec2f b2 ) {
   float a = a2.x - a1.x;
   float b = b1.x - b2.x;
   float c = a2.y - a1.y;
   float d = b1.y - b2.y;
   float e = b1.x - a1.x;
   float f = b1.y - a1.y;
   float denom = a * d - b * c;
   if ( abs( denom ) < 1e-5 ) {
     // parrallel
     return false;
   } else {
     float t = (e*d - b*f)/denom;
     float s = (a*f - e*c)/denom;
     return ( t >= 0 && t <= 1 && s >= 0 && s<=1 );
   }
 }



  1. 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]]