が傾きがおかしくなってしまいます。
どこを訂正したら正しく描画できますか?
#include<iostream> //C言語標準ライブラリのインクルード
#include<GL/glut.h> //GLUTヘッダファイルのインクルード
#include<GL/gl.h> //GLヘッダファイルのインクルード
#include<math.h>
double clt[4][3] =
{
{ 1,2,0 },
{ 2,3,0 },
{ 3,1,0 },
{ 5,1,0 },
};
//プロトタイプ宣言
void init(void);
void keyboard(unsigned char key, int x, int y);
void display(void);
void resize(int w, int h);
int main(int argc, char **argv)
{
glutInit(&argc, argv);
/* ウィンドウの生成 */
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowPosition(200, 50);
glutInitWindowSize(400, 400);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case '\33':
case 'q':
case 'Q':
exit(0);
break;
default:
break;
}
}
void display(void)
{
float x, y, x1, y1, x2, y2, t, s;
float length = 0;
for (s = 0.0; s<1.1; s += 0.1)
{
glLineWidth(1.0f);
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 1.0, 0.0);
x = pow((1 - s), 3) * clt[0][0] + 3 * pow((1 - s), 2)*s * clt[1][0] + 3 * (1 - s)*pow(s, 2) * clt[2][0] + pow(s, 3) * clt[3][0];
y = pow((1 - s), 3) * clt[0][1] + 3 * pow((1 - s), 2)*s * clt[1][1] + 3 * (1 - s)*pow(s, 2) * clt[2][1] + pow(s, 3) * clt[3][1];
x1 = -3 * pow((1 - s) ,2) * clt[0][0]+ 3 * s * (1 - s) * (1 - 3 * s) * clt[1][0] + 3 * s * (2 - 3 * s) * clt[2][0] + 3 * pow(s,2)* clt[3][0];
y1 = 3 * pow((1 - s), 2) * clt[0][1] + 3 * s * (1 - s) * (1 - 3 * s) * clt[1][1] + 3 * s * (2 - 3 * s) * clt[2][1] + 3 * pow(s, 2) * clt[3][1];
length = sqrt(x1*x1 + y1*y1);
std::cout << length << std::endl;
for (x2 = x; x2 <= x + x1; x2 += 0.01)
{
if (x1 == 0)
{
glVertex2f(x, y);
glVertex2f(x, y1);
}
else
{
y2 = (y1 / x1)*(x2 - x) + y;
glVertex2f(x2, y2);
}
}
glEnd();
}
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (t = 0; t<1.1; t += 0.1)
{
x = pow((1 - t), 3) * clt[0][0] + 3 * pow((1 - t), 2)*t * clt[1][0] + 3 * (1 - t)*pow(t, 2) * clt[2][0] + pow(t, 3) * clt[3][0];
y = pow((1 - t), 3) * clt[0][1] + 3 * pow((1 - t), 2)*t * clt[1][1] + 3 * (1 - t)*pow(t, 2) * clt[2][1] + pow(t, 3) * clt[3][1];
glVertex2f(x, y);
};
glEnd();
glFlush();
}
void resize(int w, int h)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 20.0, -5.0*(GLfloat)h / (GLfloat)w,
12.5*(GLfloat)h / (GLfloat)w, -10.0, 5.0);
else
glOrtho(-5.0*(GLfloat)w / (GLfloat)h,
5.0*(GLfloat)w / (GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glScalef(3, 3, 3);
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}