Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <math.h>
- #include <iostream>
- #define PI 3.14159265359
- #define R 20
- typedef struct point2d{ GLfloat x, y; } POINT2D;
- GLint dragged = -1;
- POINT2D moveline[6] = {200, 100, 500, 100, 200, 75, 500, 75, 200, 50, 500, 50};
- GLdouble winHeight = 500.0;
- GLdouble winWidth = 700.0;
- POINT2D points[3] = {210, 100, 210, 75, 210, 50};
- //a kör osztály
- class Circle{
- public:
- GLdouble ox, oy, fok=0, t=4*PI/180;
- Circle(GLdouble xk=350, GLdouble yk=250): ox(xk), oy(yk){}
- //kör kirajzolása
- void Draw()
- {
- glColor3f(0.0, 0.0, 0.0);
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i <= 360; ++i)
- {
- glVertex2d(ox + R * cos(fok), oy + R * sin(fok));
- fok = (i*PI / 180);
- }
- glEnd();
- }
- //valamelyik egyenes kirajzolása nem tudom melyik, majd kiderül mindjárt
- void DrawLine()
- {
- glLineWidth(1.0);
- glColor3f(0.0, 0.0, 0.0);
- for (GLfloat i = 0; i <= 4*PI; i += 2*PI/180)
- {
- glBegin(GL_LINE_STRIP);
- for (GLfloat t = 0; t <= 4*PI; t+= PI/180)
- {
- glVertex2d(ox + R*(cos(t + fok) + t*sin(t + fok)), oy + R*(sin(t + fok) - t*cos(t + fok)));
- }
- glEnd();
- fok = (i* (PI / 180));
- }
- }
- };
- POINT2D initPoint2D(GLfloat x, GLfloat y) {
- POINT2D P;
- P.x = x;
- P.y = y;
- return P;
- }
- void init()
- {
- glClearColor(0.5, 0.5, 0.9, 0.0);
- glShadeModel(GL_SMOOTH);
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(0.0, 700.0, 0.0, 500.0);
- glPointSize(7.0);
- glEnable(GL_POINT_SMOOTH);
- }
- Circle circle(350, 250);
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- circle.Draw();
- circle.DrawLine();
- glColor3f(0.0, 1.0, 0.0);
- glBegin(GL_LINES);
- for (int i = 0; i < 6; i++)
- glVertex2f(moveline[i].x, moveline[i].y);
- glEnd();
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POINTS);
- for (int i = 0; i < 6; i++)
- glVertex2f(moveline[i].x, moveline[i].y);
- glEnd();
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_POINTS);
- for (int i = 0; i < 3; ++i)
- {
- glVertex2f(points[i].x, points[i].y);
- if (points[i].x >= moveline[i/2].x && points[i].x <= moveline[i/2].x)
- {
- points[i].y = moveline[i/2].y;
- }
- /* if (points[i].x <= moveline[i].x)
- {
- points[i].x = moveline[i].x;
- points[i].y = moveline[i].y;
- glVertex2f(points[i].x, points[i].y);
- }
- if (points[i].x >= moveline[i+1].x)
- {
- points[i].x = moveline[i+1].x;
- points[i].y = moveline[i+1].y;
- glVertex2f(points[i].x, points[i].y);
- }
- if (points[i].x > moveline[i].x && points[i].x < moveline[i+1].x)
- {
- points[i].y = moveline[i].y;
- glVertex2f(points[i].x, points[i].y);
- }*/
- }
- glEnd();
- glutSwapBuffers();
- }
- GLfloat dist2(POINT2D P1, POINT2D P2) {
- GLfloat t1 = P1.x - P2.x;
- GLfloat t2 = P1.y - P2.y;
- return t1 * t1 + t2 * t2;
- }
- GLint getActivePoint1(POINT2D p[], GLint size, GLint sens, GLint x, GLint y) {
- GLint i, s = sens * sens;
- POINT2D P = initPoint2D(x, y);
- for (i = 0; i < size; i++)
- if (dist2(p[i], P) < s)
- return i;
- return -1;
- }
- void processMouse(GLint button, GLint action, GLint xMouse, GLint yMouse) {
- GLint i;
- if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
- if ((i = getActivePoint1(points, 4, 8, xMouse, winHeight - yMouse)) != -1)
- dragged = i;
- if (button == GLUT_LEFT_BUTTON && action == GLUT_UP)
- dragged = -1;
- }
- void processMouseActiveMotion(GLint xMouse, GLint yMouse) {
- GLint i;
- if (dragged >= 0) {
- points[dragged].x = xMouse;
- points[dragged].y = winHeight - yMouse;
- glutPostRedisplay();
- }
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(50, 50);
- glutInitWindowSize(700, 500);
- glutCreateWindow("Evolvens");
- init();
- glutDisplayFunc(display);
- glutMouseFunc(processMouse);
- glutMotionFunc(processMouseActiveMotion);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement