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

问题描述

我想看看一个点是否在一个三角形内,其中 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...