#16598 Score: 0
CheapMeow
Participant

就像楼上说的,出错在 rasterizer.cpp 的 set_pixel

如果 x 和 y 同时为 0,ind 此时等于 height * width,但是数组范围为 [0, height * width – 1],因此出现了越界

解决方法是不允许 x 和 y 同时为 0,也就是把小于号改成小于等于号


void rst::rasterizer::set_pixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color)
{
    //old index: auto ind = point.y() + point.x() * width;
    if (point.x() <= 0 || point.x() >= width ||
        point.y() <= 0 || point.y() >= height) return;
    // if point.x() == 0 and point.y() == 0
    // then get ind == height * width
    // but size of frame_buf == height * width
    // so available index of frame_buf is [0, height * width - 1]
    // so don't allow point.x() == 0 and point.y() == 0
    auto ind = (height-point.y())*width + point.x();
    frame_buf[ind] = color;
}