GL,C++を使った球の特性方程式について
Posted: 2010年6月25日(金) 11:56
バネにつながった球の特性方程式の根が実数根をもつとき,虚数根をもつとき,重根をもつとき,となるようにm, c, kの係数を適当に決めて,それぞれの場合における解軌跡(ドラッグを離した場所を初期値として)を示すものをOPENGLを用いて表示させるプログラムをC++で作成したのが下記のプログラムなのですが、classを用いてm, c, kの値が異なる球を複数表示させたいと思っています。Omori classとしてclassの中にはx,y,z(球の座標)、m,c,k(球の質量, 減衰係数,ばね定数)、球の大きさ、色、バネの位置と設定しょうとしているのですが、うまく作ることが出来ません。
この場合のclassの書き方をどなたか教えていただけないでしょうか?
私は今大学4年生でC++は始めて2ヶ月くらいのまだまだ初心者です。
そのため下記のプログラムもかなり読みづらいかと思いますがどうぞよろしくお願いします。
#include<string>
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
unsigned char mouseFlag = GL_FALSE;
float x0;
float x = 0.0;
int a = 0;
int samplingTime = 50;
int startFlug;
double t = 0.0;
double xStart;
double yStart;
double startFlag;
double yy;
const int width = 1000, height = 1000;
void myKeyboard(unsigned char key, int x, int y)
{
if (key == 27 ) exit(0);
if(key == 'a'){
a = 1;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
if(key == 'b'){
a = 2;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
if(key == 'c'){
a = 3;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
}
void glutDrawString(const char *str,void *font,float A,float B,float C)
{
glRasterPos3f(0,2,0);
while(*str){
glutBitmapCharacter(font, *str);
++str;
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.6, 0.0, 0.5);
glBegin(GL_LINES);
glVertex2d(-10.0, 0.0);
glVertex2d(x, 0.0);
glEnd();
glPushMatrix();
glTranslated(x, 0.0, 0.0);
glutWireSphere(0.5, 70, 70);
glPopMatrix();
glEnd();
glFlush();
}
void myInit(char *progname)
{
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(0, 0);
glutCreateWindow(progname);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double)width / (double)height, 0.1, 200.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void myMouseFunc(int button, int state, int x1, int y1)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
startFlag = 0;
t = 0.0;
x = 0.0;
x0 = 0.0;
yy = 0.0;
xStart = x1;
yStart = y1;
mouseFlag =GL_TRUE;
}else{
mouseFlag = GL_FALSE;
startFlag = 1;
}
}
void myMouseMotion(int xx, int yyy)
{
if(mouseFlag == GL_FALSE) return;
x0 = 0.01 * (xx-xStart);
glutPostRedisplay();
}
void myTimer(int value)
{
glutTimerFunc(samplingTime, myTimer, 1);
if(a == 1){
double m = 2, c = 5, k = 2, w, y, x1, x2, c1, c2;
w = sqrt(k / m);
y = c / (2 * sqrt(m * k));
x1 = -y * w + sqrt(y * y - 1) * w;
x2 = -y * w - sqrt(y * y - 1) * w;
c1 = (x2 / (x2 - x1)) * x0;
c2 = (x1 / (x1 - x2)) * x0;
if(startFlag == 1){
x = c1 * exp(x1 * t) + c2 *exp(x2 * t);
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}
if(a == 2){
double m = 2, c = 4, k = 2, w, x1, c1, c2;
w = sqrt(k / m);
x1 = -w;
c1 = x0;
c2 = x0 * w;
if(startFlag == 1){
x = (c1 + c2 * t) * exp(x1 * t);
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}if(a == 3){
double m = 3.0, c = 0.4, k = 2, w, y, q;
w = sqrt(k / m);
y = c / (2 * sqrt(m * k));
q = w * sqrt(1 - y * y);
if(startFlag == 1){
x = exp(-y * w * t) * (x0 * cos(q * t) + ((y * w * x0) / q) * sin(q * t));
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
myInit(argv[0]);
glutKeyboardFunc(myKeyboard);
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
glutTimerFunc(samplingTime, myTimer, 1);
glutMainLoop();
return 0;
}
この場合のclassの書き方をどなたか教えていただけないでしょうか?
私は今大学4年生でC++は始めて2ヶ月くらいのまだまだ初心者です。
そのため下記のプログラムもかなり読みづらいかと思いますがどうぞよろしくお願いします。
#include<string>
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
unsigned char mouseFlag = GL_FALSE;
float x0;
float x = 0.0;
int a = 0;
int samplingTime = 50;
int startFlug;
double t = 0.0;
double xStart;
double yStart;
double startFlag;
double yy;
const int width = 1000, height = 1000;
void myKeyboard(unsigned char key, int x, int y)
{
if (key == 27 ) exit(0);
if(key == 'a'){
a = 1;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
if(key == 'b'){
a = 2;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
if(key == 'c'){
a = 3;
x = 0.0;
x0 = 0.0;
t = 0.0;
}
}
void glutDrawString(const char *str,void *font,float A,float B,float C)
{
glRasterPos3f(0,2,0);
while(*str){
glutBitmapCharacter(font, *str);
++str;
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.6, 0.0, 0.5);
glBegin(GL_LINES);
glVertex2d(-10.0, 0.0);
glVertex2d(x, 0.0);
glEnd();
glPushMatrix();
glTranslated(x, 0.0, 0.0);
glutWireSphere(0.5, 70, 70);
glPopMatrix();
glEnd();
glFlush();
}
void myInit(char *progname)
{
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(0, 0);
glutCreateWindow(progname);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double)width / (double)height, 0.1, 200.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void myMouseFunc(int button, int state, int x1, int y1)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
startFlag = 0;
t = 0.0;
x = 0.0;
x0 = 0.0;
yy = 0.0;
xStart = x1;
yStart = y1;
mouseFlag =GL_TRUE;
}else{
mouseFlag = GL_FALSE;
startFlag = 1;
}
}
void myMouseMotion(int xx, int yyy)
{
if(mouseFlag == GL_FALSE) return;
x0 = 0.01 * (xx-xStart);
glutPostRedisplay();
}
void myTimer(int value)
{
glutTimerFunc(samplingTime, myTimer, 1);
if(a == 1){
double m = 2, c = 5, k = 2, w, y, x1, x2, c1, c2;
w = sqrt(k / m);
y = c / (2 * sqrt(m * k));
x1 = -y * w + sqrt(y * y - 1) * w;
x2 = -y * w - sqrt(y * y - 1) * w;
c1 = (x2 / (x2 - x1)) * x0;
c2 = (x1 / (x1 - x2)) * x0;
if(startFlag == 1){
x = c1 * exp(x1 * t) + c2 *exp(x2 * t);
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}
if(a == 2){
double m = 2, c = 4, k = 2, w, x1, c1, c2;
w = sqrt(k / m);
x1 = -w;
c1 = x0;
c2 = x0 * w;
if(startFlag == 1){
x = (c1 + c2 * t) * exp(x1 * t);
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}if(a == 3){
double m = 3.0, c = 0.4, k = 2, w, y, q;
w = sqrt(k / m);
y = c / (2 * sqrt(m * k));
q = w * sqrt(1 - y * y);
if(startFlag == 1){
x = exp(-y * w * t) * (x0 * cos(q * t) + ((y * w * x0) / q) * sin(q * t));
t += 0.2;
}else{
x = x0;
}
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
myInit(argv[0]);
glutKeyboardFunc(myKeyboard);
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
glutTimerFunc(samplingTime, myTimer, 1);
glutMainLoop();
return 0;
}