+ // We need to clip the line to the current region before storing it in the drawing
+ // list, since when we come to actually draw it the clip region may have changed.
+
+ // Liang-Barsky clipping algorithm
+ int p[4], q[4];
+ float u1 = 0.0f, u2 = 1.0f;
+ p[0] = -(x2 - x1); q[0] = (x1 - _clipx1);
+ p[1] = (x2 - x1); q[1] = (_clipx2 - x1);
+ p[2] = -(y2 - y1); q[2] = (y1 - _clipy1);
+ p[3] = (y2 - y1); q[3] = (_clipy2 - y1);
+
+ for(int i=0; i<4; ++i) {
+ if(p[i] == 0) {
+ if(q[i] < 0) {
+ // Then we have a trivial rejection of a line parallel to a clip plane
+ // completely outside the clip region.
+ return;
+ }
+ } else if(p[i] < 0) {
+ float r = (float)q[i]/(float)p[i];
+ u1 = (u1 > r ? u1 : r);
+ } else { // p[i] > 0
+ float r = (float)q[i]/(float)p[i];
+ u2 = (u2 < r ? u2 : r);
+ }
+ if(u1 > u2) {
+ // Then the line is completely outside the clip area.
+ return;
+ }
+ }
+
+ float fx1 = x1 + u1 * (float)(x2 - x1);
+ float fy1 = y1 + u1 * (float)(y2 - y1);
+ float fx2 = x1 + u2 * (float)(x2 - x1);
+ float fy2 = y1 + u2 * (float)(y2 - y1);
+ x1 = (int)(fx1 + 0.5);
+ y1 = (int)(fy1 + 0.5);
+ x2 = (int)(fx2 + 0.5);
+ y2 = (int)(fy2 + 0.5);
+
+ RA2DPrimitive prim;
+ prim.x1 = x1;
+ prim.y1 = y1;
+ prim.x2 = x2;
+ prim.y2 = y2;
+ prim.type = RA2D_LINE;
+ prim.invert = false;
+ drawing_list.push_back(prim);