現在地 >> メニュー >> サンプルコード::OpenGL >> 正規分布のpdfのグラフ描画

問題


正規分布のpdfのグラフを描画せよ

答え

#include <iostream>
#include <cmath>
#include <GL/glut.h>
#include <boost/format.hpp>
#include <boost/math/distributions/normal.hpp>

//------------ 各種外部変数 ---------------------//
const float WidthX = 5;  //x軸の最大値(+と-の取りうる値)
const float HeightY = 1; //y軸の最大値
const float slice = 0.1; //分割数
const float offset = 0; //x軸の位置を少し上げる
const float mean = 0;  //平均
const float variance = 0.2;  //分散
const float sd = sqrt(variance); //標準偏差

//------------ プロトタイプ宣言 ----------------//
void display();
void reshape(int w, int h);
void DrawPdf();

//------------- OpenGLの初期設定 -----------------------//
void GLUT_INIT()
{
	glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE);
	glutInitWindowSize(640,480);

	glutCreateWindow("math pdf");
}


void GLUT_CALL_FUNC()
{
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

}

void MY_INIT()
{
	glClearColor(1.0, 1.0, 1.0, 1.0);
}


//-------------- メイン関数 ---------------------//
int main(int argc, char *argv[])
{
	glutInit(&argc,argv);
	GLUT_INIT();

	GLUT_CALL_FUNC();
	MY_INIT();

	glutMainLoop();

	return 0;
}


//--------------- ここから各種コールバック -------------------------//
void display(void)
{
	
	//横軸描画
	glColor3f(0,0,0);
	glBegin(GL_LINES);
	glVertex2f(-WidthX,0);
	glVertex2f(WidthX,0);
	glEnd();
	glColor3f(1,1,1);

	glLineWidth(2);
	glColor3f(1,0,1);
	DrawPdf();
	glColor3f(1,1,1);
	glLineWidth(1);

	glutSwapBuffers();
}

void reshape(int w, int h)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-WidthX, WidthX, -offset, HeightY);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}


//---------------- ここから、各種関数 -----------------------//
void DrawPdf()
{
	boost::math::normal N(mean,sd); 
	
	glBegin(GL_LINE_STRIP);
		for(float x = -WidthX; x <=WidthX; x+=slice){
			glVertex2f(x,boost::math::pdf(N, x));
		}
	glEnd();

}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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