1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include "Vec2.h"
#include "Triangles.h"
#include "StringFuncts.h"
#include <SDL.h>
#include <iostream>
#include <random>
#include <GL/glut.h>
void PrintText(float x, float y, std::string& text)
{
glRasterPos2f(x, y);
for (unsigned int k = 0; k < text.size(); k++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[k]);
}
}
float CalcArea(Vec2f& a, Vec2f& b, Vec2f& c)
{
if (a.x < b.x)
base = b.x - a.x;
else if (a.x > b.x)
base = a.x - b.x;
if (a.y < c.y)
height = c.y - a.y;
else if (a.y > c.y)
height = a.y - c.y;
area = height * 0.5 * base;
return area;
}
float BaseText(Vec2f& a, Vec2f& b)
{
if (a.y < b.y) return a.y - 5;
else if (a.y > b.y) return a.y + 5;
else return 0;
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 100.0, 0, 0, 0, 0, 1.0, 0);
CalcArea(PointA, PointB, PointC);
glBegin(GL_TRIANGLES);
glVertex3f(PointA.x, PointA.y, 0);
glVertex3f(PointB.x, PointA.y, 0);
glVertex3f(PointC.x, PointC.y, 0);
glColor3f(PointA.x, PointB.x, PointC.x);
glEnd();
PrintText(15, 30, Area);
PrintText(30, 30, ToString(area));
PrintText(-36, height, Height);
PrintText(-25, height, ToString(height));
PrintText(base - 10, BaseText(PointA, PointC), Base);
PrintText(base, BaseText(PointA, PointC), ToString(base));
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(300, 300);
glutCreateWindow("Triangles!");
glutDisplayFunc(Draw);
CalcArea(PointA, PointB, PointC);
std::cout << "Area: " << area << "cm^2\n";
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 1000.0);
glutMainLoop();
return 0;
}
|