?第1關(guān):直線光柵化-DDA畫線算法
任務(wù)描述
1.本關(guān)任務(wù)
(1)根據(jù)直線DDA算法補(bǔ)全line函數(shù),其中直線斜率0<k<1; (2)當(dāng)直線方程恰好經(jīng)過P(x,y)和T(x,y+1)的中點(diǎn)M時(shí),統(tǒng)一選取直線上方的T點(diǎn)為顯示的像素點(diǎn)。
2.輸入
(1)直線兩端點(diǎn)坐標(biāo):(13, 20)和(180,140); (2)直線顏色為白色。
3.輸出
程序運(yùn)行結(jié)果為一條直線,具體結(jié)果如下圖所示:
整體代碼如下:
#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;
}
?為防止一些劉亦菲和吳彥祖?zhèn)兛床怀鰜?lái),那小Mo就把更加簡(jiǎn)潔的代碼放到下面啦
(有木有很貼心,還不給小Mo點(diǎn)個(gè)關(guān)注鼓勵(lì)一下??)
(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 *********/文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-737748.html
第2關(guān):直線光柵化-中點(diǎn)畫線算法
任務(wù)描述
1.本關(guān)任務(wù)
(1)根據(jù)直線中點(diǎn)畫線算法補(bǔ)全line函數(shù),其中直線斜率0<k<1,并將main函數(shù)中的line函數(shù)參數(shù)補(bǔ)充完整; (2)當(dāng)直線方程恰好經(jīng)過P(x,y)和T(x,y+1)的中點(diǎn)M時(shí),統(tǒng)一選取直線上方的T點(diǎn)為顯示的像素點(diǎn)。
2.輸入
(1)直線兩端點(diǎn)坐標(biāo):(100, 100)和(520,300); (2)直線顏色為紅色。
3.輸出
程序運(yùn)行結(jié)果為一條直線,具體結(jié)果如下圖所示
整體代碼如下:
#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出來(lái)嘍~
代碼段一:
?/********** 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;
? ? ? ? }文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-737748.html
? ? ? ? image.set(x,y,color);
? ? ? ? }
? ? /********** End *********/
代碼段二:
? ? /********** Begin ********/
? ? line( 100, 100 , 520 , 300 , image, red );
? ? /********** End *********/
?第3關(guān):直線光柵化-Bresenham畫線算法
任務(wù)描述:
1.本關(guān)任務(wù)
(1)根據(jù)直線Bresenham算法補(bǔ)全line函數(shù),其中直線斜率0<k<1,并將main函數(shù)中的line函數(shù)參數(shù)補(bǔ)充完整; (2)當(dāng)直線方程恰好經(jīng)過P(x,y)和T(x,y+1)的中點(diǎn)M時(shí),統(tǒng)一選取直線上方的T點(diǎn)為顯示的像素點(diǎn)。
2.輸入
(1)直線兩端點(diǎn)坐標(biāo):(20, 20)和(180,140); (2)直線顏色為白色。
3.輸出
程序運(yùn)行結(jié)果為一條直線,具體結(jié)果如下圖所示:
整體代碼如下:
#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還是會(huì)把需要填寫的代碼段po出來(lái)噠~
代碼段一:
? ? /********** 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關(guān):直線光柵化-任意斜率的Bresenham畫線算法
任務(wù)描述:
1.本關(guān)任務(wù)
(1)根據(jù)直線Bresenham算法補(bǔ)全line函數(shù)以繪制白色直線,其中直線斜率為任意情況。 (2)當(dāng)直線方程恰好經(jīng)過P(x,y)和T(x,y+1)的中點(diǎn)M時(shí),統(tǒng)一選取直線上方的T點(diǎn)為顯示的像素點(diǎn)。
2.輸入
代碼將自動(dòng)輸入一個(gè)OBJ三維人頭模型,具體模型如下圖:
3.輸出
若編寫的任意斜率的Bresenham畫線算法代碼正確,則程序會(huì)將模型轉(zhuǎn)換為線條圖片,具體結(jié)果如下圖所示:
整體代碼如下:
#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還是會(huì)把需要填寫的代碼段po出來(lái)噠~
代碼段一:
? ? /********** 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 *********/
到了這里,關(guān)于計(jì)算機(jī)圖形學(xué)頭歌實(shí)訓(xùn)平臺(tái)答案——CG1-v2.0-直線繪制的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!