1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdlib.h> |
---|
16 | |
---|
17 | void pushback(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
18 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2, int xdir, int ydir, int inside) |
---|
19 | { |
---|
20 | |
---|
21 | // determine if the lines are intersecting before we set back |
---|
22 | |
---|
23 | // if (yp1==yp2) |
---|
24 | // { |
---|
25 | // if (inside && y2>=yp1) |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | /* int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
31 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2) |
---|
32 | { |
---|
33 | int32_t mx1,my1,b1,mx2,my2,b2,side1,side2,tx2,ty2; |
---|
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; |
---|
48 | side2=xp2-x2; |
---|
49 | } else |
---|
50 | { |
---|
51 | b1=y1-(my1*x1/mx1); // calculate the y intercept |
---|
52 | side1=yp1-((my1*xp1)/mx1+b1); |
---|
53 | side2=yp2-((my1*xp2)/mx1+b1); |
---|
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 | { |
---|
71 | side1=x1-xp2; |
---|
72 | side2=x2-xp2; |
---|
73 | } else |
---|
74 | { |
---|
75 | b2=yp1-(my2*xp1/mx2); |
---|
76 | side1=y1-((my2*x1)/mx2+b2); |
---|
77 | side2=y2-((my2*x2)/mx2+b2); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | // check two wierd cases where the lines are parallel |
---|
82 | if (mx1==mx2 && mx1==0) |
---|
83 | if (y2<yp1 || y1>yp2) |
---|
84 | side1=side2=1; |
---|
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; |
---|
98 | |
---|
99 | // now find the point of intersection |
---|
100 | if (mx1==0) // if the first line is strait up & down |
---|
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! |
---|
104 | else |
---|
105 | ty2=my2*x2/mx2+b2+yadd; |
---|
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) |
---|
117 | { |
---|
118 | ty2=yp1+yadd; |
---|
119 | tx2=mx1*(ty2-b1)/my1; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | if (abs(mx1)>abs(my1)) |
---|
124 | { |
---|
125 | int32_t ae_bd=my1*mx2-mx1*my2; |
---|
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 | { |
---|
132 | int32_t db_ea=(my2*mx1-mx2*my1); |
---|
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 | |
---|
140 | if (abs(tx2-x1)<abs(x2-x1) || abs(ty2-y1)<abs(y2-y1)) |
---|
141 | { |
---|
142 | x2=tx2; |
---|
143 | y2=ty2; |
---|
144 | return 1; |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | return 0; |
---|
149 | } */ |
---|
150 | |
---|
151 | |
---|
152 | int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
153 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2, |
---|
154 | int32_t inside) // which side is inside the polygon? (0 always setback) |
---|
155 | { |
---|
156 | // the line equations will be put in the form |
---|
157 | // x(y2-y1)+y(x1-x2)-x1*y2+x2*y1=0 |
---|
158 | // A B C |
---|
159 | |
---|
160 | int32_t a1,b1,c1,a2,b2,c2,r1,r2; |
---|
161 | |
---|
162 | a1=y2-y1; |
---|
163 | b1=x1-x2; |
---|
164 | c1=-x1*y2+x2*y1; |
---|
165 | |
---|
166 | if (yp1<yp2 || (yp1==yp2 && xp1>xp2)) // use only increasing functions |
---|
167 | { |
---|
168 | r1=yp1; yp1=yp2; yp2=r1; // swap endpoints if wrong order |
---|
169 | r1=xp1; xp1=xp2; xp2=r1; |
---|
170 | } |
---|
171 | |
---|
172 | int32_t xdiff,ydiff; |
---|
173 | /* int32_t xdiff=abs(xp1-xp2),ydiff=yp1-yp2; |
---|
174 | if (xdiff>=ydiff) // increment the endpoints |
---|
175 | if (xp2<xp1) { xp2--; xp1++; } |
---|
176 | else { xp2++; xp1--; } |
---|
177 | |
---|
178 | if (xdiff<=ydiff) |
---|
179 | { |
---|
180 | yp1++; |
---|
181 | yp2--; |
---|
182 | } */ |
---|
183 | |
---|
184 | |
---|
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 | { |
---|
190 | a2=yp2-yp1; |
---|
191 | b2=xp1-xp2; |
---|
192 | c2=-xp1*yp2+xp2*yp1; |
---|
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 | { |
---|
198 | if ( ((xp1<xp2) && ((r2^inside)>0)) || |
---|
199 | (xp1>=xp2 && ((r2^inside)<0)) || |
---|
200 | inside==0 || r2==0) |
---|
201 | { |
---|
202 | int32_t ae=a1*b2,bd=b1*a2; |
---|
203 | if (ae!=bd) // co-linear returns 0 |
---|
204 | { |
---|
205 | x2=(b1*c2-b2*c1)/(ae-bd); |
---|
206 | y2=(a1*c2-a2*c1)/(bd-ae); |
---|
207 | xdiff=abs(x2-x1); |
---|
208 | ydiff=abs(y2-y1); |
---|
209 | // if (xdiff<=ydiff) // push the intersection back one pixel |
---|
210 | // { |
---|
211 | if (y2!=y1) |
---|
212 | { |
---|
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 | // } |
---|
227 | |
---|
228 | if (inside) // check to make sure end point is on the |
---|
229 | { // right side |
---|
230 | r1=x1*a2+y1*b2+c2; |
---|
231 | r2=x2*a2+y2*b2+c2; |
---|
232 | if ((r2!=0 && ((r1^r2)<0))) |
---|
233 | { |
---|
234 | x2=x1; |
---|
235 | y2=y1; |
---|
236 | } |
---|
237 | } |
---|
238 | return 1; |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | return 0; |
---|
244 | |
---|
245 | } |
---|
246 | |
---|