#include <iostream>
#include <windows.h>
#include <GL/glut.h>
#include<algorithm>
#define n 7
using namespace std;
//#pragma comment(lib, "glut32.lib")
int index;
POINT point[n];
GLfloat oldx,oldy,newx,newy;
double ratio;
int flag;
struct draw
{
double x;
double y;
}get;
draw Array[n][1200000];
draw Poi;
int cnt;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
draw reckon(draw x, draw y, double ratio)
{
draw ans;
ans.x = y.x - x.x;
ans.y = y.y - x.y;
ans.x *= ratio;
ans.y *= ratio;
ans.x += x.x;
ans.y += x.y;
return ans;
}
//draw compute(POINT x, POINT y, double ratio)
//{
// POINT temp;
// temp.x = y.x - x.x;
// temp.y = y.y - x.y;
// draw ans;
// ans.x = (FLOAT)temp.x*1.00*ratio / 400 + (FLOAT)x.x*1.00 / 400;
// ans.y = (FLOAT)temp.y*1.00*ratio / 400 + (FLOAT)x.y*1.00 / 400;
// return ans;
//}
draw GetPoint(int row, int col)
{
if (row == 0)
return Array[0][col];
return reckon(GetPoint(row - 1, col), GetPoint(row - 1, col + 1), ratio);
}
void display(void)
{
memset(Array, sizeof(Array), 0);
flag = 0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
int number = 0;
for (int i = 0; i < n-1; i++)
{
glColor3f((i*1.0+0.5)/(n*1.0), (i*0.9+0.6)/(n*1.0),1.5);
GLfloat oldx = (FLOAT)point[i].x*1.00 / 400;
GLfloat oldy = (FLOAT)point[i].y*1.00 / 400;
GLfloat newx = (FLOAT)point[i+1].x*1.00 / 400;
GLfloat newy = (FLOAT)point[i+1].y*1.00 / 400;
Array[0][i].x = point[i].x*1.00 / 400;;
Array[0][i].y = point[i].y*1.00 / 400;
Array[0][i+1].x = point[i + 1].x*1.00 / 400;
Array[0][i+1].y = point[i + 1].y*1.00 / 400;
glBegin(GLU_POINT);
glVertex2f(Array[0][i].x, Array[0][i].y);
glBegin(GLU_POINT);
glVertex2f(Array[0][i + 1].x, Array[0][i + 1].y);
glBegin(GL_LINES);
glVertex2f(Array[0][i].x, Array[0][i].y);
glVertex2f(Array[0][i + 1].x, Array[0][i + 1].y);
glEnd();
}
cnt = 0;
int temp = n-1;
for (double deep = 0.0;deep < abs(Array[0][0].x-Array[0][1].x); deep += 0.001/40)
{
ratio = deep / abs(Array[0][0].x - Array[0][1].x)*1.0;
draw poi = GetPoint(n - 1, 0);
Array[n - 1][cnt].x = poi.x;
Array[n - 1][cnt++].y = poi.y;
//while (temp > 0)//wo qu ni ma de while xun huan lao zi xie di gui le
//{
// for (int i = 0; i < temp; i++)
// {
// Poi = reckon(Array[n - 1 - temp][i], Array[n - 1 - temp][i + 1], ratio);
// Array[n - temp][i].x = Poi.x;
// Array[n - temp][i].y = Poi.y;
// }
// temp--;
//}
//draw arg1 = reckon(Array[n - 2][0], Array[n - 2][1], ratio);
//Array[n - 1][cnt].x = arg1.x;
//Array[n - 1][cnt].y = arg1.y;
//cnt += 1;
//for (int i = 0; i < 2; i++)
//{
//draw arg11 = reckon(Array[0][0], Array[0][1],ratio);
//Array[1][0].x = arg11.x;
//Array[1][0].y = arg11.y;
//draw arg12 = reckon(Array[0][1], Array[0][2], ratio);
//Array[1][1].x = arg12.x;
//Array[1][1].y = arg12.y;
//draw arg13 = reckon(Array[0][2], Array[0][3], ratio);
//Array[1][2].x = arg13.x;
//Array[1][2].y = arg13.y;
draw arg14 = reckon(arg11,arg12, ratio);
draw arg15 = reckon(arg12, arg13, ratio);
draw arg16 = reckon(arg14, arg15, ratio);
//draw arg14 = reckon(Array[1][0], Array[1][1], ratio);
//draw arg15 = reckon(Array[1][1], Array[1][2], ratio);
//draw arg16 = reckon(arg14, arg15, ratio);
//Array[2][cnt].x = arg16.x;
//Array[2][cnt++].y = arg16.y;
//}
}
glBegin(GL_POINTS);
for (int i = 0; i < cnt; i++)
{
glColor3f(cos(index), sin(index), 55);
glVertex2f(Array[n - 1][i].x, Array[n - 1][i].y);
}
glutSwapBuffers();
glEnd();
glFlush();
}
void motion(int x, int y)
{
point[index].x = x;
point[index].y = 400 - y;
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
glutMotionFunc(motion);
if (index < n)
{
cout << point[index].x << endl;
cout << point[index++].y << endl;
}
}
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("I love OpenGl");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motion);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
define n为阶数,鼠标点击即可绘制。