現在地 >> メニュー >> サンプルコード::OpenGL >> 直線の方程式2


問題


次の2点から、媒介変数tを用いて、
その2点を通る直線上の点を表せ。

A(2,3,4)
B(4,1,5)

答え


#include <iostream>
#include <cstdio>

using namespace std;

void CALC_MOVE_VECTOR(double *p1,double *p2,double *move);



int main()
{

double p1[3]={2,3,4};
double p2[3]={4,1,5};

double move[3];


CALC_MOVE_VECTOR(p1,p2,move);

printf("P = ( %.1f, %.1f, %.1f ) + t*(%.1f ,%.1f , %.1f )\n",p1[0],p1[1],p1[2],move[0],move[1],move[2]);

return 0;

}


void CALC_MOVE_VECTOR(double *p1,double *p2,double *move)
{

for(int i = 0;i<3;i++)
{
move[i]= p1[i] - p2[i];
}

}

OpenGLによる確認プログラム


「↑」、「↓」で直線上の点が移動。(媒介変数tが変化する)


#include <iostream>
#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

using namespace std;


/*******[ここから、プロトタイプ宣言]***********************************/

/* 前処理関係 */
void CALC_MOVE_VECTOR(double *p1,double *p2,double *move);


/* コールバック関係 */
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
void SpecialKey(int key, int x, int y);


/* 描画関係 */
inline void DRAW_OBJ();
void DRAW_XYZ();
void DRAW_LINE();
void DRAW_POINT_ON_LINE();


double p1[3]={2,3,4};
double p2[3]={6,6,1};


/**********[OpenGLの初期設定]********************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
glutSpecialFunc(SpecialKey);
}


double move[3];
double t;
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
CALC_MOVE_VECTOR(p1,p2,move);
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}


/********[ここから、各種コールバック]****************************************/
double xAngle = 0.0, yAngle = 0.0;
double xAngle2 = 0.0, yAngle2 = 0.0;

void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glRotated(xAngle2,1,0,0);
glRotated(yAngle2,0,1,0);
gluLookAt(13.0, 14.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);

glEnable(GL_DEPTH_TEST);

DRAW_OBJ();

glDisable(GL_DEPTH_TEST);

glutSwapBuffers();
}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}



int xStart,yStart;
bool mouseFlag = GL_FALSE;
bool mouseFlag2 = GL_FALSE;
void myMouseFunc(int button,int state,int x,int y)
{

if(button == GLUT_LEFT_BUTTON &&state == GLUT_DOWN)
{
xStart = x;
yStart = y;
mouseFlag = GL_TRUE;
}
else
{
mouseFlag = GL_FALSE;
}

if(button == GLUT_RIGHT_BUTTON &&state == GLUT_DOWN)
{
xStart = x;
yStart = y;
mouseFlag2 = GL_TRUE;

}
else
{
mouseFlag2 = GL_FALSE;
}

}

void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;

if*1 return;


if(mouseFlag == GL_TRUE)
{
xdis = x - xStart;
ydis = y - yStart;

xAngle += (double)ydis * a;
yAngle += (double)xdis * a;

xStart = x;
yStart = y;
}

if(mouseFlag2 == GL_TRUE)
{
xdis = x - xStart;
ydis = y - yStart;

xAngle2 += (double)ydis * a;
yAngle2 += (double)xdis * a;

xStart = x;
yStart = y;
}


glutPostRedisplay();
}


void SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
t += 0.05;
break;

case GLUT_KEY_DOWN:
t -= 0.05;
break;

default:
break;
}
glutPostRedisplay();
}



/*******[ここから前処理関係の関数]*****************/
void CALC_MOVE_VECTOR(double *p1,double *p2,double *move)
{
for(int i = 0;i<3;i++)
{
move[i]= p1[i] - p2[i];
}
}


/**********[ここから各種関数]***********************/
inline void DRAW_OBJ()
{
DRAW_XYZ();
DRAW_LINE();
DRAW_POINT_ON_LINE();
}

void DRAW_XYZ()
{
glBegin(GL_LINES);

glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);

glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);

glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();

}

void DRAW_LINE()
{
glBegin(GL_LINES);

glColor3d(0,1,1);
glVertex3dv(p1);
glVertex3dv(p2);
glEnd();
}

void DRAW_POINT_ON_LINE()
{
static double p[3] ={ 0,0,0};
for(int i = 0; i < 3; i++)
{
p[i] = p1[i] + t * move[i];
}

glColor3d(1,0,1);
glPointSize(3);

glBegin(GL_POINTS);
glVertex3dv(p);
glEnd();

}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます