Chào mọi người , hiện tại mình đang học C tới phần vòng lặp. Thì gặp ngay bài tập này, mình không biết bài này làm như thế nào và có thuật toán ra sao.
Mình có Google thử thì kiếm được source code nhưng mình không hiểu nó lắm không biết nó thuộc ngôn ngữ nào .Rất mong các bạn giúp mình thuật toán của bài bài này với ạ .
#include <windows.h>
#include <GL/gl.h>
#include <GL/glaux.h>
#include <GL/glut.h>
#include <cmath>
#pragma comment(lib,"glaux.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"opengl32.lib")
void DrawStripes()// ve tat ca cac soc
{
float fStartX;// diem tren cung phia ben trai cua 1 gach soc
float fStartY; // diem tren cung phia ben phai
float fEndX;//
float fEndY;
for(int i=0;i <13; ++i)
{
if(i % 2 ==0){
glColor3f(204.0/255.0,0.0,0.0);
}
else{
glColor3f(1.0,1.0,1.0);
}
fStartX = 0.0;
fEndX = 1.0;
fStartY = i *(1.0/13.0);
fEndY = (i + 1)*(1.0/13.0);
if(i > 5){
fStartX = 0.76/1.9;
}
glBegin(GL_QUADS);
glVertex3f(fStartX,fStartY,0.0);
glVertex3f(fEndX,fStartY,0.0);
glVertex3f(fEndX,fEndY,0.0);
glVertex3f(fStartX,fEndY ,0.0);
glEnd();
}
}
void Draw1Star(float fX,float fY)//ve 1 ngoi sao khi biet toa do cua no
{
const float kfPi = 3.1415926535897932384626433832795;
const float kfRadius = 0.0616/2.0;
const float kfInnerRadius = kfRadius *(1.0/(sin((2.0*kfPi)/5.0)*2.0*cos(kfPi/10.0)+sin((3.0*kfPi)/10.0)));
glColor3f(1.0,1.0,1.0);//InnerRadius:duong tron trong,Radius:duong tron ngoai
glBegin(GL_TRIANGLE_FAN);
glVertex3f(fX,fY,0.0);
for(int iVertIndex =0;iVertIndex <10 ;iVertIndex++ ){
float fAngleStart = kfPi/2.0 +(iVertIndex *2.0*kfPi)/10.0;
float fAngleEnd = fAngleStart + kfPi/5.0;
if(iVertIndex % 2 == 0){
glVertex3f(fX + kfRadius * cos(fAngleStart)/1.9,fY + kfRadius * sin(fAngleStart),0.0 );
glVertex3f(fX + kfInnerRadius * cos(fAngleEnd)/1.9,fY + kfInnerRadius * sin(fAngleEnd),0.0 );
}
else
{
glVertex3f(fX + kfInnerRadius * cos(fAngleStart)/1.9,fY + kfInnerRadius * sin(fAngleStart),0.0 );
glVertex3f(fX + kfRadius* cos(fAngleEnd)/1.9,fY + kfRadius * sin(fAngleEnd),0.0 );
}
}
glEnd();
}
void DrawAllStars()//ve 50 ngoi sao
{
for(int iStarRow = 0;iStarRow <9 ;++ iStarRow )
{
float fY = 6.0/13.0+(iStarRow + 1)*((7.0/13.0)/10);
if(iStarRow % 2 == 0)
{
for(int iStarCol = 0;iStarCol < 6;++iStarCol)
{
Draw1Star((iStarCol)*((0.76/1.9)/6.0)+ (0.76/1.9)/12.0,fY);
}
}
else
{
for(int iStarCol = 0;iStarCol < 5;++iStarCol)
{
Draw1Star((iStarCol +1)*((0.76/1.9)/6.0),fY);
}
}
}
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
DrawStripes();//ve cac soc cua la co
DrawAllStars();//ve tat ca cac ngoi sao
glFlush();//hien thi ket qua neu chuong trinh dot ngot dung lai
}
void Initialize()//khoi tao khung nhin va phep chieu
{
glClearColor(0.0,0.0,102.0/255.0,0.0);//xoa mau
glMatrixMode(GL_PROJECTION);//phep chieu
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);//chieu truc giao
}
int main(int iArgc,char** cppArgv)
{
glutInit(&iArgc,cppArgv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(950,500);
glutInitWindowPosition(200,200);
glutCreateWindow("xõa.net");
Initialize();
glutDisplayFunc(Draw);//goi ham ve chinh
glutMainLoop();//vong lap hien ket qua
return 0;
}