Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. // laborator5.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <windows.h> // sunt mentionate fisiere (biblioteci) care urmeaza sa fie incluse
  7. #include <gl/freeglut.h> // nu trebuie uitat freeglut.h (sau glut.h sau gl.h & glu.h)
  8. #include <stdlib.h>
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. struct Point{
  14. double x,y;
  15. };
  16.  
  17. Point A,B,C,M;
  18. bool IsInside=false;
  19.  
  20. void drawBitmapText(char *string,float x,float y)
  21. {
  22. char *c;
  23. glRasterPos2f(x+10, y);
  24.  
  25. for (c=string; *c != '.'; c++)
  26. {
  27. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
  28. }
  29. }
  30.  
  31. void init (void) // initializare fereastra de vizualizare
  32. {
  33. glClearColor (1.0, 1.0, 1.0, 0.0); // precizeaza culoarea de fond a ferestrei de vizualizare
  34.  
  35. glMatrixMode (GL_PROJECTION); // se precizeaza este vorba de o reprezentare 2D, realizata prin proiectie ortogonala
  36. gluOrtho2D (0.0, 800.0, 0.0, 600.0); // sunt indicate coordonatele extreme ale ferestrei de vizualizare
  37. }
  38. void desen (void) // procedura desenare
  39. {
  40.  
  41. glPointSize (5.0);
  42.  
  43. glBegin (GL_POINTS);
  44. glColor3f (0.0, 0.0, 1.0); // culoarea punctului de intersectie: albastru
  45. glVertex2i (A.x, A.y);
  46. glEnd ( );
  47. glBegin (GL_POINTS);
  48. glColor3f (0.0, 0.0, 1.0); // culoarea punctului de intersectie: albastru
  49. glVertex2i (B.x, B.y);
  50. glEnd ( );
  51. glBegin (GL_POINTS);
  52. glColor3f (0.0, 0.0, 1.0); // culoarea punctului de intersectie: albastru
  53. glVertex2i (C.x, C.y);
  54. glEnd ( );
  55. glBegin (GL_POINTS);
  56. glColor3f (1.0, 0.0, 0.0); // culoarea punctului de intersectie: albastru
  57. glVertex2i (M.x,M.y);
  58. glEnd ( );
  59.  
  60.  
  61. // segmentele
  62. glLineWidth (2.0);
  63. glBegin (GL_LINES);
  64. glColor3f (0.0, 1.0, 0.0); // culoarea celui de-al doilea segment: verde
  65. glVertex2i (A.x, A.y);
  66. glVertex2i (B.x, B.y);
  67.  
  68. glColor3f (0.0, 1.0, 0.0); // culoarea celui de-al doilea segment: verde
  69. glVertex2i (B.x, B.y);
  70. glVertex2i (C.x, C.y);
  71.  
  72. glColor3f (0.0, 1.0, 0.0); // culoarea celui de-al doilea segment: verde
  73. glVertex2i (C.x, C.y);
  74. glVertex2i (A.x, A.y);
  75. glEnd ( );
  76.  
  77. glColor3f (1.0, 0.0, 0.0);
  78.  
  79. if(IsInside==true)
  80. {
  81.  
  82. char *p = "Inside.";
  83.  
  84. drawBitmapText(p,M.x,M.y);
  85. }
  86. else
  87. {
  88.  
  89. char *p = "Outside.";
  90.  
  91. drawBitmapText(p,M.x,M.y);
  92. }
  93.  
  94.  
  95.  
  96. // punctul de intersectie
  97.  
  98.  
  99.  
  100. glFlush ( ); // proceseaza procedurile OpenGL cat mai rapid
  101. }
  102.  
  103. float area(Point x1, Point x2, Point x3)
  104. {
  105. return abs((x1.x*(x2.y-x3.y) + x2.x*(x3.y-x1.y)+ x3.x*(x1.y-x2.y))/2.0);
  106. }
  107.  
  108. bool isInside(Point x1, Point x2, Point x3, Point x)
  109. {
  110. /* Calculate area of triangle ABC */
  111. double A = area (x1,x2,x3);
  112.  
  113. /* Calculate area of triangle PBC */
  114. double A1 = area (x,x2,x3);
  115.  
  116. /* Calculate area of triangle PAC */
  117. double A2 = area (x1,x,x3);
  118.  
  119. /* Calculate area of triangle PAB */
  120. double A3 = area (x1,x2,x);
  121.  
  122. /* Check if sum of A1, A2 and A3 is same as A */
  123. return (A == A1 + A2 + A3);
  124. }
  125.  
  126. void main (int argc, char** argv)
  127. {
  128.  
  129. cout<<"xA=";cin>>A.x;
  130. cout<<"yA=";cin>>A.y;
  131.  
  132. cout<<"xB=";cin>>B.x;
  133. cout<<"yB=";cin>>B.y;
  134.  
  135. cout<<"xC=";cin>>C.x;
  136. cout<<"yC=";cin>>C.y;
  137.  
  138. cout<<"xM=";cin>>M.x;
  139. cout<<"yM=";cin>>M.y;
  140.  
  141. IsInside=isInside(A,B,C,M);
  142.  
  143. glutInit (&argc, argv); // initializare GLUT
  144. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // se utilizeaza un singur buffer | modul de colorare RedGreenBlue (= default)
  145. glutInitWindowPosition (100, 100); // pozitia initiala a ferestrei de vizualizare (in coordonate ecran)
  146. glutInitWindowSize (800, 600); // dimensiunile ferestrei
  147. glutCreateWindow ("Lab 3- Exercitiul 4"); // creeaza fereastra, indicand numele ferestrei de vizualizare - apare in partea superioara
  148.  
  149. init (); // executa procedura de initializare
  150. glClear (GL_COLOR_BUFFER_BIT); // reprezentare si colorare fereastra de vizualizare
  151. glutDisplayFunc (desen); // procedura desen este invocata ori de cate ori este nevoie
  152. glutMainLoop ( ); // ultima instructiune a programului, asteapta (eventuale) noi date de intrare
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement