#include <stdlib.h>
#include <stdio.h>

int in_rng(int x1, int x2, int d)
{
    if(abs(x1 - x2) < d)
        return 1;
    else
        return 0;
}

int in_scp(int y1, int y2, int d)
{
    if(abs(y1 - y2) < d)
        return 1;
    else
        return 0;
}

int bb_col(int x1, int y1, int bx1, int by1, int x2, int y2, int bx2, int by2)
{
    if(x1 - bx1 < x2 + bx2 && x1 + bx1 > x2 - bx2 && 
       y1 - by1 < y2 + by2 && y1 + by1 > y2 - by2)
        return 1;
    else
        return 0;
}

int insect(float x1, float y1, float x2, float y2, 
           float x3, float y3, float x4, float y4)
{
    float ua = 0;
    float ud = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
    if (ud != 0) {
        ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ud;
        float ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ud;
        if (ua <= 0 || ua >= 1 || ub <= 0 || ub >= 1)
            ua = 0;
    }
    if(ua != 0)
        return 1;
    else
        return 0;
}

int ln_col(int x1, int y1, int x2, int y2, int x, int bx, int y, int by)
{
    if(insect(x1, y1, x2, y2, x - bx, y - by, x + bx , y - by) ||
       insect(x1, y1, x2, y2, x - bx, y + by, x + bx , y + by) ||
       insect(x1, y1, x2, y2, x - bx, y - by, x - bx , y + by) ||
       insect(x1, y1, x2, y2, x + bx, y - by, x + bx , y + by)) 
        return 1;
    else
        return 0;
}