vẽ hệ mặt trời

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down

vẽ hệ mặt trời

Bài gửi by htn111 on 10/12/09, 03:44 am

file Affine.h
#ifndef AFFINE_H
#define AFFINE_H

#include "math.h"
#include "string.h"

typedef float matrix33[3][3];
struct colorentry{
GLubyte Red,Green,Blue;
colorentry()
{ Red=Green=Blue=0;
}
colorentry(GLubyte R,GLubyte G,GLubyte B)
{ Red=R;
Green=G;
Blue=B;
}
int operator == (colorentry &c)
{ return (Red==c.Red)&&(Green==c.Green)&&(Blue==c.Blue);
}
int operator != (colorentry &c)
{ return !(*this==c);
}
};
void matrix_zero(matrix33 m)
{ for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
m[i][j]=0;
}
void matrix_id(matrix33 m)
{ matrix_zero(m);
m[0][0] = 1;
m[1][1] = 1;
m[2][2] = 1;
}
void mul_matrix(int x, int y, matrix33 m, int &xout, int &yout)
{ xout = x * m[0][0] + y * m[1][0] + m[2][0];
yout = x * m[0][1] + y * m[1][1] + m[2][1];
}
void mul_matrix(matrix33 ma, matrix33 mb, matrix33 mout)
{ matrix33 mtmp;
matrix_zero(mtmp);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{ float sum = 0;
for(int k=0; k<3; k++)
{ sum += ma[i][k] * mb[k][j];
}
mtmp[i][j] = sum;
}
memcpy(mout, mtmp, sizeof(mtmp));
}
void matrix_translate(matrix33 m, float dx, float dy)
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[2][0] = dx;
mtmp[2][1] = dy;
mul_matrix(m, mtmp, m);
}
void matrix_scale(matrix33 m, float sx, float sy)
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[0][0] = sx;
mtmp[1][1] = sy;
mul_matrix(m, mtmp, m);
}
//phep quay quanh tam 0
void matrix_rotate(matrix33 m, float angle)
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[0][0] = cos(angle);
mtmp[0][1] = sin(angle);
mtmp[1][0] = - sin(angle);
mtmp[1][1] = cos(angle);
mul_matrix(m, mtmp, m);
}
//phep quay quanh tam I
void matrix_rotate(matrix33 m, float angle, float x, float y)
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[0][0] = cos(angle);
mtmp[0][1] = sin(angle);
mtmp[1][0] = - sin(angle);
mtmp[1][1] = cos(angle);
mtmp[2][0] = x * (1 - cos(angle)) + y * sin(angle);
mtmp[2][1] = y * (1 - cos(angle)) - x * sin(angle);;

mul_matrix(m, mtmp, m);
}
//phep doi xung
void matrix_reflection(matrix33 m, int Ox, int Oy)
{ if((Ox==1&&Oy==-1)||(Ox==-1&&Oy==1))
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[0][0] = Ox;
mtmp[1][1] = Oy;
mul_matrix(m, mtmp, m);
}
}
//phep bien dang
void matrix_shear(matrix33 m,int shx,int shy)
{ matrix33 mtmp;
matrix_id(mtmp);
mtmp[0][0] = shx;
mtmp[1][1] = shy;
mul_matrix(m, mtmp, m);
}

#endif

htn111
VIP mem
VIP mem

Tổng số bài gửi : 28
Số điểm : 70
Số lần được cám ơn : 15
Ngày đến diễn đàn: : 26/08/2009
Tuổi : 28

Xem lý lịch thành viên

Về Đầu Trang Go down

Re: vẽ hệ mặt trời

Bài gửi by htn111 on 10/12/09, 03:47 am

file main.cpp
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

#include "stdio.h"
#include "math.h"
#include "glut.h"
#include "affine.h"

void OnDraw(void);// su kien ve
void OnKeyDown(unsigned char key, int x, int y);
void OnSize(int width, int height);
//ham su kien nhan chuot
void OnMouse(int btn, int state, int x, int y);

