计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制

这篇具有很好参考价值的文章主要介绍了计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 第1关:直线光栅化-DDA画线算法

任务描述

1.本关任务

(1)根据直线DDA算法补全line函数,其中直线斜率0<k<1; (2)当直线方程恰好经过P(x,y)和T(x,y+1)的中点M时,统一选取直线上方的T点为显示的像素点。

2.输入

(1)直线两端点坐标:(13, 20)和(180,140); (2)直线颜色为白色。

3.输出

程序运行结果为一条直线,具体结果如下图所示:

计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制,计算机图形学头歌实训答案,头歌作业,算法,数据结构,c#,c语言,程序人生

整体代码如下:

#include "tgaimage.h"

const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);

void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color)
{
    // Please add the code here
    /********** Begin ********/
    int x;
    float y, k;
    k = (float)(y1 - y0) / (float)(x1 - x0);
    y = y0;
    for (x = x0;x <= x1;x++)
    {
        image.set(x, int(y + 0.5f),color);
        y = y + k;
    }



    /********** End *********/
}

int main(int argc, char** argv)
{
	TGAImage image(640,480, TGAImage::RGB);
	line(13, 20, 180, 140, image, white);
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_tga_file("../img_step1/test.tga");

	return 0;
}

 为防止一些刘亦菲和吴彦祖们看不出来,那小Mo就把更加简洁的代码放到下面啦

(有木有很贴心,还不给小Mo点个关注鼓励一下🥰)

(begin和end之间的代码):

代码段一:

   /********** Begin ********/

    int x;

    float y, k;

    k = (float)(y1 - y0) / (float)(x1 - x0);

    y = y0;

    for (x = x0;x <= x1;x++)

    {

        image.set(x, int(y + 0.5f),color);

        y = y + k;

    }

    /********** End *********/文章来源地址https://www.toymoban.com/news/detail-737748.html

第2关:直线光栅化-中点画线算法

任务描述

1.本关任务

(1)根据直线中点画线算法补全line函数,其中直线斜率0<k<1,并将main函数中的line函数参数补充完整; (2)当直线方程恰好经过P(x,y)和T(x,y+1)的中点M时,统一选取直线上方的T点为显示的像素点。

2.输入

(1)直线两端点坐标:(100, 100)和(520,300); (2)直线颜色为红色。

3.输出

程序运行结果为一条直线,具体结果如下图所示

计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制,计算机图形学头歌实训答案,头歌作业,算法,数据结构,c#,c语言,程序人生

整体代码如下:

#include "tgaimage.h"

const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);

void line(int x1, int y1, int xn, int yn, TGAImage &image, TGAColor color)
{
    // Please add the code here
    /********** Begin ********/
    int dx,dy,dt,db,d,x,y;
        dx = xn - x1;
        dy = yn - y1;
        d = dx - 2*dy;
        dt = 2*dx - 2*dy;
        db = -2*dy;
        x = x1;y = y1;
        image.set(x,y,color);
        while (x < xn)
        {
            if (d <= 0)
            {    x++;
                y++;
                d += dt;
                }
        else
        {
            x++;
            d += db;
        }
        image.set(x,y,color);
        }




    /********** End *********/
}

int main(int argc, char** argv)
{
	TGAImage image(640,480, TGAImage::RGB);
    // Please add the code here
    /********** Begin ********/
	line( 100, 100 , 520 , 300 , image, red );
    /********** End *********/
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_tga_file("../img_step4/test.tga");
	return 0;
}

按照小Mo惯例,还是把需要填写的代码段po出来喽~

代码段一:

 /********** Begin ********/

    int dx,dy,dt,db,d,x,y;

        dx = xn - x1;

        dy = yn - y1;

        d = dx - 2*dy;

        dt = 2*dx - 2*dy;

        db = -2*dy;

        x = x1;y = y1;

        image.set(x,y,color);

        while (x < xn)

        {

            if (d <= 0)

            {    x++;

                y++;

                d += dt;

                }

        else

        {

            x++;

            d += db;

        }

        image.set(x,y,color);

        }

    /********** End *********/

代码段二:

    /********** Begin ********/

    line( 100, 100 , 520 , 300 , image, red );

    /********** End *********/

 第3关:直线光栅化-Bresenham画线算法

任务描述:

1.本关任务

(1)根据直线Bresenham算法补全line函数,其中直线斜率0<k<1,并将main函数中的line函数参数补充完整; (2)当直线方程恰好经过P(x,y)和T(x,y+1)的中点M时,统一选取直线上方的T点为显示的像素点。

2.输入

(1)直线两端点坐标:(20, 20)和(180,140); (2)直线颜色为白色。

3.输出

程序运行结果为一条直线,具体结果如下图所示:

计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制,计算机图形学头歌实训答案,头歌作业,算法,数据结构,c#,c语言,程序人生

整体代码如下:

#include "tgaimage.h"

const TGAColor white = TGAColor(255, 255, 255, 255);
const TGAColor red = TGAColor(255, 0, 0, 255);

void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color)
{
    // Please add the code here
    /********** Begin ********/
    int dx = x1 - x0;
    int dy = y1 - y0;
    int y = y0;
    int d = -dx;
    for(int x = x0;x <=x1;x++)
    {
        image.set(x,y,color);
        d = d + 2*dy;
        if(d >= 0)
        {
            y++;
            d = d - 2*dx;
        }
    }


    /********** End *********/
}

int main(int argc, char** argv)
{
	TGAImage image(640,480, TGAImage::RGB);
    // Please add the code here
    /********** Begin ********/
	line( 20, 20 , 180 , 140 , image, white  );
    /********** End *********/
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_tga_file("../img_step2/test.tga");
	return 0;
}

