#include <windows.h> //此头文件一定要放在最前
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <conio.h>
#include "time.h"
using namespace std;
//添加这3条语句
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glaux.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
//这句是不让控制台窗体出现,如果想要出现,去掉即可。
float root2 = sqrt(2);
float base = 2*3.14159/10;
int DirectionX = 20;
int DirectionY = 30;
int WinWidth =800;
int WinHeight =600;
int xx = 200;
int yy = 200;
float angle = 0.7;
int R1 = 75;
int R2 = 36;
int cxx = 500;
int cyy = 500;
int cDirectionX = 20;
int cDirectionY = 30;
int cR = 100;
float pi = 3.14;
void drawangle(int x0,int y0,int x1,int y1,int x2,int y2)
{
glBegin(GL_TRIANGLES);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
}
void drawfive(int R1,int R2,float angle,int x,int y,float r1,float g,float b,float r2)
{
int i;
int x0,y0,x1,y1;
float a1 = cos(angle-0.01);
float a2 = sin(angle-0.01);
x0 = x + R1*a1;
y0 = y + R1*a2;
for(i=1;i<=10;i++)
{
float b1 = cos(angle+base*i);
float b2 = sin(angle+base*i);
if(i%2==1)
{
glColor3f(r1,g,b);
x1 = x+R2*b1;
y1 = y+R2*b2;
}
else
{
glColor3f(r2,g,b);
x1 = x+R1*b1;
y1 = y+R1*b2;
}
drawangle(x,y,x0,y0,x1,y1);
x0 = x1;
y0 = y1;
}
}
void PloyAppCircle(int x0,int y0,int R,int r,int g,int b,int err)
{//误差控制的多边形逼近圆弧
//float angle = 0.1;
float angle = 2*acos((R-err)*1.0/R);
printf("%f \n",angle);
int n = 2*pi/angle;
angle = 2*pi/n;
printf("%d\n",n);
int i;
glColor3f(r,g,b);
glBegin(GL_LINE_LOOP);
int x,y;
for(i=0;i<n;i++)
{
x = x0 + R*cos(i*angle);
y = y0 + R*sin(i*angle);
glVertex2i(x,y);
printf("%d %d\n",x,y);
}
glEnd();
}
void CALLBACK draw()
{
int LineWidth = 5;
int SmallLineWidth = LineWidth/2;
glColor3f(1,0,0); //红色
glLineWidth(LineWidth);//float类型,线宽取值0--10.0
glBegin(GL_LINE_LOOP);//线段,绘制边框
glVertex2f(0+SmallLineWidth,0+SmallLineWidth);
glVertex2f(0+SmallLineWidth,WinHeight-SmallLineWidth);
glVertex2f(WinWidth-SmallLineWidth,WinHeight-SmallLineWidth);
glVertex2f(WinWidth-SmallLineWidth,0);
glEnd();
angle += base;
xx+= DirectionX;
yy+= DirectionY;
if((xx+R1)>=WinWidth || (xx -R1)<=0) DirectionX= -DirectionX;
if((yy+R1)>=WinHeight || (yy -R1)<=0) DirectionY = -DirectionY;
if(angle>=6.2)angle=0;
drawfive(R1,R2,angle,xx+DirectionX,yy+DirectionY,1,0,0,0.5);
srand(time(NULL));
int num = rand()%100;
float scale = num/100.0;
if(scale<0.5) scale+=0.5;
drawfive(scale*R1,scale*R2,angle,300,300,0.5,0.5,0.5,1);
//circle
cDirectionX = rand()%60 -30;
cDirectionY = rand()%40 -20;
cxx+= cDirectionX;
cyy+= cDirectionY;
if((cxx+cR)>=WinWidth)
{
cDirectionX= -cDirectionX;
cxx = WinWidth-cR;
}
else if((cxx -cR)<=0)
{
cDirectionX= -cDirectionX;
cxx = cR;
}
if((cyy+cR)>=WinHeight)
{
cDirectionY = -cDirectionY;
cyy = WinHeight-cR;
}
else if((cyy -cR)<=0)
{
cDirectionY = -cDirectionY;
cyy = cR;
}
//cxx+= cDirectionX;
// cyy+= cDirectionY;
glLineWidth(1);
PloyAppCircle(cxx,cyy,cR,1,0,0,1);
Sleep(300);
}
void CALLBACK change()
{
glClear(GL_COLOR_BUFFER_BIT);
draw();
glFinish();
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);//黑色背景
}
void main()
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(20 ,20 ,WinWidth ,WinHeight);//定义窗口的初始位置,前两个参数(x0,y0)是窗口左上角的屏幕坐标,后两个参数(w,h)为窗口宽和高
auxInitWindow("CGOpenGL");
init();
auxIdleFunc(change);//一定要在auxMainLoop前面
auxMainLoop(draw);
}
图形学第四次作业
最新推荐文章于 2020-09-07 09:38:53 发布