Untitled

 avatar
unknown
plain_text
3 years ago
5.7 kB
3
Indexable
#include <bits/stdc++.h> // header in local directory
#include <iostream> // header in standard library
#include <fstream>
#include <cmath>
#define pi (2*acos(0.0))
#include "bitmap_image.hpp"
using namespace std;






class line
{
public:
    int x1, x2, y1, y2;
    double color[3];
};

int width, height;
int line_number;
line *lines;

bool check_x_limit(int x)
{
    if(x < 0 || x >= width) return false;
    return true;
}

bool check_y_limit(int y)
{
    if(y < 1 || y >= height) return false;
    return true;
}

void load_file()
{
    ifstream in("input.txt");

    in >> width >> height;
    in >> line_number;

    lines = new line[line_number];

    for(int i = 0 ; i < line_number ; i++)
    {
        in >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
        if(!check_x_limit(lines[i].x1) || !check_x_limit(lines[i].x2)
                || !check_y_limit(lines[i].y1) || !check_y_limit(lines[i].y2) )
        {
            cout << "Value of line is invalid" << endl;
            exit(0);
        }
        in >> lines[i].color[0] >> lines[i].color[1] >> lines[i].color[2];
    }

    for(int i=0; i< line_number; i++)
    {
        cout << lines[i].x1 << " " << lines[i].y1 << " " << lines[i].x2 << " " << lines[i].y2 << endl;
        cout << lines[i].color[0] << " " << lines[i].color[1] << " " << lines[i].color[2] << endl;
        cout << endl;
    }

}

void find_midpoint(line l,int &d, int &incrE, int &incrNE, int dx, int dy)
{
    double m = ( ( l.y2 - l.y1 ) * 1.0 ) / ( ( l.x2 - l.x1 ) * 1.0 );

    if(m <= -1)
    {
        d = dy + 2 * dx;
        incrE = 2 * dx;
        incrNE = 2 * ( dx + dy );
    }
    else if(m > -1 && m <= 0)
    {
        d = 2 * dy + dx;
        incrE = 2 * ( dy + dx );
        incrNE = 2 * dy;
    }

    else if(m > 0 && m <= 1) //ok
    {
        d = 2 * dy - dx;
        incrE = 2 * dy;
        incrNE = 2 * ( dy - dx );
    }
    else if(m > 1)
    {
        d = dy - dx * 2;
        incrE = 2 * ( dy - dx );
        incrNE = - 2 * dx;
    }
}

void IntensifyPixel(int x, int y, line l, double distance, bitmap_image *image)
{
    double intensity =  round ( abs ( distance ) );
    cout << intensity << endl;
    image->set_pixel(x, height-y, intensity * l.color[0], intensity * l.color[1], intensity * l.color[2]);
}


void midpoint_line_algorithm(line l,bitmap_image *image, bitmap_image *image2)
{
    int dx= l.x2-l.x1;
    int dy= l.y2-l.y1;
    int d, incrE, incrNE;
    int x = l.x1;
    int y = l.y1;

    find_midpoint(l, d, incrE, incrNE, dx, dy);
    image->set_pixel(x,height - y,l.color[0], l.color[1], l.color[2]);

    // code added for antialiasing
    int two_v_dx = 0;
    double invDenom = 1.0/(2.0 * sqrt( dx * dx + dy * dy ) );
    double two_dx_invDemon = 2.0 * dx * invDenom ;

    // code added for antialiasing
    IntensifyPixel(x,y, l, 0, image2);                       // start pixel
    IntensifyPixel(x,y+1, l, two_dx_invDemon, image2);       // neighbor above
    IntensifyPixel(x,y-1,l, two_dx_invDemon, image2);       // neighbor below
    //

    double m = ( ( l.y2 - l.y1 ) * 1.0 ) / ( ( l.x2 - l.x1 ) * 1.0 );
    cout << m << endl;

//    if( m > 1 || m < -1 )
//    {
    while(true)
    {
        if( abs(m) > 1 )
        {
            // m < -1 or m > 1
            if( ( m > 0 ) && ( y > l.y2) )
            {
                // m > 1
                break;
            }
            else if( ( m < 0 ) && ( y < l.y2) )
            {
                // m < -1
                // cout << "break" << endl;
                break;
            }
        }
        else
        {
            // m is in range of -1 to 1, decimal point
            if( x > l.x2 )
            {
                break;
            }
        }


        if(d<=0)
        {
            // code added for antialiasing
            two_v_dx = d + dx;

            d+=incrE;
            if( m > 1 )
            {
                // m > 1
                x++;
            }
            else if( ( m > -1 ) && ( m < 0 ) )
            {
                y--;
            }
        }
        else
        {
            // code added for antialiasing
            two_v_dx = d - dx;

            d+=incrNE;
            if( m < -1 )
            {
                x++;
            }
            else if( ( m > 0 ) && ( m < 1 ) )
            {
                y++;
            }

        }
        if( abs(m) < 1 )
        {
            x++;
        }
        else if( m > 0 )
        {
            y++;
        }
        else
        {
            y--;
        }


        // cout << x << " " << y << endl;
        image->set_pixel(x,height-y,l.color[0],l.color[1],l.color[2]);

        // code added for antialiasing
        IntensifyPixel(x,y, l, two_v_dx*invDenom, image2);
        IntensifyPixel(x,y+1,l, two_dx_invDemon - two_v_dx*invDenom, image2);
        IntensifyPixel(x,y-1, l, two_dx_invDemon + two_v_dx*invDenom, image2);
        //

    }

}

void create_midpoint_image()
{
    bitmap_image img(width, height);
    bitmap_image img2(width, height);

    for(int i = 0; i < width; i++)
    {
        for(int j = 0; j < height ; j++)
        {
            img.set_pixel(i,j,255,255,255);
            img2.set_pixel(i, j, 255, 255, 255);
        }
    }

    for(int i=0; i< line_number; i++)
    {
        midpoint_line_algorithm(lines[i],&img, &img2);
    }


    img.save_image("1_R.bmp");
    img2.save_image("2_RUA.bmp");
}



int main()
{
    load_file();
    create_midpoint_image();

}
Editor is loading...