同样,小Mo还是会把需要填写的代码段po出来哒~

代码段一:

    /********** Begin ********/

    int dx = x1 - x0;

    int dy = y1 - y0;

    int y = y0;

    int d = -dx;

    for(int x = x0;x <=x1;x++)

    {

        image.set(x,y,color);

        d = d + 2*dy;

        if(d >= 0)

        {

            y++;

            d = d - 2*dx;

        }

    }

    /********** End *********/

代码段二:

    /********** Begin ********/   

   line( 20, 20 , 180 , 140 , image, white  );

    /********** End **********/

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

任务描述:

1.本关任务

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

2.输入

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

计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制,计算机图形学头歌实训答案,头歌作业,算法,数据结构,c#,c语言,程序人生

3.输出

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

计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制,计算机图形学头歌实训答案,头歌作业,算法,数据结构,c#,c语言,程序人生

整体代码如下:

#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))
        {
            std::swap(x0,y0);
            std::swap(x1,y1);
            steep = true;
        }
        if (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);
            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;
}

同样,小Mo还是会把需要填写的代码段po出来哒~

代码段一:

    /********** Begin ********/

    bool steep = false;

        if (abs(x0 - x1) < abs(y0 - y1))

        {

            std::swap(x0,y0);

            std::swap(x1,y1);

            steep = true;

        }

        if (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);

            else

                image.set(x,y,color);

            d = d + 2*dy;

            if(d >= 0)

            {

                y += (y1 > y0 ? 1:-1);

                d = d - 2*dx;

            }

        }

    /********** End *********/

到了这里,关于计算机图形学头歌实训平台答案——CG1-v2.0-直线绘制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 头歌实训-机器学习(逻辑回归)

    1.逻辑回归简述 2.逻辑回归算法详解 3.sklearn逻辑回归 - 手写数字识别 4.逻辑回归案例 - 癌细胞精准识别

    2024年04月13日
    浏览(28)
  • 【头歌实训】PySpark Streaming 入门

    本关任务:使用 Spark Streaming 实现词频统计。 为了完成本关任务,你需要掌握: Spark Streaming 简介; Python 与 Spark Streaming; Python Spark Streaming API; Spark Streaming 初体验(套接字流)。 Spark Streaming 简介 Spark Streaming 是 Spark 的核心组件之一,为 Spark 提供了可拓展、高吞吐、容错的

    2024年02月04日
    浏览(28)
  • 获取头歌实训参考答案(EduCoder)

    头歌EduCoder平台实训答案在此,里面搜集了一些答案,可以查查有没有想看的。 https://edaser.github.io/ 一定 不要直接复制答案 ,建议还是自己做,实在不会做的,参考看完后要独立完成。 在这里可以查询一些实训的答案,后台的数据库记录了几百个实训关卡的答案,实现的方

    2024年02月11日
    浏览(32)
  • 【头歌实训】分布式文件系统 HDFS

    本关任务:使用 Hadoop 命令来操作分布式文件系统。 为了完成本关任务你需要了解的知识有:1. HDFS 的设计,2. HDFS 常用命令。 HDFS的设计 分布式文件系统 客户:帮我保存一下这几天的数据。 程序猿:好嘞,有多大呢? 客户: 1T 。 程序猿:好没问题,买个硬盘就搞定了。

    2024年04月15日
    浏览(45)
  • Educoder_头歌实训_离散数学_图论

    目录 第1关:图的概念 任务描述 相关知识 图的概念 习题参考 第2关:图的表示 任务描述 相关知识 图的表示 编程要求 测试说明 习题参考 第3关:单源最短通路问题 任务描述 相关知识 单源最短通路 Dijkstra算法 编程要求 测试说明 习题参考 本关任务:学习图的基本概念,完

    2024年02月03日
    浏览(33)
  • 数据库原理 头歌实训 数据库常用对象

    任务描述 本关任务:创建计算机系的学生信息的视图 student_cs。 相关知识 行列子集视图是指视图的结果集来源于基本表,没有经过二次计算。 #####创建视图 CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTIO

    2024年02月04日
    浏览(37)
  • 【头歌实训】Spark 完全分布式的安装和部署

    掌握 Standalone 分布式集群搭建。 我们已经掌握了 Spark 单机版安装,那么分布式集群怎么搭建呢? 接下来我们学习 Standalone 分布式集群搭建。 课程视频 如果你需要在本地配置 Spark 完全分布式环境,可以通过查看课程视频来学习。 课程视频《克隆虚拟机与配置网络》 课程视

    2024年02月04日
    浏览(43)
  • 湖南大学python头歌实训 实验2:分支语句(一)

    第二章-Python语言基础-2.1简单计算问题的求解(理科) 第1关:数据输入与输出 编程要求 根据提示,在右侧编辑器补充代码,完成如下程序的编写。 第一题 在屏幕上输出字符串:hi, \\\"how are you\\\" ,I\\\'m fine and you 第二题 从键盘输入两个整数,计算两个数相除的商与余数 假设输入

    2024年04月13日
    浏览(32)
  • 数字逻辑---头歌实训作业---加法器设计(Logisim)

    第1关:半加器设计 如有任何不解或者想要答案代码,可在评论区喊话我哦,希望我的答案对你有帮助,点个关注再走吧,感谢!!! 本关卡最终答案:   任务描述 本关任务:利用在Logisim中的“组合逻辑分析”工具自动生成半加器电路。 相关知识 半加器电路是指对两个输

    2023年04月13日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包