Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <GL/freeglut.h>
  5. #include "Point.h"
  6.  
  7.  
  8. #define X_MIN_RANGE 0.0
  9. #define X_MAX_RANGE 10.0
  10. #define Y_MIN_RANGE 0.0
  11. #define Y_MAX_RANGE 10.0
  12.  
  13. using namespace std;
  14.  
  15. void initGLUT () {
  16.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  17.     glutInitWindowSize(400, 400);
  18.     glutInitWindowPosition(100, 100);
  19.     glutCreateWindow("triunghi cu punct");
  20. }
  21.  
  22. void drawAxis() {
  23.     glLineWidth(2.0);
  24.     glColor3f(0.0, 0.0, 0.0);
  25.     glBegin(GL_LINES);
  26.         glVertex2f(0.0, 0.0);
  27.         glVertex2f(X_MAX_RANGE, 0.0);
  28.         glVertex2f(0.0, 0.0);
  29.         glVertex2f(0.0, Y_MAX_RANGE);
  30.  
  31.         for (int i = 0; i <= 10; i++) {
  32.             glVertex2f(i, 0.0);
  33.             glVertex2f(i, 0.3);
  34.  
  35.             glVertex2f(0.0, i);
  36.             glVertex2f(0.3, i);
  37.         }
  38.     glEnd();
  39. }
  40.  
  41. Point A, B, C, M;
  42. void draw() {
  43.     glClearColor(1.0, 1.0, 1.0, 0.0);
  44.     glClear(GL_COLOR_BUFFER_BIT);
  45.  
  46.     drawAxis();
  47.    
  48.     glColor3f(0.0, 0.0, 0.0);
  49.     glLineWidth(2.0);
  50.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  51.     glBegin(GL_TRIANGLES);
  52.         glVertex2f(A.getX(), A.getY());
  53.         glVertex2f(B.getX(), B.getY());
  54.         glVertex2f(C.getX(), C.getY());
  55.     glEnd();
  56.  
  57.     M.draw();
  58.  
  59.     glFlush(); 
  60. }
  61.  
  62. float computeArea (Point A, Point B, Point C) {
  63.     float xA = A.getX();
  64.     float yA = A.getY();
  65.     float xB = B.getX();
  66.     float yB = B.getY();
  67.     float xC = C.getX();
  68.     float yC = C.getY();
  69.  
  70.     float delta = xA * (yB - yC) +
  71.                   xB * (yC - yA) +
  72.                   xC * (yA - yB);
  73.     float area = abs(delta)/2;
  74.    
  75.     return area;
  76. }
  77.  
  78. void checkIfInside () {
  79.     float areaABC = computeArea (A, B, C);
  80.     float areaAMB = computeArea (A, M, B);
  81.     float areaBMC = computeArea (B, M, C);
  82.     float areaCMA = computeArea (C, M, A);
  83.  
  84.     cout << areaABC << endl;
  85.     cout << areaAMB + areaBMC + areaCMA << endl;
  86.     cout << areaAMB * areaBMC * areaCMA << endl;
  87.  
  88.     M.setSize(5.0);
  89.     if (areaABC == areaAMB + areaBMC + areaCMA) {
  90.         // set M's color and size as green 
  91.         M.setColor(0.0, 1.0, 0.0);
  92.         if (areaAMB * areaBMC * areaCMA == 0) {
  93.             // set M's color as blue
  94.             M.setColor(0.0, 0.0, 1.0);
  95.         }
  96.     }
  97.     else {
  98.         // set M's color and size as red
  99.         M.setColor(1.0, 0.0, 0.0);
  100.     }
  101.    
  102. }
  103.  
  104. void initPoints(int nargs, char* args[]) {
  105.     stringstream converter;
  106.     for (int i = 1; i < nargs; i++) {
  107.         converter << ' ' << args[i];
  108.     }
  109.  
  110.     GLfloat x, y;
  111.     converter >> x >> y;
  112.     A = Point(x, y);
  113.    
  114.     converter >> x >> y;
  115.     B = Point(x, y);
  116.  
  117.     converter >> x >> y;
  118.     C = Point(x, y);
  119.  
  120.     converter >> x >> y;
  121.     M = Point(x, y);
  122. }
  123.  
  124. void initGL () {
  125.     glMatrixMode(GL_PROJECTION);
  126.     gluOrtho2D(X_MIN_RANGE, X_MAX_RANGE, Y_MIN_RANGE, Y_MAX_RANGE);
  127.  
  128.     glutDisplayFunc(draw);
  129. }
  130.  
  131. int main (int nargs, char* args[]) {
  132.     glutInit(&nargs, args);
  133.     initGLUT();
  134.     initGL();
  135.  
  136.     initPoints(nargs, args);
  137.     checkIfInside();
  138.    
  139.     glutMainLoop();
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement