Advertisement
anik11556

OpenGL

Jan 27th, 2024
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1.  
  2. #include <GL/glut.h>
  3. #define Height 512
  4. #define Width 512
  5.  
  6.  
  7. void displayRectangleWithLinesAndPoints(void)
  8. {
  9.    glClear(GL_COLOR_BUFFER_BIT);
  10.  
  11.  
  12.    // Define the size of the rectangle
  13.    GLfloat height = 0.5;
  14.    GLfloat width = 1.0;
  15.  
  16.  
  17.    // Calculate the coordinates to center the rectangle
  18.    GLfloat xLeft = -width / 2.0;
  19.    GLfloat xRight = width / 2.0;
  20.    GLfloat yBottom = -height / 2.0;
  21.    GLfloat yTop = height / 2.0;
  22.  
  23.  
  24.    // Draw the rectangle
  25.    glBegin(GL_LINES);
  26.    glColor3f(0.0, 0.0, 1.0); // Set color to blue
  27.  
  28.  
  29.    // Bottom side
  30.    glVertex3f(xLeft, yBottom, 0.0);
  31.    glVertex3f(xRight, yBottom, 0.0);
  32.  
  33.  
  34.    // Right side
  35.    glVertex3f(xRight, yBottom, 0.0);
  36.    glVertex3f(xRight, yTop, 0.0);
  37.  
  38.  
  39.    // Top side
  40.    glVertex3f(xRight, yTop, 0.0);
  41.    glVertex3f(xLeft, yTop, 0.0);
  42.  
  43.  
  44.    // Left side
  45.    glVertex3f(xLeft, yTop, 0.0);
  46.    glVertex3f(xLeft, yBottom, 0.0);
  47.    glEnd();
  48.  
  49.  
  50.    // Draw the mid vertical line
  51.    glBegin(GL_LINES);
  52.    glColor3f(1.0, 0.0, 0.0); // Set color to red
  53.    glVertex3f(0.0, yBottom, 0.0); // Start point of the line
  54.    glVertex3f(0.0, yTop, 0.0);    // End point of the line
  55.    glEnd();
  56.  
  57.  
  58.    // Draw the mid horizontal line
  59.    glBegin(GL_LINES);
  60.    glColor3f(1.0, 0.0, 0.0); // Set color to red
  61.    glVertex3f(xLeft, 0.0, 0.0); // Start point of the line
  62.    glVertex3f(xRight, 0.0, 0.0); // End point of the line
  63.    glEnd();
  64.  
  65.  
  66.    // Calculate midpoints
  67.    GLfloat xMidVertical = 0.0;
  68.    GLfloat yMidHorizontal = 0.0;
  69.  
  70.  
  71.    // Draw points at the midpoints and four points at each side
  72.    glBegin(GL_POINTS);
  73.    glColor3f(0.0, 1.0, 0.0); // Set color to green
  74.  
  75.  
  76.    // Midpoint of the vertical line
  77.    glVertex3f(xMidVertical, (yBottom + yTop) / 2.0, 0.0);
  78.  
  79.  
  80.    // Midpoint of the horizontal line
  81.    glVertex3f((xLeft + xRight) / 2.0, yMidHorizontal, 0.0);
  82.  
  83.  
  84.    // Center of the rectangle
  85.    glVertex3f((xLeft + xRight) / 2.0, (yBottom + yTop) / 2.0, 0.0);
  86.  
  87.  
  88.    // Midpoint of the diagonal line from bottom-left to top-right
  89.    glVertex3f((xLeft + xRight) / 2.0, (yBottom + yTop) / 2.0, 0.0);
  90.  
  91.  
  92.    // Four points at each side
  93.    glVertex3f(xLeft/2, 0.1, 0.0);
  94.    glVertex3f(xLeft + (xRight) / 2.0, yBottom/2, 0.0);
  95.    glVertex3f(xRight/2, 0.1, 0.0);
  96.    glVertex3f(( xRight) / 2.0, yBottom+yTop/2, 0.0);
  97.  
  98.  
  99.    glEnd();
  100.  
  101.  
  102.    glFlush();
  103. }
  104.  
  105.  
  106. int main(int argc, char **argv)
  107. {
  108.    glutInit(&argc, argv);
  109.    glutInitWindowSize(Width, Height);
  110.    glutInitWindowPosition(10, 10);
  111.    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // Removed GLUT_DEPTH, using GLUT_SINGLE for simplicity
  112.  
  113.  
  114.    glutCreateWindow("Rectangle Example with Midpoints and Points on Sides");
  115.    glutDisplayFunc(displayRectangleWithLinesAndPoints);
  116.    glutMainLoop();
  117.  
  118.  
  119.    return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement