Advertisement
Diogo03

Projeto CG

Apr 7th, 2025
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <bits/stdc++.h>
  3.  
  4. const double PI = M_PI;
  5.  
  6. bool touch(float x1, float y1, float x2, float y2, float r1, float r2) {
  7.     double d = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  8.     return (d <= r1 + r2);
  9. }
  10.  
  11. void drawCircle(double cx, double cy, double r, int segments) {
  12.     glBegin(GL_LINE_LOOP);
  13.     for (int i = 0; i < segments; i++) {
  14.         double theta = 2.0 * PI * i / segments;
  15.         double x = r * cos(theta);
  16.         double y = r * sin(theta);
  17.         glVertex2f(cx + x, cy + y);
  18.     }
  19.     glEnd();
  20. }
  21.  
  22. void drawFilledCircle(double cx, double cy, double r, int segments) {
  23.     glBegin(GL_POLYGON);
  24.     for (int i = 0; i < segments; i++) {
  25.         double theta = 2.0 * PI * i / segments;
  26.         double x = r * cos(theta);
  27.         double y = r * sin(theta);
  28.         glVertex2f(cx + x, cy + y);
  29.     }
  30.     glEnd();
  31. }
  32.  
  33. void drawLeftPenaltyArc(double cx, double cy, double r, int segments) {
  34.     double startAngle = -0.87;
  35.     double endAngle   =  0.87;
  36.     glBegin(GL_LINE_STRIP);
  37.     for (int i = 0; i <= segments; i++) {
  38.         double theta = startAngle + (endAngle - startAngle) * i / segments;
  39.         double x = r * cos(theta);
  40.         double y = r * sin(theta);
  41.         glVertex2f(cx + x, cy + y);
  42.     }
  43.     glEnd();
  44. }
  45.  
  46. void drawRightPenaltyArc(double cx, double cy, double r, int segments) {
  47.     double startAngle = 3.14 - 0.87;
  48.     double endAngle   = 3.14 + 0.87;
  49.     glBegin(GL_LINE_STRIP);
  50.     for (int i = 0; i <= segments; i++) {
  51.         double theta = startAngle + (endAngle - startAngle) * i / segments;
  52.         double x = r * cos(theta);
  53.         double y = r * sin(theta);
  54.         glVertex2f(cx + x, cy + y);
  55.     }
  56.     glEnd();
  57. }
  58.  
  59. void drawGoalInsideArea(double x, double y) {
  60.     double goalWidth = 2.5;
  61.     double goalHeight = 10.5;
  62.    
  63.     glBegin(GL_LINE_LOOP);
  64.         glVertex2f(x, y);
  65.         glVertex2f(x + goalWidth, y);
  66.         glVertex2f(x + goalWidth, y + goalHeight);
  67.         glVertex2f(x, y + goalHeight);
  68.     glEnd();
  69. }
  70.  
  71. void display() {
  72.     glClear(GL_COLOR_BUFFER_BIT);
  73.     glColor3f(1, 1, 1);
  74.  
  75.     glBegin(GL_LINE_LOOP);
  76.         glVertex2f(0, 0);
  77.         glVertex2f(105, 0);
  78.         glVertex2f(105, 68);
  79.         glVertex2f(0, 68);
  80.     glEnd();
  81.  
  82.     glBegin(GL_LINES);
  83.         glVertex2f(52.5, 0);
  84.         glVertex2f(52.5, 68);
  85.     glEnd();
  86.  
  87.     drawCircle(52.5, 34, 9.15, 100);
  88.  
  89.     glBegin(GL_LINE_LOOP);
  90.         glVertex2f(0, 13.85);
  91.         glVertex2f(16.5, 13.85);
  92.         glVertex2f(16.5, 54.15);
  93.         glVertex2f(0, 54.15);
  94.     glEnd();
  95.  
  96.     glBegin(GL_LINE_LOOP);
  97.         glVertex2f(105, 13.85);
  98.         glVertex2f(88.5, 13.85);
  99.         glVertex2f(88.5, 54.15);
  100.         glVertex2f(105, 54.15);
  101.     glEnd();
  102.  
  103.     drawLeftPenaltyArc(10.65, 34, 9.15, 100);
  104.     drawRightPenaltyArc(94.35, 34, 9.15, 100);
  105.  
  106.     glPointSize(5.0);
  107.     glBegin(GL_POINTS);
  108.         glVertex2f(11, 34);
  109.     glEnd();
  110.     glBegin(GL_POINTS);
  111.         glVertex2f(94, 34);
  112.     glEnd();
  113.     glBegin(GL_POINTS);
  114.         glVertex2f(52.5, 34);
  115.     glEnd();
  116.  
  117.     double leftGoalY = 34 - (10.5 / 2);
  118.     double rightGoalY = 34 - (10.5 / 2);
  119.     drawGoalInsideArea(0, leftGoalY);
  120.     drawGoalInsideArea(105 - 2.5, rightGoalY);
  121.  
  122.     glutSwapBuffers();
  123. }
  124.  
  125.  
  126. void init() {
  127.     glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
  128.     glMatrixMode(GL_PROJECTION);
  129.     glLoadIdentity();
  130.     gluOrtho2D(0.0, 105.0, 0.0, 68.0);
  131. }
  132.  
  133. int main(int argc, char** argv) {
  134.     glutInit(&argc, argv);
  135.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  136.     glutInitWindowSize(1050, 680);
  137.     glutCreateWindow("Campo de Futebol 2D");
  138.     init();
  139.     glutDisplayFunc(display);
  140.     glutMainLoop();
  141.     return 0;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement