Primitives Declaration for my functions

August 5, 2008

int round(float x){
return ((int)(x + 0.5));
}

//—————————————————————————–
// Name:
// Desc:
//—————————————————————————–
Color setColor(float red, float green, float blue){
Color col;
col.red = red; col.green = green; col.blue=blue;

return col;
}

//—————————————————————————–
// Name:
// Desc:
//—————————————————————————–
Point setPoint(int x, int y){
Point p;
p.x = x; p.y = y;

return p;
}

//—————————————————————————–
// Name: setPixel
// Desc:
//—————————————————————————–
void setPixel(int x, int y, Color c){
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(c.red, c.green, c.blue);

glBegin(GL_POINTS);
glVertex3f(x,y,0);
glEnd();
glPopAttrib();
}

//—————————————————————————–
// Name:
// Desc:
//—————————————————————————–
Color getPixel(int x, int y){ // gets the color of the pixel at (x,y)
Color c;
float color[4];

glReadPixels(x,y,1,1,GL_RGBA, GL_FLOAT, color);
c.red = color[0]; c.green = color[1]; c.blue = color[2];

return c;
}

//—————————————————————————–
// Name: outputText()
// Desc: x,y, the string
// Source: http://www.lighthouse3d.com/opengl/glut/
//—————————————————————————–
void outputText(float x, float y, char *string){
char *c;

glRasterPos3f(x,-y,0);

for (c=string; *c != ”; c++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);

}

//—————————————————————————–
// Name: floatToStr()
// Desc: accepts a floating point number and returns a string
//—————————————————————————–
char * floatToStr(float x){
char *str = new char[7];
sprintf(str, “%4.2f”, x);
return str;
}


Draw any kind of bezier

August 5, 2008

//calculate the permutations
double nCr(int n,int r)
{
double nf;
double rf;
double nrf;
double ncr;

nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));

ncr=(nf/(rf*nrf));

return ncr;
}
double factorial(int number)
{
double factorial=1;

if(number==0 || number==1);

else
{
for(int count=1;count<=number;count++)
factorial=factorial*count;
}

return factorial;
}

void anybezier(const int n,Point cp[],Color thisColor)//note :n -degree of curve //can draw of any order// const int *cp
{

for(int count=0;count<n;count++)
bresline(cp[count],cp[count+1],thisColor);
double x;
double y;

for(float u=0.0005;u<=1;u+=0.0005)
{
x=0;
y=0;

for(int k=0;k<=n;k++)
{
x+=(cp[k].x * nCr(n,k) * pow(u,k) * powl((1-u),(n-k)));
y+=(cp[k+1].y * nCr(n,k) * pow(u,k) * powl((1-u),(n-k)));
}
setPixel((int)(x+0.5),(int)(y+0.5),thisColor);
}
}


Drawing a polygon

