• Bresenham画线算法

    Bresenham画线算法 #include<GL/glut.h> #include<stdio.h> #define BLACK 0 void draw_pixel(int ix,int iy,int value) { glBegin(GL_POINTS); glVertex2i(ix,iy); glEnd(); } bres(int x1,int y1,int x2,int y2) { int dx,dy,i,e; int incx,incy,inc1,inc2; int x,y; dx=x2-x1; dy=y2-y1; if(dx<0) dx=-dx; if(dy<0) dy=-dy; incx=1; if(x2<x1) incx=-1; incy=1; if(y2<y1) incy=-1; x=x1; y=y1; if(dx>dy) { draw_pixel(x,y,BLACK); e=2*dy-dx; inc1 = 2*(dy-dx); inc2 = 2*dy; for(i=0;i<dx;i++) { if(e>=0) { y+=incy; e+=inc1; } else e+=inc2; x+=incx; draw_pixel(x,y,BLACK); } } else { draw_pixel(x,y,BLACK); e=2*dx-dy; inc1=2*(dx-dy); inc2=2*dx; for(i=0;i<dy;i++) { if(e>=0) { x+=incx; e+=inc1; } else e+=inc2; y+=incy; draw_pixel(x,y,BLACK); } } } void display() { glClear(GL_COLOR_BUFFER_BIT); bres(200,200,100,50); glFlush(); } void myinit() { glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glPointSize(1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,499.0,0.0,499.0); } void main(int argc,char **argv) { /* standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );/*default,not needed*/ glutInitWindowSize(500,500); /* 500×500 pixel window */ glutInitWindowPosition(0,0);/* place window top left on display */ glutCreateWindow("Bresenham's algorithm ");/* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /*set attributes */ glutMainLoop(); /* enter event loop */ }

    0
    188
    2KB
    2010-12-10
    10
  • 图形学立方体旋转程序

    立方体旋转程序 /* Rotating cube with color interpolation */ /* Demonstration of use of homogeneous coordinate transformations and simple data structure for representing cube from Chapter 4 */ /* Colors are assigned to the vertices */ /* cube is centered at orign*/ #include <stdlib.h> #include <GL/glut.h> GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; void polygon(int a, int b, int c, int d) { /* draw a polygon via list of vertices */ glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } void colorcube(void) { /* map vertices to faces */ polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } static GLfloat theta[] = {0.0,0.0,0.0}; static GLint axis = 2; void display(void) { /* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glFlush(); glutSwapBuffers(); } void spinCube() { /* Idle callback, spin cube 2 degrees about selected axis */ theta[axis] += 2.0; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; /* display(); */ glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { /* mouse callback, selects an axis about which to rotate */ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden-surface removal */ glutMainLoop();

    0
    231
    5KB
    2010-12-10
    15
  • JAVA面向过程的程序设计

    1. 面向过程的程序设计 面向过程——面向机器 –其中心思想是用计算机能够理解的逻辑来描述和表达待解决的问题及其具体的解决过程。–算法+数据结构 l数据结构利用计算机的离散逻辑来量化表达需要解决的问题。 l –面向对象问题求解关心的不仅仅是孤立的单个过程,而是孕育所有这些过程的母体系统 –它能够用计算机逻辑来模拟、描述系统本身,包括系统的组成,系统的各种可能状态,以及系统中可能产生的各种过程与过程引起的系统状态切换。 –面向对象的程序设计是以要解决的问题中所涉及到的各种对象为主要考虑因素。 –对象是一种看问题的观点,是对现实世界各种元素的一种抽象。对象既含数据又含功能,因此具有自身处理数据的能力。对象被认为是迄今为止最接近真实事物的数据抽象。 •考虑对象—考虑过程:前者更有意义 •设计对象—设计过程:前者更富挑战性,尤其是设计可重用的对象。 • •2.面向对象程序设计中的对象是现实世界对象的模型化,它同样具有状态和行为。–对象的状态用属性来维护, –对象的行为用方法来实现。•因此可以简单地讲,对象是面向对象的程序设计模式,它由描述状态的属性(变量)和用来实现对象行为的 阅读全文

    4
    148
    270KB
    2010-12-10
    16
上传资源赚积分or赚钱