Esempio n. 1
0
void clipline(dcpt winmin,dcpt winmax,wcpt2 p1,wcpt2 p2)
{
	unsigned char encode(wcpt2,dcpt,dcpt);
	unsigned char code1,code2;
	int done = 0 , draw = 0;
	float m;
	void swapcode(unsigned char *c1,unsigned char *c2);
	void swappts(wcpt2 *p1,wcpt2 *p2);

	while(!done)
	{
		code1 = encode(p1,winmin,winmax);
		code2 = encode(p2,winmin,winmax);
		if(ACCEPT(code1,code2))
		{
			draw = 1;
			done = 1;
		}
		else if(REJECT(code1,code2))
			done = 1;
		else if(INSIDE(code1))
		{
				swappts(&p1,&p2);
				swapcode(&code1,&code2);
		}
		if(code1 & LEFT_EDGE)
		{
				p1.y += (winmin.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
				p1.x = winmin.x;
		}
		else if(code1 & RIGHT_EDGE)
		{
				p1.y += (winmax.x - p1.x) *  (p2.y - p1.y) / (p2.x - p1.x);
				p1.x = winmax.x;
		}
		else if(code1 & TOP_EDGE)
		{
			if(p2.x != p1.x)
				p1.x += (winmin.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
				p1.y = winmin.y;
		}
		else if(code1 & BOTTOM_EDGE)
		{
			if(p2.x != p1.x)
				p1.x += (winmax.y - p1.y) *  (p2.x - p1.x) / (p2.y - p1.y);
				p1.y = winmax.y;
		}
    }
    if(draw)
    {
	setcolor(5);
	line(p1.x,p1.y,p2.x,p2.y);
    }
}
Esempio n. 2
0
static int
_clipaaline (SDL_Rect *clip, float x1, float _y1, float x2, float y2,
    float *outpts)
{
    int left = clip->x + 1;
    int right = clip->x + clip->w - 2;
    int top = clip->y + 1;
    int bottom = clip->y + clip->h - 2;
    int code1, code2;
    int draw = 0;
    float swaptmp;
    int intswaptmp;
    float m; /*slope*/

    if (!outpts)
    {
        SDL_SetError ("outpts argument NULL");
        return 0;
    }

    while (1)
    {
        code1 = _fencode (x1, _y1, left, top, right, bottom);
        code2 = _fencode (x2, y2, left, top, right, bottom);
        if (ACCEPT (code1, code2))
        {
            draw = 1;
            break;
        }
        else if (REJECT (code1, code2))
            break;
        else
        {
            if (INSIDE (code1))
            {
                swaptmp = x2;
                x2 = x1;
                x1 = swaptmp;
                swaptmp = y2;
                y2 = _y1;
                _y1 = swaptmp;
                intswaptmp = code2;
                code2 = code1;
                code1 = intswaptmp;
            }
            if (x2 != x1)
                m = (y2 - _y1) / (x2 - x1);
            else
                m = 1.0f;
            if (code1 & LEFT_EDGE)
            {
                _y1 += ((float)left - x1) * m;
                x1 = (float)left;
            }
            else if (code1 & RIGHT_EDGE)
            {
                _y1 += ((float)right - x1) * m;
                x1 = (float)right;
            }
            else if (code1 & BOTTOM_EDGE)
            {
                if (x2 != x1)
                    x1 += ((float)bottom - _y1) / m;
                _y1 = (float)bottom;
            }
            else if (code1 & TOP_EDGE)
            {
                if(x2 != x1)
                    x1 += ((float)top - _y1) / m;
                _y1 = (float)top;
            }
        }
    }
    
    if (draw)
    {
        outpts[0] = x1;
        outpts[1] = _y1;
        outpts[2] = x2;
        outpts[3] = y2;
    }
    return draw;
}