示例#1
0
void fill_t::solid_fill(point_t p,float width,float height){
	//std::cout<<"width  "<<width<<" and height "<<height<<std::endl;
	std::queue <point_t> fillQueue;
	fillQueue.push(p);
	float pointx=p.getX();
	float pointy=p.getY();
	color_t c=colorArray[(int)pointx][(int)pointy];
	
    color_t pixels;
    
    while(!fillQueue.empty()){
    	// std::cout<<c.getR()<<" ANSSS"<<std::endl;
    	// exit(0);
		p=fillQueue.front();
		pointx=p.getX();
		pointy=p.getY();
		fillQueue.pop();
		
		//Added Canvas size check
		if(pointx>=width || pointy>=height || pointx<0 || pointy<0)
			continue;
		pixels = colorArray[(int)pointx][(int)pointy];
		
		if( (pixels.getR()==c.getR()) && (pixels.getG()==c.getG()) && (pixels.getB()==c.getB()) )
		{
			
			point_t p1(pointx, pointy);
			p1.draw(pen_t(color1,1));
			fillQueue.push(point_t(pointx+1,pointy));
			fillQueue.push(point_t(pointx,pointy+1));
			fillQueue.push(point_t(pointx-1,pointy));
			fillQueue.push(point_t(pointx,pointy-1));
		}
	}
}
示例#2
0
/** Returns point of intersection between the two line segments.*/
point_t lineSegmentsIntersectCoord(point_t a1, point_t a2,point_t b1,point_t b2) {
	point_t DEFAULT_RETURN = {-1,-1};
	//solve for intersection:
	float m_a;
	float b_a;
	if (a1.getX()!=a2.getX()) {
		m_a = (a1-a2).getSlope();
		b_a = a1.getY()-a1.getX()*m_a;
	}
	float m_b;
	float b_b;
	if (b1.getX()!=b2.getX()) {
		m_b = (b1-b2).getSlope();
		b_b = b1.getY()-b1.getX()*m_b;
	}
	if (a1.getX()==a2.getX()&&b1.getX()==b2.getX())
		return DEFAULT_RETURN;
	if (a1.getX()==a2.getX())
		return {a1.getX(),b_b+m_b*a1.getX()};
	if (b1.getX()==b2.getX())
		return {b1.getX(),b_a+m_a*b1.getX()};

	//overlapping lines do not intersect.
	if (m_a==m_b) return DEFAULT_RETURN;

	float x_intersect = (b_a+b_b)/(m_a-m_b);
	return {x_intersect, b_a + x_intersect*m_a};
}
示例#3
0
void World::queryLine(point_t v1, point_t v2, QueryCallback* qc) {
	if (use_partitioning) {
		//spatial hash version: check every rect in bucket intersecting line
		int min_x = (v1.getX()) / bucket_width;
		int max_x = (v2.getX()) / bucket_width;
		if (min_x > max_x)
			std::swap(min_x, max_x);
		int min_y = (v1.getY()) / bucket_height;
		int max_y = (v2.getY()) / bucket_height;
		if (min_y > max_y)
			std::swap(min_y, max_y);

		//todo: iterates over full box containing line; just iterate over any bucket intersecting.
		//iterate over all buckets intersecting line:
		for (int i = min_x; i <= max_x; i++)
			for (int j = min_y; j <= max_y; j++)
				if (bucket* b = getBucket(i, j))
					//iterate over all rects in bucket:
					for (auto iter : b->rect_v)
						//check if rect intersects the line:
						if (iter->queryOnLine(v1, v2))
							//halt if QueryCallback returns false.
							if (!qc->onMatch(iter,
									iter->getContactPointOnLine(v1, v2)))
								return;
	} else {
		//non spatial hash version: check every rect
		for (auto iter : v_rect) {
			if (iter->queryOnLine(v1, v2))
				if (!qc->onMatch(iter, iter->getContactPointOnLine(v1, v2)))
					return;
		}
	}
}
示例#4
0
/** Returns true if intersection between the two line segments.*/
bool lineSegmentsIntersect(point_t a1, point_t a2,point_t b1,point_t b2) {
	//if lines vertical:
	if (a1.getX()==a2.getX())
		if ((a1.getX()>b1.getX()&&a1.getX()<b2.getX())
				||(a1.getX()>b2.getX()&&a1.getX()<b1.getX())) {
			float m_b = (b1-b2).getSlope();
			float b_b = b1.getY()-b1.getX()*m_b;
			float y_intersect = b_b + m_b*a1.getX();
			return ((y_intersect>a1.getY())!=(y_intersect>a2.getY()));
		}
	if (b1.getX()==b2.getX())
		if ((b1.getX()>a1.getX()&&b1.getX()<a2.getX())
				||(b1.getX()>a2.getX()&&b1.getX()<a1.getX())) {
			float m_a = (a1-a2).getSlope();
			float b_a = a1.getY()-a1.getX()*m_a;
			float y_intersect = b_a + m_a*b1.getX();
			return ((y_intersect>b1.getY())!=(y_intersect>b2.getY()));
		}
	//solve for intersection:
	float m_a = (a1-a2).getSlope();
	float m_b = (b1-b2).getSlope();
	float b_a = a1.getY()-a1.getX()*m_a;
	float b_b = b1.getY()-b1.getX()*m_b;

	//overlapping lines do not intersect.
	if (m_a==m_b) return false;

	float x_intersect = (b_a+b_b)/(m_a-m_b);
	return ((x_intersect>a1.getX()&&x_intersect<a2.getX())||(x_intersect>a2.getX()&&x_intersect<a1.getX())) &&
			((x_intersect>b1.getX()&&x_intersect<b2.getX())||(x_intersect>b2.getX()&&x_intersect<b1.getX()));
}
示例#5
0
void fill_t::find_min_max(point_t p,int& YMin, int& YMax, int& XMin, int& XMax,float width,float height){
	//std::cout<<"Min max\n";
	std::queue <point_t> fmmQueue;
	fmmQueue.push(p);
	float pointx = p.getX();
	float pointy = p.getY();
	
	color_t c=colorArray[(int)pointx][(int)pointy];
    color_t pixels;
    
     while(!fmmQueue.empty()){
		p=fmmQueue.front();
		pointx=p.getX();
		pointy=p.getY();
		fmmQueue.pop();
		//Added Canvas size check
		if(pointx>=width || pointy>=height || pointx<0 || pointy<0)
			continue;
		
		pixels = colorArray[(int)pointx][(int)pointy];
		//glReadPixels(pointx,pointy,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
		
		if( (pixels.getR()==c.getR()) && (pixels.getG()==c.getG()) && (pixels.getB()==c.getB()) )
		{
			if(pointy<YMin){
				YMin=pointy;
			}
			if(pointy>YMax){
				YMax=pointy;
			}
			
			if(pointx<XMin){
				XMin=pointx;
			}
			if(pointx>XMax){
				XMax=pointx;
			}
			p.draw(pen_t(color_t(0.5,0.5,0.5),1));
			//std::cout<<" let me pass!!! I am the point "<<pointx<<" and "<<pointy<<std::endl;
			fmmQueue.push(point_t(pointx+1,pointy));
			fmmQueue.push(point_t(pointx,pointy+1));
			fmmQueue.push(point_t(pointx-1,pointy));
			fmmQueue.push(point_t(pointx,pointy-1));
			// fmmQueue.push(point_t(pointx+1,pointy+1));
			// fmmQueue.push(point_t(pointx-1,pointy-1));
			// fmmQueue.push(point_t(pointx+1,pointy-1));
			// fmmQueue.push(point_t(pointx-1,pointy+1));
		}
	}
}
示例#6
0
bool Rect::queryOnLine(point_t v1, point_t v2) {
	//checks each edge of rect to see if line intersects
	return (lineSegmentsIntersect({x,y},{x+w,y},v1,v2) ||
			lineSegmentsIntersect({x,y},{x,y+h},v1,v2) ||
			lineSegmentsIntersect({x,y+h},{x+w,y+h},v1,v2) ||
			lineSegmentsIntersect({x+w,y},{x+w,y+h},v1,v2) ||

			//checks if line contained within rect
			queryContains(v1.getX(),v1.getY()));
}
示例#7
0
chassis_id PhysicsRegion::queryFirstPoint(point_t point,
		flag_plane p) {
	struct : public rects::QueryCallback {
		const PhysicsRegion* parent;
		chassis_id ret=NO_CHASSIS;
		bool onMatch(rects::Rect* r, point_t at) {
			ret = parent->getChassisFromRect(r);
			return false;
		}
	} qc;
	qc.parent = this;
	for (byte i = 0; i < getPlaneCount(); i++) {
		flag_plane p_it = 1 << i;
		if (p_it & p)
			planes[i].queryPoint(point.getX(), point.getY(), &qc);
		if (qc.ret!=NO_CHASSIS)
			return qc.ret;
	}
	return qc.ret;
}
示例#8
0
void fill_t::printPointColor(point_t p){
	std::cout<<"COLOR "<<colorArray[(int)p.getX()][(int)p.getY()].getR()<<std::endl;
}
示例#9
0
void fill_t::checkerboard_fill(point_t p,float width,float height){
	int minx=p.getX();
	int miny=p.getY();
	int maxx=p.getX();
	int maxy=p.getY();
	find_min_max(p,miny,maxy,minx,maxx,width,height);

	std::queue <point_t> fillQueue;
	fillQueue.push(p);
	float pointx=p.getX();
	float pointy=p.getY();
	color_t c=colorArray[(int)pointx][(int)pointy];
	
    color_t pixels;
    float trans_x,trans_y;
    while(!fillQueue.empty()){
    	// std::cout<<c.getR()<<" ANSSS"<<std::endl;
    	// exit(0);
		p=fillQueue.front();
		pointx=p.getX();
		pointy=p.getY();
		fillQueue.pop();
		
		//Added Canvas size check
		if(pointx>=width || pointy>=height || pointx<0 || pointy<0)
			continue;
		//glReadPixels(pointx,pointy,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
		pixels = colorArray[(int)pointx][(int)pointy];
		
		if( (pixels.getR()==c.getR()) && (pixels.getG()==c.getG()) && (pixels.getB()==c.getB()) )
		{
			point_t p1(pointx, pointy);
			trans_x=pointx-minx;
			trans_y=pointy-miny;
			if((((int)trans_x/16)%2)==0){
				if((((int)trans_y/16)%2)==0){
					p1.draw(pen_t(color1,1));
				}
				else{
					p1.draw(pen_t(color2,1));
				}
			}
			else{
				if((((int)trans_y/16)%2)==0){
					p1.draw(pen_t(color2,1));
				}
				else{
					p1.draw(pen_t(color1,1));
				}
			}
			
			
			fillQueue.push(point_t(pointx+1,pointy));
			fillQueue.push(point_t(pointx,pointy+1));
			fillQueue.push(point_t(pointx-1,pointy));
			fillQueue.push(point_t(pointx,pointy-1));
		}
		
	}
	
}