示例#1
0
文件: myround.c 项目: PoetLiu/program
int main()
{
	int i = 0;
	float f[]	= {
		1.5, 1.45, 1.445, 1.4445, 1.44445,
		1.4, 1.44, 1.444, 1.4444, 1.44444
	};

//	for (i = 0; i < sizeof(f)/sizeof(float); i++)
		printf("%f\n", myroundf(f[3], 1));

	return 0;
}
示例#2
0
void MyBrush::draw() {
    // Set up camera for drawing
    setup2DDrawing( Color(0,0,0), screenWidth, screenHeight );

    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    // Draw a border around the actual image
    glColor3f(1.0f, 1.0f, 1.0f);
    glBegin(GL_LINE_LOOP);
    glVertex2i( 0,            0 );
    glVertex2i( imageWidth+1, 0 );
    glVertex2i( imageWidth+1, imageHeight+1 );
    glVertex2i( 0,            imageHeight+1 );
    glEnd();


    glRasterPos2i(0, 0);
    // Copy data into window
	//for ( int iX = 0; iX < 100; iX++ )
		//putPixel( iX, iX, Color(1,0,0) );

    glDrawPixels(imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, &pixelData[0]);

	// These 5 lines draw a white line across your canvas
	// Remove this and replace it with intelligent OpenGL preview code
	/*
	glLineWidth( 10);
	glBegin( GL_LINES );
	glVertex2i( 100, 100 );
	glVertex2i( 200, 200 );
	glEnd();
	*/

    // Add in your OpenGL pre-view code here

    // display draw in progress (mouse is down)
	ToolType type = brushUI->getToolType();
    if (!isMouseDown) {
		switch(type) {
			case TOOL_BRUSH:{
				int xCenter = mouseDrag[0];
				int yCenter = mouseDrag[1];
				int radius = brushUI->getRadius();
				int segments = radius * 4;
				glBegin(GL_LINE_LOOP);
				for (int i = 0; i < segments; i++)
				{
					float angle = 2.0f * 3.1415926f * float(i) / float(segments);
					float x = radius * cosf(angle);
					float y = radius * sinf(angle); 
					glVertex2f(x + xCenter, y + yCenter);
				}
				glEnd();
			}break;
		}
    }
	else{
		switch (type) {
			case TOOL_LINE:{
				const int radius = brushUI->getRadius();

				int x0 = mouseDown[0];
				int y0 = mouseDown[1];
				int x1 = mouseDrag[0];
				int y1 = mouseDrag[1];

				LineSegment ls(x0, y0, x1, y1);
				std::vector<LineSegment> lineList;

				//find four corners of thick line
				if (ls.v0.horizontal){
					LineSegment ls0(x0, (int)(y0 - floor(radius / (float)2)), x1, (int)(y1 - floor(radius / (float)2)));
					LineSegment ls1(x0, (int)(y0 + ceil(radius / (float)2)), x1, (int)(y1 + ceil(radius / (float)2)));
					lineList.push_back(ls0);
					lineList.push_back(ls1);
				}
				else if (ls.v0.type == VERTICAL){
					LineSegment ls0((int)(x0 - floor(radius / (float)2)), y0, (int)(x1 + ceil(radius / (float)2)), y0);
					LineSegment ls1((int)(x0 - floor(radius / (float)2)), y1, (int)(x1 + ceil(radius / (float)2)), y1);
					lineList.push_back(ls0);
					lineList.push_back(ls1);
				}
				else{
					int rise = y1 - y0;
					int run = x1 - x0;
					float normFactor = (float)sqrt(rise*rise + run*run);
					float dy = run / normFactor;
					float dx = rise / normFactor;

					float halfRadius = radius / (float)2;
					int fR = (int)floor(halfRadius);
					int cR = (int)ceil(halfRadius);

					LineSegment ls0(x0 - myroundf(dx*fR), y0 + myroundf(dy*fR), x1 - myroundf(dx*fR), y1 + myroundf(dy*fR));
					LineSegment ls1(x1 + myroundf(dx*cR), y1 - myroundf(dy*cR), x0 + myroundf(dx*cR), y0 - myroundf(dy*cR));
					lineList.push_back(ls0);
					lineList.push_back(ls1);
				}
				glBegin(GL_LINE_LOOP);
				glVertex2f((GLfloat)lineList[0].v0.x, (GLfloat)lineList[0].v0.y);
				glVertex2f((GLfloat)lineList[0].v1.x, (GLfloat)lineList[0].v1.y);
				glVertex2f((GLfloat)lineList[1].v0.x, (GLfloat)lineList[1].v0.y);
				glVertex2f((GLfloat)lineList[1].v1.x, (GLfloat)lineList[1].v1.y);
				glEnd();
			}break;
			case TOOL_CIRCLE:{
				int xCenter = mouseDown[0];
				int yCenter = mouseDown[1];
				int thickness = brushUI->getRadius();

				int xExtent = mouseDrag[0];
				int yExtent = mouseDrag[1];

				int radius = myroundf((float)sqrt((xExtent - xCenter)*(xExtent - xCenter) + (yExtent - yCenter)*(yExtent - yCenter)));

				int innerRadius = radius - (int)floor(thickness / (float)2);
				int outerRadius = radius + (int)ceil(thickness / (float)2);

				int segments = innerRadius * 4;
				glBegin(GL_LINE_LOOP);
				for (int i = 0; i < segments; i++)
				{
					float angle = 2.0f * 3.1415926f * float(i) / float(segments);
					float x = innerRadius * cosf(angle);
					float y = innerRadius * sinf(angle);
					glVertex2f(x + xCenter, y + yCenter);
				}
				glEnd();

				segments = outerRadius * 4;
				glBegin(GL_LINE_LOOP);
				for (int i = 0; i < segments; i++)
				{
					float angle = 2.0f * 3.1415926f * float(i) / float(segments);
					float x = outerRadius * cosf(angle);
					float y = outerRadius * sinf(angle);
					glVertex2f((GLfloat)(x + xCenter), (GLfloat)(y + yCenter));
				}
				glEnd();
			}break;
			case TOOL_POLYGON:{
				glBegin(GL_LINE_LOOP);
				for (size_t i = 0; i < polygon.size(); ++i){
					glVertex2f((GLfloat)polygon[i][0], (GLfloat)polygon[i][1]);
				}
				glVertex2f((GLfloat)mouseDrag[0], (GLfloat)mouseDrag[1]);
				glEnd();
			}break;
		}
	}
    endDrawing();
}