bresenham算法

//吓人的鸟
#include <iostream>
#include <vector>
using namespace std;

typedef int32_t int32;
typedef uint32_t uint32;

struct Location {
	int32 x;
	int32 y;
	Location() { x=0, y=0; }
	Location(int32 a, int32 b) { x=a, y=b; }
};

void Bresenham(int32 x1, int32 y1, int32 x2, int32 y2, vector<Location>& locationVec)
{
        bool swapflag = false;
        if (x1 > x2){
                int32 tmpx = x1;
                int32 tmpy = y1;
                x1 = x2;
                y1 = y2;
                x2 = tmpx;
                y2 = tmpy;
                swapflag = true;
        }
        
        int32 dx = x2-x1;
        int32 dy = y2-y1;
        int32 x = x1;
        int32 y = y1;
        int32 sub = (dy<<1)-dx;
        locationVec.push_back(Location(x, y));
        while(x<x2){
                ++x;
                if (sub > 0){
                        sub += (dy<<1) - (dx<<1);
                        ++y;
                }else {
                        sub += (dy<<1);
                }
                locationVec.push_back(Location(x, y));
        }

        if (swapflag){
                uint32 size = locationVec.size();
                for (uint32 i = 0; i < size/2 ; ++i){
                        Location tmp = locationVec[i];
                        locationVec[i] = locationVec[size-i-1];
                        locationVec[size-i-1] = tmp;
                }
        }
}

void CalcShortestDistance(const Location& startPos, const Location& endPos, vector<Location>& locationVec)
{
	if (startPos.x==endPos.x && startPos.y==endPos.y)
		return ;
	
	if (endPos.x == startPos.x){ //x相同
		if (endPos.y > startPos.y){
			for (uint32 i = 0; i < (uint32)(endPos.y-startPos.y); ++i){
				locationVec.push_back(Location(startPos.x, startPos.y+i+1));
			}
		}else{
			for (uint32 i = 0; i < (uint32)(startPos.y-endPos.y); ++i){
				locationVec.push_back(Location(startPos.x, startPos.y-i-1));
			}
		}
		return ;
	}

	float k = (float)(endPos.y-startPos.y)/(endPos.x-startPos.x);

	if (k >= 0 && k <= 1){ //斜率为0~1

		Bresenham(startPos.x,startPos.y,endPos.x,endPos.y,locationVec);

	}else if (k > 1){ //斜率为1~无穷大

		Bresenham(startPos.y,startPos.x,endPos.y,endPos.x,locationVec);
		for (vector<Location>::iterator it = locationVec.begin(); it!=locationVec.end(); ++it){
			int tmp = (*it).x;
			(*it).x = (*it).y;
			(*it).y = tmp;
		}

	}else if (k >= -1 && k < 0){ //斜率为-1~0

		Bresenham(startPos.x,-startPos.y,endPos.x,-endPos.y,locationVec);
		for (vector<Location>::iterator it = locationVec.begin(); it!=locationVec.end(); ++it)
			(*it).y = -(*it).y;

	}else if (k < -1){ //斜率为无穷小~-1

		Bresenham(-startPos.y,startPos.x,-endPos.y,endPos.x,locationVec);
		for (vector<Location>::iterator it = locationVec.begin(); it!=locationVec.end(); ++it){
			int tmp = (*it).x;
			(*it).x = (*it).y;
			(*it).y = tmp;
			(*it).y = -(*it).y;
		}
	}

	locationVec.erase(locationVec.begin());
}

int main(int argc, char** argv)
{
	Location startPos, endPos;
	cout << "x1:";
	cin >> startPos.x;
	cout << "y1:";
	cin >> startPos.y;
        cout << "x2:";
        cin >> endPos.x;
        cout << "y2:";
        cin >> endPos.y;
	vector<Location> posVec;
	CalcShortestDistance(startPos, endPos, posVec);
	cout << "(" << startPos.x << "," << startPos.y << ")" << endl;
	for (vector<Location>::iterator it = posVec.begin(); it != posVec.end(); ++it)
		cout << "(" << (*it).x << "," << (*it).y << ")" << endl;	
}


你可能感兴趣的:(算法,struct,iterator,include,float)