Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <math.h>
  3. #include <iostream>
  4. #define PI 3.14159265359
  5. #define R 20
  6. typedef struct point2d{ GLfloat x, y; } POINT2D;
  7. GLint dragged = -1;
  8. POINT2D moveline[6] = {200, 100, 500, 100, 200, 75, 500, 75, 200, 50, 500, 50};
  9. GLdouble winHeight = 500.0;
  10. GLdouble winWidth = 700.0;
  11. POINT2D points[3] = {210, 100, 210, 75, 210, 50};
  12. //a kör osztály
  13. class Circle{
  14. public:
  15.     GLdouble ox, oy, fok=0, t=4*PI/180;
  16.     Circle(GLdouble xk=350, GLdouble yk=250): ox(xk), oy(yk){}
  17.  
  18. //kör kirajzolása
  19.     void Draw()
  20.     {
  21.         glColor3f(0.0, 0.0, 0.0);
  22.         glBegin(GL_LINE_LOOP);
  23.         for (int i = 0; i <= 360; ++i)
  24.         {
  25.             glVertex2d(ox + R * cos(fok), oy + R * sin(fok));
  26.             fok = (i*PI / 180);
  27.         }
  28.         glEnd();
  29.     }
  30. //valamelyik egyenes kirajzolása nem tudom melyik, majd kiderül mindjárt
  31.     void DrawLine()
  32.     {
  33.         glLineWidth(1.0);
  34.         glColor3f(0.0, 0.0, 0.0);
  35.         for (GLfloat i = 0; i <= 4*PI; i += 2*PI/180)
  36.         {
  37.             glBegin(GL_LINE_STRIP);
  38.             for (GLfloat t = 0; t <= 4*PI; t+= PI/180)
  39.             {
  40.                 glVertex2d(ox + R*(cos(t + fok) + t*sin(t + fok)), oy + R*(sin(t + fok) - t*cos(t + fok)));
  41.             }
  42.             glEnd();
  43.             fok = (i* (PI / 180));
  44.         }
  45.  
  46.     }
  47. };
  48.  
  49. POINT2D initPoint2D(GLfloat x, GLfloat y) {
  50.     POINT2D P;
  51.     P.x = x;
  52.     P.y = y;
  53.     return P;
  54. }
  55.  
  56. void init()
  57. {
  58.     glClearColor(0.5, 0.5, 0.9, 0.0);  
  59.     glShadeModel(GL_SMOOTH);
  60.     glMatrixMode(GL_PROJECTION);       
  61.     gluOrtho2D(0.0, 700.0, 0.0, 500.0);
  62.     glPointSize(7.0);
  63.     glEnable(GL_POINT_SMOOTH);
  64. }
  65. Circle circle(350, 250);
  66. void display()
  67. {
  68.     glClear(GL_COLOR_BUFFER_BIT);
  69.     circle.Draw();
  70.     circle.DrawLine();
  71.  
  72.     glColor3f(0.0, 1.0, 0.0);
  73.     glBegin(GL_LINES);
  74.     for (int i = 0; i < 6; i++)
  75.         glVertex2f(moveline[i].x, moveline[i].y);
  76.     glEnd();
  77.  
  78.     glColor3f(1.0, 0.0, 0.0);
  79.     glBegin(GL_POINTS);
  80.     for (int i = 0; i < 6; i++)
  81.         glVertex2f(moveline[i].x, moveline[i].y);
  82.     glEnd();
  83.  
  84.     glColor3f(0.0, 0.0, 1.0);
  85.     glBegin(GL_POINTS);
  86.     for (int i = 0; i < 3; ++i)
  87.     {
  88.         glVertex2f(points[i].x, points[i].y);
  89.  
  90.         if (points[i].x >= moveline[i/2].x && points[i].x <= moveline[i/2].x)
  91.         {
  92.             points[i].y = moveline[i/2].y;
  93.         }
  94.           /*      if (points[i].x <= moveline[i].x)
  95.                 {
  96.                         points[i].x = moveline[i].x;
  97.                         points[i].y = moveline[i].y;
  98.                         glVertex2f(points[i].x, points[i].y);
  99.                 }
  100.  
  101.                 if (points[i].x >= moveline[i+1].x)
  102.                 {
  103.                         points[i].x = moveline[i+1].x;
  104.                         points[i].y = moveline[i+1].y;
  105.                         glVertex2f(points[i].x, points[i].y);
  106.                 }
  107.  
  108.                 if (points[i].x > moveline[i].x && points[i].x < moveline[i+1].x)
  109.                 {
  110.                         points[i].y = moveline[i].y;
  111.                         glVertex2f(points[i].x, points[i].y);
  112.                 }*/
  113.     }
  114.     glEnd();
  115.  
  116.     glutSwapBuffers();
  117. }
  118.  
  119. GLfloat dist2(POINT2D P1, POINT2D P2) {
  120.     GLfloat t1 = P1.x - P2.x;
  121.     GLfloat t2 = P1.y - P2.y;
  122.     return t1 * t1 + t2 * t2;
  123. }
  124.  
  125. GLint getActivePoint1(POINT2D p[], GLint size, GLint sens, GLint x, GLint y) {
  126.     GLint i, s = sens * sens;
  127.     POINT2D P = initPoint2D(x, y);
  128.  
  129.     for (i = 0; i < size; i++)
  130.         if (dist2(p[i], P) < s)
  131.             return i;
  132.     return -1;
  133. }
  134.  
  135. void processMouse(GLint button, GLint action, GLint xMouse, GLint yMouse) {
  136.     GLint i;
  137.     if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
  138.         if ((i = getActivePoint1(points, 4, 8, xMouse, winHeight - yMouse)) != -1)
  139.             dragged = i;
  140.     if (button == GLUT_LEFT_BUTTON && action == GLUT_UP)
  141.         dragged = -1;
  142. }
  143.  
  144. void processMouseActiveMotion(GLint xMouse, GLint yMouse) {
  145.     GLint i;
  146.     if (dragged >= 0) {
  147.         points[dragged].x = xMouse;
  148.         points[dragged].y = winHeight - yMouse;
  149.         glutPostRedisplay();
  150.     }
  151. }
  152.  
  153.  
  154. int main(int argc, char** argv)
  155. {
  156.     glutInit(&argc, argv);
  157.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  158.     glutInitWindowPosition(50, 50);
  159.     glutInitWindowSize(700, 500);
  160.     glutCreateWindow("Evolvens");
  161.     init();
  162.     glutDisplayFunc(display);
  163.     glutMouseFunc(processMouse);
  164.     glutMotionFunc(processMouseActiveMotion);
  165.     glutMainLoop();
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement