소스 11-9 직선 그리기 알고리즘 구현 함수(IppFeature.cpp)
    void IppDrawLine(IppByteImage& img, IppLineParam line, BYTE c)
    {
        int w = img.GetWidth();
        int h = img.GetHeight();
    
        // (rho, ang) 파라미터를 이용하여 직선의 시작 좌표와 끝 좌표를 계산
    
    
        int x1, y1, x2, y2;
        if ((line.ang >= 0 && line.ang < PI / 4) || (line.ang >= 3 * PI / 4 && line.ang < PI))
        {
            x1 = 0;
            y1 = static_cast<int>(floor(line.rho / cos(line.ang) + 0.5));
            x2 = w - 1;
            y2 = static_cast<int>(floor((line.rho - x2 * sin(line.ang)) / cos(line.ang) + 0.5));
        }
        else
        {
            y1 = 0;
            x1 = static_cast<int>(floor(line.rho / sin(line.ang) + 0.5));
            y2 = h - 1;
            x2 = static_cast<int>(floor((line.rho - y2 * cos(line.ang)) / sin(line.ang) + 0.5));
        }
    
        IppDrawLine(img, x1, y1, x2, y2, c);
    }
    
    void IppDrawLine(IppByteImage& img, int x1, int y1, int x2, int y2, BYTE c)
    {
        int w = img.GetWidth();
        int h = img.GetHeight();
        BYTE** ptr = img.GetPixels2D();
    
        // 브레제남 알고리즘(Bresenham’s Algorithm)에 의한 직선 그리기
    
        int dx, dy, inc_x, inc_y, fraction;
    
        dx = x2 - x1;
        inc_x = (dx > 0) ? 1 : -1;
        dx = abs(dx) << 1;
    
        dy = y2 - y1;
        inc_y = (dy > 0) ? 1 : -1;
        dy = abs(dy) << 1;
    
        if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h)
            ptr[y1][x1] = c;
    
        if (dx >= dy)
        {
            fraction = dy - (dx >> 1);
    
            while (x1 != x2)
            {
                if ((fraction >= 0) && (fraction || (inc_x > 0)))
                {
                    fraction -= dx;
                    y1 += inc_y;
                }
    
                fraction += dy;
                x1 += inc_x;
    
                if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h)
                    ptr[y1][x1] = c;
            }
        }
        else
        {
            fraction = dx - (dy >> 1);
    
            while (y1 != y2)
            {
                if ((fraction >= 0) && (fraction || (inc_y > 0)))
                {
                    fraction -= dy;
                    x1 += inc_x;
                }
    
                fraction += dx;
                y1 += inc_y;
    
                if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h)
                ptr[y1][x1] = c;
            }
        }
    }
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.