1. Напишите программу вывода графика функции y= tg (x)+5*cos (x), с помощью точек, а затем с помощью линий.
1.1 С помощью точек
Решение:
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <math.h>
GLint windW, windH;
void CALLBACK Reshape(int width, int height)
{ glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15,15,-15,15);
glMatrixMode(GL_MODELVIEW);
}
void CALLBACK Draw(void)
{
glClearColor(1.0,1.0,1.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3ub(190,190,190);
for (int i=-4; i<=4; i++)
{
glVertex2f(float(i), -6);//явное преобразование типа
glVertex2f(float(i), 15);
}
for (i=-6; i<=15; i++)
{
glVertex2f(-4, float(i));
glVertex2f(4, float(i));
}
glEnd();
glColor3ub(0,0,0);
glBegin(GL_LINES);
glVertex2f (-5, 0);
glVertex2f(5, 0);
glVertex2f(0,16);
glVertex2f(0,-7);
glEnd();
glPointSize(2);
glBegin(GL_POINTS);
glColor3ub(0,0,255);
int n;
double a,b,dx,x,y;
a=-1.415;
b=1.5;
n=20;
dx=(b-a)/(n-1);
x=a;
y=0;
for (i=1; i<=n; i++)
{
y=float(tan(x)+5*cos(x));
glVertex2d (x, y);
x=x+dx;
}
glEnd();
glFinish();
auxSwapBuffers();
}
void main(int argc, char **argv)
{
windW = 800;
windH = 800;
auxInitPosition(100, 100, windW, windH);
auxInitDisplayMode(AUX_RGB | AUX_DOUBLE);
auxInitWindow("v11_01_1");
glTranslated(0,-4,0);
auxReshapeFunc(Reshape);
auxMainLoop(Draw);
}
Результат:
2.2 с помощью линий
Решение:
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <math.h>
GLint windW, windH;
void CALLBACK Reshape(int width, int height)
{ glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15,15,-15,15);
glMatrixMode(GL_MODELVIEW);
}
void CALLBACK Draw(void)
{
glClearColor(1.0,1.0,1.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3ub(190,190,190);
for (int i=-4; i<=4; i++)
{
glVertex2f(float(i), -6);//явное преобразование типа
glVertex2f(float(i), 15);
}
for (i=-6; i<=15; i++)
{
glVertex2f(-4, float(i));
glVertex2f(4, float(i));
}
glEnd();
glColor3ub(0,0,0);
glBegin(GL_LINES);
glVertex2f (-5, 0);
glVertex2f(5, 0);
glVertex2f(0,16);
glVertex2f(0,-7);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3ub(0,0,255);
int n;
double a,b,dx,x,y;
a=-1.415;
b=1.5;
n=100;
dx=(b-a)/(n-1);
x=a;
y=0;
for (i=1; i<=n; i++)
{
y=float(tan(x)+5*cos(x));
glVertex2d (x, y);
x=x+dx;
}
glEnd();
glFinish();
auxSwapBuffers();
}
void main(int argc, char **argv)
{
windW = 800;
windH = 800;
auxInitPosition(100, 100, windW, windH);
auxInitDisplayMode(AUX_RGB | AUX_DOUBLE);
auxInitWindow("v11_01_1");
glTranslated(0,-4,0);
auxReshapeFunc(Reshape);
auxMainLoop(Draw);
}
Результат:
2. Нарисуйте каркас призмы, в основании которой лежит правильный 14-угольник
Решение:
//v11_02
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <math.h>
void CALLBACK resize (int width, int height)
{
glViewport (0,0,width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-15,15,-10,10, -10,20);
gluLookAt(1,-1,1, 0,0,0, 0,0,1);
glMatrixMode(GL_MODELVIEW);
}
void CALLBACK display (void)
{
GLUquadricObj *quadObj;
quadObj = gluNewQuadric();
glClearColor(1.0,1.0,1.0,1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(0,0,1);
// каркас
gluQuadricDrawStyle(quadObj, GLU_SILHOUETTE);
//призма в основании которой лежит правильный 14-угольник
gluCylinder(quadObj, 2, 2, 5, 14, 14);
glBegin(GL_LINES);
glColor3ub(0, 0, 0);
glVertex2f(6, 0);
glVertex2f(-6, 0);
glVertex2f(0, 6);
glVertex2f(0, -6);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 8);
for (int i=-5; i<=5; i++)
{
if (i!=0)
{
glColor3ub(190,190,190);
glVertex2f(float(i), -5);
glVertex2f(float(i), 5);
}
}
for (i=-5; i<=5; i++)
{
if (i!=0)
{
glColor3ub(190,190,190);
glVertex2f(-5, float(i));
glVertex2f(5, float(i));
}
}
glEnd();
glFinish();
auxSwapBuffers();
}
void main ()
{
auxInitPosition (100,100,800,600);
auxInitDisplayMode (AUX_RGB | AUX_DEPTH | AUX_DOUBLE);
auxInitWindow ("v11_02");
glScaled(1.5, 1.5, 1.5);
glTranslated(0,0,-1);
auxIdleFunc (display);
auxReshapeFunc(resize);
glEnable (GL_DEPTH_TEST);
auxMainLoop(display);
}
Результат:
3. Напишите программу вывода графика функции x=2*sin(z)*cos(y)-3*tg(y) используя алгоритм плавающего горизонта. ............