//ham su kien di chuyen chuot va nhan
void OnMotion(int x, int y);
void OnIdle();
// ham su kien thoi gian
void OnTime(int ms);
void DrawRectangle_Rotate(int xc,int yc,int a, int b, int alpha,
colorentry c) ;

void veelip(int xc,int yc,int rx,int ry,int st_a,int en_a,colorentry c);

void FillCircle(int xc,int yc,int r,colorentry c) ;
void HeMatTroi(int alpha1,int alpha2);

#define M_PI 3.1415926535897932384626433832795
#define RAD(goc) ((goc)*(M_PI/180.0))
int _xc, _yc;
float _alpha1=30;
float _alpha2=60;
float alpha = 30;

int main()
{ /* Thiet lap thuoc tinh cua so */
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,640);

/* Tao cua so */
glutCreateWindow("Bai Affine!");

/* Mot so lenh khoi tao */
glClearColor(1.0,1.0,1.0,0.0);

/* Dang ky su kien */
glutDisplayFunc(OnDraw);
glutKeyboardFunc(OnKeyDown);
glutReshapeFunc(OnSize);
glutIdleFunc(OnDraw);
glutMouseFunc(OnMouse);
glutMotionFunc(OnMotion);
glutTimerFunc(100,OnTime,100);
/* Vong lap thong diep */
glutMainLoop();
return 0;
}// main

void OnDraw()
{ /* Xoa man hinh */
glClear(GL_COLOR_BUFFER_BIT);

int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);

// Display
int xMax = glutGet(GLUT_WINDOW_WIDTH);
int yMax = glutGet(GLUT_WINDOW_HEIGHT);
// DrawRectangle_Rotate(xMax/2,yMax/2,200,100,_alpha1,
colorentry(255,0,0));

/* Ve hinh */
HeMatTroi(_alpha1,_alpha2);


/* Ket xuat ket qua ve */
glFlush();
/* Chuyen hau canh va tien canh */
glutSwapBuffers();
}// OnDraw

void OnKeyDown(unsigned char key, int x, int y)
{ printf("%d\n",key);
switch(key)
{case 27: // ESC
case 3: // Ctrl+C
exit(0);
break;
}// switch
}// OnKeyDown
void OnSize(int width, int height)
{ glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,width-1,0,height-1);
}
void OnMouse(int btn, int state, int x, int y)
{ int xMax = glutGet(GLUT_WINDOW_WIDTH);
int yMax = glutGet(GLUT_WINDOW_HEIGHT);
_xc = x;
_yc = yMax-y;
}// OnMouse
void OnMotion(int x, int y)
{ int xMax = glutGet(GLUT_WINDOW_WIDTH);
int yMax = glutGet(GLUT_WINDOW_HEIGHT);
_xc = x;
_yc = yMax-y;
}// OnMotion

