扫描线填充法

#include "pngimage.h"
#include<stdio.h>
#include <iostream>
 
struct Vec2i
{
	int x, y;
};
 
void triangle(Vec2i t0, Vec2i t1, Vec2i t2, PNGImage& image, PNGColor color) {
    // Please add your code here
    /********** Begin ********/ 
 
    
    if (t0.y>t1.y) std::swap(t0, t1); 
    if (t0.y>t2.y) std::swap(t0, t2); 
    if (t1.y>t2.y) std::swap(t1, t2);
    int total_height = t2.y-t0.y;
        for (int y=t0.y; y<=t1.y; y++) { 
        int segment_height = t1.y-t0.y+1;    //be careful with divisions by zero
        float alpha = (float)(y-t0.y)/total_height;      //edge t0t2
        float beta  = (float)(y-t0.y)/segment_height;    //edge t0t1 
        Vec2i A,B;
        A.y=B.y=y;
        A.x = t0.x + (t2.x-t0.x)*alpha; 
        B.x = t0.x + (t1.x-t0.x)*beta; 
        if (A.x>B.x) std::swap(A.x, B.x); 
        for (int x=A.x; x<=B.x; x++) { 
            image.set(x, y, color);  
        } 
    }
    for (int y = t1.y; y <= t2.y; y++)//感觉这个就是上半部分。
 
    {
        int segment_height = t2.y-t1.y+1;    
        float alpha2 = (float)(y-t0.y)/total_height;      //edge t0t2
        float beta2  = (float)(y-t1.y)/segment_height;    //edge t0t1 
        Vec2i A2,B2;
        A2.y=B2.y=y;
        A2.x = t0.x + (t2.x-t0.x)*alpha2; 
        B2.x = t1.x - (t1.x-t2.x)*beta2; 
        if (A2.x>B2.x) std::swap(A2.x, B2.x); 
        for (int x=A2.x; x<=B2.x; x++) { 
            image.set(x, y, color);  
        } 
 
 
 
    
    }
}
 
    
 
 
 
 
 
 
    /********** End **********/
 
 
 
int main(int argc, char** argv) {
	PNGColor white = PNGColor(255, 255, 255, 255);
	PNGColor black = PNGColor(0, 0, 0, 255);
	PNGColor red = PNGColor(255, 0, 0, 255);
    PNGColor blue = PNGColor(0, 255, 255, 255);
 
	int width = 400;
	int height = 400;
	PNGImage image(width, height, PNGImage::RGBA); //Error when RGB because lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size() in encode
	image.init(black);
    // Please add your code here
    /********** Begin ********/
    Vec2i t0 = {125 ,50 }, t1 = {300 , 200}, t2 = {200 ,350 };
	triangle( t0,t1 ,t2, image,PNGColor(0, 255, 255, 255)  );
    /********** End **********/
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_png_file("../img_step3/test.png");
	return 0;
}

重心坐标填充法


#include "pngimage.h"
#include<stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct Vec2i
{
	int x, y;
};
 
//Cross product 
float CrossProduct(Vec2i a,Vec2i b)
{
    return a.x * b.y - b.x * a.y;
}
//DotProduct
float DotProduct(float cp1,float cp2)
{
    return cp1*cp2;
}
//Determine if P1 and P2 are on the same side
bool SameSide( Vec2i p,Vec2i a, Vec2i b, Vec2i c )//同侧判断法
{
    // Please add your code here
    /********** Begin ********/
     /*float cp3,cp4;
    
     cp3=CrossProduct(ba,p1-a);
     cp4=CrossProduct(ba,p2-a);
     if (DotProduct(cp3,cp4)>=0)
     return TRUE;*/
 
    Vec2i AB = {b.x - a.x, b.y - a.y};
    Vec2i AC = {c.x - a.x, c.y - a.y};
    Vec2i AP = {p.x - a.x, p.y - a.y};
    float v1 = CrossProduct(AB, AC);
    float v2 = CrossProduct(AB, AP);
    if (DotProduct(v1, v2) >= 0) 
    return 1;
    else return 0;
    
 
 
 
    /********** End **********/
}
 
//Determine the point coordinates are in the triangle
float PointInTriangle(Vec2i p,Vec2i a, Vec2i b, Vec2i c)//判断点是否在三角形内部
{
    // Please add your code here
    /********** Begin ********/
    /*
    if(SameSide(p,a,b,c)&&SameSide(p,b,a,c)&&SameSide(p,c,a,b))
    return 1;*/
 
     return SameSide(a, b, c, p) && SameSide(b, c, a, p) && SameSide(c, a, b, p); 
 
 
    /********** End **********/
}
 
