直线光栅化-DDA画线算法
#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 ********/
double dy=y1-y0;
double dx=x1-x0;
double k=dy/dx;
float y=y0;
for(int x=x0;x<=x1;x++){
image.set(x,(int)(y+0.5),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;
}
直线光栅化-中点画线算法
#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 a=y0-y1;
int b=x1-x0;
int d1=2*(a+b);
int d2=2*a;
int d=2*a+b;
int x=x0;
int y=y0;
image.set(x,y,color);
while(x<x1){
if (d<0) {
x++;y++;
d=d+d1;
}else{
x++;d=d+d2;
}
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;
}
直线光栅化-Bresenham画线算法
#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,y,dx,dy,d,d1,d2;
dx=x1-x0;
dy=y1-y0;
d=2*dy-dx;
d1=2*dy;
d2=2*(dy-dx);
x=x0;
y=y0;
image.set(x,y,color);
while(x<x1){
x=x+1;
if(d<0){
d=d+d1;
}else{
y=y+1;
d=d+d2;
}
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(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;
}
直线光栅化-任意斜率的Bresenham画线算法
#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 (std::abs(x0 - x1) < std::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 = std::abs(y1 - y0);
int error = 0;
int y = y0;
int ystep = (y0 < y1) ? 1 : -1;
for (int x = x0; x <= x1; x++) {
if (steep) {
// 根据直线方程经过的中点选择像素点
if (y >= 0 && y < height && x >= 0 && x < width) {
image.set(y, x, color);
}
} else {
// 根据直线方程经过的中点选择像素点
if (x >= 0 && x < width && y >= 0 && y < height) {
image.set(x, y, color);
}
}
error += dy;
if (error * 2 >= dx) {
y += ystep;
error -= 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;
}