source: abuse/trunk/src/intsect.cpp @ 682

Last change on this file since 682 was 555, checked in by Sam Hocevar, 9 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 5.6 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[555]11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
[56]14
[2]15#include <stdlib.h>
16
[16]17void pushback(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2,
[124]18             int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2, int xdir, int ydir, int inside)
[2]19{
20
21   // determine if the lines are intersecting before we set back
22
23//   if (yp1==yp2)
24//   {
25//     if (inside && y2>=yp1)
[124]26
[2]27}
28
29
[16]30/* int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2,
[124]31             int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2)
[2]32{
[16]33  int32_t mx1,my1,b1,mx2,my2,b2,side1,side2,tx2,ty2;
[2]34  my1=(y2-y1);
35  if (!my1)                  // is the first line strait across?
36  {
37    side1=yp1-y2;          // if yes, check to see if the other is top/bottom
38    side2=yp2-y2;
39    mx1=(x2-x1);          // make sure we give this a value
40    b1=y2;
41  }
42  else
43  {
44    mx1=(x2-x1);           // if the line strait up and down?
45    if (!mx1)
46    {
47      side1=xp1-x1;
[124]48      side2=xp2-x2;
[2]49    } else
50    {
51      b1=y1-(my1*x1/mx1);    // calculate the y intercept
[124]52      side1=yp1-((my1*xp1)/mx1+b1);
53      side2=yp2-((my1*xp2)/mx1+b1);
[2]54    }
55  }
56  if ((side1>=0 && side2<=0) || (side1<=0 && side2>=0))
57  {
58    my2=(yp2-yp1);
59    if (!my2)
60    {
61      side1=y1-yp2;
62      side2=y2-yp2;
63      b2=yp1;
64      mx2=(xp2-xp1);
65    }
66    else
67    {
68      mx2=(xp2-xp1);
69      if (!mx2)
70      {
[124]71    side1=x1-xp2;
72    side2=x2-xp2;
[2]73      } else
74      {
[124]75    b2=yp1-(my2*xp1/mx2);
76    side1=y1-((my2*x1)/mx2+b2);
77    side2=y2-((my2*x2)/mx2+b2);
[2]78      }
79    }
80
81    // check two wierd cases where the lines are parallel
[124]82    if (mx1==mx2 && mx1==0)
[2]83      if (y2<yp1 || y1>yp2)
[124]84        side1=side2=1;
[2]85      else side1=side2=0;
86
87    if (my1==my2 && my2==0)
88      if (x2<xp1 || x1>xp2)
89        side1=side2=1;
90      else side1=side2=0;
91
92    if ((side1>=0 && side2<=0) || (side1<=0 && side2>=0))
93    {
94      tx2=x2;  ty2=y2;
95      int xadd,yadd;
96      if (x1>x2) xadd=1; else if (x1<x2) xadd=-1; else xadd=0;
97      if (y1>y2) yadd=1; else if (y1<y2) yadd=-1; else yadd=0;
[124]98
[2]99      // now find the point of intersection
100      if (mx1==0)  // if the first line is strait up & down
[124]101      {
102    if (mx2==0)   // if they are both strait up then
103    { tx2=x1; ty2=y1; }  // they connect everywhere, don't let it move!
[2]104        else
[124]105          ty2=my2*x2/mx2+b2+yadd;
[2]106      } else if (mx2==0)
107      { tx2=xp1+xadd;
108        ty2=my1*tx2/mx1+b1;
109      }
110      else
111      {
112        if (my1==0)
113          if (my2==0)
114            { tx2=x1; ty2=y1; }
115          else tx2=mx2*(y1-b2)/my2+xadd;
116        else if (my2==0)
[494]117        {
[2]118          ty2=yp1+yadd;
[494]119          tx2=mx1*(ty2-b1)/my1;
120        }
[124]121        else
[2]122        {
123          if (abs(mx1)>abs(my1))
124          {
[16]125            int32_t ae_bd=my1*mx2-mx1*my2;
[2]126            CONDITION(ae_bd,"line intersect fuck up");
127            tx2=(mx1*mx2*(b2-b1))/ae_bd+xadd;
128            ty2=my1*tx2/mx1+b1;
129          }
130          else
131          {
[16]132            int32_t db_ea=(my2*mx1-mx2*my1);
[2]133            CONDITION(db_ea,"line intersect fuck up");
134            ty2=(mx1*b1*my2-my1*mx2*b2)/db_ea+yadd;
135            tx2=mx1*(ty2-b1)/my1;
136          }
137        }
138      }
139
[124]140      if (abs(tx2-x1)<abs(x2-x1) || abs(ty2-y1)<abs(y2-y1))
[2]141      {
142        x2=tx2;
143        y2=ty2;
144        return 1;
[124]145      }
[2]146    }
147  }
[124]148  return 0;
[2]149} */
150
151
[16]152int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2,
[124]153              int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2,
[16]154                     int32_t inside)  // which side is inside the polygon? (0 always setback)
[2]155{
156  // the line equations will be put in the form
157  // x(y2-y1)+y(x1-x2)-x1*y2+x2*y1=0
[124]158  //     A        B        C
[2]159
[124]160  int32_t a1,b1,c1,a2,b2,c2,r1,r2;
[2]161
[124]162  a1=y2-y1;
163  b1=x1-x2;
164  c1=-x1*y2+x2*y1;
[2]165
166  if (yp1<yp2 || (yp1==yp2 && xp1>xp2))    // use only increasing functions
[124]167  {
[2]168    r1=yp1; yp1=yp2; yp2=r1;        // swap endpoints if wrong order
169    r1=xp1; xp1=xp2; xp2=r1;
[124]170  }
[2]171
[16]172  int32_t xdiff,ydiff;
[124]173/*  int32_t xdiff=abs(xp1-xp2),ydiff=yp1-yp2;
174  if (xdiff>=ydiff)                              // increment the endpoints
[494]175    if (xp2<xp1) { xp2--; xp1++; }
[124]176    else { xp2++; xp1--; }
177
[2]178  if (xdiff<=ydiff)
179  {
[124]180    yp1++;
[2]181    yp2--;
182  } */
183
[124]184
[2]185  r1=xp1*a1+yp1*b1+c1;
186  r2=xp2*a1+yp2*b1+c1;
187
188  if ((r1^r2)<=0 || r1==0 || r2==0)           // signs must be different to intersect
189  {
[124]190    a2=yp2-yp1;
191    b2=xp1-xp2;
192    c2=-xp1*yp2+xp2*yp1;
[2]193    r1=x1*a2+y1*b2+c2;
194    r2=x2*a2+y2*b2+c2;
195
196    if ((r1^r2)<=0 || r1==0 || r2==0)
197    {
[124]198      if ( ((xp1<xp2) && ((r2^inside)>0)) ||
[2]199           (xp1>=xp2 && ((r2^inside)<0)) ||
[124]200       inside==0 || r2==0)
201      {
[494]202    int32_t ae=a1*b2,bd=b1*a2;
[124]203    if (ae!=bd)         // co-linear returns 0
204    {
205      x2=(b1*c2-b2*c1)/(ae-bd);
[494]206      y2=(a1*c2-a2*c1)/(bd-ae);
[124]207      xdiff=abs(x2-x1);
[494]208      ydiff=abs(y2-y1);
[124]209//      if (xdiff<=ydiff)            // push the intersection back one pixel
210//      {
211        if (y2!=y1)
[494]212        {
[124]213            if (y2>y1)
214            y2--;
215          else y2++;
216        }
217//      }
218//      if (xdiff>=ydiff)
219//      {
220        if (x2!=x1)
221        {
222          if (x2>x1)
223            x2--;
224          else x2++;
225        }
226//      }
[2]227
[124]228      if (inside)        // check to make sure end point is on the
229      {                  // right side
[494]230        r1=x1*a2+y1*b2+c2;
231        r2=x2*a2+y2*b2+c2;
[124]232        if ((r2!=0 && ((r1^r2)<0)))
[494]233        {
[124]234          x2=x1;
235          y2=y1;
[494]236        }
237      }
238      return 1;
[124]239    }
[2]240      }
[124]241    }
242  }
[2]243  return 0;
[124]244
[2]245}
246
Note: See TracBrowser for help on using the repository browser.