August 5, 2008
void Polygon(int n,Point coord[],Color thisColor)
    {
       if(n>=2)
	  {
		  bresline(coord[0],coord[1],thisColor);

	     for(int count=1;count<(n-1);count++)
	        if (count=2)
	  {
		  bresline(coord[0],coord[1],thisColor);

	     for(int count=1;count<(n-1);count++)
	        if (count<3)
			 bresline(coord[count],coord[count+1],thisColor);
	  }
      bresline(coord[3],coord[0],thisColor);
	
 }

Drawing Bezier curves etc

August 5, 2008
void drawdots(Point m,Color thisColor){
 
 setPixel(m.x,m.y, thisColor);

 }

// Calculate the next bezier point.
Point drawBezier(Point A, Point B, Point C, double t) {
	Point P;

	P.x = pow((1 - t), 2) * A.x + 2 * t * (1 -t) * B.x + pow(t, 2) * C.x;
	P.y = pow((1 - t), 2) * A.y + 2 * t * (1 -t) * B.y + pow(t, 2) * C.y;

	return P;
}


void drawbeziercurve(Point abc[],Color thisColor,int &NUMPOINTS,bool &click,Color pt){


	// Draw the red  dot.
	
	drawdots(abc[0],pt);

	// If 3 points are drawn do the curve.

		bresline(abc[0],abc[1],thisColor); //draw lines
		bresline(abc[1],abc[2],thisColor); //draw lines
		
		Point POld = abc[0];//hold previous value
		
		for(double t = 0.0;t <= 1.0; t += 0.1) {
			Point P = drawBezier(abc[0], abc[1], abc[2], t);
			bresline(POld,P,thisColor);
			POld = P;
		}
		
  }

Midpoint Elipse algo

August 5, 2008
void midpoint_ellipse(Point center,int a/*rx*/,int b/*ry*/,Color thisColor)
    {
       float aa=(a*a);
       float bb=(b*b);
       float aa2=(aa*2);
       float bb2=(bb*2);

       float x=0;
       float y=b;

       float fx=0;
       float fy=(aa2*b);

       float p=(int)(bb-(aa*b)+(0.25*aa)+0.5);

       setPixel((center.x+x),(center.y+y),thisColor);
       setPixel((center.x+x),(center.y-y),thisColor);
       setPixel((center.x-x),(center.y-y),thisColor);
       setPixel((center.x-x),(center.y+y),thisColor);

       while(fx<fy)
	  {
	     x++;
	     fx+=bb2;

	     if(p0)
	  {
	     y--;
	     fy-=aa2;

	     if(p>=0)
		p+=(aa-fy);

	     else
		{
		   x++;
		   fx+=bb2;
		   p+=(fx+aa-fy);
		}

	     setPixel((center.x+x),(center.y+y),thisColor);
	     setPixel((center.x+x),(center.y-y),thisColor);
	     setPixel((center.x-x),(center.y-y),thisColor);
	     setPixel((center.x-x),(center.y+y),thisColor);
	  }
    }


Draw a circle using bresenham algo

August 5, 2008
void bresCircle(int Radius,Point center, Color thisColor)//int Radius,int xC,int yC)
{
	int P;
	int x,y;
	//void Draw(int x,int y,center.x,center.y);
	P = 1 - Radius;
	x = 0;
	y = Radius;
	Draw(x,y,center,thisColor);
	while (x<=y)
	{
		x++;
		if (P<0)
		{
			P += 2 * x + 1;
		}
		else
		{
			P += 2 * (x - y) + 1;
			y--;
		}
		Draw(x,y,center,thisColor);
	}

}
void Draw(int x,int y,Point center ,Color thisColor)
{
	setPixel(center.x + x,center.y + y,thisColor);
	setPixel(center.x + x,center.y - y,thisColor);
	setPixel(center.x - x,center.y + y,thisColor);
	setPixel(center.x - x,center.y - y,thisColor);
	setPixel(center.x + y,center.y + x,thisColor);
	setPixel(center.x - y,center.y + x,thisColor);
	setPixel(center.x + y,center.y - x,thisColor);
	setPixel(center.x - y,center.y - x,thisColor);
}


Draw a line accroding to breshenham algorithm

August 5, 2008
void bresline(Point start,Point end,Color thisColor){

 int x,y,endl,steps=0,p,dx,dy ,c=0,current=0;
	
    dx=abs(end.x -start.x ); //abs of delta x
	dy=abs(end.y -start.y ); //abs of delta y
 	
	
	if(dx>dy)
 			{
 				p=2*dy-dx;
  				if(start.x<end.x)
  				  {
   					 x=start.x;
					 y=start.y;
					 endl=end.x;
   					 
					 if(start.y x at start
  				{
   					x=end.x;
					y=end.y;
					endl=start.x;

   					if(end.y<start.y)
						steps=1;
					else
						steps=-1;
  				}
  				while(x<=endl)
  				{
  					setPixel(x,y,thisColor);

  					if(pdx
			{
				p=2*dx-dy;
				if(start.y<end.y)
				{
					y=start.y;
					x=start.x;
					endl=end.y;

					if(start.x<end.x)
						steps=1;
					else
						steps=-1;
				}
				else
				{
					y=end.y;
					x=end.x;
					endl=start.y;

					if(end.x<start.x)
						steps=1;
					else
						steps=-1;
				}
				while(y<=endl)
  				{
 					setPixel(x,y,thisColor);

 					if(p<0)
						p=p+2*dx;
 					else
 						{
 							x=x+steps;
							p=p+2*(dx-dy);
   						}
   					y++;

   					if(current==0&&c==10)
   					{
    						current=1;
							c=-1;
   					}

   					if(current==1&&c==6)
   					{
   						current=0;
						c=-1;
   					}
   					c++;
  				}//while
 	}//1st if


}//bresline

Draws a line according to the DDA algo

August 5, 2008
void DDA(Point start, Point end, Color thisColor){
	int steps;
	float dx, dy, xIncrement, yIncrement, x, y;

	dx = end.x - start.x;
	dy = end.y - start.y;

	if (abs(dx)>abs(dy))
		steps = abs(dx);
	else 
		steps = abs(dy);

	xIncrement = dx/steps;
	yIncrement = dy/steps;

	x = start.x; 
	y = start.y;

	setPixel(x,y,thisColor);

	for (int k=1; k<=steps; ++k){
		x = x+xIncrement;
		y = y+yIncrement;

		setPixel(x,y,thisColor);
	}
}

Follow

Get every new post delivered to your Inbox.