2013-03-11 106 views
0

我想为我的openGL类项目绘制一个9分的圆圈,这个问题?我的老师并没有教会我们如何在圈子里做任何事情。他给了我们关于如何绘制一个三角形的代码,并告诉我们用什么公式来找到正常圆的顶点和中心点。但从来没有碰到过如何使用它们进行编码,特别是他希望我们这样做的方式。他告诉我们要使用在openGL中绘制一个9个点的圆圈?

class GLintPoint { 
public: 
GLint x, y; 
}; 

,我们能叫出他们,B,C然后找到顶点说A = B - 答:我认为这将是AX = Bx的 - 斧和同为为什么,但我不知道肯定。他不得不离开去接受医疗(我承担了手术),所以我不能问他,他在本周末仍然有病。

他离开前给了我们这段代码。

#include <stdlib.h> 
#include <math.h> 
#include <GL/glut.h> 
#include "canvas.h" 

const int screenWidth = 640; 
const int screenHeight = 480; 

Canvas cvs(screenWidth, screenHeight, "Relative Drawing Example", 1); 



class GLintPoint { 
public: 
GLint x, y; 
}; 

#define NUM 3 
static GLintPoint List[NUM]; 

// myInit 

void myInit(void) 
{ 
glClearColor(1.0, 1.0, 1.0, 0.0); 
glColor3f(0.0f, 0.0f, 0.0f); 
glPointSize(1.0); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); 
} 

// myDisplay 
void myDisplay(void) 
{ 
glClear(GL_COLOR_BUFFER_BIT); 
glFlush(); 
} 

void mykey(unsigned char key, int x, int y) 
{ 
if (key == 'Q' || key == 'q') exit(0); 
} 



// draw arc 

void draw_arc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble startAngle, 
GLdouble sweepAngle) 
{ 
glBegin(GL_LINE_STRIP); 
for (GLdouble t = startAngle; t < startAngle+sweepAngle; t += 0.001) 
{ 
    GLdouble x = cx + r*cos(DEG2RAD*t); 
    GLdouble y = cy + r*sin(DEG2RAD*t); 
    glVertex2d(x, y); 
} 
glEnd(); 
} 


// myMouse 

void myMouse(int button, int state, int x, int y) 
{ 

static int last = -1; 



if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && last < (NUM -1)) 
{ 
    List[++last].x = x; 
    List[ last].y = screenHeight - y; 
    glClear(GL_COLOR_BUFFER_BIT); 
    glBegin(GL_LINE_STRIP); 
     for (int i = 0; i <= last; i++) {   
    if(last <=0){ 
    cvs.moveTo(List[i].x, List[i].y);  
    } 
    else 
    cvs.lineTo(List[i].x, List[i].y); 
    } 
    int i = 0; 
    cvs.lineTo(List[i].x, List[i].y); 
    glEnd(); 
    glFlush(); 
} 
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) 
    last = -1; 

GLintPoint a.x = 

} 

// main 



int main(int argc, char ** argv) 
{ 
//glutInit(&argc, argv); 
//glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB); 
//glutInitWindowSize(screenWidth, screenHeight); 
//glutInitWindowPosition(100, 150); 
//glutCreateWindow("case study 4.2"); 
glutDisplayFunc(myDisplay); 
glutMouseFunc(myMouse); 
glutKeyboardFunc(mykey); 
myInit(); 
glutMainLoop(); 
return 0; 
} 

#include <math.h> 
// define some ascii key names 
#define GLUT_KEY_ESC 27 

// Keeps track of a single point (turtle position) 
class Point2d { 
public: 
Point2d() { x = y = 0.0f; } 
Point2d(float xx, float yy) { x = xx; y = yy; } 
void set(float xx, float yy) { x = xx; y = yy; } 
void setX(float xx) { x = xx; } 
void setY(float yy) { y = yy; } 
float getX() { return x; } 
float getY() { return y; } 
void draw(void) { glBegin(GL_POINTS); 
        glVertex2f((GLfloat)x, (GLfloat)y); 
        glEnd(); 
        } 
private: 
float x, y; 
}; 

//Data type for an array of points 
class Point2dArray { 
static const int MAX_NUM = 100; 
public: 
    int num; 
Point2d pt[MAX_NUM]; 
}; 

class IntRect { 
public: 
IntRect() { l = 0; r = 100; b = 0; t = 100; } 
IntRect(int left, int right, int bottom, int top) 
    { l = left; r = right; b = bottom; t = top; } 
void set(int left, int right, int bottom, int top) 
    { l = left; r = right; b = bottom; t = top; } 
void draw(void) { glRecti(l, b, r, t); } 
int getL() { return l; } 
int getR() { return r; } 
int getT() { return t; } 
int getB() { return b; } 

    private: 
    int l, r, b, t; 
}; 

class RealRect { 
public: 
RealRect() { l = 0; r = 100; b = 0; t = 100; } 
RealRect(float left, float right, float bottom, float top) 
    { l = left; r = right; b = bottom; t = top; } 
void set(float left, float right, float bottom, float top) 
    { l = left; r = right; b = bottom; t = top; } 
void draw(void) { glRectf(l, b, r, t); } 
float ratio(void) { return (r - l)/(t - b); } 
float getL() { return l; } 
float getR() { return r; } 
float getT() { return t; } 
float getB() { return b; } 

    private: 
    float l, r, b, t; 
}; 