// Please draw point in bounding box
void Triangle(Vec2i t0, Vec2i t1, Vec2i t2, PNGImage& image, PNGColor color)//这个就是绘制三角形
{
    // Please add your code here
    /********** Begin ********/
    int xmax,ymax,x,y;
    Vec2i A;
    xmax=300,ymax=350;
    for(x=0;x<=xmax;x++)
    {
    for(y=0;y<=ymax;y++)
    {
        A.x=x;
        A.y=y;
        if(PointInTriangle(A,t0,t1 ,t2 ))
        image.set(x,y,color);
    }
    }
 
 
 
    /********** End **********/
}
 
 
int main(int argc, char** argv) {
	PNGColor white = PNGColor(255, 255, 255, 255);
	PNGColor black = PNGColor(0, 0, 0, 255);
	PNGColor red = PNGColor(255, 0, 0, 255);
    PNGColor green = PNGColor(0, 255, 0, 255);    
	PNGColor blue = PNGColor(0, 255, 255, 255);
	int width = 400;
	int height = 400;
	PNGImage image(width, height, PNGImage::RGBA); //Error when RGB because lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size() in encode
	image.init(black);
    // Please add your code here
    /********** Begin ********/
    Vec2i t0 = { 50 , 50 }, t1 = {300  ,200  }, t2 = {200  ,350  };
    Triangle(t0 ,t1  , t2 , image, PNGColor(255, 0, 0, 255));
    /********** End **********/
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_png_file("../img_step1/test.png");
	return 0;
}

同侧判断填充法

#include "pngimage.h"
#include<stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct Vec2i
{
	int x, y;
};
 
//Cross product 
float CrossProduct(Vec2i a,Vec2i b)
{
    return a.x * b.y - b.x * a.y;
}
//DotProduct
float DotProduct(float cp1,float cp2)
{
    return cp1*cp2;
}
//Determine if P1 and P2 are on the same side
bool SameSide( Vec2i p,Vec2i a, Vec2i b, Vec2i c )//同侧判断法
{
    // Please add your code here
    /********** Begin ********/
     /*float cp3,cp4;
    
     cp3=CrossProduct(ba,p1-a);
     cp4=CrossProduct(ba,p2-a);
     if (DotProduct(cp3,cp4)>=0)
     return TRUE;*/
 
    Vec2i AB = {b.x - a.x, b.y - a.y};
    Vec2i AC = {c.x - a.x, c.y - a.y};
    Vec2i AP = {p.x - a.x, p.y - a.y};
    float v1 = CrossProduct(AB, AC);
    float v2 = CrossProduct(AB, AP);
    if (DotProduct(v1, v2) >= 0) 
    return 1;
    else return 0;
    
 
 
 
    /********** End **********/
}
 
//Determine the point coordinates are in the triangle
float PointInTriangle(Vec2i p,Vec2i a, Vec2i b, Vec2i c)//判断点是否在三角形内部
{
    // Please add your code here
    /********** Begin ********/
    /*
    if(SameSide(p,a,b,c)&&SameSide(p,b,a,c)&&SameSide(p,c,a,b))
    return 1;*/
 
     return SameSide(a, b, c, p) && SameSide(b, c, a, p) && SameSide(c, a, b, p); 
 
 
    /********** End **********/
}
 
// Please draw point in bounding box
void Triangle(Vec2i t0, Vec2i t1, Vec2i t2, PNGImage& image, PNGColor color)//这个就是绘制三角形
{
    // Please add your code here
    /********** Begin ********/
    int xmax,ymax,x,y;
    Vec2i A;
    xmax=300,ymax=350;
    for(x=0;x<=xmax;x++)
    {
    for(y=0;y<=ymax;y++)
    {
        A.x=x;
        A.y=y;
        if(PointInTriangle(A,t0,t1 ,t2 ))
        image.set(x,y,color);
    }
    }
 
 
 
    /********** End **********/
}
 
 
int main(int argc, char** argv) {
	PNGColor white = PNGColor(255, 255, 255, 255);
	PNGColor black = PNGColor(0, 0, 0, 255);
	PNGColor red = PNGColor(255, 0, 0, 255);
    PNGColor green = PNGColor(0, 255, 0, 255);    
	PNGColor blue = PNGColor(0, 255, 255, 255);
	int width = 400;
	int height = 400;
	PNGImage image(width, height, PNGImage::RGBA); //Error when RGB because lodepng_get_raw_size_lct(w, h, colortype, bitdepth) > in.size() in encode
	image.init(black);
    // Please add your code here
    /********** Begin ********/
    Vec2i t0 = { 50 , 50 }, t1 = {300  ,200  }, t2 = {200  ,350  };
    Triangle(t0 ,t1  , t2 , image, PNGColor(255, 0, 0, 255));
    /********** End **********/
	image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
	image.write_png_file("../img_step1/test.png");
	return 0;
}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