头歌实践教学平台:CG1-v2.0-直线绘制

第4关:直线光栅化-任意斜率的Bresenham画线算法

一.任务描述

1.本关任务

(1)根据直线Bresenham算法补全line函数以绘制白色直线,其中直线斜率为任意情况。 (2)当直线方程恰好经过P(x,y)和T(x,y+1)的中点M时,统一选取直线上方的T点为显示的像素点。

2.输入

代码将自动输入一个OBJ三维人头模型,具体模型如下图:

3.输出

若编写的任意斜率的Bresenham画线算法代码正确,则程序会将模型转换为线条图片,具体结果如下图所示:

二.相关知识

1.绘制点函数

image.set(x, y, color)函数是绘制点的函数,参数包括x、y和color。参数x为绘制点的x坐标,参数y为绘制点的y坐标,参数color为绘制点的颜色。

2.Bresenham算法

Bresenham算法相关知识点,请参考教材与课件或有关资料。

三.操作说明

(1)按要求补全line函数; (2)点击窗口右下角"测评"按钮,等待测评结果,如果通过后可进行下一关任务。


开始你的任务吧,祝你成功!

四、实验代码

复制代码
#include "tgaimage.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "model.h"
#include "geometry.h"

const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);
Model *model = NULL;
const int width = 800;
const int height = 800;

void line(int x0, int y0, int x1, int y1, TGAImage& image, TGAColor color)
{
    // Please add the code here
    /********** Begin ********/
    bool steep = false;
    if (abs(x0 - x1) < abs(y0 - y1))//k>1情况,如果正确就做对称变换
    {
        std::swap(x0, y0);
        std::swap(x1, y1);
        steep = true;
    }
    if (x0 > x1)//如果x0大于x1,就交换坐标
    {
        std::swap(x0, x1);
        std::swap(y0, y1);
    }
    int dx = x1 - x0;
    int dy = abs(y1 - y0);
    int y = y0;
    int d = -dx;
    for (int x = x0; x <= x1; x++)
    {
        if (steep)
            image.set(y, x, color);//k>1情况,如果正确就做对称变换,即x和y交换
        else
            image.set(x, y, color);
        d = d + 2 * dy;
        if (d >= 0)
        {
            y += (y1 > y0 ? 1 : -1);
            d = d - 2 * dx;
        }
    }
    /********** End *********/
}

int main(int argc, char** argv)
{
	model = new Model("african_head.obj");
	TGAImage image(width, height, TGAImage::RGB);
	for (int i = 0; i < model->nfaces(); i++) {
		std::vector<int> face = model->face(i);
		for (int j = 0; j < 3; j++) {
			Vec3f v0 = model->vert(face[j]);
			Vec3f v1 = model->vert(face[(j + 1) % 3]);
			int x0 = (v0.x + 1.)*width / 2.;
			int y0 = (v0.y + 1.)*height / 2.;
			int x1 = (v1.x + 1.)*width / 2.;
			int y1 = (v1.y + 1.)*height / 2.;
			line(x0, y0, x1, y1, image, white);
		}
	}
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_tga_file("../img_step3/test.tga");
	delete model;
	return 0;
}
相关推荐
Mintopia2 小时前
图形学中的数学基础与 JavaScript 实践
前端·javascript·计算机图形学
Mintopia1 天前
向量在图形变化中的应用教学
前端·javascript·计算机图形学
Mintopia2 天前
向量在几何图像学中的应用基础学习
前端·javascript·计算机图形学
Mintopia3 天前
向量基础学习:从概念到 JavaScript 实现
javascript·计算机视觉·计算机图形学
Mintopia4 天前
计算机图形学学习指南
前端·javascript·计算机图形学
yumuing12 天前
告别 “生成废图”!UNO 让你一键掌控多物体图像生成: Less-to-More Generalization
aigc·计算机图形学
Mr.Winter`17 天前
运动规划实战案例 | 基于四叉树分解的路径规划(附ROS C++/Python仿真)
人工智能·机器人·自动驾驶·ros·计算机图形学·ros2·路径规划
一牛19 天前
Metal 进阶:读取可绘制对象的像素
ios·swift·计算机图形学
XZen1 个月前
DeepSeek + 码上掘金 学习shadertoy之 —— Catmull-Rom样条插值
javascript·webgl·计算机图形学
jllws11 个月前
数据类设计_图片类设计之4_规则类图形混合算法(前端架构)
c++·计算机图形学·前端架构·数据类设计