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

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

一.任务描述

1.本关任务

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

2.输入

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

test

3.输出

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

test

二.相关知识

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;
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值