void OnIdle()
{ // yeu cau ve lai man hinh cua so
glutPostRedisplay();
}// OnIdle
void OnTime(int ms)
{ // Ham xu ly thoi gian
_alpha1 = int((_alpha1+5))%360;
_alpha2 = int((_alpha2+20))%360;

// Lap lai cho lan ke tiep
glutTimerFunc(ms,OnTime,ms);
}// OnTime
void DrawRectangle_Rotate(int xc,int yc,int a, int b, int alpha,
colorentry c)
{ int dPoints[][2] = { {-a,b},{a,b},{a,-b},{-a,-b} };
int n = 4; float x,y; int xout,yout;
matrix33 mkq; matrix_id(mkq);
matrix_rotate(mkq,RAD(alpha),xc,yc); //tam bat ki
// matrix_translate(mkq, -xc, -yc); dichuyen ve tam 0
// matrix_rotate(mkq,RAD(alpha)); quay quanh tam 0
// matrix_translate(mkq, xc, yc); tinh tien lai diem ban dau
glBegin(GL_LINE_LOOP);
glColor3ub(c.Red,c.Green,c.Blue);
for(int i=0;i
{ x = xc + dPoints[i][0]; y = yc + dPoints[i][1];
mul_matrix(x,y,mkq,xout,yout);
glVertex2f(xout,yout);
}// for i
glEnd();
}// DrawRectangle_Rotate
//ve he mat troi
void veelip(int xc,int yc,int rx,int ry,int st_a,int en_a,colorentry c)
{ int n=100;
//float da=2*P/n;
while((en_a - st_a) <0) en_a += 360;
float da =RAD((en_a - st_a)%360)/n;
// glColor3ub(255,255,255);
glBegin(GL_POLYGON);
{ for(int i=0;i
{ int x=xc+rx*cos(RAD(st_a)+i*da);
int y=yc+ry*sin(RAD(st_a)+i*da);
glVertex2f(x,y);
}
}
glEnd();
}
void FillCircle(int xc,int yc,int r,colorentry c)
{ int n=2*M_PI*r;
double da=(2*M_PI)/n;
float xi,yi;

glBegin(GL_POLYGON);
glColor3ub(c.Red,c.Green,c.Blue);
for(int i=0;i
{ xi= xc + r*cos(i*da);
yi= yc + r*sin(i*da);
glVertex2f(xi,yi);
}
glEnd();
}
void HeMatTroi(int alpha1,int alpha2)
{ int xMax = glutGet(GLUT_WINDOW_WIDTH);
int yMax = glutGet(GLUT_WINDOW_HEIGHT);

int x,y, xout, yout;

matrix33 msun,mtia,mkq,mearth,mmoon;
int ntia = 20;

matrix_id(msun);
matrix_id(mtia);
///////////////////
matrix_translate(msun,xMax/2,yMax/2);

mul_matrix(0,0,msun,xout,yout);
FillCircle(xout,yout,100,colorentry(255,0,0));

/////////2 con mat
matrix33 mat,mieng;
matrix_id(mat);

mul_matrix(mat,msun,mkq);

mul_matrix(40,40,mkq,xout,yout);
FillCircle(xout,yout,10,colorentry(255,255,0));
FillCircle(xout-80,yout,10,colorentry(255,255,0));

////////mieng
matrix_id(mieng);

mul_matrix(mieng,msun,mkq);

mul_matrix(0,-30,mkq,xout,yout);
veelip(xout,yout,50,25,180,360,colorentry(0,255,0));
// mul_matrix(-30,-30,mkq,xout,yout);

//////////tia
for(int i=0;i
{ glBegin(GL_LINES);
glColor3ub(255,0,0);

mul_matrix(mtia,msun,mkq);

mul_matrix(0,105,mkq,xout,yout);
glVertex2f(xout,yout);

mul_matrix(0,125,mkq,xout,yout);
glVertex2f(xout,yout);
glEnd();
matrix_rotate(mtia,2*M_PI/ntia);
}
//ve trai dat
matrix_id(mearth);
matrix_rotate(mearth,RAD(alpha1));

mul_matrix(mearth,msun,mkq);

mul_matrix(200,0,mkq,xout,yout);
FillCircle(xout,yout,50,colorentry(0,0,255));
//ve mat trang
matrix_id(mmoon);

matrix_translate(mmoon,-200,0);
matrix_rotate(mmoon,RAD(alpha2));
matrix_translate(mmoon,xout,yout);

mul_matrix(300,0,mmoon,xout,yout);
FillCircle(xout,yout,20,colorentry(255,255,0));
}

htn111
VIP mem
VIP mem

Tổng số bài gửi : 28
Số điểm : 70
Số lần được cám ơn : 15
Ngày đến diễn đàn: : 26/08/2009
Tuổi : 28

Xem lý lịch thành viên

Về Đầu Trang Go down

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang


 
Permissions in this forum:
Bạn không có quyền trả lời bài viết