微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

笛卡尔坐标到重心坐标函数给出意外行为

如何解决笛卡尔坐标到重心坐标函数给出意外行为

我想看看一个点是否在一个三角形内,其中 3 个点在笛卡尔空间中。我使用 bresenham 为您勾勒出坐标,所有该区域都应填充白色像素,并且仅该区域。我使用 tinyrenderer https://github.com/ssloy/tinyrenderer/wiki/Lesson-2:-Triangle-rasterization-and-back-face-culling 作为指导。我把他的东西从他的设置转换成我的。

void computeTriangle(Vec2i a,Vec2i b,Vec2i c,TGaimage &image,TGAColor color) {
    //find bounding Box dimensions
    int maxX = a.x;
    int maxY = a.y;
    int minX = a.x;
    int minY = a.y;
    
    //max coordinate
    if (maxX <= b.x) {
        maxX = b.x;
    }
    if (maxX <= c.x) {
        maxX = c.x;
    }
    
    if (maxY <= b.y) {
        maxY = b.y;
    }
    if (maxY <= c.y) {
        maxY = c.y;
    }
    
    //min coordinate
    if (minX >= b.x) {
        minX = b.x;
    }
    if (minX >= c.x) {
        minX = c.x;
    }
    
    if (minY >= b.y) {
        minY = b.y;
    }
    if (minY >= c.y) {
        minY = c.y;
    }
    
    minX--;
    minY--;
    maxX++;
    maxY++;
    //bound loop
    for (int pX=minX; pX<=maxX; pX++) {
        for (int pY=minY; pY<=maxY; pY++) {
            Vec3f u = cross(Vec3f(c.x - a.x,b.x - a.x,a.x - pX),Vec3f(c.y - a.y,b.y - a.y,a.y - pY));
            Vec3f bcScreen;
            if (std::abs(u.z)<1) {
                bcScreen = Vec3f(-1,1,1);
            } else {
            bcScreen = Vec3f(1.f-(u.x+u.y)/u.z,u.y/u.z,u.x/u.z);
            }
            if (bcScreen.x<0 || bcScreen.y<0 || bcScreen.z<0) {
                image.set(pX,pY,color);
            }
        }
    }
}

Picture of the rendered image

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。