comparison src/video/SDL_rect.c @ 2909:3da0bb421d83

Added line clipping
author Sam Lantinga <slouken@libsdl.org>
date Tue, 23 Dec 2008 02:23:18 +0000
parents 99210400e8b9
children cdb01906cb7e
comparison
equal deleted inserted replaced
2908:aa6ba38c1714 2909:3da0bb421d83
114 Amin = Bmin; 114 Amin = Bmin;
115 result->y = Amin; 115 result->y = Amin;
116 if (Bmax > Amax) 116 if (Bmax > Amax)
117 Amax = Bmax; 117 Amax = Bmax;
118 result->h = Amax - Amin; 118 result->h = Amax - Amin;
119 }
120
121 SDL_bool
122 SDL_IntersectRectAndLine(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
123 {
124 int x1, y1;
125 int x2, y2;
126 int rectx1;
127 int recty1;
128 int rectx2;
129 int recty2;
130
131 if (!rect || !X1 || !Y1 || !X2 || !Y2) {
132 SDL_FALSE;
133 }
134
135 x1 = *X1;
136 y1 = *Y1;
137 x2 = *X2;
138 y2 = *Y2;
139 rectx1 = rect->x;
140 recty1 = rect->y;
141 rectx2 = rect->x + rect->w - 1;
142 recty2 = rect->y + rect->h - 1;
143
144 /* Check to see if entire line is inside rect */
145 if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
146 y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
147 return SDL_TRUE;
148 }
149
150 /* Check to see if entire line is outside rect */
151 if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
152 (y1 < recty1 && y2 < recty2) || (y1 > recty2 && y2 > recty2)) {
153 return SDL_FALSE;
154 }
155
156 if (y1 = y2) {
157 /* Horizontal line, easy to clip */
158 if (x1 < rectx1) {
159 *X1 = rectx1;
160 } else if (x1 > rectx2) {
161 *X1 = rectx2;
162 }
163 if (x2 < rectx1) {
164 *X2 = rectx1;
165 } else if (x2 > rectx2) {
166 *X2 = rectx2;
167 }
168 return SDL_TRUE;
169 }
170
171 if (x1 == x2) {
172 /* Vertical line, easy to clip */
173 if (y1 < recty1) {
174 *Y1 = recty1;
175 } else if (y1 > recty2) {
176 *Y1 = recty2;
177 }
178 if (y2 < recty1) {
179 *Y2 = recty1;
180 } else if (y2 > recty2) {
181 *Y2 = recty2;
182 }
183 return SDL_TRUE;
184 }
185
186 /* FIXME: need code to clip diagonal line to rect */
187 return SDL_FALSE;
119 } 188 }
120 189
121 void 190 void
122 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect) 191 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
123 { 192 {