src/video/SDL_rect.c
 changeset 2997 e4f025078c1c parent 2994 7563b99e9a49 child 3004 f3d7226a8dfd
equal inserted replaced
2996:972a69e47cd9 2997:e4f025078c1c
182             *Y2 = recty2;
182             *Y2 = recty2;
183         }
183         }
184         return SDL_TRUE;
184         return SDL_TRUE;
185     }
185     }
186
186
187     else
187     else {
188     {
188         /* The task of clipping a line with finite slope ratios in a fixed-
189     /* The task of clipping a line with finite slope ratios in a fixed-
189          * precision coordinate space is not as immediately simple as it is
190      * precision coordinate space is not as immediately simple as it is
190          * with coordinates of arbitrary precision. If the ratio of slopes
191      * with coordinates of arbitrary precision. If the ratio of slopes
191          * between the input line segment and the result line segment is not
192      * between the input line segment and the result line segment is not
192          * a whole number, you have in fact *moved* the line segment a bit,
193      * a whole number, you have in fact *moved* the line segment a bit,
193          * and there can be no avoiding it without more precision
194      * and there can be no avoiding it without more precision
194          */
195      */
195         int *x_result_[] = { X1, X2, NULL }, **x_result = x_result_;
196         int *x_result_[] = {X1, X2, NULL}, **x_result = x_result_;
196         int *y_result_[] = { Y1, Y2, NULL }, **y_result = y_result_;
197         int *y_result_[] = {Y1, Y2, NULL}, **y_result = y_result_;

198         SDL_bool intersection = SDL_FALSE;
197         SDL_bool intersection = SDL_FALSE;
199         double b, m, left, right, bottom, top;
198         double b, m, left, right, bottom, top;
200         int xl, xh, yl, yh;
199         int xl, xh, yl, yh;
201
200
202         /* solve mx+b line formula */
201         /* solve mx+b line formula */
203         m = (double)(y1-y2) / (double)(x1-x2);
202         m = (double) (y1 - y2) / (double) (x1 - x2);
204         b = y2 - m * (double) x2;
203         b = y2 - m * (double) x2;
205
204
206         /* find some linear intersections */
205         /* find some linear intersections */
207         left = (m * (double) rectx1) + b;
206         left = (m * (double) rectx1) + b;
208         right = (m * (double) rectx2) + b;
207         right = (m * (double) rectx2) + b;
230         /* check for a point that's entirely inside the rect */
229         /* check for a point that's entirely inside the rect */
231         if (RISING(rectx1, x1, rectx2) && RISING(recty1, y1, recty2)) {
230         if (RISING(rectx1, x1, rectx2) && RISING(recty1, y1, recty2)) {
232             x_result++;
231             x_result++;
233             y_result++;
232             y_result++;
234             intersection = SDL_TRUE;
233             intersection = SDL_TRUE;
235         } else /* it was determined earlier that *both* end-points are not contained */
234         } else
236
235             /* it was determined earlier that *both* end-points are not contained */
237         if (RISING(rectx1, x2, rectx2) && RISING(recty1, y2, recty2)) {
236         if (RISING(rectx1, x2, rectx2) && RISING(recty1, y2, recty2)) {
238             **(x_result++) = x2;
237             **(x_result++) = x2;
239             **(y_result++) = y2;
238             **(y_result++) = y2;
240             intersection = SDL_TRUE;
239             intersection = SDL_TRUE;
241         }
240         }
244             **(x_result++) = rectx1;
243             **(x_result++) = rectx1;
245             **(y_result++) = (int) left;
244             **(y_result++) = (int) left;
246             intersection = SDL_TRUE;
245             intersection = SDL_TRUE;
247         }
246         }
248
247
249         if (*x_result == NULL) return intersection;
248         if (*x_result == NULL)

249             return intersection;
250         if (RISING(recty1, right, recty2) && RISING(xl, rectx2, xh)) {
250         if (RISING(recty1, right, recty2) && RISING(xl, rectx2, xh)) {
251             **(x_result++) = rectx2;
251             **(x_result++) = rectx2;
252             **(y_result++) = (int) right;
252             **(y_result++) = (int) right;
253             intersection = SDL_TRUE;
253             intersection = SDL_TRUE;
254         }
254         }
255
255
256         if (*x_result == NULL) return intersection;
256         if (*x_result == NULL)

257             return intersection;
257         if (RISING(rectx1, top, rectx2) && RISING(yl, recty1, yh)) {
258         if (RISING(rectx1, top, rectx2) && RISING(yl, recty1, yh)) {
258             **(x_result++) = (int) top;
259             **(x_result++) = (int) top;
259             **(y_result++) = recty1;
260             **(y_result++) = recty1;
260             intersection = SDL_TRUE;
261             intersection = SDL_TRUE;
261         }
262         }
262
263
263         if (*x_result == NULL) return intersection;
264         if (*x_result == NULL)

265             return intersection;
264         if (RISING(rectx1, bottom, rectx2) && RISING(yl, recty2, yh)) {
266         if (RISING(rectx1, bottom, rectx2) && RISING(yl, recty2, yh)) {
265             **(x_result++) = (int) bottom;
267             **(x_result++) = (int) bottom;
266             **(y_result++) = recty2;
268             **(y_result++) = recty2;
267             intersection = SDL_TRUE;
269             intersection = SDL_TRUE;
268         }
270         }