最終更新: mikk_ni3_92 2007年11月03日(土) 16:30:29履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 平面の方程式
次の三点を通る平面の方程式を求めよ。
A(1,2,0)
B(0,1,1)
C(3,3,1)
※平面の式
ax + by +cz + d = 0
のa,b,c,dを求め出力する。
その他条件:
平面のおおよその位置がわかるように、3点でポリゴンを作って描画する。
マウスで視点を変更できる。→マウスと視点(改)?を利用する。
#include <cstdio>
#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void CALC_PLANE();
void DRAW_XYZ();
void DRAW_TRI();
using namespace std;
typedef struct _POINT_3D
{
double x;
double y;
double z;
}POINT_3D;
POINT_3D a={1,2,0};
POINT_3D b={0,1,1};
POINT_3D c={3,3,1};
POINT_3D n;
double d;
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);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
CALC_PLANE();
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(5.0, 6.0, 7.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_XYZ();
glPushMatrix();
glColor4d(1,1,0,0.5);
glNormal3d(n.x,n.y,n.z);
DRAW_TRI();
glPopMatrix();
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();
}
/**********[ここから各種関数]***********************/
inline void CALC_PLANE()
{
POINT_3D ab;
ab.x= a.x - b.x;
ab.y= a.y - b.y;
ab.z= a.z - b.z;
POINT_3D ac;
ac.x = a.x - c.x;
ac.y = a.y - c.y;
ac.z = a.z - c.z;
/*外積計算*/
n.x = ab.y*ac.z - ab.z*ac.y;
n.y = ab.z*ac.x - ab.x*ac.z;
n.z = ab.x*ac.y - ab.y*ac.x;
d = -(n.x *a.x +n.y*a.y + n.z * a.z); //a(x− nx) + b(y − ny) + c(z − nz)=0より変形
printf("plane is \n%fx + %fy + %fz + %f = 0\n",n.x,n.y,n.z,d);
}
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_TRI()
{
glBegin(GL_TRIANGLES);
glVertex3d(a.x,a.y,a.z);
glVertex3d(b.x,b.y,b.z);
glVertex3d(c.x,c.y,c.z);
glEnd();
}
平面の式ax + by + cz + d = 0のa,b,cが法線ベクトルになっている。
参考
http://www.crossroad.jp/mathnavi/kousiki/kuukan-to...
次の三点を通る平面の方程式を求めよ。
A(1,2,0)
B(0,1,1)
C(3,3,1)
※平面の式
ax + by +cz + d = 0
のa,b,c,dを求め出力する。
その他条件:
平面のおおよその位置がわかるように、3点でポリゴンを作って描画する。
マウスで視点を変更できる。→マウスと視点(改)?を利用する。
#include <cstdio>
#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void CALC_PLANE();
void DRAW_XYZ();
void DRAW_TRI();
using namespace std;
typedef struct _POINT_3D
{
double x;
double y;
double z;
}POINT_3D;
POINT_3D a={1,2,0};
POINT_3D b={0,1,1};
POINT_3D c={3,3,1};
POINT_3D n;
double d;
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);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
CALC_PLANE();
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(5.0, 6.0, 7.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_XYZ();
glPushMatrix();
glColor4d(1,1,0,0.5);
glNormal3d(n.x,n.y,n.z);
DRAW_TRI();
glPopMatrix();
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();
}
/**********[ここから各種関数]***********************/
inline void CALC_PLANE()
{
POINT_3D ab;
ab.x= a.x - b.x;
ab.y= a.y - b.y;
ab.z= a.z - b.z;
POINT_3D ac;
ac.x = a.x - c.x;
ac.y = a.y - c.y;
ac.z = a.z - c.z;
/*外積計算*/
n.x = ab.y*ac.z - ab.z*ac.y;
n.y = ab.z*ac.x - ab.x*ac.z;
n.z = ab.x*ac.y - ab.y*ac.x;
d = -(n.x *a.x +n.y*a.y + n.z * a.z); //a(x− nx) + b(y − ny) + c(z − nz)=0より変形
printf("plane is \n%fx + %fy + %fz + %f = 0\n",n.x,n.y,n.z,d);
}
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_TRI()
{
glBegin(GL_TRIANGLES);
glVertex3d(a.x,a.y,a.z);
glVertex3d(b.x,b.y,b.z);
glVertex3d(c.x,c.y,c.z);
glEnd();
}
平面の式ax + by + cz + d = 0のa,b,cが法線ベクトルになっている。
参考
http://www.crossroad.jp/mathnavi/kousiki/kuukan-to...