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