Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Intersecting lines (See related posts)

   1  
   2    
   3    void intersect()
   4    {
   5       float m1,m2,b1,b2,ix,iy;
   6       Position p1a = new Position(mouseX, mouseY);
   7       Position p1b = new Position(points[maximum-1]);
   8       Position p2a = new Position();
   9       Position p2b = new Position();
  10  
  11       strokeWeight(4);
  12       stroke(0,255,0);
  13       line(p1a.x, p1a.y, p1b.x, p1b.y);
  14       
  15       // comparison line segment
  16       m1 = (p1a.y - p1b.y) / (p1a.x - p1b.x);
  17       b1 = p1a.y-m1*p1a.x;
  18       
  19       // cycle through each line segment on the gesture backwards
  20       for (int i = maximum-1; i > 0; i--) {
  21         
  22         //if (points[i].x < 0 && points[i].y < 0) { continue; }
  23         
  24         p2a.set(points[i]);
  25         p2b.set(points[i-1]);
  26         
  27         // second line segment
  28         m2 = (p2a.y - p2b.y) / (p2a.x - p2b.x);
  29         b2 = p2a.y - m2*p2a.x;
  30         
  31         if (m1 != m2) { // if they're not parallel
  32         
  33           // find the intersection
  34           ix = (b2-b1)/(m1-m2);
  35           iy = m1*ix+b1;
  36           
  37           // is the intersection on the line segments?
  38           if (ix > min(p1a.x, p1b.x) && ix < max(p1a.x, p1b.x) &&
  39               iy > min(p1a.y, p1b.y) && iy < max(p1a.y, p1b.y) &&
  40               ix > min(p2a.x, p2b.x) && ix < max(p2a.x, p2b.x) &&
  41               iy > min(p2a.y, p2b.y) && iy < max(p2a.y, p2b.y)) {
  42               
  43               if (i_count < i_max) {
  44                 intersects[i_count].set(ix, iy);
  45                 i_count++;
  46               } else {
  47                 for (int j = 0; j < i_max-1; j++) {
  48                   intersects[j].set(intersects[j+1]);
  49                 }
  50                 intersects[i_max-1].set(ix, iy);
  51               }
  52               
  53               println("something");
  54               // send the points to the shape object
  55               s.set(points, i);
  56               reset();
  57               break;   
  58           }
  59         }
  60       }

You need to create an account or log in to post comments to this site.


Click here to browse all 5349 code snippets

Related Posts