import torch


def draw_binary_line(x0, y0, x1, y1, imsize=224, width=1):
    """Non-differentiable way to draw a line with no fuzz

    :param x0: int, x coordinate of point 0
    :param y0: int, y coordinate of point 0
    :param x1: int, x coordinate of point 1
    :param y1: int, y coordinate of point 1
    :param imsize: size of image frame
    :param width: width of line
    :return template: torch Tensor of imsize x imsize
    """
    if width % 2 == 0:
        width += 1
    hw = int((width - 1) / 2)

    template = torch.zeros((imsize, imsize))
    dx, dy = x1 - x0, y1 - y0
    is_steep = abs(dy) > abs(dx)

    if is_steep:
        x0, y0 = y0, x0
        x1, y1 = y1, x1

    swapped = False
    if x0 > x1:
        x0, x1 = x1, x0
        y0, y1 = y1, y0
        swapped = True

    dx = x1 - x0
    dy = y1 - y0

    error = int(dx / 2.0)
    ystep = 1 if y0 < y1 else  -1

    y = y0
    for x in range(x0, x1 + 1):
        if is_steep:
            if hw > 0:
                template[max(y-hw, 0):min(y+hw, imsize), 
                         max(x-hw, 0):min(x+hw, imsize)] = 1
            else:
                template[y, x] = 1
        else:
            if hw > 0:
                template[max(x-hw, 0):min(x+hw, imsize), 
                         max(y-hw, 0):min(y+hw, imsize)] = 1
            else:
                template[x, y] = 1
        error -= abs(dy)
        if error < 0:
            y += ystep
            error += dx

    return template