class Canvas { 
public: 
Canvas(int width, int height, char* windowTitle, int buffer); 
void setWindow(float l, float r, float b, float t); 
void setViewport(int w, int h); 
void autoSetWindow(void); 

float getWindowAspectRatio(void); 

void lineTo(float x, float y); 
void lineTo(Point2d p); 
void moveTo(float x, float y); 
void moveTo(Point2d p); 
void turnTo(float angle); 
void turn(float angle); 
void forward(float dist, int visible); 
int getWinId(void); 
void initCT(void); 
void scale2D(double, double); 
void translate2D(double, double); 
void rotate2D(double); 

    private: 
    Point2d CP; 
    float CD; 
    IntRect viewport; 
    RealRect window; 
    int winId; 
    float delx, dely; 
    char code1, code2; 
    char formCode(Point2d p); 
    void chopLine(Point2d &p, char c); 
    int clipSegment(Point2d &p1, Point2d &p2); 
    }; 

Canvas::Canvas(int width, int height, char* windowTitle, int buffer = 1) { 
char* argv[1]; 
char dummyString[1]; 
argv[0] = dummyString; 
int argc = 1; 

glutInit(&argc, argv); 

glutInitDisplayMode(((buffer == 1) ? GLUT_SINGLE : GLUT_DOUBLE) | GLUT_RGB); 
glutInitWindowSize(width, height); 
glutInitWindowPosition((1024 - width)/2, (768 - height)/2); 
winId = glutCreateWindow(windowTitle); 
setWindow(1000.0f, -1000.0f, 1000.0f, -1000.0f); 
CP.set(0.0f, 0.0f); 
CD = 0.0f; 
} 

int Canvas::getWinId(void) 
{ 
    return winId; 
} 

float Canvas::getWindowAspectRatio(void) 
{ 
    return (window.getR() - window.getL())/(window.getT() - window.getB()); 
} 

void Canvas::setWindow(float l, float r, float b, float t) { 
window.set(l, r, b, t); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D((GLdouble) l, (GLdouble) r, (GLdouble) b, (GLdouble) t); 
} 

void Canvas::autoSetWindow(void) { 
float l = window.getL(); 
float r = window.getR(); 
float b = window.getB(); 
float t = window.getT(); 

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2D((GLdouble) l, (GLdouble) r, (GLdouble) b, (GLdouble) t); 
} 

void Canvas::setViewport(GLsizei width, GLsizei height) { 
float aspectRatio = getWindowAspectRatio(); 
GLint l, b; 
GLsizei w, h; 

if ((float) width/(float) height >= aspectRatio) { 
l = (GLint) ((width - height * aspectRatio)/2.0); 
    b = 0; 
    w = (GLsizei) (height * aspectRatio); 
h = height; 
    } 
    else { 
    l = 0; 
b = (int) ((height - width/aspectRatio)/2.0); 
w = width; 
    h = (GLsizei) (width/aspectRatio); 
    } 
    glViewport(l, b, w, h); 
    } 

void Canvas::lineTo(float x, float y) { 
glBegin(GL_LINES); 
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY()); 
glVertex2f((GLfloat) x, (GLfloat) y); 
glEnd(); 
CP.set(x, y); 
glFlush(); 
} 

void Canvas::lineTo(Point2d p) { 
glBegin(GL_LINES); 
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY()); 
glVertex2f((GLfloat) p.getX(), (GLfloat) p.getY()); 
glEnd(); 
CP.set(p.getX(), p.getY()); 
glFlush(); 
} 

void Canvas::moveTo(float x, float y) { 
CP.set(x, y); 
} 

void Canvas::moveTo(Point2d p) { 
CP.set(p.getX(), p.getY()); 
} 

void Canvas::turn(float angle) { 
CD += angle; 
} 

void Canvas::turnTo(float angle) { 
CD = angle; 
} 

void Canvas::forward(float dist, int visible) { 
const float RadPerDeg=0.017453393; 
float x = CP.getX()+dist*cos(RadPerDeg*CD); 
float y = CP.getY()+dist*sin(RadPerDeg*CD); 
float l = window.getL(); 
float r = window.getR(); 
float b = window.getB(); 
float t = window.getT(); 

l = (x < l)?x:l; 
r = (x > r)?x:r; 
b = (y < b)?y:b; 
t = (y > t)?y:t; 
window.set(l, r, b, t); 
if (visible) 
lineTo(x, y); 
else 
moveTo(x, y); 
} 

而这些公式。书面就像一张纸,他给我读 A = B -A B = C - B Ç - A - ç

c = A = 1/2((a + ((b(dot product) c)/(matrix of a (dotproduct)c)) * (matrix of a)) 

r = (magnitude of a)/2 squareroot(((b(dot product) c)/(matrix of a (dotproduct)c))^2 +   1) 

我不想问,但我知道,当我在过我我的头,任何人都可以做他要求我做的事情的头或尾?或者任何人都可以带领我走向正确的方向去创造一个9分的圈子?它的价值是我最终成绩的10%,所以我不要求代码只是复制/粘贴“wtf我该做什么?”。我只是在问。

+0

是不是一个9点圆只是一个有9个顶点的多边形? – deepmax 2013-03-11 15:36:47

回答

1

您不能在OpenGL中绘制曲线,只能绘制多边形。所以如果有人要求我在OpenGL中绘制一个九点圆,我会认为它们是一个9边的多边形,每个点在圆周上均匀分布。

+0

他说我们做了一个三角形,然后计算点数并绘制弧线,但是他从来没有教过我们如何在代码中使用数学。这本书同样没用,只有公式,并没有在整章中使用它的例子。 我们只是使用2D到目前为止,不知道这是否有所作为,他不会回答任何有关3D的问题。 – user2060164 2013-03-11 19:57:18

+0

当你编译老师给你的代码时会发生什么? – bwroga 2013-03-11 23:08:37