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